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