| 1 | // ************************************************************************* |
| 2 | // * GSM TA/ME library |
| 3 | // * |
| 4 | // * File: gsmsmsd.cc |
| 5 | // * |
| 6 | // * Purpose: SMS receiver daemon |
| 7 | // * |
| 8 | // * Author: Peter Hofmann ([email protected]) |
| 9 | // * |
| 10 | // * Created: 5.6.1999 |
| 11 | // ************************************************************************* |
| 12 | |
| 13 | #ifdef HAVE_CONFIG_H |
| 14 | #include <gsm_config.h> |
| 15 | #endif |
| 16 | #include <gsmlib/gsm_nls.h> |
| 17 | #include <string> |
| 18 | |
| 19 | #ifdef WIN32 |
| 20 | #include <io.h> |
| 21 | #include <gsmlib/gsm_util.h> |
| 22 | #include <gsmlib/gsm_win32_serial.h> |
| 23 | #define popen _popen |
| 24 | #define pclose _pclose |
| 25 | #else |
| 26 | #include <gsmlib/gsm_unix_serial.h> |
| 27 | #include <unistd.h> |
| 28 | #include <dirent.h> |
| 29 | #include <syslog.h> |
| 30 | #endif |
| 31 | #if defined(HAVE_GETOPT_LONG) || defined(WIN32) |
| 32 | #include <getopt.h> |
| 33 | #endif |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <errno.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <signal.h> |
| 39 | #include <fstream> |
| 40 | #include <iostream> |
| 41 | #include <gsmlib/gsm_me_ta.h> |
| 42 | #include <gsmlib/gsm_event.h> |
| 43 | #include <cstring> |
| 44 | |
| 45 | #ifdef HAVE_GETOPT_LONG |
| 46 | static struct option longOpts[] = |
| 47 | { |
| 48 | {"requeststat", no_argument, (int*)NULL, 'r'}, |
| 49 | {"direct", no_argument, (int*)NULL, 'D'}, |
| 50 | {"xonxoff", no_argument, (int*)NULL, 'X'}, |
| 51 | {"init", required_argument, (int*)NULL, 'I'}, |
| 52 | {"store", required_argument, (int*)NULL, 't'}, |
| 53 | {"device", required_argument, (int*)NULL, 'd'}, |
| 54 | {"spool", required_argument, (int*)NULL, 's'}, |
| 55 | {"sent", required_argument, (int*)NULL, 'S'}, |
| 56 | {"failed", required_argument, (int*)NULL, 'F'}, |
| 57 | {"priorities", required_argument, (int*)NULL, 'P'}, |
| 58 | #ifndef WIN32 |
| 59 | {"syslog", no_argument, (int*)NULL, 'L'}, |
| 60 | #endif |
| 61 | {"sca", required_argument, (int*)NULL, 'C'}, |
| 62 | {"flush", no_argument, (int*)NULL, 'f'}, |
| 63 | {"concatenate", required_argument, (int*)NULL, 'c'}, |
| 64 | {"action", required_argument, (int*)NULL, 'a'}, |
| 65 | {"baudrate", required_argument, (int*)NULL, 'b'}, |
| 66 | {"help", no_argument, (int*)NULL, 'h'}, |
| 67 | {"version", no_argument, (int*)NULL, 'v'}, |
| 68 | {(char*)NULL, 0, (int*)NULL, 0} |
| 69 | }; |
| 70 | #else |
| 71 | #define getopt_long(argc, argv, options, longopts, indexptr) \ |
| 72 | getopt(argc, argv, options) |
| 73 | #endif |
| 74 | |
| 75 | // my ME |
| 76 | |
| 77 | static gsmlib::MeTa *me = NULL; |
| 78 | std::string receiveStoreName; // store name for received SMSs |
| 79 | |
| 80 | // service centre address (set on command line) |
| 81 | |
| 82 | static std::string serviceCentreAddress; |
| 83 | |
| 84 | // ID if concatenated messages should be sent |
| 85 | |
| 86 | static int concatenatedMessageId = -1; |
| 87 | |
| 88 | // signal handler for terminate signal |
| 89 | |
| 90 | bool terminateSent = false; |
| 91 | |
| 92 | void terminateHandler(int signum) |
| 93 | { |
| 94 | terminateSent = true; |
| 95 | } |
| 96 | |
| 97 | // local class to handle SMS events |
| 98 | |
| 99 | struct IncomingMessage |
| 100 | { |
| 101 | // used if new message is put into store |
| 102 | int _index; // -1 means message want send directly |
| 103 | std::string _storeName; |
| 104 | // used if SMS message was sent directly to TA |
| 105 | gsmlib::SMSMessageRef _newSMSMessage; |
| 106 | // used if CB message was sent directly to TA |
| 107 | gsmlib::CBMessageRef _newCBMessage; |
| 108 | // used in both cases |
| 109 | gsmlib::GsmEvent::SMSMessageType _messageType; |
| 110 | |
| 111 | IncomingMessage() : _index(-1) {} |
| 112 | }; |
| 113 | |
| 114 | std::vector<IncomingMessage> newMessages; |
| 115 | |
| 116 | class EventHandler : public gsmlib::GsmEvent |
| 117 | { |
| 118 | public: |
| 119 | // inherited from GsmEvent |
| 120 | void SMSReception(gsmlib::SMSMessageRef newMessage, |
| 121 | gsmlib::GsmEvent::SMSMessageType messageType); |
| 122 | void CBReception(gsmlib::CBMessageRef newMessage); |
| 123 | void SMSReceptionIndication(std::string storeName, unsigned int index, |
| 124 | gsmlib::GsmEvent::SMSMessageType messageType); |
| 125 | |
| 126 | virtual ~EventHandler() {} |
| 127 | }; |
| 128 | |
| 129 | void EventHandler::SMSReception(gsmlib::SMSMessageRef newMessage, |
| 130 | gsmlib::GsmEvent::SMSMessageType messageType) |
| 131 | { |
| 132 | IncomingMessage m; |
| 133 | m._messageType = messageType; |
| 134 | m._newSMSMessage = newMessage; |
| 135 | newMessages.push_back(m); |
| 136 | } |
| 137 | |
| 138 | void EventHandler::CBReception(gsmlib::CBMessageRef newMessage) |
| 139 | { |
| 140 | IncomingMessage m; |
| 141 | m._messageType = gsmlib::GsmEvent::CellBroadcastSMS; |
| 142 | m._newCBMessage = newMessage; |
| 143 | newMessages.push_back(m); |
| 144 | } |
| 145 | |
| 146 | void EventHandler::SMSReceptionIndication(std::string storeName, unsigned int index, |
| 147 | gsmlib::GsmEvent::SMSMessageType messageType) |
| 148 | { |
| 149 | IncomingMessage m; |
| 150 | m._index = index; |
| 151 | |
| 152 | if (receiveStoreName != "" && ( storeName == "MT" || storeName == "mt")) |
| 153 | m._storeName = receiveStoreName; |
| 154 | else |
| 155 | m._storeName = storeName; |
| 156 | |
| 157 | m._messageType = messageType; |
| 158 | newMessages.push_back(m); |
| 159 | } |
| 160 | |
| 161 | // execute action on std::string |
| 162 | |
| 163 | void doAction(std::string action, std::string result) |
| 164 | { |
| 165 | if (action != "") |
| 166 | { |
| 167 | FILE *fd = popen(action.c_str(), "w"); |
| 168 | if (fd == NULL) |
| 169 | throw gsmlib::GsmException(gsmlib::stringPrintf(_("could not execute '%s'"), |
| 170 | action.c_str()), gsmlib::OSError); |
| 171 | fputs(result.c_str(), fd); |
| 172 | if (ferror(fd)) |
| 173 | throw gsmlib::GsmException(gsmlib::stringPrintf(_("error writing to '%s'"), |
| 174 | action.c_str()), gsmlib::OSError); |
| 175 | pclose(fd); |
| 176 | } |
| 177 | else |
| 178 | // default if no action: output on stdout |
| 179 | std::cout << result << std::endl; |
| 180 | } |
| 181 | |
| 182 | // send all SMS messages in spool dir |
| 183 | |
| 184 | bool requestStatusReport = false; |
| 185 | |
| 186 | void sendSMS(std::string spoolDirBase, std::string sentDirBase, std::string failedDirBase, |
| 187 | unsigned int priority, bool enableSyslog, gsmlib::Ref<gsmlib::GsmAt> at) |
| 188 | { |
| 189 | std::string spoolDir = spoolDirBase; |
| 190 | std::string sentDir = sentDirBase; |
| 191 | std::string failedDir = failedDirBase; |
| 192 | if ( priority >= 1 ) |
| 193 | { |
| 194 | spoolDir = spoolDirBase + gsmlib::stringPrintf(_("%d"),priority); |
| 195 | sentDir = sentDirBase + gsmlib::stringPrintf(_("%d"),priority); |
| 196 | failedDir = failedDirBase + gsmlib::stringPrintf(_("%d"),priority); |
| 197 | } |
| 198 | if ( priority > 1 ) |
| 199 | sendSMS(spoolDirBase, sentDirBase, failedDirBase, priority-1, enableSyslog, at); |
| 200 | if (spoolDirBase != "") |
| 201 | { |
| 202 | // look into spoolDir for any outgoing SMS that should be sent |
| 203 | #ifdef WIN32 |
| 204 | struct _finddata_t fileInfo; |
| 205 | long fileHandle; |
| 206 | std::string pattern = spoolDir + "\\*"; |
| 207 | fileHandle = _findfirst(pattern.c_str(), &fileInfo); |
| 208 | bool moreFiles = fileHandle != -1L; |
| 209 | #else |
| 210 | DIR *dir = opendir(spoolDir.c_str()); |
| 211 | if (dir == (DIR*)NULL) |
| 212 | throw gsmlib::GsmException( |
| 213 | gsmlib::stringPrintf(_("error when calling opendir('%s')" |
| 214 | "(errno: %d/%s)"), |
| 215 | spoolDir.c_str(), errno, strerror(errno)), |
| 216 | gsmlib::OSError); |
| 217 | #endif |
| 218 | |
| 219 | #ifdef WIN32 |
| 220 | while (moreFiles) |
| 221 | { |
| 222 | if (strcmp(fileInfo.name, ".") != 0 && |
| 223 | strcmp(fileInfo.name, "..") != 0) |
| 224 | #else |
| 225 | struct dirent *entry; |
| 226 | while ((entry = readdir(dir)) != (struct dirent*)NULL) |
| 227 | if (strcmp(entry->d_name, ".") != 0 && |
| 228 | strcmp(entry->d_name, "..") != 0) |
| 229 | #endif |
| 230 | { |
| 231 | if ( priority > 1 ) |
| 232 | sendSMS(spoolDirBase, sentDirBase, failedDirBase, priority-1, enableSyslog, at); |
| 233 | // read in file |
| 234 | // the first line is interpreted as the phone number |
| 235 | // the rest is the message |
| 236 | #ifdef WIN32 |
| 237 | std::string filename = spoolDir + "\\" + fileInfo.name; |
| 238 | #else |
| 239 | std::string filename = spoolDir + "/" + entry->d_name; |
| 240 | #endif |
| 241 | std::ifstream ifs(filename.c_str()); |
| 242 | if (! ifs) |
| 243 | { |
| 244 | #ifndef WIN32 |
| 245 | if (enableSyslog) |
| 246 | { |
| 247 | syslog(LOG_WARNING, "Could not open SMS spool file %s", |
| 248 | filename.c_str()); |
| 249 | if (failedDirBase != "") { |
| 250 | std::string failedfilename = failedDir + "/" + entry->d_name; |
| 251 | rename(filename.c_str(),failedfilename.c_str()); |
| 252 | } |
| 253 | continue; |
| 254 | } |
| 255 | else |
| 256 | #endif |
| 257 | throw gsmlib::GsmException( |
| 258 | gsmlib::stringPrintf(_("count not open SMS spool file %s"), |
| 259 | filename.c_str()), gsmlib::ParameterError); |
| 260 | } |
| 261 | char phoneBuf[1001]; |
| 262 | ifs.getline(phoneBuf, 1000); |
| 263 | for (int i=0;i<1000;i++) |
| 264 | if (phoneBuf[i]=='\t' || phoneBuf[i]==0) |
| 265 | { // ignore everything after a <TAB> in the phone number |
| 266 | phoneBuf[i]=0; |
| 267 | break; |
| 268 | } |
| 269 | std::string text; |
| 270 | while (!ifs.eof()) |
| 271 | { |
| 272 | char c; |
| 273 | ifs.get(c); |
| 274 | text += c; |
| 275 | } |
| 276 | ifs.close(); |
| 277 | |
| 278 | // remove trailing newline/linefeed |
| 279 | while (text[text.length() - 1] == '\n' || |
| 280 | text[text.length() - 1] == '\r') |
| 281 | text = text.substr(0, text.length() - 1); |
| 282 | |
| 283 | // send the message |
| 284 | std::string phoneNumber(phoneBuf); |
| 285 | gsmlib::Ref<gsmlib::SMSSubmitMessage> submitSMS = new gsmlib::SMSSubmitMessage(); |
| 286 | // set service centre address in new submit PDU if requested by user |
| 287 | if (serviceCentreAddress != "") |
| 288 | { |
| 289 | gsmlib::Address sca(serviceCentreAddress); |
| 290 | submitSMS->setServiceCentreAddress(sca); |
| 291 | } |
| 292 | submitSMS->setStatusReportRequest(requestStatusReport); |
| 293 | gsmlib::Address destAddr(phoneNumber); |
| 294 | submitSMS->setDestinationAddress(destAddr); |
| 295 | try |
| 296 | { |
| 297 | if (concatenatedMessageId == -1) |
| 298 | me->sendSMSs(submitSMS, text, true); |
| 299 | else |
| 300 | { |
| 301 | // maximum for concatenatedMessageId is 255 |
| 302 | if (concatenatedMessageId > 256) |
| 303 | concatenatedMessageId = 0; |
| 304 | me->sendSMSs(submitSMS, text, false, concatenatedMessageId++); |
| 305 | } |
| 306 | #ifndef WIN32 |
| 307 | if (enableSyslog) |
| 308 | syslog(LOG_NOTICE, "Sent SMS to %s from file %s", phoneBuf, filename.c_str()); |
| 309 | #endif |
| 310 | if (sentDirBase != "") { |
| 311 | #ifdef WIN32 |
| 312 | std::string sentfilename = sentDir + "\\" + fileInfo.name; |
| 313 | #else |
| 314 | std::string sentfilename = sentDir + "/" + entry->d_name; |
| 315 | #endif |
| 316 | rename(filename.c_str(),sentfilename.c_str()); |
| 317 | } else { |
| 318 | unlink(filename.c_str()); |
| 319 | } |
| 320 | } |
| 321 | catch (gsmlib::GsmException &me) |
| 322 | { |
| 323 | #ifndef WIN32 |
| 324 | if (enableSyslog) |
| 325 | syslog(LOG_WARNING, "Failed sending SMS to %s from file %s: %s", phoneBuf, |
| 326 | filename.c_str(), me.what()); |
| 327 | else |
| 328 | #endif |
| 329 | std::cerr << "Failed sending SMS to " << phoneBuf << " from " |
| 330 | << filename << ": " << me.what() << std::endl; |
| 331 | if (failedDirBase != "") { |
| 332 | #ifdef WIN32 |
| 333 | std::string failedfilename = failedDir + "\\" + fileInfo.name; |
| 334 | #else |
| 335 | std::string failedfilename = failedDir + "/" + entry->d_name; |
| 336 | #endif |
| 337 | rename(filename.c_str(),failedfilename.c_str()); |
| 338 | } |
| 339 | } |
| 340 | #ifdef WIN32 |
| 341 | } |
| 342 | moreFiles = _findnext(fileHandle, &fileInfo) == 0; |
| 343 | #endif |
| 344 | } |
| 345 | #ifdef WIN32 |
| 346 | _findclose(fileHandle); |
| 347 | #else |
| 348 | closedir(dir); |
| 349 | #endif |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | #ifndef WIN32 |
| 354 | void syslogExit(int exitcode, int *dummy) |
| 355 | { |
| 356 | syslog(LOG_NOTICE, "exited (exit %d)",exitcode); |
| 357 | } |
| 358 | #endif |
| 359 | |
| 360 | // *** main program |
| 361 | |
| 362 | int main(int argc, char *argv[]) |
| 363 | { |
| 364 | bool enableSyslog = false; |
| 365 | try |
| 366 | { |
| 367 | std::string device = "/dev/mobilephone"; |
| 368 | std::string action; |
| 369 | std::string baudrate; |
| 370 | bool enableSMS = true; |
| 371 | bool enableCB = true; |
| 372 | bool enableStat = true; |
| 373 | bool flushSMS = false; |
| 374 | bool onlyReceptionIndication = true; |
| 375 | std::string spoolDir; |
| 376 | std::string sentDir = ""; |
| 377 | std::string failedDir = ""; |
| 378 | unsigned int priorities = 0; |
| 379 | std::string initString = gsmlib::DEFAULT_INIT_STRING; |
| 380 | bool swHandshake = false; |
| 381 | std::string concatenatedMessageIdStr; |
| 382 | |
| 383 | int opt; |
| 384 | int dummy; |
| 385 | while((opt = getopt_long(argc, argv, "c:C:I:t:fd:a:b:hvs:S:F:P:LXDr", |
| 386 | longOpts, &dummy)) != -1) |
| 387 | switch (opt) |
| 388 | { |
| 389 | case 'c': |
| 390 | concatenatedMessageIdStr = optarg; |
| 391 | break; |
| 392 | case 'r': |
| 393 | requestStatusReport = true; |
| 394 | break; |
| 395 | case 'D': |
| 396 | onlyReceptionIndication = false; |
| 397 | break; |
| 398 | case 'X': |
| 399 | swHandshake = true; |
| 400 | break; |
| 401 | case 'I': |
| 402 | initString = optarg; |
| 403 | break; |
| 404 | case 't': |
| 405 | receiveStoreName = optarg; |
| 406 | break; |
| 407 | case 'd': |
| 408 | device = optarg; |
| 409 | break; |
| 410 | case 'C': |
| 411 | serviceCentreAddress = optarg; |
| 412 | break; |
| 413 | case 's': |
| 414 | spoolDir = optarg; |
| 415 | break; |
| 416 | case 'L': |
| 417 | enableSyslog = true; |
| 418 | break; |
| 419 | case 'S': |
| 420 | sentDir = optarg; |
| 421 | break; |
| 422 | case 'F': |
| 423 | failedDir = optarg; |
| 424 | break; |
| 425 | case 'P': |
| 426 | priorities = abs(atoi(optarg)); |
| 427 | break; |
| 428 | case 'f': |
| 429 | flushSMS = true; |
| 430 | break; |
| 431 | case 'a': |
| 432 | action = optarg; |
| 433 | break; |
| 434 | case 'b': |
| 435 | baudrate = optarg; |
| 436 | break; |
| 437 | case 'v': |
| 438 | std::cerr << argv[0] << gsmlib::stringPrintf(_(": version %s [compiled %s]"), |
| 439 | VERSION, __DATE__) << std::endl; |
| 440 | exit(0); |
| 441 | break; |
| 442 | case 'h': |
| 443 | std::cerr << argv[0] << _(": [-a action][-b baudrate][-C sca][-d device]" |
| 444 | "[-f][-h][-I init string]\n" |
| 445 | " [-s spool dir][-t][-v]{sms_type}") |
| 446 | << std::endl << std::endl |
| 447 | << _(" -a, --action the action to execute when an SMS " |
| 448 | "arrives\n" |
| 449 | " (SMS is send to stdin of action)") |
| 450 | << std::endl |
| 451 | << _(" -b, --baudrate baudrate to use for device " |
| 452 | "(default: 38400)") |
| 453 | << std::endl |
| 454 | << _(" -c, --concatenate start ID for concatenated SMS messages") |
| 455 | << std::endl |
| 456 | << _(" -C, --sca SMS service centre address") << std::endl |
| 457 | << _(" -d, --device sets the device to connect to") << std::endl |
| 458 | << _(" -D, --direct enable direct routing of SMSs") << std::endl |
| 459 | << _(" -f, --flush flush SMS from store") << std::endl |
| 460 | << _(" -F, --failed directory to move failed SMS to,") << std::endl |
| 461 | << _(" if unset, the SMS will be deleted") << std::endl |
| 462 | << _(" -h, --help prints this message") << std::endl |
| 463 | << _(" -I, --init device AT init sequence") << std::endl |
| 464 | #ifndef WIN32 |
| 465 | << _(" -L, --syslog log errors and information to syslog") |
| 466 | << std::endl |
| 467 | #endif |
| 468 | << _(" -P, --priorities number of priority levels to use,") << std::endl |
| 469 | << _(" (default: none)") << std::endl |
| 470 | << _(" -r, --requeststat request SMS status report") << std::endl |
| 471 | << _(" -s, --spool spool directory for outgoing SMS") |
| 472 | << std::endl |
| 473 | << _(" -S, --sent directory to move sent SMS to,") << std::endl |
| 474 | << _(" if unset, the SMS will be deleted") << std::endl |
| 475 | << _(" -t, --store name of SMS store to use for flush\n" |
| 476 | " and/or temporary SMS storage") << std::endl |
| 477 | << std::endl |
| 478 | << _(" -v, --version prints version and exits") << std::endl |
| 479 | << _(" -X, --xonxoff switch on software handshake") << std::endl |
| 480 | << std::endl |
| 481 | << _(" sms_type may be any combination of") << std::endl << std::endl |
| 482 | << _(" sms, no_sms controls reception of normal SMS") |
| 483 | << std::endl |
| 484 | << _(" cb, no_cb controls reception of cell broadcast" |
| 485 | " messages") << std::endl |
| 486 | << _(" stat, no_stat controls reception of status reports") |
| 487 | << std::endl << std::endl |
| 488 | << _(" default is \"sms cb stat\"") << std::endl << std::endl |
| 489 | << _("If no action is given, the SMS is printed to stdout") |
| 490 | << std::endl << std::endl |
| 491 | << _("If -P is given, it activates the priority system and sets the") << std::endl |
| 492 | << _("number or levels to use. For every level, there must be directories") << std::endl |
| 493 | << _("named <spool directory>+<priority level>.") << std::endl |
| 494 | << _("For example \"-P 2 -s queue -S send -F failed\" needs the following") <<std::endl |
| 495 | << _("directories: queue1/ queue2/ send1/ send2/ failed1/ failed2/") <<std::endl |
| 496 | << _("Before sending one SMS from queue2, all pending SMS from queue1") <<std::endl |
| 497 | << _("will be sent.") <<std::endl |
| 498 | << std::endl << std::endl; |
| 499 | exit(0); |
| 500 | break; |
| 501 | case '?': |
| 502 | throw gsmlib::GsmException(_("unknown option"), gsmlib::ParameterError); |
| 503 | break; |
| 504 | } |
| 505 | |
| 506 | // find out which kind of message to route |
| 507 | for (int i = optind; i < argc; ++i) |
| 508 | { |
| 509 | std::string s = gsmlib::lowercase(argv[i]); |
| 510 | if (s == "sms") |
| 511 | enableSMS = true; |
| 512 | else if (s == "no_sms") |
| 513 | enableSMS = false; |
| 514 | else if (s == "cb") |
| 515 | enableCB = true; |
| 516 | else if (s == "no_cb") |
| 517 | enableCB = false; |
| 518 | else if (s == "stat") |
| 519 | enableStat = true; |
| 520 | else if (s == "no_stat") |
| 521 | enableStat = false; |
| 522 | } |
| 523 | |
| 524 | // check parameters |
| 525 | if (concatenatedMessageIdStr != "") |
| 526 | concatenatedMessageId = gsmlib::checkNumber(concatenatedMessageIdStr); |
| 527 | |
| 528 | // register signal handler for terminate signal |
| 529 | #ifndef WIN32 |
| 530 | struct sigaction terminateAction; |
| 531 | terminateAction.sa_handler = terminateHandler; |
| 532 | sigemptyset(&terminateAction.sa_mask); |
| 533 | terminateAction.sa_flags = SA_RESTART; |
| 534 | if (sigaction(SIGINT, &terminateAction, NULL) != 0 || |
| 535 | sigaction(SIGTERM, &terminateAction, NULL) != 0) |
| 536 | #else |
| 537 | if(signal(SIGINT, terminateHandler) == SIG_ERR || |
| 538 | signal(SIGTERM, terminateHandler) == SIG_ERR) |
| 539 | #endif |
| 540 | throw gsmlib::GsmException( |
| 541 | gsmlib::stringPrintf(_("error when calling sigaction() (errno: %d/%s)"), |
| 542 | errno, strerror(errno)), |
| 543 | gsmlib::OSError); |
| 544 | |
| 545 | // open GSM device |
| 546 | me = new gsmlib::MeTa(new |
| 547 | #ifdef WIN32 |
| 548 | gsmlib::Win32SerialPort |
| 549 | #else |
| 550 | gsmlib::UnixSerialPort |
| 551 | #endif |
| 552 | (device, |
| 553 | baudrate == "" ? gsmlib::DEFAULT_BAUD_RATE : |
| 554 | gsmlib::baudRateStrToSpeed(baudrate), initString, |
| 555 | swHandshake)); |
| 556 | |
| 557 | // if flush option is given get all SMS from store and dispatch them |
| 558 | if (flushSMS) |
| 559 | { |
| 560 | if (receiveStoreName == "") |
| 561 | throw gsmlib::GsmException(_("store name must be given for flush option"), |
| 562 | gsmlib::ParameterError); |
| 563 | |
| 564 | gsmlib::SMSStoreRef store = me->getSMSStore(receiveStoreName); |
| 565 | |
| 566 | for (gsmlib::SMSStore::iterator s = store->begin(); s != store->end(); ++s) |
| 567 | if (! s->empty()) |
| 568 | { |
| 569 | std::string result = _("Type of message: "); |
| 570 | switch (s->message()->messageType()) |
| 571 | { |
| 572 | case gsmlib::SMSMessage::SMS_DELIVER: |
| 573 | result += _("SMS message\n"); |
| 574 | break; |
| 575 | case gsmlib::SMSMessage::SMS_SUBMIT_REPORT: |
| 576 | result += _("submit report message\n"); |
| 577 | break; |
| 578 | case gsmlib::SMSMessage::SMS_STATUS_REPORT: |
| 579 | result += _("status report message\n"); |
| 580 | break; |
| 581 | } |
| 582 | result += s->message()->toString(); |
| 583 | doAction(action, result); |
| 584 | store->erase(s); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | // set default SMS store if -t option was given or |
| 589 | // read from ME otherwise |
| 590 | if (receiveStoreName == "") |
| 591 | { |
| 592 | std::string dummy1, dummy2; |
| 593 | me->getSMSStore(dummy1, dummy2, receiveStoreName ); |
| 594 | } |
| 595 | else |
| 596 | me->setSMSStore(receiveStoreName, 3); |
| 597 | |
| 598 | // switch message service level to 1 |
| 599 | // this enables SMS routing to TA |
| 600 | me->setMessageService(1); |
| 601 | |
| 602 | // switch on SMS routing |
| 603 | me->setSMSRoutingToTA(enableSMS, enableCB, enableStat, |
| 604 | onlyReceptionIndication); |
| 605 | |
| 606 | // register event handler to handle routed SMSs, CBMs, and status reports |
| 607 | me->setEventHandler(new EventHandler()); |
| 608 | |
| 609 | // wait for new messages |
| 610 | bool exitScheduled = false; |
| 611 | while (1) |
| 612 | { |
| 613 | #ifdef WIN32 |
| 614 | ::timeval timeoutVal; |
| 615 | timeoutVal.tv_sec = 5; |
| 616 | timeoutVal.tv_usec = 0; |
| 617 | me->waitEvent((gsmlib::timeval *)&timeoutVal); |
| 618 | #else |
| 619 | struct timeval timeoutVal; |
| 620 | timeoutVal.tv_sec = 5; |
| 621 | timeoutVal.tv_usec = 0; |
| 622 | me->waitEvent(&timeoutVal); |
| 623 | #endif |
| 624 | // if it returns, there was an event or a timeout |
| 625 | while (newMessages.size() > 0) |
| 626 | { |
| 627 | // get first new message and remove it from the vector |
| 628 | gsmlib::SMSMessageRef newSMSMessage = newMessages.begin()->_newSMSMessage; |
| 629 | gsmlib::CBMessageRef newCBMessage = newMessages.begin()->_newCBMessage; |
| 630 | gsmlib::GsmEvent::SMSMessageType messageType = |
| 631 | newMessages.begin()->_messageType; |
| 632 | int index = newMessages.begin()->_index; |
| 633 | std::string storeName = newMessages.begin()->_storeName; |
| 634 | newMessages.erase(newMessages.begin()); |
| 635 | |
| 636 | // process the new message |
| 637 | std::string result = _("Type of message: "); |
| 638 | switch (messageType) |
| 639 | { |
| 640 | case gsmlib::GsmEvent::NormalSMS: |
| 641 | result += _("SMS message\n"); |
| 642 | break; |
| 643 | case gsmlib::GsmEvent::CellBroadcastSMS: |
| 644 | result += _("cell broadcast message\n"); |
| 645 | break; |
| 646 | case gsmlib::GsmEvent::StatusReportSMS: |
| 647 | result += _("status report message\n"); |
| 648 | break; |
| 649 | } |
| 650 | if (!newSMSMessage.isnull()) |
| 651 | result += newSMSMessage->toString(); |
| 652 | else if (! newCBMessage.isnull()) |
| 653 | result += newCBMessage->toString(); |
| 654 | else |
| 655 | { |
| 656 | gsmlib::SMSStoreRef store = me->getSMSStore(storeName); |
| 657 | store->setCaching(false); |
| 658 | |
| 659 | if (messageType == gsmlib::GsmEvent::CellBroadcastSMS) |
| 660 | result += (*store.getptr())[index].cbMessage()->toString(); |
| 661 | else |
| 662 | result += (*store.getptr())[index].message()->toString(); |
| 663 | |
| 664 | store->erase(store->begin() + index); |
| 665 | } |
| 666 | |
| 667 | // call the action |
| 668 | doAction(action, result); |
| 669 | } |
| 670 | |
| 671 | // if no new SMS came in and program exit was scheduled, then exit |
| 672 | if (exitScheduled) |
| 673 | exit(0); |
| 674 | |
| 675 | // handle terminate signal |
| 676 | if (terminateSent) |
| 677 | { |
| 678 | exitScheduled = true; |
| 679 | // switch off SMS routing |
| 680 | try |
| 681 | { |
| 682 | me->setSMSRoutingToTA(false, false, false); |
| 683 | } |
| 684 | catch (gsmlib::GsmException &ge) |
| 685 | { |
| 686 | // some phones (e.g. Motorola Timeport 260) don't allow to switch |
| 687 | // off SMS routing which results in an error. Just ignore this. |
| 688 | } |
| 689 | // the AT sequences involved in switching of SMS routing |
| 690 | // may yield more SMS events, so go round the loop one more time |
| 691 | } |
| 692 | |
| 693 | // send spooled SMS |
| 694 | if (!terminateSent) |
| 695 | sendSMS(spoolDir, sentDir, failedDir, priorities, enableSyslog, me->getAt()); |
| 696 | } |
| 697 | } |
| 698 | catch (gsmlib::GsmException &ge) |
| 699 | { |
| 700 | std::cerr << argv[0] << _("[ERROR]: ") << ge.what() << std::endl; |
| 701 | if (ge.getErrorClass() == gsmlib::MeTaCapabilityError) |
| 702 | std::cerr << argv[0] << _("[ERROR]: ") |
| 703 | << _("(try setting sms_type, please refer to gsmsmsd manpage)") |
| 704 | << std::endl; |
| 705 | // switch off message routing, so that following invocations of gsmsmd |
| 706 | // are not swamped with message deliveries while they start up |
| 707 | if (me != NULL) |
| 708 | { |
| 709 | try |
| 710 | { |
| 711 | me->setSMSRoutingToTA(false, false, false); |
| 712 | } |
| 713 | catch (gsmlib::GsmException &ge) |
| 714 | { |
| 715 | // some phones (e.g. Motorola Timeport 260) don't allow to switch |
| 716 | // off SMS routing which results in an error. Just ignore this. |
| 717 | } |
| 718 | } |
| 719 | return 1; |
| 720 | } |
| 721 | return 0; |
| 722 | } |