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

Contents of /smsdaemon/SmsToolTransceiver.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 167 - (show annotations) (download)
Tue Dec 9 19:42:45 2008 UTC (15 years, 5 months ago) by torben
File size: 3515 byte(s)
Since smstools doesn't have a locking mechanism for incoming files, 
create a event handler script that moves the file to another directory and
make SmsToolTransceiver handle the messages from there.

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 inbox = "/var/spool/sms/inbox/";
63 std::vector<SMS> vec;
64 DIR* dir = opendir( inbox.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 Logger::logMessage( string("SmsTool::ReadSms >") +entry->d_name);
74
75 try
76 {
77 SMS sms = ParseFile( inbox + entry->d_name);
78 vec.push_back(sms);
79 }
80 catch (std::exception& e)
81 {
82 Logger::logMessage(e.what());
83 }
84
85 if (!readAll)
86 break;
87 }
88 }
89 else
90 {
91 Logger::logMessage( string("SmsToolTransceiver could open inbox dir ") + strerror(errno) );
92 }
93 closedir(dir);
94
95 return vec;
96 }
97
98 void SmsToolTransceiver::DeleteSms(std::string smsIndex)
99 {
100 }
101
102 SMS SmsToolTransceiver::ParseFile(std::string path)
103 {
104 SMS sms;
105
106 string file = Util::readfile(path);
107 int result = ::unlink(path.c_str());
108 if (result)
109 {
110 string message = "SmsTool> unlink ";
111 message += path;
112 message += " failed: ";
113 message += strerror(errno);
114 throw std::runtime_error( message );
115 }
116
117
118 unsigned int pos = file.find("\n\n");
119
120 if (pos != string::npos) {
121 string header = file.substr(0,pos);
122 string body = file.substr(pos+2,1024);
123
124 sms.SetIndex(path);
125 sms.SetMessage(body);
126
127 ParseHeaders(header, sms);
128
129 } else {
130 throw std::runtime_error("SmsTool: invalid incomming file");
131 }
132
133 return sms;
134 }
135
136 void SmsToolTransceiver::ParseHeaders(std::string& headerstring, SMS& sms)
137 {
138 std::vector<std::string> headers = Util::str_split(headerstring, "\n");
139
140 for (unsigned int i=0; i<headers.size(); i++)
141 {
142 unsigned int pos = headers[i].find(":");
143 if (pos != string::npos)
144 {
145 string key = Util::str_trim( headers[i].substr(0,pos) );
146 string val = Util::str_trim( headers[i].substr(pos+1) );
147
148 if (key == "From")
149 sms.SetSender(val);
150
151 if (key == "Received")
152 sms.SetTimestamp(val);
153 }
154 else
155 {
156 Logger::logMessage(string("SmsTool: Invalid sms file header> ") + headers[i]);
157 }
158 }
159
160 }
161
162 int SmsToolTransceiver::DeleteAllSms()
163 {
164 return 0;
165 }

  ViewVC Help
Powered by ViewVC 1.1.20