1 |
#include "SmsToolTransceiver.h" |
2 |
|
3 |
#include "Logger.h" |
4 |
#include "Util.h" |
5 |
#include "Common.h" |
6 |
#include "ConfigFile.h" |
7 |
|
8 |
#include <stdexcept> |
9 |
#include <iostream> |
10 |
#include <fstream> |
11 |
#include <sstream> |
12 |
#include <iomanip> |
13 |
|
14 |
#include <cstring> |
15 |
#include <errno.h> |
16 |
#include <time.h> |
17 |
#include <stdlib.h> |
18 |
#include <sys/types.h> |
19 |
#include <dirent.h> |
20 |
|
21 |
using namespace std; |
22 |
|
23 |
string appendSlash(string& str) |
24 |
{ |
25 |
if (str.at(str.length()-1) != '/'); |
26 |
str += "/"; |
27 |
return str; |
28 |
} |
29 |
|
30 |
SmsToolTransceiver::SmsToolTransceiver() |
31 |
{ |
32 |
Common* cmn = Common::instance(); |
33 |
|
34 |
try |
35 |
{ |
36 |
inboxdir = cmn->GetConfigfile()->GetValue("smstools","inboxdir").StringValue(); |
37 |
} catch (...) { |
38 |
Logger::logMessage("Config error> smstools::inboxdir not specified"); |
39 |
exit(1); |
40 |
} |
41 |
try |
42 |
{ |
43 |
outgoingdir = cmn->GetConfigfile()->GetValue("smstools","outgoingdir").StringValue(); |
44 |
} catch (...) { |
45 |
Logger::logMessage("Config error> smstools::outgoingdir not specified"); |
46 |
exit(1); |
47 |
} |
48 |
|
49 |
inboxdir = appendSlash(inboxdir); |
50 |
outgoingdir = appendSlash(outgoingdir); |
51 |
|
52 |
} |
53 |
|
54 |
|
55 |
void SmsToolTransceiver::SendSms(std::string to, std::string message, bool allowMultipart) |
56 |
{ |
57 |
std::string filename = CreateFilename(); |
58 |
Logger::logMessage( std::string("Sending sms to: ") + to); |
59 |
|
60 |
string tempfile = outgoingdir + filename + ".LOCK"; |
61 |
|
62 |
string destfile = outgoingdir + filename; |
63 |
|
64 |
ofstream out( tempfile.c_str() ); |
65 |
if (!out ) |
66 |
{ |
67 |
Logger::logMessage("SmsToolTransceiver:: could not create temp file"); |
68 |
return; |
69 |
} |
70 |
out << "To: " << to << "\n\n" << message ; |
71 |
out.close(); |
72 |
|
73 |
Common::instance()->smsCounter.outgoing++; |
74 |
|
75 |
int result = rename(tempfile.c_str(), destfile.c_str()); |
76 |
|
77 |
if (result) |
78 |
Logger::logMessage( string("SmsToolTransceiver could not spool file: ") + strerror(errno) ); |
79 |
} |
80 |
|
81 |
std::string SmsToolTransceiver::CreateFilename() |
82 |
{ |
83 |
std::stringstream ss; |
84 |
ss << "smsdaemon_"; |
85 |
ss << std::setw(8) << std::setfill('0') << std::uppercase << std::hex << time(0) << "_"; |
86 |
ss << std::setw(8) << std::setfill('0') << std::uppercase << std::hex << rand(); |
87 |
ss << std::setw(8) << std::setfill('0') << std::uppercase << std::hex << rand(); |
88 |
return ss.str(); |
89 |
} |
90 |
|
91 |
std::vector<SMS> SmsToolTransceiver::ReadSms(bool readAll) |
92 |
{ |
93 |
std::vector<SMS> vec; |
94 |
DIR* dir = opendir( inboxdir.c_str() ); |
95 |
|
96 |
if (dir != 0) |
97 |
{ |
98 |
dirent* entry; |
99 |
while ( (entry = readdir(dir)) != 0) |
100 |
{ |
101 |
if (entry->d_name[0] == '.') |
102 |
continue; |
103 |
Logger::logMessage( string("SmsTool::ReadSms >") +entry->d_name); |
104 |
|
105 |
try |
106 |
{ |
107 |
SMS sms = ParseFile( inboxdir + entry->d_name); |
108 |
vec.push_back(sms); |
109 |
} |
110 |
catch (std::exception& e) |
111 |
{ |
112 |
Logger::logMessage(e.what()); |
113 |
} |
114 |
|
115 |
if (!readAll) |
116 |
break; |
117 |
} |
118 |
} |
119 |
else |
120 |
{ |
121 |
Logger::logMessage( string("SmsToolTransceiver could open inbox dir ") + strerror(errno) ); |
122 |
} |
123 |
closedir(dir); |
124 |
|
125 |
return vec; |
126 |
} |
127 |
|
128 |
|
129 |
SMS SmsToolTransceiver::ParseFile(std::string path) |
130 |
{ |
131 |
SMS sms; |
132 |
|
133 |
string file = Util::readfile(path); |
134 |
int result = ::unlink(path.c_str()); |
135 |
if (result) |
136 |
{ |
137 |
string message = "SmsTool> unlink "; |
138 |
message += path; |
139 |
message += " failed: "; |
140 |
message += strerror(errno); |
141 |
throw std::runtime_error( message ); |
142 |
} |
143 |
|
144 |
|
145 |
unsigned int pos = file.find("\n\n"); |
146 |
|
147 |
if (pos != string::npos) { |
148 |
string header = file.substr(0,pos); |
149 |
string body = file.substr(pos+2,1024); |
150 |
|
151 |
sms.SetIndex(path); |
152 |
sms.SetMessage(body); |
153 |
|
154 |
ParseHeaders(header, sms); |
155 |
|
156 |
} else { |
157 |
throw std::runtime_error("SmsTool: invalid incomming file"); |
158 |
} |
159 |
|
160 |
return sms; |
161 |
} |
162 |
|
163 |
void SmsToolTransceiver::ParseHeaders(std::string& headerstring, SMS& sms) |
164 |
{ |
165 |
std::vector<std::string> headers = Util::str_split(headerstring, "\n"); |
166 |
|
167 |
for (unsigned int i=0; i<headers.size(); i++) |
168 |
{ |
169 |
unsigned int pos = headers[i].find(":"); |
170 |
if (pos != string::npos) |
171 |
{ |
172 |
string key = Util::str_trim( headers[i].substr(0,pos) ); |
173 |
string val = Util::str_trim( headers[i].substr(pos+1) ); |
174 |
|
175 |
if (key == "From") |
176 |
sms.SetSender(val); |
177 |
|
178 |
if (key == "Received") |
179 |
sms.SetTimestamp(val); |
180 |
} |
181 |
else |
182 |
{ |
183 |
Logger::logMessage(string("SmsTool: Invalid sms file header> ") + headers[i]); |
184 |
} |
185 |
} |
186 |
|
187 |
} |
188 |
|
189 |
int SmsToolTransceiver::DeleteAllSms() |
190 |
{ |
191 |
return 0; |
192 |
} |