13 // SMS queuing application for use with asterisk app_sms
14 // by Adrian Kennard, 2004
16 /* reads next USC character from null terminated UTF-8 string and advanced pointer */
17 /* for non valid UTF-8 sequences, returns character as is */
18 /* Does not advance pointer for null termination */
19 static int utf8decode (unsigned char **pp)
21 unsigned char *p = *pp;
23 return 0; /* null termination of string */
26 return *p; /* ascii or continuation character */
29 if (*p < 0xC2 || (p[1] & 0xC0) != 0x80)
30 return *p; /* not valid UTF-8 */
32 return ((*p & 0x1F) << 6) + (p[1] & 0x3F);
36 if ((*p == 0xE0 && p[1] < 0xA0) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80)
37 return *p; /* not valid UTF-8 */
39 return ((*p & 0x0F) << 12) + ((p[1] & 0x3F) << 6) + (p[2] & 0x3F);
43 if ((*p == 0xF0 && p[1] < 0x90) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80)
44 return *p; /* not valid UTF-8 */
46 return ((*p & 0x07) << 18) + ((p[1] & 0x3F) << 12) + ((p[2] & 0x3F) << 6) + (p[3] & 0x3F);
50 if ((*p == 0xF8 && p[1] < 0x88) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80
51 || (p[4] & 0xC0) != 0x80)
52 return *p; /* not valid UTF-8 */
54 return ((*p & 0x03) << 24) + ((p[1] & 0x3F) << 18) + ((p[2] & 0x3F) << 12) + ((p[3] & 0x3F) << 6) + (p[4] & 0x3F);
58 if ((*p == 0xFC && p[1] < 0x84) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80
59 || (p[4] & 0xC0) != 0x80 || (p[5] & 0xC0) != 0x80)
60 return *p; /* not valid UTF-8 */
62 return ((*p & 0x01) << 30) + ((p[1] & 0x3F) << 24) + ((p[2] & 0x3F) << 18) + ((p[3] & 0x3F) << 12) + ((p[4] & 0x3F) << 6) +
65 return *p; /* not sensible */
68 // check for any queued messages in specific queue (queue="" means any queue)
69 // returns 0 if nothing queued, 1 if queued and outgoing set up OK, 2 of outgoing exists
70 static char txqcheck (char *dir, char *queue, char subaddress, char *channel, char *callerid, int wait, int delay, int retries, int concurrent)
78 int ql = strlen (queue);
80 snprintf (dirname, sizeof(dirname), "sms/%s", dir);
81 d = opendir (dirname);
84 while ((fn = readdir (d))
85 && !(*fn->d_name != '.'
86 && ((!ql && (p = strchr (fn->d_name, '.'))) || (ql && !strncmp (fn->d_name, queue, ql) && fn->d_name[ql] == '.'))));
93 { // not searching any specific queue, so use whatr we found as the queue
97 p = strchr (queue, '-');
98 if (p && p < queue + ql)
103 snprintf (temp, sizeof(temp), "sms/.smsq-%d", getpid ());
104 f = fopen (temp, "w");
111 fprintf (f, "Channel: ");
113 fprintf (f, "Local/%.*s\n", ql, queue);
116 p = strchr (channel, '/');
121 fprintf (f, "%.*s%c%s\n", p - channel, channel, subaddress, p + 1);
123 fprintf (f, "%s\n", channel);
125 fprintf (f, "Callerid: SMS <");
127 fprintf (f, "%.*s", ql, queue);
130 p = strchr (callerid, 'X');
132 fprintf (f, "%.*s%c%s", p - callerid, callerid, subaddress, p + 1);
134 fprintf (f, "%s", callerid);
137 fprintf (f, "Application: SMS\n");
138 fprintf (f, "Data: %.*s", ql, queue);
141 fprintf (f, "\nMaxRetries: %d\n", retries);
142 fprintf (f, "RetryTime: %d\n", delay);
143 fprintf (f, "WaitTime: %d\n", wait);
148 while (try < concurrent)
151 snprintf(ogname, sizeof(ogname), "outgoing/smsq.%s.%s.%d", dir, queue, try);
152 if (!link (temp, ogname))
159 // failed to create call queue
164 // Process received queue entries and run through a process, setting environment variables
165 static void rxqcheck (char *dir, char *queue, char *process)
171 int ql = strlen (queue);
173 snprintf(temp, sizeof(temp), "sms/.smsq-%d", getpid ());
174 snprintf(dirname, sizeof(dirname), "sms/%s", dir);
175 d = opendir (dirname);
178 while ((fn = readdir (d)))
179 if ((*fn->d_name != '.'
180 && ((!ql && (p = strchr (fn->d_name, '.'))) || (ql && !strncmp (fn->d_name, queue, ql) && fn->d_name[ql] == '.'))))
184 unsigned short ud[160];
185 unsigned char udl = 0;
187 snprintf (filename, sizeof(filename), "sms/%s/%s", dir, fn->d_name);
188 if (rename (filename, temp))
189 continue; // cannot access file
190 f = fopen (temp, "r");
215 setenv ("queue", queue, 1);
217 while (fgets (line, sizeof (line), f))
219 for (p = line; *p && *p != '\n' && *p != '\r'; p++);
220 *p = 0; /* strip eoln */
222 if (!*p || *p == ';')
223 continue; /* blank line or comment, ignore */
234 if (!strcmp (line, "oa") || !strcmp (line, "da") || !strcmp (line, "scts") || !strcmp (line, "pid")
235 || !strcmp (line, "dcs") || !strcmp (line, "mr") || !strcmp (line, "vp"))
237 else if ((!strcmp (line, "srr") || !strcmp (line, "rp")) && atoi (p))
238 setenv (line, "", 1);
239 else if (!strcmp (line, "ud"))
240 { // read the user data as UTF-8
243 while ((v = utf8decode (&p)) && udl < 160)
244 if (v && v <= 0xFFFF)
247 } else if (*p == '#')
253 if (!strcmp (line, "udh"))
255 else if (!strcmp (line, "ud"))
256 { // read user data UCS-2
258 while (*p && udl < 160)
260 if (isxdigit (*p) && isxdigit (p[1]) && isxdigit (p[2]) && isxdigit (p[3]))
263 (((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 12) +
264 (((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF)) << 8) +
265 (((isalpha (p[2]) ? 9 : 0) + (p[2] & 0xF)) << 4) + ((isalpha (p[3]) ? 9 : 0) + (p[3] & 0xF));
273 if (!strcmp (line, "ud"))
274 { // read user data UCS-1
276 while (*p && udl < 160)
278 if (isxdigit (*p) && isxdigit (p[1]))
280 ud[udl++] = (((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF));
290 // set up user data variables
295 for (n = 0, p = 0; p < udl; p++)
297 unsigned short v = ud[p];
304 temp[n++] = (0xC0 + (v >> 6));
305 temp[n++] = (0x80 + (v & 0x3F));
308 temp[n++] = (0xE0 + (v >> 12));
309 temp[n++] = (0x80 + ((v >> 6) & 0x3F));
310 temp[n++] = (0x80 + (v & 0x3F));
315 setenv ("ud", temp, 1);
316 for (n = 0, p = 0; p < udl; p++)
318 unsigned short v = ud[p];
319 if (v < ' ' || v == '\\')
334 temp[n++] = '0' + (v >> 6);
335 temp[n++] = '0' + ((v >> 3) & 7);
336 temp[n++] = '0' + (v & 7);
342 temp[n++] = (0xC0 + (v >> 6));
343 temp[n++] = (0x80 + (v & 0x3F));
346 temp[n++] = (0xE0 + (v >> 12));
347 temp[n++] = (0x80 + ((v >> 6) & 0x3F));
348 temp[n++] = (0x80 + (v & 0x3F));
352 setenv ("ude", temp, 1);
353 for (p = 0; p < udl && ud[p] < 0x100; p++);
356 for (n = 0, p = 0; p < udl; p++)
358 sprintf (temp + n, "%02X", ud[p]);
361 setenv ("ud8", temp, 1);
363 for (n = 0, p = 0; p < udl; p++)
365 sprintf (temp + n, "%04X", ud[p]);
368 setenv ("ud16", temp, 1);
378 main (int argc, const char *argv[])
404 unsigned short ud[160];
405 unsigned char *uds = 0,
412 *spooldir = "/var/spool/asterisk",
413 *motxchannel = "Local/1709400X",
416 *mttxcallerid = "080058752X0",
417 *defaultsubaddress = "9",
420 poptContext optCon; // context for parsing command-line options
421 const struct poptOption optionsTable[] = {
422 {"queue", 'q', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &queue, 0, "Queue [inc sub address]", "number[-X]"},
423 {"da", 'd', POPT_ARG_STRING, &da, 0, "Destination address", "number"},
424 {"oa", 'o', POPT_ARG_STRING, &oa, 0, "Origination address", "number"},
425 {"ud", 'm', POPT_ARG_STRING, &uds, 0, "Message", "text"},
426 {"ud-file", 'f', POPT_ARG_STRING, &udfile, 0, "Message file", "filename"},
427 {"UTF-8", 0, POPT_ARG_NONE, &utf8, 0, "File treated as null terminated UTF-8 (default)", 0},
428 {"UCS-1", 0, POPT_ARG_NONE, &ucs1, 0, "File treated as UCS-1", 0},
429 {"UCS-2", 0, POPT_ARG_NONE, &ucs2, 0, "File treated as UCS-2", 0},
430 {"mt", 't', POPT_ARG_NONE, &mt, 0, "Mobile Terminated", 0},
431 {"mo", 0, POPT_ARG_NONE, &mo, 0, "Mobile Originated", 0},
432 {"tx", 0, POPT_ARG_NONE, &tx, 0, "Send message", 0},
433 {"rx", 'r', POPT_ARG_NONE, &rx, 0, "Queue for receipt", 0},
434 {"process", 'e', POPT_ARG_STRING, &process, 0, "Rx queue process command", "command"},
435 {"no-dial", 'x', POPT_ARG_NONE, &nodial, 0, "Do not dial", 0},
436 {"no-wait", 0, POPT_ARG_NONE, &nowait, 0, "Do not wait if already calling", 0},
437 {"concurrent", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &concurrent, 0, "Number of concurrent calls to allow", "n"},
438 {"motx-channel", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &motxchannel, 0, "Channel for motx calls", "channel"},
439 {"motx-callerid", 0, POPT_ARG_STRING, &motxcallerid, 0,
440 "Caller ID for motx calls (default is queue name without sub address)", "number"},
441 {"motx-wait", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &motxwait, 0, "Time to wait for motx call to answer",
443 {"motx-delay", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &motxdelay, 0, "Time between motx call retries", "seconds"},
444 {"motx-retries", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &motxretries, 0, "Number of retries for motx call", "n"},
445 {"mttx-channel", 0, POPT_ARG_STRING, &mttxchannel, 0,
446 "Channel for mttx calls (default is Local/ and queue name without sub address)", "channel"},
447 {"mttx-callerid", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &mttxcallerid, 0,
448 "Caller ID for mttx calls (default is queue name without sub address)", "number"},
449 {"mttx-wait", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &mttxwait, 0, "Time to wait for mttx call to answer",
451 {"mttx-delay", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &mttxdelay, 0, "Time between mttx call retries", "seconds"},
452 {"mttx-retries", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &mttxretries, 0, "Number of retries for mttx call", "n"},
453 {"mr", 'n', POPT_ARG_INT, &mr, 0, "Message reference", "n"},
454 {"pid", 'p', POPT_ARG_INT, &pid, 0, "Protocol ID", "n"},
455 {"dcs", 'c', POPT_ARG_INT, &dcs, 0, "Data Coding Scheme", "n"},
456 {"udh", 0, POPT_ARG_STRING, &udh, 0, "User data header", "hex"},
457 {"srr", 0, POPT_ARG_NONE, &srr, 0, "Status Report Request", 0},
458 {"rp", 0, POPT_ARG_NONE, &rp, 0, "Return Path request", 0},
459 {"v", 0, POPT_ARG_INT, &vp, 0, "Validity Period", "seconds"},
460 {"scts", 0, POPT_ARG_STRING, &scts, 0, "Timestamp", "YYYY-MM-SSTHH:MM:SS"},
461 {"default-sub-address", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &defaultsubaddress, 0, "Default sub address", "X"},
462 {"spool-dir", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &spooldir, 0, "Asterisk spool dir", "dirname"},
463 POPT_AUTOHELP {NULL, 0, 0, NULL, 0}
466 optCon = poptGetContext (NULL, argc, argv, optionsTable, 0);
467 poptSetOtherOptionHelp (optCon, "<oa/da> <message>");
469 /* Now do options processing, get portname */
470 if ((c = poptGetNextOpt (optCon)) < -1)
472 /* an error occurred during option processing */
473 fprintf (stderr, "%s: %s\n", poptBadOption (optCon, POPT_BADOPTION_NOALIAS), poptStrerror (c));
478 if (utf8 + ucs1 + ucs2 > 1)
480 fprintf (stderr, "Pick one of UTF-8, UCS-1 or UCS-2 only\n");
483 if (!udfile && (ucs1 || ucs2))
485 fprintf (stderr, "Command line arguments always treated as UTF-8\n");
488 // if (!where && poptPeekArg (optCon)) where = (char *) poptGetArg (optCon);
489 if (!mt && !mo && process)
491 if (!mt && !mo && oa)
497 fprintf (stderr, "Cannot be --mt and --mo\n");
500 if (!rx && !tx && process)
506 fprintf (stderr, "Cannot be --tx and --rx\n");
513 fprintf (stderr, "Cannot have --ud and --ud-file\n");
516 if (mo && !da && poptPeekArg (optCon))
517 da = (char *) poptGetArg (optCon);
518 if (mt && !oa && poptPeekArg (optCon))
519 oa = (char *) poptGetArg (optCon);
522 fprintf (stderr, "--oa makes no sense with --mo as CLI is used (i.e. queue name)\n");
527 fprintf (stderr, "--da makes no sense with --mt as called number is used (i.e. queue name)\n");
530 if (da && strlen (da) > 20)
532 fprintf (stderr, "--da too long\n");
535 if (oa && strlen (oa) > 20)
537 fprintf (stderr, "--oa too long\n");
540 if (queue && strlen (queue) > 20)
542 fprintf (stderr, "--queue name too long\n");
547 fprintf (stderr, "scts is set my service centre\n");
551 { /* simple user data command line option in \UTF-8 */
552 while (udl < 160 && *uds)
554 int v = utf8decode (&uds);
557 fprintf (stderr, "Invalid character U+%X at %d\n", v, udl);
563 if (!uds && !udfile && poptPeekArg (optCon))
564 { /* multiple command line arguments in UTF-8 */
565 while (poptPeekArg (optCon) && udl < 160)
567 unsigned char *a = (char *) poptGetArg (optCon);
568 if (udl && udl < 160)
569 ud[udl++] = ' '; /* space between arguments */
570 while (udl < 160 && *a)
572 int v = utf8decode (&a);
575 fprintf (stderr, "Invalid character U+%X at %d\n", v, udl);
582 if (poptPeekArg (optCon))
584 fprintf (stderr, "Unknown argument %s\n", poptGetArg (optCon));
588 { // get message from file
589 unsigned char dat[1204],
595 f = open (udfile, O_RDONLY);
603 n = read (f, dat, sizeof (dat));
614 while (p < e && udl < 160 && *p)
615 ud[udl++] = utf8decode (&p);
618 while (p < e && udl < 160)
622 while (p + 1 < e && udl < 160)
624 ud[udl++] = (*p << 8) + p[1];
631 char *d = strrchr (queue, '-');
635 subaddress = *defaultsubaddress;
638 if (chdir (spooldir))
648 *dir = (mo ? rx ? "sms/morx" : "sms/motx" : rx ? "sms/mtrx" : "sms/mttx");
650 snprintf (temp, sizeof(temp), "sms/.smsq-%d", getpid ());
651 mkdir ("sms", 0777); // ensure directory exists
652 mkdir (dir, 0777); // ensure directory exists
653 snprintf (queuename, sizeof(queuename), "%s/%s.%ld-%d", dir, *queue ? queue : "0", (long)time (0), getpid ());
654 f = fopen (temp, "w");
661 fprintf (f, "oa=%s\n", oa);
663 fprintf (f, "da=%s\n", da);
665 fprintf (f, "scts=%s\n", scts);
667 fprintf (f, "pid=%d\n", pid);
669 fprintf (f, "dcs=%d\n", dcs);
671 fprintf (f, "mr=%d\n", mr);
673 fprintf (f, "srr=1\n");
675 fprintf (f, "rp=1\n");
677 fprintf (f, "udh#%s\n", udh);
679 fprintf (f, "vp=%d\n", vp);
683 for (p = 0; p < udl && ud[p] < 0x100; p++);
686 for (p = 0; p < udl && ud[p] < 0x80 && ud[p] >= 0x20; p++);
690 for (p = 0; p < udl; p++)
693 { /* use one byte hex */
695 for (p = 0; p < udl; p++)
696 fprintf (f, "%02X", ud[p]);
699 { /* use two byte hex */
701 for (p = 0; p < udl; p++)
702 fprintf (f, "%04X", ud[p]);
707 if (rename (temp, queuename))
715 if (!nodial && tx && !process)
716 { // dial to send messages
724 ret = txqcheck ("motx", queue, subaddress, motxchannel, motxcallerid, motxwait, motxdelay, motxretries, concurrent);
726 ret = txqcheck ("mttx", queue, subaddress, mttxchannel, mttxcallerid, mttxwait, mttxdelay, mttxretries, concurrent);
728 break; // sent, or queued OK
732 if (ret == 2 && !nowait)
733 fprintf (stderr, "No call scheduled as already sending\n");
736 rxqcheck (mo ? rx ? "morx" : "motx" : rx ? "mtrx" : "mttx", queue, process);