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

Annotation of /smsdaemon/SmsToolTransceiver.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 157 - (hide annotations) (download)
Mon Dec 8 21:28:40 2008 UTC (15 years, 5 months ago) by torben
File size: 3251 byte(s)
Move logpart to its own files

1 torben 151 #include "SmsToolTransceiver.h"
2    
3 torben 157 #include "Logger.h"
4 torben 151 #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 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    
41     int result = rename(tempfile.c_str(), destfile.c_str());
42    
43     if (result)
44 torben 157 Logger::logMessage( string("SmsToolTransceiver could not spool file: ") + strerror(errno) );
45 torben 151 }
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 torben 157 Logger::logMessage(e.what());
79 torben 151 }
80    
81     if (!readAll)
82     break;
83     }
84     }
85     else
86     {
87 torben 157 Logger::logMessage( string("SmsToolTransceiver could open incoming dir ") + strerror(errno) );
88 torben 151 }
89 torben 155 closedir(dir);
90 torben 151
91     return vec;
92     }
93    
94     void SmsToolTransceiver::DeleteSms(std::string smsIndex)
95     {
96     }
97    
98     SMS SmsToolTransceiver::ParseFile(std::string path)
99     {
100     SMS sms;
101    
102     string file = Util::readfile(path);
103     int result = ::unlink(path.c_str());
104     if (result)
105     {
106     string message = "SmsTool> unlink ";
107     message += path;
108     message += " failed: ";
109     message += strerror(errno);
110     throw std::runtime_error( message );
111     }
112    
113    
114     unsigned int pos = file.find("\n\n");
115    
116     if (pos != string::npos) {
117     string header = file.substr(0,pos);
118     string body = file.substr(pos+2,1024);
119    
120     sms.SetIndex(path);
121     sms.SetMessage(body);
122    
123     ParseHeaders(header, sms);
124    
125     } else {
126     throw std::runtime_error("SmsTool: invalid incomming file");
127     }
128    
129     return sms;
130     }
131    
132     void SmsToolTransceiver::ParseHeaders(std::string& headerstring, SMS& sms)
133     {
134     std::vector<std::string> headers = Util::str_split(headerstring, "\n");
135    
136     for (unsigned int i=0; i<headers.size(); i++)
137     {
138     int pos = headers[i].find(":");
139     string key = Util::str_trim( headers[i].substr(0,pos) );
140     string val = Util::str_trim( headers[i].substr(pos+1) );
141    
142     if (key == "From")
143     sms.SetSender(val);
144    
145     if (key == "Received")
146     sms.SetTimestamp(val);
147     }
148    
149     }
150    
151     int SmsToolTransceiver::DeleteAllSms()
152     {
153     return 0;
154     }

  ViewVC Help
Powered by ViewVC 1.1.20