13 #include <solaris-compat/compat.h>
14 #define POPT_ARGFLAG_SHOW_DEFAULT 0x00800000
16 #if !defined(POPT_ARGFLAG_SHOW_DEFAULT)
17 #define POPT_ARGFLAG_SHOW_DEFAULT 0x00800000
20 /* SMS queuing application for use with asterisk app_sms */
21 /* by Adrian Kennard, 2004 - 2005 */
23 /* reads next USC character from null terminated UTF-8 string and advanced pointer */
24 /* for non valid UTF-8 sequences, returns character as is */
25 /* Does not advance pointer for null termination */
26 static int utf8decode (unsigned char **pp)
28 unsigned char *p = *pp;
30 return 0; /* null termination of string */
33 return *p; /* ascii or continuation character */
36 if (*p < 0xC2 || (p[1] & 0xC0) != 0x80)
37 return *p; /* not valid UTF-8 */
39 return ((*p & 0x1F) << 6) + (p[1] & 0x3F);
43 if ((*p == 0xE0 && p[1] < 0xA0) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80)
44 return *p; /* not valid UTF-8 */
46 return ((*p & 0x0F) << 12) + ((p[1] & 0x3F) << 6) + (p[2] & 0x3F);
50 if ((*p == 0xF0 && p[1] < 0x90) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80)
51 return *p; /* not valid UTF-8 */
53 return ((*p & 0x07) << 18) + ((p[1] & 0x3F) << 12) + ((p[2] & 0x3F) << 6) + (p[3] & 0x3F);
57 if ((*p == 0xF8 && p[1] < 0x88) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80
58 || (p[4] & 0xC0) != 0x80)
59 return *p; /* not valid UTF-8 */
61 return ((*p & 0x03) << 24) + ((p[1] & 0x3F) << 18) + ((p[2] & 0x3F) << 12) + ((p[3] & 0x3F) << 6) + (p[4] & 0x3F);
65 if ((*p == 0xFC && p[1] < 0x84) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80
66 || (p[4] & 0xC0) != 0x80 || (p[5] & 0xC0) != 0x80)
67 return *p; /* not valid UTF-8 */
69 return ((*p & 0x01) << 30) + ((p[1] & 0x3F) << 24) + ((p[2] & 0x3F) << 18) + ((p[3] & 0x3F) << 12) + ((p[4] & 0x3F) << 6) +
72 return *p; /* not sensible */
75 /* check for any queued messages in specific queue (queue="" means any queue) */
76 /* returns 0 if nothing queued, 1 if queued and outgoing set up OK, 2 of outgoing exists */
77 static char txqcheck (char *dir, char *queue, char subaddress, char *channel, char *callerid, int wait, int delay, int retries, int concurrent)
85 int ql = strlen (queue), qfl = ql;
87 snprintf (dirname, sizeof(dirname), "sms/%s", dir);
88 d = opendir (dirname);
91 while ((fn = readdir (d))
92 && !(*fn->d_name != '.'
93 && ((!ql && (p = strchr (fn->d_name, '.'))) || (ql && !strncmp (fn->d_name, queue, ql) && fn->d_name[ql] == '.'))));
100 { /* not searching any specific queue, so use whatr we found as the queue */
102 qfl = ql = p - queue;
104 p = strchr (queue, '-');
105 if (p && p < queue + ql)
110 snprintf (temp, sizeof(temp), "sms/.smsq-%d", getpid ());
111 f = fopen (temp, "w");
118 fprintf (f, "Channel: ");
120 fprintf (f, "Local/%.*s\n", ql, queue);
123 p = strchr (channel, '/');
128 fprintf (f, "%.*s%c%s\n", p - channel, channel, subaddress, p + 1);
130 fprintf (f, "%s\n", channel);
132 fprintf (f, "Callerid: SMS <");
134 fprintf (f, "%.*s", ql, queue);
137 p = strchr (callerid, 'X');
139 fprintf (f, "%.*s%c%s", p - callerid, callerid, subaddress, p + 1);
141 fprintf (f, "%s", callerid);
144 fprintf (f, "Application: SMS\n");
145 fprintf (f, "Data: %.*s", qfl, queue);
148 fprintf (f, "\nMaxRetries: %d\n", retries);
149 fprintf (f, "RetryTime: %d\n", delay);
150 fprintf (f, "WaitTime: %d\n", wait);
155 while (try < concurrent)
158 snprintf(ogname, sizeof(ogname), "outgoing/smsq.%s.%s.%d", dir, queue, try);
159 if (!link (temp, ogname))
166 /* failed to create call queue */
171 /* Process received queue entries and run through a process, setting environment variables */
172 static void rxqcheck (char *dir, char *queue, char *process)
178 int ql = strlen (queue);
180 snprintf(temp, sizeof(temp), "sms/.smsq-%d", getpid ());
181 snprintf(dirname, sizeof(dirname), "sms/%s", dir);
182 d = opendir (dirname);
185 while ((fn = readdir (d)))
186 if ((*fn->d_name != '.'
187 && ((!ql && (p = strchr (fn->d_name, '.'))) || (ql && !strncmp (fn->d_name, queue, ql) && fn->d_name[ql] == '.'))))
191 unsigned short ud[160];
192 unsigned char udl = 0;
194 snprintf (filename, sizeof(filename), "sms/%s/%s", dir, fn->d_name);
195 if (rename (filename, temp))
196 continue; /* cannot access file */
197 f = fopen (temp, "r");
222 setenv ("queue", queue, 1);
224 while (fgets (line, sizeof (line), f))
226 for (p = line; *p && *p != '\n' && *p != '\r'; p++);
227 *p = 0; /* strip eoln */
229 if (!*p || *p == ';')
230 continue; /* blank line or comment, ignore */
241 if (!strcmp (line, "oa") || !strcmp (line, "da") || !strcmp (line, "scts") || !strcmp (line, "pid")
242 || !strcmp (line, "dcs") || !strcmp (line, "mr") || !strcmp (line, "vp"))
244 else if ((!strcmp (line, "srr") || !strcmp (line, "rp")) && atoi (p))
245 setenv (line, "", 1);
246 else if (!strcmp (line, "ud"))
247 { /* read the user data as UTF-8 */
250 while ((v = utf8decode (&p)) && udl < 160)
251 if (v && v <= 0xFFFF)
254 } else if (*p == '#')
260 if (!strcmp (line, "udh"))
262 else if (!strcmp (line, "ud"))
263 { /* read user data UCS-2 */
265 while (*p && udl < 160)
267 if (isxdigit (*p) && isxdigit (p[1]) && isxdigit (p[2]) && isxdigit (p[3]))
270 (((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 12) +
271 (((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF)) << 8) +
272 (((isalpha (p[2]) ? 9 : 0) + (p[2] & 0xF)) << 4) + ((isalpha (p[3]) ? 9 : 0) + (p[3] & 0xF));
280 if (!strcmp (line, "ud"))
281 { /* read user data UCS-1 */
283 while (*p && udl < 160)
285 if (isxdigit (*p) && isxdigit (p[1]))
287 ud[udl++] = (((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF));
297 /* set up user data variables */
302 for (n = 0, p = 0; p < udl; p++)
304 unsigned short v = ud[p];
311 temp[n++] = (0xC0 + (v >> 6));
312 temp[n++] = (0x80 + (v & 0x3F));
315 temp[n++] = (0xE0 + (v >> 12));
316 temp[n++] = (0x80 + ((v >> 6) & 0x3F));
317 temp[n++] = (0x80 + (v & 0x3F));
322 setenv ("ud", temp, 1);
323 for (n = 0, p = 0; p < udl; p++)
325 unsigned short v = ud[p];
326 if (v < ' ' || v == '\\')
341 temp[n++] = '0' + (v >> 6);
342 temp[n++] = '0' + ((v >> 3) & 7);
343 temp[n++] = '0' + (v & 7);
349 temp[n++] = (0xC0 + (v >> 6));
350 temp[n++] = (0x80 + (v & 0x3F));
353 temp[n++] = (0xE0 + (v >> 12));
354 temp[n++] = (0x80 + ((v >> 6) & 0x3F));
355 temp[n++] = (0x80 + (v & 0x3F));
359 setenv ("ude", temp, 1);
360 for (p = 0; p < udl && ud[p] < 0x100; p++);
363 for (n = 0, p = 0; p < udl; p++)
365 sprintf (temp + n, "%02X", ud[p]);
368 setenv ("ud8", temp, 1);
370 for (n = 0, p = 0; p < udl; p++)
372 sprintf (temp + n, "%04X", ud[p]);
375 setenv ("ud16", temp, 1);
377 /* run the command */
385 main (int argc, const char *argv[])
411 unsigned short ud[160];
412 unsigned char *uds = 0,
419 *spooldir = "/var/spool/asterisk",
420 *motxchannel = "Local/1709400X",
423 *mttxcallerid = "080058752X0",
424 *defaultsubaddress = "9",
427 poptContext optCon; /* context for parsing command-line options */
428 const struct poptOption optionsTable[] = {
429 {"queue", 'q', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &queue, 0, "Queue [inc sub address]", "number[-X]"},
430 {"da", 'd', POPT_ARG_STRING, &da, 0, "Destination address", "number"},
431 {"oa", 'o', POPT_ARG_STRING, &oa, 0, "Origination address", "number"},
432 {"ud", 'm', POPT_ARG_STRING, &uds, 0, "Message", "text"},
433 {"ud-file", 'f', POPT_ARG_STRING, &udfile, 0, "Message file", "filename"},
434 {"UTF-8", 0, POPT_ARG_NONE, &utf8, 0, "File treated as null terminated UTF-8 (default)", 0},
435 {"UCS-1", 0, POPT_ARG_NONE, &ucs1, 0, "File treated as UCS-1", 0},
436 {"UCS-2", 0, POPT_ARG_NONE, &ucs2, 0, "File treated as UCS-2", 0},
437 {"mt", 't', POPT_ARG_NONE, &mt, 0, "Mobile Terminated", 0},
438 {"mo", 0, POPT_ARG_NONE, &mo, 0, "Mobile Originated", 0},
439 {"tx", 0, POPT_ARG_NONE, &tx, 0, "Send message", 0},
440 {"rx", 'r', POPT_ARG_NONE, &rx, 0, "Queue for receipt", 0},
441 {"process", 'e', POPT_ARG_STRING, &process, 0, "Rx queue process command", "command"},
442 {"no-dial", 'x', POPT_ARG_NONE, &nodial, 0, "Do not dial", 0},
443 {"no-wait", 0, POPT_ARG_NONE, &nowait, 0, "Do not wait if already calling", 0},
444 {"concurrent", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &concurrent, 0, "Number of concurrent calls to allow", "n"},
445 {"motx-channel", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &motxchannel, 0, "Channel for motx calls", "channel"},
446 {"motx-callerid", 0, POPT_ARG_STRING, &motxcallerid, 0,
447 "Caller ID for motx calls (default is queue name without sub address)", "number"},
448 {"motx-wait", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &motxwait, 0, "Time to wait for motx call to answer",
450 {"motx-delay", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &motxdelay, 0, "Time between motx call retries", "seconds"},
451 {"motx-retries", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &motxretries, 0, "Number of retries for motx call", "n"},
452 {"mttx-channel", 0, POPT_ARG_STRING, &mttxchannel, 0,
453 "Channel for mttx calls (default is Local/ and queue name without sub address)", "channel"},
454 {"mttx-callerid", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &mttxcallerid, 0,
455 "Caller ID for mttx calls (default is queue name without sub address)", "number"},
456 {"mttx-wait", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &mttxwait, 0, "Time to wait for mttx call to answer",
458 {"mttx-delay", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &mttxdelay, 0, "Time between mttx call retries", "seconds"},
459 {"mttx-retries", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &mttxretries, 0, "Number of retries for mttx call", "n"},
460 {"mr", 'n', POPT_ARG_INT, &mr, 0, "Message reference", "n"},
461 {"pid", 'p', POPT_ARG_INT, &pid, 0, "Protocol ID", "n"},
462 {"dcs", 'c', POPT_ARG_INT, &dcs, 0, "Data Coding Scheme", "n"},
463 {"udh", 0, POPT_ARG_STRING, &udh, 0, "User data header", "hex"},
464 {"srr", 0, POPT_ARG_NONE, &srr, 0, "Status Report Request", 0},
465 {"rp", 0, POPT_ARG_NONE, &rp, 0, "Return Path request", 0},
466 {"v", 0, POPT_ARG_INT, &vp, 0, "Validity Period", "seconds"},
467 {"scts", 0, POPT_ARG_STRING, &scts, 0, "Timestamp", "YYYY-MM-SSTHH:MM:SS"},
468 {"default-sub-address", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &defaultsubaddress, 0, "Default sub address", "X"},
469 {"spool-dir", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &spooldir, 0, "Asterisk spool dir", "dirname"},
470 POPT_AUTOHELP {NULL, 0, 0, NULL, 0}
473 optCon = poptGetContext (NULL, argc, argv, optionsTable, 0);
474 poptSetOtherOptionHelp (optCon, "<oa/da> <message>");
476 /* Now do options processing, get portname */
477 if ((c = poptGetNextOpt (optCon)) < -1)
479 /* an error occurred during option processing */
480 fprintf (stderr, "%s: %s\n", poptBadOption (optCon, POPT_BADOPTION_NOALIAS), poptStrerror (c));
485 if (utf8 + ucs1 + ucs2 > 1)
487 fprintf (stderr, "Pick one of UTF-8, UCS-1 or UCS-2 only\n");
490 if (!udfile && (ucs1 || ucs2))
492 fprintf (stderr, "Command line arguments always treated as UTF-8\n");
495 /* if (!where && poptPeekArg (optCon)) where = (char *) poptGetArg (optCon); */
496 if (!mt && !mo && process)
498 if (!mt && !mo && oa)
504 fprintf (stderr, "Cannot be --mt and --mo\n");
507 if (!rx && !tx && process)
513 fprintf (stderr, "Cannot be --tx and --rx\n");
520 fprintf (stderr, "Cannot have --ud and --ud-file\n");
523 if (mo && !da && poptPeekArg (optCon))
524 da = (char *) poptGetArg (optCon);
525 if (mt && !oa && poptPeekArg (optCon))
526 oa = (char *) poptGetArg (optCon);
529 fprintf (stderr, "--oa makes no sense with --mo as CLI is used (i.e. queue name)\n");
534 fprintf (stderr, "--da makes no sense with --mt as called number is used (i.e. queue name)\n");
537 if (da && strlen (da) > 20)
539 fprintf (stderr, "--da too long\n");
542 if (oa && strlen (oa) > 20)
544 fprintf (stderr, "--oa too long\n");
547 if (queue && strlen (queue) > 20)
549 fprintf (stderr, "--queue name too long\n");
554 fprintf (stderr, "scts is set my service centre\n");
558 { /* simple user data command line option in \UTF-8 */
559 while (udl < 160 && *uds)
561 int v = utf8decode (&uds);
564 fprintf (stderr, "Invalid character U+%X at %d\n", v, udl);
570 if (!uds && !udfile && poptPeekArg (optCon))
571 { /* multiple command line arguments in UTF-8 */
572 while (poptPeekArg (optCon) && udl < 160)
574 unsigned char *a = (char *) poptGetArg (optCon);
575 if (udl && udl < 160)
576 ud[udl++] = ' '; /* space between arguments */
577 while (udl < 160 && *a)
579 int v = utf8decode (&a);
582 fprintf (stderr, "Invalid character U+%X at %d\n", v, udl);
589 if (poptPeekArg (optCon))
591 fprintf (stderr, "Unknown argument %s\n", poptGetArg (optCon));
595 { /* get message from file */
596 unsigned char dat[1204],
602 f = open (udfile, O_RDONLY);
610 n = read (f, dat, sizeof (dat));
621 while (p < e && udl < 160 && *p)
622 ud[udl++] = utf8decode (&p);
625 while (p < e && udl < 160)
629 while (p + 1 < e && udl < 160)
631 ud[udl++] = (*p << 8) + p[1];
638 char *d = strrchr (queue, '-');
642 subaddress = *defaultsubaddress;
645 if (chdir (spooldir))
655 *dir = (mo ? rx ? "sms/morx" : "sms/motx" : rx ? "sms/mtrx" : "sms/mttx");
657 snprintf (temp, sizeof(temp), "sms/.smsq-%d", getpid ());
658 mkdir ("sms", 0777); /* ensure directory exists */
659 mkdir (dir, 0777); /* ensure directory exists */
660 snprintf (queuename, sizeof(queuename), "%s/%s.%ld-%d", dir, *queue ? queue : "0", (long)time (0), getpid ());
661 f = fopen (temp, "w");
668 fprintf (f, "oa=%s\n", oa);
670 fprintf (f, "da=%s\n", da);
672 fprintf (f, "scts=%s\n", scts);
674 fprintf (f, "pid=%d\n", pid);
676 fprintf (f, "dcs=%d\n", dcs);
678 fprintf (f, "mr=%d\n", mr);
680 fprintf (f, "srr=1\n");
682 fprintf (f, "rp=1\n");
684 fprintf (f, "udh#%s\n", udh);
686 fprintf (f, "vp=%d\n", vp);
690 for (p = 0; p < udl && ud[p] < 0x100; p++);
693 for (p = 0; p < udl && ud[p] < 0x80 && ud[p] >= 0x20; p++);
697 for (p = 0; p < udl; p++)
700 { /* use one byte hex */
702 for (p = 0; p < udl; p++)
703 fprintf (f, "%02X", ud[p]);
706 { /* use two byte hex */
708 for (p = 0; p < udl; p++)
709 fprintf (f, "%04X", ud[p]);
714 if (rename (temp, queuename))
722 if (!nodial && tx && !process)
723 { /* dial to send messages */
731 ret = txqcheck ("motx", queue, subaddress, motxchannel, motxcallerid, motxwait, motxdelay, motxretries, concurrent);
733 ret = txqcheck ("mttx", queue, subaddress, mttxchannel, mttxcallerid, mttxwait, mttxdelay, mttxretries, concurrent);
735 break; /* sent, or queued OK */
739 if (ret == 2 && !nowait)
740 fprintf (stderr, "No call scheduled as already sending\n");
743 rxqcheck (mo ? rx ? "morx" : "motx" : rx ? "mtrx" : "mttx", queue, process);