smsq.c 25 KB

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