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

Annotation of /smsdaemon/SmsToolTransceiver.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 151 - (hide annotations) (download)
Mon Dec 8 10:42:04 2008 UTC (15 years, 5 months ago) by torben
File size: 3295 byte(s)
Added a transceiver which uses smstools for interfaceing with the gsm modem.
-> http://smstools3.kekekasvi.com/


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

  ViewVC Help
Powered by ViewVC 1.1.20