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

Annotation of /smsdaemon/SmsToolTransceiver.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 160 - (hide annotations) (download)
Mon Dec 8 22:11:01 2008 UTC (15 years, 5 months ago) by torben
File size: 3296 byte(s)
Remember to increase the smscounter

1 torben 151 #include "SmsToolTransceiver.h"
2    
3 torben 157 #include "Logger.h"
4 torben 158 #include "Util.h"
5 torben 151
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 torben 157 Logger::logMessage( std::string("Sending sms to: ") + to);
25 torben 151
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 torben 157 Logger::logMessage("SmsToolTransceiver:: could not create temp file");
36 torben 151 return;
37     }
38     out << "To: " << to << "\n\n" << message ;
39     out.close();
40 torben 160
41     Common::instance()->smsCounter.outgoing++;
42 torben 151
43     int result = rename(tempfile.c_str(), destfile.c_str());
44    
45     if (result)
46 torben 157 Logger::logMessage( string("SmsToolTransceiver could not spool file: ") + strerror(errno) );
47 torben 151 }
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 torben 157 Logger::logMessage(e.what());
81 torben 151 }
82    
83     if (!readAll)
84     break;
85     }
86     }
87     else
88     {
89 torben 157 Logger::logMessage( string("SmsToolTransceiver could open incoming dir ") + strerror(errno) );
90 torben 151 }
91 torben 155 closedir(dir);
92 torben 151
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     }

  ViewVC Help
Powered by ViewVC 1.1.20