123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773 |
- /*
- * Asterisk -- An open source telephony toolkit.
- *
- * Copyright (C) 2004 - 2005
- *
- * SMS queuing application for use with asterisk app_sms
- * by Adrian Kennard
- *
- * See http://www.asterisk.org for more information about
- * the Asterisk project. Please do not directly contact
- * any of the maintainers of this project for assistance;
- * the project provides a web site, mailing lists and IRC
- * channels for your use.
- *
- * This program is free software, distributed under the terms of
- * the GNU General Public License Version 2. See the LICENSE file
- * at the top of the source tree.
- */
- /*** MODULEINFO
- <support_level>extended</support_level>
- ***/
- #include "asterisk.h"
- #include <popt.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <dirent.h>
- #include <ctype.h>
- #include <time.h>
- #if !defined(POPT_ARGFLAG_SHOW_DEFAULT)
- #define POPT_ARGFLAG_SHOW_DEFAULT 0x00800000
- #endif
- /*!
- * \brief reads next USC character from null terminated UTF-8 string and advanced pointer
- * for non valid UTF-8 sequences.
- * \return character as is Does \b NOT advance pointer for null termination
- */
- static int utf8decode (unsigned char **pp)
- {
- unsigned char *p = *pp;
- if (!*p)
- return 0; /* null termination of string */
- (*pp)++;
- if (*p < 0xC0)
- return *p; /* ascii or continuation character */
- if (*p < 0xE0)
- {
- if (*p < 0xC2 || (p[1] & 0xC0) != 0x80)
- return *p; /* not valid UTF-8 */
- (*pp)++;
- return ((*p & 0x1F) << 6) + (p[1] & 0x3F);
- }
- if (*p < 0xF0)
- {
- if ((*p == 0xE0 && p[1] < 0xA0) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80)
- return *p; /* not valid UTF-8 */
- (*pp) += 2;
- return ((*p & 0x0F) << 12) + ((p[1] & 0x3F) << 6) + (p[2] & 0x3F);
- }
- if (*p < 0xF8)
- {
- if ((*p == 0xF0 && p[1] < 0x90) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80)
- return *p; /* not valid UTF-8 */
- (*pp) += 3;
- return ((*p & 0x07) << 18) + ((p[1] & 0x3F) << 12) + ((p[2] & 0x3F) << 6) + (p[3] & 0x3F);
- }
- if (*p < 0xFC)
- {
- if ((*p == 0xF8 && p[1] < 0x88) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80
- || (p[4] & 0xC0) != 0x80)
- return *p; /* not valid UTF-8 */
- (*pp) += 4;
- return ((*p & 0x03) << 24) + ((p[1] & 0x3F) << 18) + ((p[2] & 0x3F) << 12) + ((p[3] & 0x3F) << 6) + (p[4] & 0x3F);
- }
- if (*p < 0xFE)
- {
- if ((*p == 0xFC && p[1] < 0x84) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80
- || (p[4] & 0xC0) != 0x80 || (p[5] & 0xC0) != 0x80)
- return *p; /* not valid UTF-8 */
- (*pp) += 5;
- return ((*p & 0x01) << 30) + ((p[1] & 0x3F) << 24) + ((p[2] & 0x3F) << 18) + ((p[3] & 0x3F) << 12) + ((p[4] & 0x3F) << 6) +
- (p[5] & 0x3F);
- }
- return *p; /* not sensible */
- }
- /*!
- * \brief check for any queued messages in specific queue (queue="" means any queue)
- * \param dir,queue,subaddress,channel,callerid,wait,delay,retries,concurrent
- * \retval 0 if nothing queued
- * \retval 1 if queued and outgoing set up OK
- * \retval 2 of outgoing exists
- */
- static char txqcheck (char *dir, char *queue, char subaddress, char *channel, char *callerid, int wait, int delay, int retries, int concurrent)
- {
- char ogname[100],
- temp[100],
- dirname[100],
- *p=NULL;
- FILE *f;
- DIR *d;
- int ql = strlen (queue), qfl = ql;
- struct dirent *fn;
- snprintf (dirname, sizeof(dirname), "sms/%s", dir);
- d = opendir (dirname);
- if (!d)
- return 0;
- while ((fn = readdir (d))
- && !(*fn->d_name != '.'
- && ((!ql && (p = strchr (fn->d_name, '.'))) || (ql && !strncmp (fn->d_name, queue, ql) && fn->d_name[ql] == '.'))));
- if (!fn)
- {
- closedir (d);
- return 0;
- }
- if (!ql)
- { /* not searching any specific queue, so use whatr we found as the queue */
- queue = fn->d_name;
- qfl = ql = p - queue;
- }
- p = strchr (queue, '-');
- if (p && p < queue + ql)
- {
- ql = p - queue;
- subaddress = p[1];
- }
- snprintf (temp, sizeof(temp), "sms/.smsq-%d", (int)getpid ());
- f = fopen (temp, "w");
- if (!f)
- {
- perror (temp);
- closedir (d);
- return 0;
- }
- fprintf (f, "Channel: ");
- if (!channel)
- fprintf (f, "Local/%.*s\n", ql, queue);
- else
- {
- p = strchr (channel, '/');
- if (!p)
- p = channel;
- p = strchr (p, 'X');
- if (p)
- fprintf (f, "%.*s%c%s\n", (int)(p - channel), channel, subaddress, p + 1);
- else
- fprintf (f, "%s\n", channel);
- }
- fprintf (f, "Callerid: SMS <");
- if (!callerid)
- fprintf (f, "%.*s", ql, queue);
- else
- {
- p = strchr (callerid, 'X');
- if (p)
- fprintf (f, "%.*s%c%s", (int)(p - callerid), callerid, subaddress, p + 1);
- else
- fprintf (f, "%s", callerid);
- }
- fprintf (f, ">\n");
- fprintf (f, "Application: SMS\n");
- fprintf (f, "Data: %.*s", qfl, queue);
- if (dir[1] == 't')
- fprintf (f, ",s");
- fprintf (f, "\nMaxRetries: %d\n", retries);
- fprintf (f, "RetryTime: %d\n", delay);
- fprintf (f, "WaitTime: %d\n", wait);
- fclose (f);
- closedir (d);
- {
- int try = 0;
- while (try < concurrent)
- {
- try++;
- snprintf(ogname, sizeof(ogname), "outgoing/smsq.%s.%s.%d", dir, queue, try);
- if (!link (temp, ogname))
- { /* queued OK */
- unlink (temp);
- return 1;
- }
- }
- }
- /* failed to create call queue */
- unlink (temp);
- return 2;
- }
- /*!
- * \brief Process received queue entries
- * Run through a process, setting environment variables
- */
- static void rxqcheck (char *dir, char *queue, char *process)
- {
- char *p;
- void *pp = &p;
- char dirname[100],
- temp[100];
- DIR *d;
- int ql = strlen (queue);
- struct dirent *fn;
- snprintf(temp, sizeof(temp), "sms/.smsq-%d", (int)getpid ());
- snprintf(dirname, sizeof(dirname), "sms/%s", dir);
- d = opendir (dirname);
- if (!d)
- return;
- while ((fn = readdir (d)))
- if ((*fn->d_name != '.'
- && ((!ql && (p = strchr (fn->d_name, '.'))) || (ql && !strncmp (fn->d_name, queue, ql) && fn->d_name[ql] == '.'))))
- { /* process file */
- char filename[1010];
- char line[1000];
- unsigned short ud[160];
- unsigned char udl = 0;
- FILE *f;
- snprintf (filename, sizeof(filename), "sms/%s/%s", dir, fn->d_name);
- if (rename (filename, temp))
- continue; /* cannot access file */
- f = fopen (temp, "r");
- unlink (temp);
- if (!f)
- {
- perror (temp);
- continue;
- }
- unsetenv ("oa");
- unsetenv ("da");
- unsetenv ("scts");
- unsetenv ("pid");
- unsetenv ("dcs");
- unsetenv ("mr");
- unsetenv ("srr");
- unsetenv ("rp");
- unsetenv ("vp");
- unsetenv ("udh");
- unsetenv ("ud");
- unsetenv ("ude");
- unsetenv ("ud8");
- unsetenv ("ud16");
- unsetenv ("morx");
- unsetenv ("motx");
- unsetenv ("queue");
- if (*queue)
- setenv ("queue", queue, 1);
- setenv (dir, "", 1);
- while (fgets (line, sizeof (line), f))
- {
- for (p = line; *p && *p != '\n' && *p != '\r'; p++);
- *p = 0; /* strip eoln */
- p = line;
- if (!*p || *p == ';')
- continue; /* blank line or comment, ignore */
- while (isalnum (*p))
- {
- *p = tolower (*p);
- p++;
- }
- while (isspace (*p))
- *p++ = 0;
- if (*p == '=')
- { /* = */
- *p++ = 0;
- if (!strcmp (line, "oa") || !strcmp (line, "da") || !strcmp (line, "scts") || !strcmp (line, "pid")
- || !strcmp (line, "dcs") || !strcmp (line, "mr") || !strcmp (line, "vp"))
- setenv (line, p, 1);
- else if ((!strcmp (line, "srr") || !strcmp (line, "rp")) && atoi (p))
- setenv (line, "", 1);
- else if (!strcmp (line, "ud"))
- { /* read the user data as UTF-8 */
- long v;
- udl = 0;
- while ((v = utf8decode (pp)) && udl < 160)
- if (v && v <= 0xFFFF)
- ud[udl++] = v;
- }
- } else if (*p == '#')
- {
- *p++ = 0;
- if (*p == '#')
- { /* ## */
- p++;
- if (!strcmp (line, "udh"))
- setenv (line, p, 1);
- else if (!strcmp (line, "ud"))
- { /* read user data UCS-2 */
- udl = 0;
- while (*p && udl < 160)
- {
- if (isxdigit (*p) && isxdigit (p[1]) && isxdigit (p[2]) && isxdigit (p[3]))
- {
- ud[udl++] =
- (((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 12) +
- (((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF)) << 8) +
- (((isalpha (p[2]) ? 9 : 0) + (p[2] & 0xF)) << 4) + ((isalpha (p[3]) ? 9 : 0) + (p[3] & 0xF));
- p += 4;
- } else
- break;
- }
- }
- } else
- { /* # */
- if (!strcmp (line, "ud"))
- { /* read user data UCS-1 */
- udl = 0;
- while (*p && udl < 160)
- {
- if (isxdigit (*p) && isxdigit (p[1]))
- {
- ud[udl++] = (((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF));
- p += 2;
- } else
- break;
- }
- }
- }
- }
- }
- fclose (f);
- /* set up user data variables */
- {
- char tmp[481];
- int n, x;
- for (n = 0, x = 0; x < udl; x++)
- {
- unsigned short v = ud[x];
- if (v)
- {
- if (v < 0x80)
- tmp[n++] = v;
- else if (v < 0x800)
- {
- tmp[n++] = (0xC0 + (v >> 6));
- tmp[n++] = (0x80 + (v & 0x3F));
- } else
- {
- tmp[n++] = (0xE0 + (v >> 12));
- tmp[n++] = (0x80 + ((v >> 6) & 0x3F));
- tmp[n++] = (0x80 + (v & 0x3F));
- }
- }
- }
- tmp[n] = 0;
- setenv ("ud", tmp, 1);
- for (n = 0, x = 0; x < udl; x++)
- {
- unsigned short v = ud[x];
- if (v < ' ' || v == '\\')
- {
- tmp[n++] = '\\';
- if (v == '\\')
- tmp[n++] = '\\';
- else if (v == '\n')
- tmp[n++] = 'n';
- else if (v == '\r')
- tmp[n++] = 'r';
- else if (v == '\t')
- tmp[n++] = 't';
- else if (v == '\f')
- tmp[n++] = 'f';
- else
- {
- tmp[n++] = '0' + (v >> 6);
- tmp[n++] = '0' + ((v >> 3) & 7);
- tmp[n++] = '0' + (v & 7);
- }
- } else if (v < 0x80)
- tmp[n++] = v;
- else if (v < 0x800)
- {
- tmp[n++] = (0xC0 + (v >> 6));
- tmp[n++] = (0x80 + (v & 0x3F));
- } else
- {
- tmp[n++] = (0xE0 + (v >> 12));
- tmp[n++] = (0x80 + ((v >> 6) & 0x3F));
- tmp[n++] = (0x80 + (v & 0x3F));
- }
- }
- tmp[n] = 0;
- setenv ("ude", tmp, 1);
- for (x = 0; x < udl && ud[x] < 0x100; x++);
- if (x == udl)
- {
- for (n = 0, x = 0; x < udl; x++)
- {
- sprintf (tmp + n, "%02hX", ud[x]);
- n += 2;
- }
- setenv ("ud8", tmp, 1);
- }
- for (n = 0, x = 0; x < udl; x++)
- {
- sprintf (tmp + n, "%04hX", ud[x]);
- n += 4;
- }
- setenv ("ud16", tmp, 1);
- }
- /* run the command */
- if (system (process) == -1) {
- fprintf(stderr, "Failed to fork process '%s'\n", process);
- }
- }
- closedir (d);
- }
- /* Main app */
- int
- main (int argc, const char *argv[])
- {
- char c;
- int mt = 0,
- mo = 0,
- tx = 0,
- rx = 0,
- nodial = 0,
- nowait = 0,
- concurrent = 1,
- motxwait = 10,
- motxdelay = 1,
- motxretries = 10,
- mttxwait = 10,
- mttxdelay = 30,
- mttxretries = 100,
- mr = -1,
- pid = -1,
- dcs = -1,
- srr = 0,
- rp = 0,
- vp = 0,
- udl = 0,
- utf8 = 0,
- ucs1 = 0,
- ucs2 = 0;
- unsigned short ud[160];
- unsigned char *uds = 0,
- *udh = 0;
- char *da = 0,
- *oa = 0,
- *queue = "",
- *udfile = 0,
- *process = 0,
- *spooldir = "/var/spool/asterisk",
- *motxchannel = "Local/1709400X",
- *motxcallerid = 0,
- *mttxchannel = 0,
- *mttxcallerid = "080058752X0",
- *defaultsubaddress = "9",
- subaddress = 0,
- *scts = 0;
- poptContext optCon; /* context for parsing command-line options */
- const struct poptOption optionsTable[] = {
- {"queue", 'q', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &queue, 0, "Queue [inc sub address]", "number[-X]"},
- {"da", 'd', POPT_ARG_STRING, &da, 0, "Destination address", "number"},
- {"oa", 'o', POPT_ARG_STRING, &oa, 0, "Origination address", "number"},
- {"ud", 'm', POPT_ARG_STRING, &uds, 0, "Message", "text"},
- {"ud-file", 'f', POPT_ARG_STRING, &udfile, 0, "Message file", "filename"},
- {"UTF-8", 0, POPT_ARG_NONE, &utf8, 0, "File treated as null terminated UTF-8 (default)", 0},
- {"UCS-1", 0, POPT_ARG_NONE, &ucs1, 0, "File treated as UCS-1", 0},
- {"UCS-2", 0, POPT_ARG_NONE, &ucs2, 0, "File treated as UCS-2", 0},
- {"mt", 't', POPT_ARG_NONE, &mt, 0, "Mobile Terminated", 0},
- {"mo", 0, POPT_ARG_NONE, &mo, 0, "Mobile Originated", 0},
- {"tx", 0, POPT_ARG_NONE, &tx, 0, "Send message", 0},
- {"rx", 'r', POPT_ARG_NONE, &rx, 0, "Queue for receipt", 0},
- {"process", 'e', POPT_ARG_STRING, &process, 0, "Rx queue process command", "command"},
- {"no-dial", 'x', POPT_ARG_NONE, &nodial, 0, "Do not dial", 0},
- {"no-wait", 0, POPT_ARG_NONE, &nowait, 0, "Do not wait if already calling", 0},
- {"concurrent", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &concurrent, 0, "Number of concurrent calls to allow", "n"},
- {"motx-channel", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &motxchannel, 0, "Channel for motx calls", "channel"},
- {"motx-callerid", 0, POPT_ARG_STRING, &motxcallerid, 0,
- "Caller ID for motx calls (default is queue name without sub address)", "number"},
- {"motx-wait", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &motxwait, 0, "Time to wait for motx call to answer",
- "seconds"},
- {"motx-delay", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &motxdelay, 0, "Time between motx call retries", "seconds"},
- {"motx-retries", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &motxretries, 0, "Number of retries for motx call", "n"},
- {"mttx-channel", 0, POPT_ARG_STRING, &mttxchannel, 0,
- "Channel for mttx calls (default is Local/ and queue name without sub address)", "channel"},
- {"mttx-callerid", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &mttxcallerid, 0,
- "Caller ID for mttx calls (default is queue name without sub address)", "number"},
- {"mttx-wait", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &mttxwait, 0, "Time to wait for mttx call to answer",
- "seconds"},
- {"mttx-delay", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &mttxdelay, 0, "Time between mttx call retries", "seconds"},
- {"mttx-retries", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &mttxretries, 0, "Number of retries for mttx call", "n"},
- {"mr", 'n', POPT_ARG_INT, &mr, 0, "Message reference", "n"},
- {"pid", 'p', POPT_ARG_INT, &pid, 0, "Protocol ID", "n"},
- {"dcs", 'c', POPT_ARG_INT, &dcs, 0, "Data Coding Scheme", "n"},
- {"udh", 0, POPT_ARG_STRING, &udh, 0, "User data header", "hex"},
- {"srr", 0, POPT_ARG_NONE, &srr, 0, "Status Report Request", 0},
- {"rp", 0, POPT_ARG_NONE, &rp, 0, "Return Path request", 0},
- {"v", 0, POPT_ARG_INT, &vp, 0, "Validity Period", "seconds"},
- {"scts", 0, POPT_ARG_STRING, &scts, 0, "Timestamp", "YYYY-MM-SSTHH:MM:SS"},
- {"default-sub-address", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &defaultsubaddress, 0, "Default sub address", "X"},
- {"spool-dir", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &spooldir, 0, "Asterisk spool dir", "dirname"},
- POPT_AUTOHELP {NULL, 0, 0, NULL, 0}
- };
- optCon = poptGetContext (NULL, argc, argv, optionsTable, 0);
- poptSetOtherOptionHelp (optCon, "<oa/da> <message>");
- /* Now do options processing, get portname */
- if ((c = poptGetNextOpt (optCon)) < -1)
- {
- /* an error occurred during option processing */
- fprintf (stderr, "%s: %s\n", poptBadOption (optCon, POPT_BADOPTION_NOALIAS), poptStrerror (c));
- return 1;
- }
- if (!ucs1 && !ucs2)
- utf8 = 1;
- if (utf8 + ucs1 + ucs2 > 1)
- {
- fprintf (stderr, "Pick one of UTF-8, UCS-1 or UCS-2 only\n");
- return 1;
- }
- if (!udfile && (ucs1 || ucs2))
- {
- fprintf (stderr, "Command line arguments always treated as UTF-8\n");
- return 1;
- }
- /* if (!where && poptPeekArg (optCon)) where = (char *) poptGetArg (optCon); */
- if (!mt && !mo && process)
- mt = 1;
- if (!mt && !mo && oa)
- mt = 1;
- if (!mt)
- mo = 1;
- if (mt && mo)
- {
- fprintf (stderr, "Cannot be --mt and --mo\n");
- return 1;
- }
- if (!rx && !tx && process)
- rx = 1;
- if (!rx)
- tx = 1;
- if (tx && rx)
- {
- fprintf (stderr, "Cannot be --tx and --rx\n");
- return 1;
- }
- if (rx)
- nodial = 1;
- if (uds && udfile)
- {
- fprintf (stderr, "Cannot have --ud and --ud-file\n");
- return 1;
- }
- if (mo && !da && poptPeekArg (optCon))
- da = (char *) poptGetArg (optCon);
- if (mt && !oa && poptPeekArg (optCon))
- oa = (char *) poptGetArg (optCon);
- if (tx && oa && mo)
- {
- fprintf (stderr, "--oa makes no sense with --mo as CLI is used (i.e. queue name)\n");
- return 1;
- }
- if (tx && da && mt)
- {
- fprintf (stderr, "--da makes no sense with --mt as called number is used (i.e. queue name)\n");
- return 1;
- }
- if (da && strlen (da) > 20)
- {
- fprintf (stderr, "--da too long\n");
- return 1;
- }
- if (oa && strlen (oa) > 20)
- {
- fprintf (stderr, "--oa too long\n");
- return 1;
- }
- if (queue && strlen (queue) > 20)
- {
- fprintf (stderr, "--queue name too long\n");
- return 1;
- }
- if (mo && scts)
- {
- fprintf (stderr, "scts is set my service centre\n");
- return 1;
- }
- if (uds)
- { /* simple user data command line option in \UTF-8 */
- while (udl < 160 && *uds)
- {
- int v = utf8decode (&uds);
- if (v > 0xFFFF)
- {
- fprintf (stderr, "Invalid character U+%X at %d\n", v, udl);
- return 1;
- }
- ud[udl++] = v;
- }
- }
- if (!uds && !udfile && poptPeekArg (optCon))
- { /* multiple command line arguments in UTF-8 */
- while (poptPeekArg (optCon) && udl < 160)
- {
- unsigned char *a = (unsigned char *) poptGetArg (optCon);
- if (udl && udl < 160)
- ud[udl++] = ' '; /* space between arguments */
- while (udl < 160 && *a)
- {
- int v = utf8decode (&a);
- if (v > 0xFFFF)
- {
- fprintf (stderr, "Invalid character U+%X at %d\n", v, udl);
- return 1;
- }
- ud[udl++] = v;
- }
- }
- }
- if (poptPeekArg (optCon))
- {
- fprintf (stderr, "Unknown argument %s\n", poptGetArg (optCon));
- return 1;
- }
- if (udfile)
- { /* get message from file */
- unsigned char dat[1204],
- *p = dat,
- *e;
- int f,
- n;
- if (*udfile)
- f = open (udfile, O_RDONLY);
- else
- f = fileno (stdin);
- if (f < 0)
- {
- perror (udfile);
- return 1;
- }
- n = read (f, dat, sizeof (dat));
- if (n < 0)
- {
- perror (udfile);
- return 1;
- }
- if (*udfile)
- close (f);
- e = dat + n;
- if (utf8)
- { /* UTF-8 */
- while (p < e && udl < 160 && *p)
- ud[udl++] = utf8decode (&p);
- } else if (ucs1)
- { /* UCS-1 */
- while (p < e && udl < 160)
- ud[udl++] = *p++;
- } else
- { /* UCS-2 */
- while (p + 1 < e && udl < 160)
- {
- ud[udl++] = (*p << 8) + p[1];
- p += 2;
- }
- }
- }
- if (queue)
- {
- char *d = strrchr (queue, '-');
- if (d && d[1])
- subaddress = d[1];
- else
- subaddress = *defaultsubaddress;
- }
- if (chdir (spooldir))
- {
- perror (spooldir);
- return 1;
- }
- if (oa || da)
- { /* send message */
- char temp[100],
- queuename[100],
- *dir = (mo ? rx ? "sms/morx" : "sms/motx" : rx ? "sms/mtrx" : "sms/mttx");
- FILE *f;
- snprintf (temp, sizeof(temp), "sms/.smsq-%d", (int)getpid ());
- mkdir ("sms", 0777); /* ensure directory exists */
- mkdir (dir, 0777); /* ensure directory exists */
- snprintf (queuename, sizeof(queuename), "%s/%s.%ld-%d", dir, *queue ? queue : "0", (long)time (0), (int)getpid ());
- f = fopen (temp, "w");
- if (!f)
- {
- perror (temp);
- return 1;
- }
- if (oa)
- fprintf (f, "oa=%s\n", oa);
- if (da)
- fprintf (f, "da=%s\n", da);
- if (scts)
- fprintf (f, "scts=%s\n", scts);
- if (pid >= 0)
- fprintf (f, "pid=%d\n", pid);
- if (dcs >= 0)
- fprintf (f, "dcs=%d\n", dcs);
- if (mr >= 0)
- fprintf (f, "mr=%d\n", mr);
- if (srr)
- fprintf (f, "srr=1\n");
- if (rp)
- fprintf (f, "rp=1\n");
- if (udh)
- fprintf (f, "udh#%s\n", udh);
- if (vp > 0)
- fprintf (f, "vp=%d\n", vp);
- if (udl)
- {
- int p;
- for (p = 0; p < udl && ud[p] < 0x100; p++);
- if (p == udl)
- {
- for (p = 0; p < udl && ud[p] < 0x80 && ud[p] >= 0x20; p++);
- if (p == udl)
- { /* use text */
- fprintf (f, "ud=");
- for (p = 0; p < udl; p++)
- fputc (ud[p], f);
- } else
- { /* use one byte hex */
- fprintf (f, "ud#");
- for (p = 0; p < udl; p++)
- fprintf (f, "%02hX", ud[p]);
- }
- } else
- { /* use two byte hex */
- fprintf (f, "ud##");
- for (p = 0; p < udl; p++)
- fprintf (f, "%04hX", ud[p]);
- }
- fprintf (f, "\n");
- }
- fclose (f);
- if (rename (temp, queuename))
- {
- perror (queuename);
- unlink (temp);
- return 1;
- }
- }
- if (!nodial && tx && !process)
- { /* dial to send messages */
- char ret=0,
- try = 3;
- if (nowait)
- try = 1;
- while (try--)
- {
- if (mo)
- ret = txqcheck ("motx", queue, subaddress, motxchannel, motxcallerid, motxwait, motxdelay, motxretries, concurrent);
- else
- ret = txqcheck ("mttx", queue, subaddress, mttxchannel, mttxcallerid, mttxwait, mttxdelay, mttxretries, concurrent);
- if (ret < 2)
- break; /* sent, or queued OK */
- if (try)
- sleep (1);
- }
- if (ret == 2 && !nowait)
- fprintf (stderr, "No call scheduled as already sending\n");
- }
- if (process)
- rxqcheck (mo ? rx ? "morx" : "motx" : rx ? "mtrx" : "mttx", queue, process);
- return 0;
- }
|