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