1 |
#include "SmsToolTransceiver.h" |
#include "SmsToolTransceiver.h" |
2 |
|
|
3 |
#include "Logger.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 |
|
} 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) |
void SmsToolTransceiver::SendSms(std::string to, std::string message, bool allowMultipart) |
56 |
{ |
{ |
57 |
std::string filename = CreateFilename(); |
std::string filename = CreateFilename(); |
58 |
Logger::logMessage( std::string("Sending sms to: ") + to); |
Logger::logMessage( std::string("Sending sms to: ") + to); |
59 |
|
|
60 |
string tempfile = "/var/spool/sms/"; |
string tempfile = outgoingdir + filename + ".LOCK"; |
|
tempfile += filename; |
|
61 |
|
|
62 |
string destfile = "/var/spool/sms/outgoing/"; |
string destfile = outgoingdir + filename; |
|
destfile += filename; |
|
63 |
|
|
64 |
ofstream out( tempfile.c_str() ); |
ofstream out( tempfile.c_str() ); |
65 |
if (!out ) |
if (!out ) |
69 |
} |
} |
70 |
out << "To: " << to << "\n\n" << message ; |
out << "To: " << to << "\n\n" << message ; |
71 |
out.close(); |
out.close(); |
72 |
|
|
73 |
|
Common::instance()->smsCounter.outgoing++; |
74 |
|
|
75 |
int result = rename(tempfile.c_str(), destfile.c_str()); |
int result = rename(tempfile.c_str(), destfile.c_str()); |
76 |
|
|
90 |
|
|
91 |
std::vector<SMS> SmsToolTransceiver::ReadSms(bool readAll) |
std::vector<SMS> SmsToolTransceiver::ReadSms(bool readAll) |
92 |
{ |
{ |
|
const std::string incoming = "/var/spool/sms/incoming/"; |
|
93 |
std::vector<SMS> vec; |
std::vector<SMS> vec; |
94 |
DIR* dir = opendir( incoming.c_str() ); |
DIR* dir = opendir( inboxdir.c_str() ); |
95 |
|
|
96 |
if (dir != 0) |
if (dir != 0) |
97 |
{ |
{ |
100 |
{ |
{ |
101 |
if (entry->d_name[0] == '.') |
if (entry->d_name[0] == '.') |
102 |
continue; |
continue; |
103 |
|
Logger::logMessage( string("SmsTool::ReadSms >") +entry->d_name); |
104 |
|
|
105 |
try |
try |
106 |
{ |
{ |
107 |
SMS sms = ParseFile( incoming + entry->d_name); |
SMS sms = ParseFile( inboxdir + entry->d_name); |
108 |
vec.push_back(sms); |
vec.push_back(sms); |
109 |
} |
} |
110 |
catch (std::exception& e) |
catch (std::exception& e) |
118 |
} |
} |
119 |
else |
else |
120 |
{ |
{ |
121 |
Logger::logMessage( string("SmsToolTransceiver could open incoming dir ") + strerror(errno) ); |
Logger::logMessage( string("SmsToolTransceiver could open inbox dir ") + strerror(errno) ); |
122 |
} |
} |
123 |
closedir(dir); |
closedir(dir); |
124 |
|
|
125 |
return vec; |
return vec; |
126 |
} |
} |
127 |
|
|
|
void SmsToolTransceiver::DeleteSms(std::string smsIndex) |
|
|
{ |
|
|
} |
|
128 |
|
|
129 |
SMS SmsToolTransceiver::ParseFile(std::string path) |
SMS SmsToolTransceiver::ParseFile(std::string path) |
130 |
{ |
{ |
166 |
|
|
167 |
for (unsigned int i=0; i<headers.size(); i++) |
for (unsigned int i=0; i<headers.size(); i++) |
168 |
{ |
{ |
169 |
int pos = headers[i].find(":"); |
unsigned int pos = headers[i].find(":"); |
170 |
string key = Util::str_trim( headers[i].substr(0,pos) ); |
if (pos != string::npos) |
171 |
string val = Util::str_trim( headers[i].substr(pos+1) ); |
{ |
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") |
if (key == "From") |
176 |
sms.SetSender(val); |
sms.SetSender(val); |
177 |
|
|
178 |
if (key == "Received") |
if (key == "Received") |
179 |
sms.SetTimestamp(val); |
sms.SetTimestamp(val); |
180 |
|
} |
181 |
|
else |
182 |
|
{ |
183 |
|
Logger::logMessage(string("SmsTool: Invalid sms file header> ") + headers[i]); |
184 |
|
} |
185 |
} |
} |
186 |
|
|
187 |
} |
} |