/[projects]/smsdaemon/SmsToolTransceiver.cpp
ViewVC logotype

Contents of /smsdaemon/SmsToolTransceiver.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 168 - (show annotations) (download)
Tue Dec 9 21:44:15 2008 UTC (15 years, 5 months ago) by torben
File size: 4116 byte(s)
1) SmsTool, outgoingdir - use .LOCK files
2) SmsTool: make it possible to configure inbox and outgoing directories
3) Value: make some extra data accessor functions


1 #include "SmsToolTransceiver.h"
2
3 #include "Logger.h"
4 #include "Util.h"
5 #include "Common.h"
6 #include "ConfigFile.h"
7
8 #include <stdexcept>
9 #include <iostream>
10 #include <fstream>
11 #include <sstream>
12 #include <iomanip>
13
14 #include <cstring>
15 #include <errno.h>
16 #include <time.h>
17 #include <stdlib.h>
18 #include <sys/types.h>
19 #include <dirent.h>
20
21 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)
56 {
57 std::string filename = CreateFilename();
58 Logger::logMessage( std::string("Sending sms to: ") + to);
59
60 string tempfile = outgoingdir + filename + ".LOCK";
61
62 string destfile = outgoingdir + filename;
63
64 ofstream out( tempfile.c_str() );
65 if (!out )
66 {
67 Logger::logMessage("SmsToolTransceiver:: could not create temp file");
68 return;
69 }
70 out << "To: " << to << "\n\n" << message ;
71 out.close();
72
73 Common::instance()->smsCounter.outgoing++;
74
75 int result = rename(tempfile.c_str(), destfile.c_str());
76
77 if (result)
78 Logger::logMessage( string("SmsToolTransceiver could not spool file: ") + strerror(errno) );
79 }
80
81 std::string SmsToolTransceiver::CreateFilename()
82 {
83 std::stringstream ss;
84 ss << "smsdaemon_";
85 ss << std::setw(8) << std::setfill('0') << std::uppercase << std::hex << time(0) << "_";
86 ss << std::setw(8) << std::setfill('0') << std::uppercase << std::hex << rand();
87 ss << std::setw(8) << std::setfill('0') << std::uppercase << std::hex << rand();
88 return ss.str();
89 }
90
91 std::vector<SMS> SmsToolTransceiver::ReadSms(bool readAll)
92 {
93 std::vector<SMS> vec;
94 DIR* dir = opendir( inboxdir.c_str() );
95
96 if (dir != 0)
97 {
98 dirent* entry;
99 while ( (entry = readdir(dir)) != 0)
100 {
101 if (entry->d_name[0] == '.')
102 continue;
103 Logger::logMessage( string("SmsTool::ReadSms >") +entry->d_name);
104
105 try
106 {
107 SMS sms = ParseFile( inboxdir + entry->d_name);
108 vec.push_back(sms);
109 }
110 catch (std::exception& e)
111 {
112 Logger::logMessage(e.what());
113 }
114
115 if (!readAll)
116 break;
117 }
118 }
119 else
120 {
121 Logger::logMessage( string("SmsToolTransceiver could open inbox dir ") + strerror(errno) );
122 }
123 closedir(dir);
124
125 return vec;
126 }
127
128 void SmsToolTransceiver::DeleteSms(std::string smsIndex)
129 {
130 }
131
132 SMS SmsToolTransceiver::ParseFile(std::string path)
133 {
134 SMS sms;
135
136 string file = Util::readfile(path);
137 int result = ::unlink(path.c_str());
138 if (result)
139 {
140 string message = "SmsTool> unlink ";
141 message += path;
142 message += " failed: ";
143 message += strerror(errno);
144 throw std::runtime_error( message );
145 }
146
147
148 unsigned int pos = file.find("\n\n");
149
150 if (pos != string::npos) {
151 string header = file.substr(0,pos);
152 string body = file.substr(pos+2,1024);
153
154 sms.SetIndex(path);
155 sms.SetMessage(body);
156
157 ParseHeaders(header, sms);
158
159 } else {
160 throw std::runtime_error("SmsTool: invalid incomming file");
161 }
162
163 return sms;
164 }
165
166 void SmsToolTransceiver::ParseHeaders(std::string& headerstring, SMS& sms)
167 {
168 std::vector<std::string> headers = Util::str_split(headerstring, "\n");
169
170 for (unsigned int i=0; i<headers.size(); i++)
171 {
172 unsigned int pos = headers[i].find(":");
173 if (pos != string::npos)
174 {
175 string key = Util::str_trim( headers[i].substr(0,pos) );
176 string val = Util::str_trim( headers[i].substr(pos+1) );
177
178 if (key == "From")
179 sms.SetSender(val);
180
181 if (key == "Received")
182 sms.SetTimestamp(val);
183 }
184 else
185 {
186 Logger::logMessage(string("SmsTool: Invalid sms file header> ") + headers[i]);
187 }
188 }
189
190 }
191
192 int SmsToolTransceiver::DeleteAllSms()
193 {
194 return 0;
195 }

  ViewVC Help
Powered by ViewVC 1.1.20