1 |
#include "common.h" |
#include "Common.h" |
2 |
|
|
3 |
|
|
4 |
|
#include "version.h" |
5 |
|
|
6 |
|
#include <stdlib.h> |
7 |
#include <string> |
#include <string> |
8 |
#include <iostream> |
#include <iostream> |
9 |
#include <iomanip> |
#include <iomanip> |
|
#include <fstream> |
|
10 |
#include <sstream> |
#include <sstream> |
11 |
#include <time.h> |
#include <time.h> |
12 |
|
|
13 |
|
#include "TaskManager.h" |
14 |
|
#include "PluginManager.h" |
15 |
|
#include "ConfigFile.h" |
16 |
|
#include "Util.h" |
17 |
|
#include "Logger.h" |
18 |
|
|
19 |
using namespace std; |
using namespace std; |
|
char* Months[] = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec"}; |
|
20 |
|
|
21 |
Common* Common::instance() |
Common* Common::instance() |
22 |
{ |
{ |
25 |
} |
} |
26 |
|
|
27 |
|
|
28 |
void Common::logMessage(string msg) |
Common::Common() |
29 |
{ |
: _pluginManager(0), _taskManager(0) |
30 |
time_t t = time(0); |
{ |
31 |
tm now; |
_pluginManager = new PluginManager(); |
32 |
localtime_r(&t, &now); |
_taskManager = new TaskManager(); |
33 |
|
configFilePath = "/etc/smsdaemon.conf"; |
34 |
ostringstream out; |
_configFile = new ConfigFile(); |
|
out << Months[ now.tm_mon ] << " " << setw(2) << setfill('0') << now.tm_mday; |
|
|
out << " " << setw(2) << setfill('0') << now.tm_hour; |
|
|
out << ":" << setw(2) << setfill('0') << now.tm_min; |
|
|
out << ":" << setw(2) << setfill('0') << now.tm_sec; |
|
|
out << " " << msg << endl; |
|
35 |
|
|
36 |
if (this->isDaemon && _logFile != "") |
daemonized = false; |
37 |
{ |
reloadConfig = false; |
38 |
seteuid(0); |
} |
39 |
ofstream of( _logFile.c_str(), ios_base::out | ios_base::app | ios_base::ate);//append mode |
|
40 |
if (of) |
|
41 |
{ |
Common::~Common() |
42 |
of << out.str(); |
{ |
43 |
of.flush(); |
if (_pluginManager) |
44 |
of.close(); |
delete _pluginManager; |
45 |
} |
|
46 |
seteuid( this->uid); |
if (_taskManager) |
47 |
} |
delete _taskManager; |
48 |
else |
} |
49 |
{ |
|
50 |
cout << out.str(); |
PluginManager* Common::GetPluginManager() |
51 |
cout.flush(); |
{ |
52 |
} |
return _pluginManager; |
53 |
|
} |
54 |
|
|
55 |
|
TaskManager* Common::GetTaskManager() |
56 |
|
{ |
57 |
|
return _taskManager; |
58 |
|
} |
59 |
|
|
60 |
|
ConfigFile* Common::GetConfigfile() |
61 |
|
{ |
62 |
|
return _configFile; |
63 |
} |
} |
64 |
|
|
65 |
|
|
66 |
void Common::printUsage() |
void Common::printUsage() |
67 |
{ |
{ |
68 |
cout << VERSION << endl; |
cout << VERSION << endl; |
69 |
|
cout << SVNVERSION << endl << endl; |
70 |
cout << "Usage --daemon|--debug [arguments]" << endl; |
cout << "Usage --daemon|--debug [arguments]" << endl; |
71 |
cout << "Commandline arguments :" << endl; |
cout << "Commandline arguments :" << endl; |
72 |
cout << " --daemon : Run in daemon (background) mode" << endl; |
cout << " --daemon : Run in daemon (background) mode" << endl; |
73 |
cout << " --debug : Run as normal (frontground) process," << endl; |
cout << " --debug : Run as normal (frontground) process," << endl; |
74 |
cout << " with all messages written to std out" << endl; |
cout << " with all messages written to std out" << endl; |
75 |
cout << " --help : Show this help screen" << endl; |
cout << " --config <file>: Specify which config file to use " << endl; |
76 |
|
cout << " default" << configFilePath << endl; |
77 |
|
cout << " --help : Show this help screen" << endl; |
78 |
|
|
79 |
exit(0); |
exit(0); |
80 |
} |
} |
87 |
for (int i = 1; i< argc; i++) |
for (int i = 1; i< argc; i++) |
88 |
{ |
{ |
89 |
string current(argv[i]); |
string current(argv[i]); |
90 |
if (current == "--daemon") |
if (current == "--daemon") |
91 |
{ |
{ |
92 |
isDaemon = true; |
isDaemon = true; |
93 |
isDebug = false; |
isDebug = false; |
94 |
|
|
95 |
} |
} |
96 |
else if (current == "--debug") |
else if (current == "--debug") |
97 |
{ |
{ |
98 |
isDaemon = false; |
isDaemon = false; |
99 |
isDebug = true; |
isDebug = true; |
100 |
} |
} |
101 |
|
else if (current == "--config") |
102 |
|
{ |
103 |
|
i++; |
104 |
|
if ( i<argc) |
105 |
|
{ |
106 |
|
configFilePath = argv[i]; |
107 |
|
} |
108 |
|
else |
109 |
|
{ |
110 |
|
printUsage(); |
111 |
|
exit(1); |
112 |
|
} |
113 |
|
|
114 |
|
} |
115 |
else if (current == "-h" || current == "--help") |
else if (current == "-h" || current == "--help") |
116 |
{ |
{ |
117 |
printUsage(); |
printUsage(); |
118 |
} |
} |
119 |
else |
else |
120 |
{ |
{ |
121 |
cout << VERSION; |
cout << "Unknown argument : " << current << endl << endl; |
|
cout << "Unknown argument : " << current << endl; |
|
122 |
} |
} |
123 |
} |
} |
124 |
|
|
138 |
out << "Uptime " << diff << " seconds. "; |
out << "Uptime " << diff << " seconds. "; |
139 |
out << "Messages received:" << this->smsCounter.incomming << ". "; |
out << "Messages received:" << this->smsCounter.incomming << ". "; |
140 |
out << "Messages sent:" << this->smsCounter.outgoing << "."; |
out << "Messages sent:" << this->smsCounter.outgoing << "."; |
141 |
|
|
142 |
return out.str(); |
return out.str(); |
143 |
} |
} |
144 |
|
|
145 |
|
void Common::LoadWorker(std::set<std::string>& set, std::string config) |
146 |
|
{ |
147 |
|
set.clear(); |
148 |
|
|
149 |
|
std::vector<std::string> items = Util::str_split( |
150 |
|
_configFile->GetValue("smsdaemon",config,""), |
151 |
|
","); |
152 |
|
|
153 |
|
std::ostringstream log; |
154 |
|
log << "Loaded list '" << config << "': "; |
155 |
|
for (unsigned i=0; i<items.size(); i++) |
156 |
|
{ |
157 |
|
set.insert( Util::str_trim(items[i]) ); |
158 |
|
log << items[i] << " "; |
159 |
|
} |
160 |
|
Logger::logMessage(log.str()); |
161 |
|
} |
162 |
|
|
163 |
|
void Common::LoadLists() |
164 |
|
{ |
165 |
|
LoadWorker(_blacklist, "blacklist"); |
166 |
|
LoadWorker(_privileged, "privileged"); |
167 |
|
} |
168 |
|
|
169 |
|
|
170 |
|
bool Common::IsBlacklisted(const std::string& phone) |
171 |
|
{ |
172 |
|
std::set<std::string>::iterator it = _blacklist.find(phone); |
173 |
|
return ( it != _blacklist.end() ); |
174 |
|
} |
175 |
|
|
176 |
|
bool Common::IsPrivileged(const std::string& phone) |
177 |
|
{ |
178 |
|
std::set<std::string>::iterator it = _privileged.find(phone); |
179 |
|
return ( it != _privileged.end() ); |
180 |
|
} |