3 |
#include "Logger.h" |
#include "Logger.h" |
4 |
#include "Util.h" |
#include "Util.h" |
5 |
#include "Common.h" |
#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 |
|
if (allowMultipart == false && message.size() > 160) |
62 |
|
message = message.substr(0,160); |
63 |
|
|
64 |
|
|
65 |
std::string filename = CreateFilename(); |
std::string filename = CreateFilename(); |
66 |
Logger::logMessage( std::string("Sending sms to: ") + to); |
Logger::logMessage( std::string("Sending sms to: ") + to); |
67 |
|
|
68 |
string tempfile = "/var/spool/sms/"; |
string tempfile = outgoingdir + filename + ".LOCK"; |
|
tempfile += filename; |
|
69 |
|
|
70 |
string destfile = "/var/spool/sms/outgoing/"; |
string destfile = outgoingdir + filename; |
|
destfile += filename; |
|
71 |
|
|
72 |
ofstream out( tempfile.c_str() ); |
ofstream out( tempfile.c_str() ); |
73 |
if (!out ) |
if (!out ) |
79 |
out.close(); |
out.close(); |
80 |
|
|
81 |
Common::instance()->smsCounter.outgoing++; |
Common::instance()->smsCounter.outgoing++; |
82 |
|
|
83 |
int result = rename(tempfile.c_str(), destfile.c_str()); |
int result = rename(tempfile.c_str(), destfile.c_str()); |
84 |
|
|
85 |
if (result) |
if (result) |
98 |
|
|
99 |
std::vector<SMS> SmsToolTransceiver::ReadSms(bool readAll) |
std::vector<SMS> SmsToolTransceiver::ReadSms(bool readAll) |
100 |
{ |
{ |
|
const std::string inbox = "/var/spool/sms/inbox/"; |
|
101 |
std::vector<SMS> vec; |
std::vector<SMS> vec; |
102 |
DIR* dir = opendir( inbox.c_str() ); |
DIR* dir = opendir( inboxdir.c_str() ); |
103 |
|
|
104 |
if (dir != 0) |
if (dir != 0) |
105 |
{ |
{ |
106 |
dirent* entry; |
dirent* entry; |
107 |
while ( (entry = readdir(dir)) != 0) |
while ( (entry = readdir(dir)) != 0) |
112 |
|
|
113 |
try |
try |
114 |
{ |
{ |
115 |
SMS sms = ParseFile( inbox + entry->d_name); |
SMS sms = ParseFile( inboxdir + entry->d_name); |
116 |
vec.push_back(sms); |
vec.push_back(sms); |
117 |
} |
} |
118 |
catch (std::exception& e) |
catch (std::exception& e) |
119 |
{ |
{ |
120 |
Logger::logMessage(e.what()); |
Logger::logMessage(e.what()); |
121 |
} |
} |
133 |
return vec; |
return vec; |
134 |
} |
} |
135 |
|
|
|
void SmsToolTransceiver::DeleteSms(std::string smsIndex) |
|
|
{ |
|
|
} |
|
136 |
|
|
137 |
SMS SmsToolTransceiver::ParseFile(std::string path) |
SMS SmsToolTransceiver::ParseFile(std::string path) |
138 |
{ |
{ |
139 |
SMS sms; |
SMS sms; |
140 |
|
|
141 |
string file = Util::readfile(path); |
string file = Util::readfile(path); |
142 |
int result = ::unlink(path.c_str()); |
int result = ::unlink(path.c_str()); |
143 |
if (result) |
if (result) |
144 |
{ |
{ |
145 |
string message = "SmsTool> unlink "; |
string message = "SmsTool> unlink "; |
146 |
message += path; |
message += path; |
147 |
message += " failed: "; |
message += " failed: "; |
148 |
message += strerror(errno); |
message += strerror(errno); |
149 |
throw std::runtime_error( message ); |
throw std::runtime_error( message ); |
152 |
|
|
153 |
unsigned int pos = file.find("\n\n"); |
unsigned int pos = file.find("\n\n"); |
154 |
|
|
155 |
if (pos != string::npos) { |
if (pos != string::npos) |
156 |
|
{ |
157 |
string header = file.substr(0,pos); |
string header = file.substr(0,pos); |
158 |
string body = file.substr(pos+2,1024); |
string body = file.substr(pos+2,1024); |
159 |
|
|
162 |
|
|
163 |
ParseHeaders(header, sms); |
ParseHeaders(header, sms); |
164 |
|
|
165 |
} else { |
} |
166 |
|
else |
167 |
|
{ |
168 |
throw std::runtime_error("SmsTool: invalid incomming file"); |
throw std::runtime_error("SmsTool: invalid incomming file"); |
169 |
} |
} |
170 |
|
|
175 |
{ |
{ |
176 |
std::vector<std::string> headers = Util::str_split(headerstring, "\n"); |
std::vector<std::string> headers = Util::str_split(headerstring, "\n"); |
177 |
|
|
178 |
for (unsigned int i=0; i<headers.size(); i++) |
for (unsigned int i=0; i<headers.size(); i++) |
179 |
{ |
{ |
180 |
unsigned int pos = headers[i].find(":"); |
unsigned int pos = headers[i].find(":"); |
181 |
if (pos != string::npos) |
if (pos != string::npos) |
197 |
|
|
198 |
} |
} |
199 |
|
|
|
int SmsToolTransceiver::DeleteAllSms() |
|
|
{ |
|
|
return 0; |
|
|
} |
|
200 |
|
|
201 |
|
|