#include "SmsToolTransceiver.h" #include "Logger.h" #include "Util.h" #include "Common.h" #include #include #include #include #include #include #include #include #include #include #include using namespace std; void SmsToolTransceiver::SendSms(std::string to, std::string message, bool allowMultipart) { std::string filename = CreateFilename(); Logger::logMessage( std::string("Sending sms to: ") + to); string tempfile = "/var/spool/sms/"; tempfile += filename; string destfile = "/var/spool/sms/outgoing/"; destfile += filename; ofstream out( tempfile.c_str() ); if (!out ) { Logger::logMessage("SmsToolTransceiver:: could not create temp file"); return; } out << "To: " << to << "\n\n" << message ; out.close(); Common::instance()->smsCounter.outgoing++; int result = rename(tempfile.c_str(), destfile.c_str()); if (result) Logger::logMessage( string("SmsToolTransceiver could not spool file: ") + strerror(errno) ); } std::string SmsToolTransceiver::CreateFilename() { std::stringstream ss; ss << "smsdaemon_"; ss << std::setw(8) << std::setfill('0') << std::uppercase << std::hex << time(0) << "_"; ss << std::setw(8) << std::setfill('0') << std::uppercase << std::hex << rand(); ss << std::setw(8) << std::setfill('0') << std::uppercase << std::hex << rand(); return ss.str(); } std::vector SmsToolTransceiver::ReadSms(bool readAll) { const std::string incoming = "/var/spool/sms/incoming/"; std::vector vec; DIR* dir = opendir( incoming.c_str() ); if (dir != 0) { dirent* entry; while ( (entry = readdir(dir)) != 0) { if (entry->d_name[0] == '.') continue; Logger::logMessage( string("SmsTool::ReadSms >") +entry->d_name); try { SMS sms = ParseFile( incoming + entry->d_name); vec.push_back(sms); } catch (std::exception& e) { Logger::logMessage(e.what()); } if (!readAll) break; } } else { Logger::logMessage( string("SmsToolTransceiver could open incoming 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) { string message = "SmsTool> unlink "; message += path; message += " failed: "; message += strerror(errno); throw std::runtime_error( message ); } unsigned int pos = file.find("\n\n"); if (pos != string::npos) { string header = file.substr(0,pos); string body = file.substr(pos+2,1024); sms.SetIndex(path); sms.SetMessage(body); ParseHeaders(header, sms); } else { throw std::runtime_error("SmsTool: invalid incomming file"); } return sms; } void SmsToolTransceiver::ParseHeaders(std::string& headerstring, SMS& sms) { std::vector headers = Util::str_split(headerstring, "\n"); for (unsigned int i=0; i ") + headers[i]); } } } int SmsToolTransceiver::DeleteAllSms() { return 0; }