--- smsdaemon/SmsToolTransceiver.cpp 2008/12/09 09:35:36 162 +++ smsdaemon/SmsToolTransceiver.cpp 2008/12/21 21:14:40 210 @@ -3,6 +3,7 @@ #include "Logger.h" #include "Util.h" #include "Common.h" +#include "ConfigFile.h" #include #include @@ -19,16 +20,54 @@ using namespace std; +string appendSlash(string& str) +{ + if (str.at(str.length()-1) != '/'); + str += "/"; + return str; +} + +SmsToolTransceiver::SmsToolTransceiver() +{ + Common* cmn = Common::instance(); + + try + { + inboxdir = cmn->GetConfigfile()->GetValue("smstools","inboxdir").StringValue(); + } + catch (...) + { + Logger::logMessage("Config error> smstools::inboxdir not specified"); + exit(1); + } + try + { + outgoingdir = cmn->GetConfigfile()->GetValue("smstools","outgoingdir").StringValue(); + } + catch (...) + { + Logger::logMessage("Config error> smstools::outgoingdir not specified"); + exit(1); + } + + inboxdir = appendSlash(inboxdir); + outgoingdir = appendSlash(outgoingdir); + +} + + void SmsToolTransceiver::SendSms(std::string to, std::string message, bool allowMultipart) { + if (allowMultipart == false && message.size() > 160) + message = message.substr(0,160); + + std::string filename = CreateFilename(); Logger::logMessage( std::string("Sending sms to: ") + to); - string tempfile = "/var/spool/sms/"; - tempfile += filename; + string tempfile = outgoingdir + filename + ".LOCK"; - string destfile = "/var/spool/sms/outgoing/"; - destfile += filename; + string destfile = outgoingdir + filename; ofstream out( tempfile.c_str() ); if (!out ) @@ -40,7 +79,7 @@ out.close(); Common::instance()->smsCounter.outgoing++; - + int result = rename(tempfile.c_str(), destfile.c_str()); if (result) @@ -59,11 +98,10 @@ std::vector SmsToolTransceiver::ReadSms(bool readAll) { - const std::string incoming = "/var/spool/sms/incoming/"; std::vector vec; - DIR* dir = opendir( incoming.c_str() ); + DIR* dir = opendir( inboxdir.c_str() ); - if (dir != 0) + if (dir != 0) { dirent* entry; while ( (entry = readdir(dir)) != 0) @@ -74,10 +112,10 @@ try { - SMS sms = ParseFile( incoming + entry->d_name); + SMS sms = ParseFile( inboxdir + entry->d_name); vec.push_back(sms); - } - catch (std::exception& e) + } + catch (std::exception& e) { Logger::logMessage(e.what()); } @@ -88,27 +126,24 @@ } else { - Logger::logMessage( string("SmsToolTransceiver could open incoming dir ") + strerror(errno) ); + Logger::logMessage( string("SmsToolTransceiver could open inbox dir ") + strerror(errno) ); } closedir(dir); return vec; } -void SmsToolTransceiver::DeleteSms(std::string smsIndex) -{ -} SMS SmsToolTransceiver::ParseFile(std::string path) { SMS sms; - + string file = Util::readfile(path); int result = ::unlink(path.c_str()); - if (result) + if (result) { string message = "SmsTool> unlink "; - message += path; + message += path; message += " failed: "; message += strerror(errno); throw std::runtime_error( message ); @@ -117,7 +152,8 @@ unsigned int pos = file.find("\n\n"); - if (pos != string::npos) { + if (pos != string::npos) + { string header = file.substr(0,pos); string body = file.substr(pos+2,1024); @@ -126,7 +162,9 @@ ParseHeaders(header, sms); - } else { + } + else + { throw std::runtime_error("SmsTool: invalid incomming file"); } @@ -137,7 +175,7 @@ { std::vector headers = Util::str_split(headerstring, "\n"); - for (unsigned int i=0; i