2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2004 - 2005
6 * SMS queuing application for use with asterisk app_sms
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
20 #include "asterisk/autoconfig.h"
24 #include <sys/types.h>
34 #include <asterisk/compat.h>
36 #define POPT_ARGFLAG_SHOW_DEFAULT 0x00800000
38 #if !defined(POPT_ARGFLAG_SHOW_DEFAULT)
39 #define POPT_ARGFLAG_SHOW_DEFAULT 0x00800000
43 /* reads next USC character from null terminated UTF-8 string and advanced pointer */
44 /* for non valid UTF-8 sequences, returns character as is */
45 /* Does not advance pointer for null termination */
46 static int utf8decode (unsigned char **pp)
48 unsigned char *p = *pp;
50 return 0; /* null termination of string */
53 return *p; /* ascii or continuation character */
56 if (*p < 0xC2 || (p[1] & 0xC0) != 0x80)
57 return *p; /* not valid UTF-8 */
59 return ((*p & 0x1F) << 6) + (p[1] & 0x3F);
63 if ((*p == 0xE0 && p[1] < 0xA0) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80)
64 return *p; /* not valid UTF-8 */
66 return ((*p & 0x0F) << 12) + ((p[1] & 0x3F) << 6) + (p[2] & 0x3F);
70 if ((*p == 0xF0 && p[1] < 0x90) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80)
71 return *p; /* not valid UTF-8 */
73 return ((*p & 0x07) << 18) + ((p[1] & 0x3F) << 12) + ((p[2] & 0x3F) << 6) + (p[3] & 0x3F);
77 if ((*p == 0xF8 && p[1] < 0x88) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80
78 || (p[4] & 0xC0) != 0x80)
79 return *p; /* not valid UTF-8 */
81 return ((*p & 0x03) << 24) + ((p[1] & 0x3F) << 18) + ((p[2] & 0x3F) << 12) + ((p[3] & 0x3F) << 6) + (p[4] & 0x3F);
85 if ((*p == 0xFC && p[1] < 0x84) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80
86 || (p[4] & 0xC0) != 0x80 || (p[5] & 0xC0) != 0x80)
87 return *p; /* not valid UTF-8 */
89 return ((*p & 0x01) << 30) + ((p[1] & 0x3F) << 24) + ((p[2] & 0x3F) << 18) + ((p[3] & 0x3F) << 12) + ((p[4] & 0x3F) << 6) +
92 return *p; /* not sensible */
95 /* check for any queued messages in specific queue (queue="" means any queue) */
96 /* returns 0 if nothing queued, 1 if queued and outgoing set up OK, 2 of outgoing exists */
97 static char txqcheck (char *dir, char *queue, char subaddress, char *channel, char *callerid, int wait, int delay, int retries, int concurrent)
105 int ql = strlen (queue), qfl = ql;
107 snprintf (dirname, sizeof(dirname), "sms/%s", dir);
108 d = opendir (dirname);
111 while ((fn = readdir (d))
112 && !(*fn->d_name != '.'
113 && ((!ql && (p = strchr (fn->d_name, '.'))) || (ql && !strncmp (fn->d_name, queue, ql) && fn->d_name[ql] == '.'))));
120 { /* not searching any specific queue, so use whatr we found as the queue */
122 qfl = ql = p - queue;
124 p = strchr (queue, '-');
125 if (p && p < queue + ql)
130 snprintf (temp, sizeof(temp), "sms/.smsq-%d", getpid ());
131 f = fopen (temp, "w");
138 fprintf (f, "Channel: ");
140 fprintf (f, "Local/%.*s\n", ql, queue);
143 p = strchr (channel, '/');
148 fprintf (f, "%.*s%c%s\n", (int)(p - channel), channel, subaddress, p + 1);
150 fprintf (f, "%s\n", channel);
152 fprintf (f, "Callerid: SMS <");
154 fprintf (f, "%.*s", ql, queue);
157 p = strchr (callerid, 'X');
159 fprintf (f, "%.*s%c%s", (int)(p - callerid), callerid, subaddress, p + 1);
161 fprintf (f, "%s", callerid);
164 fprintf (f, "Application: SMS\n");
165 fprintf (f, "Data: %.*s", qfl, queue);
168 fprintf (f, "\nMaxRetries: %d\n", retries);
169 fprintf (f, "RetryTime: %d\n", delay);
170 fprintf (f, "WaitTime: %d\n", wait);
175 while (try < concurrent)
178 snprintf(ogname, sizeof(ogname), "outgoing/smsq.%s.%s.%d", dir, queue, try);
179 if (!link (temp, ogname))
186 /* failed to create call queue */
191 /* Process received queue entries and run through a process, setting environment variables */
192 static void rxqcheck (char *dir, char *queue, char *process)
198 int ql = strlen (queue);
200 snprintf(temp, sizeof(temp), "sms/.smsq-%d", getpid ());
201 snprintf(dirname, sizeof(dirname), "sms/%s", dir);
202 d = opendir (dirname);
205 while ((fn = readdir (d)))
206 if ((*fn->d_name != '.'
207 && ((!ql && (p = strchr (fn->d_name, '.'))) || (ql && !strncmp (fn->d_name, queue, ql) && fn->d_name[ql] == '.'))))
211 unsigned short ud[160];
212 unsigned char udl = 0;
214 snprintf (filename, sizeof(filename), "sms/%s/%s", dir, fn->d_name);
215 if (rename (filename, temp))
216 continue; /* cannot access file */
217 f = fopen (temp, "r");
242 setenv ("queue", queue, 1);
244 while (fgets (line, sizeof (line), f))
246 for (p = line; *p && *p != '\n' && *p != '\r'; p++);
247 *p = 0; /* strip eoln */
249 if (!*p || *p == ';')
250 continue; /* blank line or comment, ignore */
261 if (!strcmp (line, "oa") || !strcmp (line, "da") || !strcmp (line, "scts") || !strcmp (line, "pid")
262 || !strcmp (line, "dcs") || !strcmp (line, "mr") || !strcmp (line, "vp"))
264 else if ((!strcmp (line, "srr") || !strcmp (line, "rp")) && atoi (p))
265 setenv (line, "", 1);
266 else if (!strcmp (line, "ud"))
267 { /* read the user data as UTF-8 */
270 while ((v = utf8decode (&p)) && udl < 160)
271 if (v && v <= 0xFFFF)
274 } else if (*p == '#')
280 if (!strcmp (line, "udh"))
282 else if (!strcmp (line, "ud"))
283 { /* read user data UCS-2 */
285 while (*p && udl < 160)
287 if (isxdigit (*p) && isxdigit (p[1]) && isxdigit (p[2]) && isxdigit (p[3]))
290 (((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 12) +
291 (((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF)) << 8) +
292 (((isalpha (p[2]) ? 9 : 0) + (p[2] & 0xF)) << 4) + ((isalpha (p[3]) ? 9 : 0) + (p[3] & 0xF));
300 if (!strcmp (line, "ud"))
301 { /* read user data UCS-1 */
303 while (*p && udl < 160)
305 if (isxdigit (*p) && isxdigit (p[1]))
307 ud[udl++] = (((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF));
317 /* set up user data variables */
322 for (n = 0, p = 0; p < udl; p++)
324 unsigned short v = ud[p];
331 temp[n++] = (0xC0 + (v >> 6));
332 temp[n++] = (0x80 + (v & 0x3F));
335 temp[n++] = (0xE0 + (v >> 12));
336 temp[n++] = (0x80 + ((v >> 6) & 0x3F));
337 temp[n++] = (0x80 + (v & 0x3F));
342 setenv ("ud", temp, 1);
343 for (n = 0, p = 0; p < udl; p++)
345 unsigned short v = ud[p];
346 if (v < ' ' || v == '\\')
361 temp[n++] = '0' + (v >> 6);
362 temp[n++] = '0' + ((v >> 3) & 7);
363 temp[n++] = '0' + (v & 7);
369 temp[n++] = (0xC0 + (v >> 6));
370 temp[n++] = (0x80 + (v & 0x3F));
373 temp[n++] = (0xE0 + (v >> 12));
374 temp[n++] = (0x80 + ((v >> 6) & 0x3F));
375 temp[n++] = (0x80 + (v & 0x3F));
379 setenv ("ude", temp, 1);
380 for (p = 0; p < udl && ud[p] < 0x100; p++);
383 for (n = 0, p = 0; p < udl; p++)
385 sprintf (temp + n, "%02X", ud[p]);
388 setenv ("ud8", temp, 1);
390 for (n = 0, p = 0; p < udl; p++)
392 sprintf (temp + n, "%04X", ud[p]);
395 setenv ("ud16", temp, 1);
397 /* run the command */
405 main (int argc, const char *argv[])
431 unsigned short ud[160];
432 unsigned char *uds = 0,
439 *spooldir = "/var/spool/asterisk",
440 *motxchannel = "Local/1709400X",
443 *mttxcallerid = "080058752X0",
444 *defaultsubaddress = "9",
447 poptContext optCon; /* context for parsing command-line options */
448 const struct poptOption optionsTable[] = {
449 {"queue", 'q', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &queue, 0, "Queue [inc sub address]", "number[-X]"},
450 {"da", 'd', POPT_ARG_STRING, &da, 0, "Destination address", "number"},
451 {"oa", 'o', POPT_ARG_STRING, &oa, 0, "Origination address", "number"},
452 {"ud", 'm', POPT_ARG_STRING, &uds, 0, "Message", "text"},
453 {"ud-file", 'f', POPT_ARG_STRING, &udfile, 0, "Message file", "filename"},
454 {"UTF-8", 0, POPT_ARG_NONE, &utf8, 0, "File treated as null terminated UTF-8 (default)", 0},
455 {"UCS-1", 0, POPT_ARG_NONE, &ucs1, 0, "File treated as UCS-1", 0},
456 {"UCS-2", 0, POPT_ARG_NONE, &ucs2, 0, "File treated as UCS-2", 0},
457 {"mt", 't', POPT_ARG_NONE, &mt, 0, "Mobile Terminated", 0},
458 {"mo", 0, POPT_ARG_NONE, &mo, 0, "Mobile Originated", 0},
459 {"tx", 0, POPT_ARG_NONE, &tx, 0, "Send message", 0},
460 {"rx", 'r', POPT_ARG_NONE, &rx, 0, "Queue for receipt", 0},
461 {"process", 'e', POPT_ARG_STRING, &process, 0, "Rx queue process command", "command"},
462 {"no-dial", 'x', POPT_ARG_NONE, &nodial, 0, "Do not dial", 0},
463 {"no-wait", 0, POPT_ARG_NONE, &nowait, 0, "Do not wait if already calling", 0},
464 {"concurrent", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &concurrent, 0, "Number of concurrent calls to allow", "n"},
465 {"motx-channel", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &motxchannel, 0, "Channel for motx calls", "channel"},
466 {"motx-callerid", 0, POPT_ARG_STRING, &motxcallerid, 0,
467 "Caller ID for motx calls (default is queue name without sub address)", "number"},
468 {"motx-wait", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &motxwait, 0, "Time to wait for motx call to answer",
470 {"motx-delay", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &motxdelay, 0, "Time between motx call retries", "seconds"},
471 {"motx-retries", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &motxretries, 0, "Number of retries for motx call", "n"},
472 {"mttx-channel", 0, POPT_ARG_STRING, &mttxchannel, 0,
473 "Channel for mttx calls (default is Local/ and queue name without sub address)", "channel"},
474 {"mttx-callerid", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &mttxcallerid, 0,
475 "Caller ID for mttx calls (default is queue name without sub address)", "number"},
476 {"mttx-wait", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &mttxwait, 0, "Time to wait for mttx call to answer",
478 {"mttx-delay", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &mttxdelay, 0, "Time between mttx call retries", "seconds"},
479 {"mttx-retries", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &mttxretries, 0, "Number of retries for mttx call", "n"},
480 {"mr", 'n', POPT_ARG_INT, &mr, 0, "Message reference", "n"},
481 {"pid", 'p', POPT_ARG_INT, &pid, 0, "Protocol ID", "n"},
482 {"dcs", 'c', POPT_ARG_INT, &dcs, 0, "Data Coding Scheme", "n"},
483 {"udh", 0, POPT_ARG_STRING, &udh, 0, "User data header", "hex"},
484 {"srr", 0, POPT_ARG_NONE, &srr, 0, "Status Report Request", 0},
485 {"rp", 0, POPT_ARG_NONE, &rp, 0, "Return Path request", 0},
486 {"v", 0, POPT_ARG_INT, &vp, 0, "Validity Period", "seconds"},
487 {"scts", 0, POPT_ARG_STRING, &scts, 0, "Timestamp", "YYYY-MM-SSTHH:MM:SS"},
488 {"default-sub-address", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &defaultsubaddress, 0, "Default sub address", "X"},
489 {"spool-dir", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &spooldir, 0, "Asterisk spool dir", "dirname"},
490 POPT_AUTOHELP {NULL, 0, 0, NULL, 0}
493 optCon = poptGetContext (NULL, argc, argv, optionsTable, 0);
494 poptSetOtherOptionHelp (optCon, "<oa/da> <message>");
496 /* Now do options processing, get portname */
497 if ((c = poptGetNextOpt (optCon)) < -1)
499 /* an error occurred during option processing */
500 fprintf (stderr, "%s: %s\n", poptBadOption (optCon, POPT_BADOPTION_NOALIAS), poptStrerror (c));
505 if (utf8 + ucs1 + ucs2 > 1)
507 fprintf (stderr, "Pick one of UTF-8, UCS-1 or UCS-2 only\n");
510 if (!udfile && (ucs1 || ucs2))
512 fprintf (stderr, "Command line arguments always treated as UTF-8\n");
515 /* if (!where && poptPeekArg (optCon)) where = (char *) poptGetArg (optCon); */
516 if (!mt && !mo && process)
518 if (!mt && !mo && oa)
524 fprintf (stderr, "Cannot be --mt and --mo\n");
527 if (!rx && !tx && process)
533 fprintf (stderr, "Cannot be --tx and --rx\n");
540 fprintf (stderr, "Cannot have --ud and --ud-file\n");
543 if (mo && !da && poptPeekArg (optCon))
544 da = (char *) poptGetArg (optCon);
545 if (mt && !oa && poptPeekArg (optCon))
546 oa = (char *) poptGetArg (optCon);
549 fprintf (stderr, "--oa makes no sense with --mo as CLI is used (i.e. queue name)\n");
554 fprintf (stderr, "--da makes no sense with --mt as called number is used (i.e. queue name)\n");
557 if (da && strlen (da) > 20)
559 fprintf (stderr, "--da too long\n");
562 if (oa && strlen (oa) > 20)
564 fprintf (stderr, "--oa too long\n");
567 if (queue && strlen (queue) > 20)
569 fprintf (stderr, "--queue name too long\n");
574 fprintf (stderr, "scts is set my service centre\n");
578 { /* simple user data command line option in \UTF-8 */
579 while (udl < 160 && *uds)
581 int v = utf8decode (&uds);
584 fprintf (stderr, "Invalid character U+%X at %d\n", v, udl);
590 if (!uds && !udfile && poptPeekArg (optCon))
591 { /* multiple command line arguments in UTF-8 */
592 while (poptPeekArg (optCon) && udl < 160)
594 unsigned char *a = (char *) poptGetArg (optCon);
595 if (udl && udl < 160)
596 ud[udl++] = ' '; /* space between arguments */
597 while (udl < 160 && *a)
599 int v = utf8decode (&a);
602 fprintf (stderr, "Invalid character U+%X at %d\n", v, udl);
609 if (poptPeekArg (optCon))
611 fprintf (stderr, "Unknown argument %s\n", poptGetArg (optCon));
615 { /* get message from file */
616 unsigned char dat[1204],
622 f = open (udfile, O_RDONLY);
630 n = read (f, dat, sizeof (dat));
641 while (p < e && udl < 160 && *p)
642 ud[udl++] = utf8decode (&p);
645 while (p < e && udl < 160)
649 while (p + 1 < e && udl < 160)
651 ud[udl++] = (*p << 8) + p[1];
658 char *d = strrchr (queue, '-');
662 subaddress = *defaultsubaddress;
665 if (chdir (spooldir))
675 *dir = (mo ? rx ? "sms/morx" : "sms/motx" : rx ? "sms/mtrx" : "sms/mttx");
677 snprintf (temp, sizeof(temp), "sms/.smsq-%d", getpid ());
678 mkdir ("sms", 0777); /* ensure directory exists */
679 mkdir (dir, 0777); /* ensure directory exists */
680 snprintf (queuename, sizeof(queuename), "%s/%s.%ld-%d", dir, *queue ? queue : "0", (long)time (0), getpid ());
681 f = fopen (temp, "w");
688 fprintf (f, "oa=%s\n", oa);
690 fprintf (f, "da=%s\n", da);
692 fprintf (f, "scts=%s\n", scts);
694 fprintf (f, "pid=%d\n", pid);
696 fprintf (f, "dcs=%d\n", dcs);
698 fprintf (f, "mr=%d\n", mr);
700 fprintf (f, "srr=1\n");
702 fprintf (f, "rp=1\n");
704 fprintf (f, "udh#%s\n", udh);
706 fprintf (f, "vp=%d\n", vp);
710 for (p = 0; p < udl && ud[p] < 0x100; p++);
713 for (p = 0; p < udl && ud[p] < 0x80 && ud[p] >= 0x20; p++);
717 for (p = 0; p < udl; p++)
720 { /* use one byte hex */
722 for (p = 0; p < udl; p++)
723 fprintf (f, "%02X", ud[p]);
726 { /* use two byte hex */
728 for (p = 0; p < udl; p++)
729 fprintf (f, "%04X", ud[p]);
734 if (rename (temp, queuename))
742 if (!nodial && tx && !process)
743 { /* dial to send messages */
751 ret = txqcheck ("motx", queue, subaddress, motxchannel, motxcallerid, motxwait, motxdelay, motxretries, concurrent);
753 ret = txqcheck ("mttx", queue, subaddress, mttxchannel, mttxcallerid, mttxwait, mttxdelay, mttxretries, concurrent);
755 break; /* sent, or queued OK */
759 if (ret == 2 && !nowait)
760 fprintf (stderr, "No call scheduled as already sending\n");
763 rxqcheck (mo ? rx ? "morx" : "motx" : rx ? "mtrx" : "mttx", queue, process);