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