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