smsq.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2004 - 2005
  5. *
  6. * SMS queuing application for use with asterisk app_sms
  7. * by Adrian Kennard
  8. *
  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.
  14. *
  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.
  18. */
  19. /*** MODULEINFO
  20. <support_level>extended</support_level>
  21. ***/
  22. #include "asterisk.h"
  23. #include <popt.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <dirent.h>
  27. #include <ctype.h>
  28. #include <time.h>
  29. #ifdef SOLARIS
  30. #define POPT_ARGFLAG_SHOW_DEFAULT 0x00800000
  31. #endif
  32. #if !defined(POPT_ARGFLAG_SHOW_DEFAULT)
  33. #define POPT_ARGFLAG_SHOW_DEFAULT 0x00800000
  34. #endif
  35. /*!
  36. * \brief reads next USC character from null terminated UTF-8 string and advanced pointer
  37. * for non valid UTF-8 sequences.
  38. * \return character as is Does \b NOT advance pointer for null termination
  39. */
  40. static int utf8decode (unsigned char **pp)
  41. {
  42. unsigned char *p = *pp;
  43. if (!*p)
  44. return 0; /* null termination of string */
  45. (*pp)++;
  46. if (*p < 0xC0)
  47. return *p; /* ascii or continuation character */
  48. if (*p < 0xE0)
  49. {
  50. if (*p < 0xC2 || (p[1] & 0xC0) != 0x80)
  51. return *p; /* not valid UTF-8 */
  52. (*pp)++;
  53. return ((*p & 0x1F) << 6) + (p[1] & 0x3F);
  54. }
  55. if (*p < 0xF0)
  56. {
  57. if ((*p == 0xE0 && p[1] < 0xA0) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80)
  58. return *p; /* not valid UTF-8 */
  59. (*pp) += 2;
  60. return ((*p & 0x0F) << 12) + ((p[1] & 0x3F) << 6) + (p[2] & 0x3F);
  61. }
  62. if (*p < 0xF8)
  63. {
  64. if ((*p == 0xF0 && p[1] < 0x90) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80)
  65. return *p; /* not valid UTF-8 */
  66. (*pp) += 3;
  67. return ((*p & 0x07) << 18) + ((p[1] & 0x3F) << 12) + ((p[2] & 0x3F) << 6) + (p[3] & 0x3F);
  68. }
  69. if (*p < 0xFC)
  70. {
  71. if ((*p == 0xF8 && p[1] < 0x88) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80
  72. || (p[4] & 0xC0) != 0x80)
  73. return *p; /* not valid UTF-8 */
  74. (*pp) += 4;
  75. return ((*p & 0x03) << 24) + ((p[1] & 0x3F) << 18) + ((p[2] & 0x3F) << 12) + ((p[3] & 0x3F) << 6) + (p[4] & 0x3F);
  76. }
  77. if (*p < 0xFE)
  78. {
  79. if ((*p == 0xFC && p[1] < 0x84) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80
  80. || (p[4] & 0xC0) != 0x80 || (p[5] & 0xC0) != 0x80)
  81. return *p; /* not valid UTF-8 */
  82. (*pp) += 5;
  83. return ((*p & 0x01) << 30) + ((p[1] & 0x3F) << 24) + ((p[2] & 0x3F) << 18) + ((p[3] & 0x3F) << 12) + ((p[4] & 0x3F) << 6) +
  84. (p[5] & 0x3F);
  85. }
  86. return *p; /* not sensible */
  87. }
  88. /*!
  89. * \brief check for any queued messages in specific queue (queue="" means any queue)
  90. * \param dir,queue,subaddress,channel,callerid,wait,delay,retries,concurrent
  91. * \retval 0 if nothing queued
  92. * \retval 1 if queued and outgoing set up OK
  93. * \retval 2 of outgoing exists
  94. */
  95. static char txqcheck (char *dir, char *queue, char subaddress, char *channel, char *callerid, int wait, int delay, int retries, int concurrent)
  96. {
  97. char ogname[100],
  98. temp[100],
  99. dirname[100],
  100. *p=NULL;
  101. FILE *f;
  102. DIR *d;
  103. int ql = strlen (queue), qfl = ql;
  104. struct dirent *fn;
  105. snprintf (dirname, sizeof(dirname), "sms/%s", dir);
  106. d = opendir (dirname);
  107. if (!d)
  108. return 0;
  109. while ((fn = readdir (d))
  110. && !(*fn->d_name != '.'
  111. && ((!ql && (p = strchr (fn->d_name, '.'))) || (ql && !strncmp (fn->d_name, queue, ql) && fn->d_name[ql] == '.'))));
  112. if (!fn)
  113. {
  114. closedir (d);
  115. return 0;
  116. }
  117. if (!ql)
  118. { /* not searching any specific queue, so use whatr we found as the queue */
  119. queue = fn->d_name;
  120. qfl = ql = p - queue;
  121. }
  122. p = strchr (queue, '-');
  123. if (p && p < queue + ql)
  124. {
  125. ql = p - queue;
  126. subaddress = p[1];
  127. }
  128. snprintf (temp, sizeof(temp), "sms/.smsq-%d", (int)getpid ());
  129. f = fopen (temp, "w");
  130. if (!f)
  131. {
  132. perror (temp);
  133. closedir (d);
  134. return 0;
  135. }
  136. fprintf (f, "Channel: ");
  137. if (!channel)
  138. fprintf (f, "Local/%.*s\n", ql, queue);
  139. else
  140. {
  141. p = strchr (channel, '/');
  142. if (!p)
  143. p = channel;
  144. p = strchr (p, 'X');
  145. if (p)
  146. fprintf (f, "%.*s%c%s\n", (int)(p - channel), channel, subaddress, p + 1);
  147. else
  148. fprintf (f, "%s\n", channel);
  149. }
  150. fprintf (f, "Callerid: SMS <");
  151. if (!callerid)
  152. fprintf (f, "%.*s", ql, queue);
  153. else
  154. {
  155. p = strchr (callerid, 'X');
  156. if (p)
  157. fprintf (f, "%.*s%c%s", (int)(p - callerid), callerid, subaddress, p + 1);
  158. else
  159. fprintf (f, "%s", callerid);
  160. }
  161. fprintf (f, ">\n");
  162. fprintf (f, "Application: SMS\n");
  163. fprintf (f, "Data: %.*s", qfl, queue);
  164. if (dir[1] == 't')
  165. fprintf (f, ",s");
  166. fprintf (f, "\nMaxRetries: %d\n", retries);
  167. fprintf (f, "RetryTime: %d\n", delay);
  168. fprintf (f, "WaitTime: %d\n", wait);
  169. fclose (f);
  170. closedir (d);
  171. {
  172. int try = 0;
  173. while (try < concurrent)
  174. {
  175. try++;
  176. snprintf(ogname, sizeof(ogname), "outgoing/smsq.%s.%s.%d", dir, queue, try);
  177. if (!link (temp, ogname))
  178. { /* queued OK */
  179. unlink (temp);
  180. return 1;
  181. }
  182. }
  183. }
  184. /* failed to create call queue */
  185. unlink (temp);
  186. return 2;
  187. }
  188. /*!
  189. * \brief Process received queue entries
  190. * Run through a process, setting environment variables
  191. */
  192. static void rxqcheck (char *dir, char *queue, char *process)
  193. {
  194. char *p;
  195. void *pp = &p;
  196. char dirname[100],
  197. temp[100];
  198. DIR *d;
  199. int ql = strlen (queue);
  200. struct dirent *fn;
  201. snprintf(temp, sizeof(temp), "sms/.smsq-%d", (int)getpid ());
  202. snprintf(dirname, sizeof(dirname), "sms/%s", dir);
  203. d = opendir (dirname);
  204. if (!d)
  205. return;
  206. while ((fn = readdir (d)))
  207. if ((*fn->d_name != '.'
  208. && ((!ql && (p = strchr (fn->d_name, '.'))) || (ql && !strncmp (fn->d_name, queue, ql) && fn->d_name[ql] == '.'))))
  209. { /* process file */
  210. char filename[1010];
  211. char line[1000];
  212. unsigned short ud[160];
  213. unsigned char udl = 0;
  214. FILE *f;
  215. snprintf (filename, sizeof(filename), "sms/%s/%s", dir, fn->d_name);
  216. if (rename (filename, temp))
  217. continue; /* cannot access file */
  218. f = fopen (temp, "r");
  219. unlink (temp);
  220. if (!f)
  221. {
  222. perror (temp);
  223. continue;
  224. }
  225. unsetenv ("oa");
  226. unsetenv ("da");
  227. unsetenv ("scts");
  228. unsetenv ("pid");
  229. unsetenv ("dcs");
  230. unsetenv ("mr");
  231. unsetenv ("srr");
  232. unsetenv ("rp");
  233. unsetenv ("vp");
  234. unsetenv ("udh");
  235. unsetenv ("ud");
  236. unsetenv ("ude");
  237. unsetenv ("ud8");
  238. unsetenv ("ud16");
  239. unsetenv ("morx");
  240. unsetenv ("motx");
  241. unsetenv ("queue");
  242. if (*queue)
  243. setenv ("queue", queue, 1);
  244. setenv (dir, "", 1);
  245. while (fgets (line, sizeof (line), f))
  246. {
  247. for (p = line; *p && *p != '\n' && *p != '\r'; p++);
  248. *p = 0; /* strip eoln */
  249. p = line;
  250. if (!*p || *p == ';')
  251. continue; /* blank line or comment, ignore */
  252. while (isalnum (*p))
  253. {
  254. *p = tolower (*p);
  255. p++;
  256. }
  257. while (isspace (*p))
  258. *p++ = 0;
  259. if (*p == '=')
  260. { /* = */
  261. *p++ = 0;
  262. if (!strcmp (line, "oa") || !strcmp (line, "da") || !strcmp (line, "scts") || !strcmp (line, "pid")
  263. || !strcmp (line, "dcs") || !strcmp (line, "mr") || !strcmp (line, "vp"))
  264. setenv (line, p, 1);
  265. else if ((!strcmp (line, "srr") || !strcmp (line, "rp")) && atoi (p))
  266. setenv (line, "", 1);
  267. else if (!strcmp (line, "ud"))
  268. { /* read the user data as UTF-8 */
  269. long v;
  270. udl = 0;
  271. while ((v = utf8decode (pp)) && udl < 160)
  272. if (v && v <= 0xFFFF)
  273. ud[udl++] = v;
  274. }
  275. } else if (*p == '#')
  276. {
  277. *p++ = 0;
  278. if (*p == '#')
  279. { /* ## */
  280. p++;
  281. if (!strcmp (line, "udh"))
  282. setenv (line, p, 1);
  283. else if (!strcmp (line, "ud"))
  284. { /* read user data UCS-2 */
  285. udl = 0;
  286. while (*p && udl < 160)
  287. {
  288. if (isxdigit (*p) && isxdigit (p[1]) && isxdigit (p[2]) && isxdigit (p[3]))
  289. {
  290. ud[udl++] =
  291. (((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 12) +
  292. (((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF)) << 8) +
  293. (((isalpha (p[2]) ? 9 : 0) + (p[2] & 0xF)) << 4) + ((isalpha (p[3]) ? 9 : 0) + (p[3] & 0xF));
  294. p += 4;
  295. } else
  296. break;
  297. }
  298. }
  299. } else
  300. { /* # */
  301. if (!strcmp (line, "ud"))
  302. { /* read user data UCS-1 */
  303. udl = 0;
  304. while (*p && udl < 160)
  305. {
  306. if (isxdigit (*p) && isxdigit (p[1]))
  307. {
  308. ud[udl++] = (((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF));
  309. p += 2;
  310. } else
  311. break;
  312. }
  313. }
  314. }
  315. }
  316. }
  317. fclose (f);
  318. /* set up user data variables */
  319. {
  320. char tmp[481];
  321. int n, x;
  322. for (n = 0, x = 0; x < udl; x++)
  323. {
  324. unsigned short v = ud[x];
  325. if (v)
  326. {
  327. if (v < 0x80)
  328. tmp[n++] = v;
  329. else if (v < 0x800)
  330. {
  331. tmp[n++] = (0xC0 + (v >> 6));
  332. tmp[n++] = (0x80 + (v & 0x3F));
  333. } else
  334. {
  335. tmp[n++] = (0xE0 + (v >> 12));
  336. tmp[n++] = (0x80 + ((v >> 6) & 0x3F));
  337. tmp[n++] = (0x80 + (v & 0x3F));
  338. }
  339. }
  340. }
  341. tmp[n] = 0;
  342. setenv ("ud", tmp, 1);
  343. for (n = 0, x = 0; x < udl; x++)
  344. {
  345. unsigned short v = ud[x];
  346. if (v < ' ' || v == '\\')
  347. {
  348. tmp[n++] = '\\';
  349. if (v == '\\')
  350. tmp[n++] = '\\';
  351. else if (v == '\n')
  352. tmp[n++] = 'n';
  353. else if (v == '\r')
  354. tmp[n++] = 'r';
  355. else if (v == '\t')
  356. tmp[n++] = 't';
  357. else if (v == '\f')
  358. tmp[n++] = 'f';
  359. else
  360. {
  361. tmp[n++] = '0' + (v >> 6);
  362. tmp[n++] = '0' + ((v >> 3) & 7);
  363. tmp[n++] = '0' + (v & 7);
  364. }
  365. } else if (v < 0x80)
  366. tmp[n++] = v;
  367. else if (v < 0x800)
  368. {
  369. tmp[n++] = (0xC0 + (v >> 6));
  370. tmp[n++] = (0x80 + (v & 0x3F));
  371. } else
  372. {
  373. tmp[n++] = (0xE0 + (v >> 12));
  374. tmp[n++] = (0x80 + ((v >> 6) & 0x3F));
  375. tmp[n++] = (0x80 + (v & 0x3F));
  376. }
  377. }
  378. tmp[n] = 0;
  379. setenv ("ude", tmp, 1);
  380. for (x = 0; x < udl && ud[x] < 0x100; x++);
  381. if (x == udl)
  382. {
  383. for (n = 0, x = 0; x < udl; x++)
  384. {
  385. sprintf (tmp + n, "%02X", ud[x]);
  386. n += 2;
  387. }
  388. setenv ("ud8", tmp, 1);
  389. }
  390. for (n = 0, x = 0; x < udl; x++)
  391. {
  392. sprintf (tmp + n, "%04X", ud[x]);
  393. n += 4;
  394. }
  395. setenv ("ud16", tmp, 1);
  396. }
  397. /* run the command */
  398. if (system (process) == -1) {
  399. fprintf(stderr, "Failed to fork process '%s'\n", process);
  400. }
  401. }
  402. closedir (d);
  403. }
  404. /* Main app */
  405. int
  406. main (int argc, const char *argv[])
  407. {
  408. char c;
  409. int mt = 0,
  410. mo = 0,
  411. tx = 0,
  412. rx = 0,
  413. nodial = 0,
  414. nowait = 0,
  415. concurrent = 1,
  416. motxwait = 10,
  417. motxdelay = 1,
  418. motxretries = 10,
  419. mttxwait = 10,
  420. mttxdelay = 30,
  421. mttxretries = 100,
  422. mr = -1,
  423. pid = -1,
  424. dcs = -1,
  425. srr = 0,
  426. rp = 0,
  427. vp = 0,
  428. udl = 0,
  429. utf8 = 0,
  430. ucs1 = 0,
  431. ucs2 = 0;
  432. unsigned short ud[160];
  433. unsigned char *uds = 0,
  434. *udh = 0;
  435. char *da = 0,
  436. *oa = 0,
  437. *queue = "",
  438. *udfile = 0,
  439. *process = 0,
  440. *spooldir = "/var/spool/asterisk",
  441. *motxchannel = "Local/1709400X",
  442. *motxcallerid = 0,
  443. *mttxchannel = 0,
  444. *mttxcallerid = "080058752X0",
  445. *defaultsubaddress = "9",
  446. subaddress = 0,
  447. *scts = 0;
  448. poptContext optCon; /* context for parsing command-line options */
  449. const struct poptOption optionsTable[] = {
  450. {"queue", 'q', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &queue, 0, "Queue [inc sub address]", "number[-X]"},
  451. {"da", 'd', POPT_ARG_STRING, &da, 0, "Destination address", "number"},
  452. {"oa", 'o', POPT_ARG_STRING, &oa, 0, "Origination address", "number"},
  453. {"ud", 'm', POPT_ARG_STRING, &uds, 0, "Message", "text"},
  454. {"ud-file", 'f', POPT_ARG_STRING, &udfile, 0, "Message file", "filename"},
  455. {"UTF-8", 0, POPT_ARG_NONE, &utf8, 0, "File treated as null terminated UTF-8 (default)", 0},
  456. {"UCS-1", 0, POPT_ARG_NONE, &ucs1, 0, "File treated as UCS-1", 0},
  457. {"UCS-2", 0, POPT_ARG_NONE, &ucs2, 0, "File treated as UCS-2", 0},
  458. {"mt", 't', POPT_ARG_NONE, &mt, 0, "Mobile Terminated", 0},
  459. {"mo", 0, POPT_ARG_NONE, &mo, 0, "Mobile Originated", 0},
  460. {"tx", 0, POPT_ARG_NONE, &tx, 0, "Send message", 0},
  461. {"rx", 'r', POPT_ARG_NONE, &rx, 0, "Queue for receipt", 0},
  462. {"process", 'e', POPT_ARG_STRING, &process, 0, "Rx queue process command", "command"},
  463. {"no-dial", 'x', POPT_ARG_NONE, &nodial, 0, "Do not dial", 0},
  464. {"no-wait", 0, POPT_ARG_NONE, &nowait, 0, "Do not wait if already calling", 0},
  465. {"concurrent", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &concurrent, 0, "Number of concurrent calls to allow", "n"},
  466. {"motx-channel", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &motxchannel, 0, "Channel for motx calls", "channel"},
  467. {"motx-callerid", 0, POPT_ARG_STRING, &motxcallerid, 0,
  468. "Caller ID for motx calls (default is queue name without sub address)", "number"},
  469. {"motx-wait", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &motxwait, 0, "Time to wait for motx call to answer",
  470. "seconds"},
  471. {"motx-delay", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &motxdelay, 0, "Time between motx call retries", "seconds"},
  472. {"motx-retries", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &motxretries, 0, "Number of retries for motx call", "n"},
  473. {"mttx-channel", 0, POPT_ARG_STRING, &mttxchannel, 0,
  474. "Channel for mttx calls (default is Local/ and queue name without sub address)", "channel"},
  475. {"mttx-callerid", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &mttxcallerid, 0,
  476. "Caller ID for mttx calls (default is queue name without sub address)", "number"},
  477. {"mttx-wait", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &mttxwait, 0, "Time to wait for mttx call to answer",
  478. "seconds"},
  479. {"mttx-delay", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &mttxdelay, 0, "Time between mttx call retries", "seconds"},
  480. {"mttx-retries", 0, POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &mttxretries, 0, "Number of retries for mttx call", "n"},
  481. {"mr", 'n', POPT_ARG_INT, &mr, 0, "Message reference", "n"},
  482. {"pid", 'p', POPT_ARG_INT, &pid, 0, "Protocol ID", "n"},
  483. {"dcs", 'c', POPT_ARG_INT, &dcs, 0, "Data Coding Scheme", "n"},
  484. {"udh", 0, POPT_ARG_STRING, &udh, 0, "User data header", "hex"},
  485. {"srr", 0, POPT_ARG_NONE, &srr, 0, "Status Report Request", 0},
  486. {"rp", 0, POPT_ARG_NONE, &rp, 0, "Return Path request", 0},
  487. {"v", 0, POPT_ARG_INT, &vp, 0, "Validity Period", "seconds"},
  488. {"scts", 0, POPT_ARG_STRING, &scts, 0, "Timestamp", "YYYY-MM-SSTHH:MM:SS"},
  489. {"default-sub-address", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &defaultsubaddress, 0, "Default sub address", "X"},
  490. {"spool-dir", 0, POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &spooldir, 0, "Asterisk spool dir", "dirname"},
  491. POPT_AUTOHELP {NULL, 0, 0, NULL, 0}
  492. };
  493. optCon = poptGetContext (NULL, argc, argv, optionsTable, 0);
  494. poptSetOtherOptionHelp (optCon, "<oa/da> <message>");
  495. /* Now do options processing, get portname */
  496. if ((c = poptGetNextOpt (optCon)) < -1)
  497. {
  498. /* an error occurred during option processing */
  499. fprintf (stderr, "%s: %s\n", poptBadOption (optCon, POPT_BADOPTION_NOALIAS), poptStrerror (c));
  500. return 1;
  501. }
  502. if (!ucs1 && !ucs2)
  503. utf8 = 1;
  504. if (utf8 + ucs1 + ucs2 > 1)
  505. {
  506. fprintf (stderr, "Pick one of UTF-8, UCS-1 or UCS-2 only\n");
  507. return 1;
  508. }
  509. if (!udfile && (ucs1 || ucs2))
  510. {
  511. fprintf (stderr, "Command line arguments always treated as UTF-8\n");
  512. return 1;
  513. }
  514. /* if (!where && poptPeekArg (optCon)) where = (char *) poptGetArg (optCon); */
  515. if (!mt && !mo && process)
  516. mt = 1;
  517. if (!mt && !mo && oa)
  518. mt = 1;
  519. if (!mt)
  520. mo = 1;
  521. if (mt && mo)
  522. {
  523. fprintf (stderr, "Cannot be --mt and --mo\n");
  524. return 1;
  525. }
  526. if (!rx && !tx && process)
  527. rx = 1;
  528. if (!rx)
  529. tx = 1;
  530. if (tx && rx)
  531. {
  532. fprintf (stderr, "Cannot be --tx and --rx\n");
  533. return 1;
  534. }
  535. if (rx)
  536. nodial = 1;
  537. if (uds && udfile)
  538. {
  539. fprintf (stderr, "Cannot have --ud and --ud-file\n");
  540. return 1;
  541. }
  542. if (mo && !da && poptPeekArg (optCon))
  543. da = (char *) poptGetArg (optCon);
  544. if (mt && !oa && poptPeekArg (optCon))
  545. oa = (char *) poptGetArg (optCon);
  546. if (tx && oa && mo)
  547. {
  548. fprintf (stderr, "--oa makes no sense with --mo as CLI is used (i.e. queue name)\n");
  549. return 1;
  550. }
  551. if (tx && da && mt)
  552. {
  553. fprintf (stderr, "--da makes no sense with --mt as called number is used (i.e. queue name)\n");
  554. return 1;
  555. }
  556. if (da && strlen (da) > 20)
  557. {
  558. fprintf (stderr, "--da too long\n");
  559. return 1;
  560. }
  561. if (oa && strlen (oa) > 20)
  562. {
  563. fprintf (stderr, "--oa too long\n");
  564. return 1;
  565. }
  566. if (queue && strlen (queue) > 20)
  567. {
  568. fprintf (stderr, "--queue name too long\n");
  569. return 1;
  570. }
  571. if (mo && scts)
  572. {
  573. fprintf (stderr, "scts is set my service centre\n");
  574. return 1;
  575. }
  576. if (uds)
  577. { /* simple user data command line option in \UTF-8 */
  578. while (udl < 160 && *uds)
  579. {
  580. int v = utf8decode (&uds);
  581. if (v > 0xFFFF)
  582. {
  583. fprintf (stderr, "Invalid character U+%X at %d\n", v, udl);
  584. return 1;
  585. }
  586. ud[udl++] = v;
  587. }
  588. }
  589. if (!uds && !udfile && poptPeekArg (optCon))
  590. { /* multiple command line arguments in UTF-8 */
  591. while (poptPeekArg (optCon) && udl < 160)
  592. {
  593. unsigned char *a = (unsigned char *) poptGetArg (optCon);
  594. if (udl && udl < 160)
  595. ud[udl++] = ' '; /* space between arguments */
  596. while (udl < 160 && *a)
  597. {
  598. int v = utf8decode (&a);
  599. if (v > 0xFFFF)
  600. {
  601. fprintf (stderr, "Invalid character U+%X at %d\n", v, udl);
  602. return 1;
  603. }
  604. ud[udl++] = v;
  605. }
  606. }
  607. }
  608. if (poptPeekArg (optCon))
  609. {
  610. fprintf (stderr, "Unknown argument %s\n", poptGetArg (optCon));
  611. return 1;
  612. }
  613. if (udfile)
  614. { /* get message from file */
  615. unsigned char dat[1204],
  616. *p = dat,
  617. *e;
  618. int f,
  619. n;
  620. if (*udfile)
  621. f = open (udfile, O_RDONLY);
  622. else
  623. f = fileno (stdin);
  624. if (f < 0)
  625. {
  626. perror (udfile);
  627. return 1;
  628. }
  629. n = read (f, dat, sizeof (dat));
  630. if (n < 0)
  631. {
  632. perror (udfile);
  633. return 1;
  634. }
  635. if (*udfile)
  636. close (f);
  637. e = dat + n;
  638. if (utf8)
  639. { /* UTF-8 */
  640. while (p < e && udl < 160 && *p)
  641. ud[udl++] = utf8decode (&p);
  642. } else if (ucs1)
  643. { /* UCS-1 */
  644. while (p < e && udl < 160)
  645. ud[udl++] = *p++;
  646. } else
  647. { /* UCS-2 */
  648. while (p + 1 < e && udl < 160)
  649. {
  650. ud[udl++] = (*p << 8) + p[1];
  651. p += 2;
  652. }
  653. }
  654. }
  655. if (queue)
  656. {
  657. char *d = strrchr (queue, '-');
  658. if (d && d[1])
  659. subaddress = d[1];
  660. else
  661. subaddress = *defaultsubaddress;
  662. }
  663. if (chdir (spooldir))
  664. {
  665. perror (spooldir);
  666. return 1;
  667. }
  668. if (oa || da)
  669. { /* send message */
  670. char temp[100],
  671. queuename[100],
  672. *dir = (mo ? rx ? "sms/morx" : "sms/motx" : rx ? "sms/mtrx" : "sms/mttx");
  673. FILE *f;
  674. snprintf (temp, sizeof(temp), "sms/.smsq-%d", (int)getpid ());
  675. mkdir ("sms", 0777); /* ensure directory exists */
  676. mkdir (dir, 0777); /* ensure directory exists */
  677. snprintf (queuename, sizeof(queuename), "%s/%s.%ld-%d", dir, *queue ? queue : "0", (long)time (0), (int)getpid ());
  678. f = fopen (temp, "w");
  679. if (!f)
  680. {
  681. perror (temp);
  682. return 1;
  683. }
  684. if (oa)
  685. fprintf (f, "oa=%s\n", oa);
  686. if (da)
  687. fprintf (f, "da=%s\n", da);
  688. if (scts)
  689. fprintf (f, "scts=%s\n", scts);
  690. if (pid >= 0)
  691. fprintf (f, "pid=%d\n", pid);
  692. if (dcs >= 0)
  693. fprintf (f, "dcs=%d\n", dcs);
  694. if (mr >= 0)
  695. fprintf (f, "mr=%d\n", mr);
  696. if (srr)
  697. fprintf (f, "srr=1\n");
  698. if (rp)
  699. fprintf (f, "rp=1\n");
  700. if (udh)
  701. fprintf (f, "udh#%s\n", udh);
  702. if (vp > 0)
  703. fprintf (f, "vp=%d\n", vp);
  704. if (udl)
  705. {
  706. int p;
  707. for (p = 0; p < udl && ud[p] < 0x100; p++);
  708. if (p == udl)
  709. {
  710. for (p = 0; p < udl && ud[p] < 0x80 && ud[p] >= 0x20; p++);
  711. if (p == udl)
  712. { /* use text */
  713. fprintf (f, "ud=");
  714. for (p = 0; p < udl; p++)
  715. fputc (ud[p], f);
  716. } else
  717. { /* use one byte hex */
  718. fprintf (f, "ud#");
  719. for (p = 0; p < udl; p++)
  720. fprintf (f, "%02X", ud[p]);
  721. }
  722. } else
  723. { /* use two byte hex */
  724. fprintf (f, "ud##");
  725. for (p = 0; p < udl; p++)
  726. fprintf (f, "%04X", ud[p]);
  727. }
  728. fprintf (f, "\n");
  729. }
  730. fclose (f);
  731. if (rename (temp, queuename))
  732. {
  733. perror (queuename);
  734. unlink (temp);
  735. return 1;
  736. }
  737. }
  738. if (!nodial && tx && !process)
  739. { /* dial to send messages */
  740. char ret=0,
  741. try = 3;
  742. if (nowait)
  743. try = 1;
  744. while (try--)
  745. {
  746. if (mo)
  747. ret = txqcheck ("motx", queue, subaddress, motxchannel, motxcallerid, motxwait, motxdelay, motxretries, concurrent);
  748. else
  749. ret = txqcheck ("mttx", queue, subaddress, mttxchannel, mttxcallerid, mttxwait, mttxdelay, mttxretries, concurrent);
  750. if (ret < 2)
  751. break; /* sent, or queued OK */
  752. if (try)
  753. sleep (1);
  754. }
  755. if (ret == 2 && !nowait)
  756. fprintf (stderr, "No call scheduled as already sending\n");
  757. }
  758. if (process)
  759. rxqcheck (mo ? rx ? "morx" : "motx" : rx ? "mtrx" : "mttx", queue, process);
  760. return 0;
  761. }