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