aboutsummaryrefslogtreecommitdiffstats
path: root/utils/smsq.c
blob: 1463b6209ff2836a69b89b29a7ac56a97b14de93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
#include <stdio.h>
#include <popt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#ifdef SOLARIS
#include <solaris-compat/compat.h>
#define     POPT_ARGFLAG_SHOW_DEFAULT 0x00800000
#endif
#if !defined(POPT_ARGFLAG_SHOW_DEFAULT)
#define     POPT_ARGFLAG_SHOW_DEFAULT 0x00800000
#endif

/* SMS queuing application for use with asterisk app_sms */
/* by Adrian Kennard, 2004 - 2005 */

/* reads next USC character from null terminated UTF-8 string and advanced pointer */
/* for non valid UTF-8 sequences, returns character as is */
/* Does 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 */
}

/* check for any queued messages in specific queue (queue="" means any queue) */
/* returns 0 if nothing queued, 1 if queued and outgoing set up OK, 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", 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", 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", 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;
}

/* Process received queue entries and run through a process, setting environment variables */
static void rxqcheck (char *dir, char *queue, char *process)
{
   unsigned char *p;
   char dirname[100],
     temp[100];
   DIR *d;
   int ql = strlen (queue);
   struct dirent *fn;
   snprintf(temp, sizeof(temp), "sms/.smsq-%d", 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 (&p)) && 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 temp[481];
            int n,
              p;
            for (n = 0, p = 0; p < udl; p++)
            {
               unsigned short v = ud[p];
               if (v)
               {
                  if (v < 0x80)
                     temp[n++] = v;
                  else if (v < 0x800)
                  {
                     temp[n++] = (0xC0 + (v >> 6));
                     temp[n++] = (0x80 + (v & 0x3F));
                  } else
                  {
                     temp[n++] = (0xE0 + (v >> 12));
                     temp[n++] = (0x80 + ((v >> 6) & 0x3F));
                     temp[n++] = (0x80 + (v & 0x3F));
                  }
               }
            }
            temp[n] = 0;
            setenv ("ud", temp, 1);
            for (n = 0, p = 0; p < udl; p++)
            {
               unsigned short v = ud[p];
               if (v < ' ' || v == '\\')
               {
                  temp[n++] = '\\';
                  if (v == '\\')
                     temp[n++] = '\\';
                  else if (v == '\n')
                     temp[n++] = 'n';
                  else if (v == '\r')
                     temp[n++] = 'r';
                  else if (v == '\t')
                     temp[n++] = 't';
                  else if (v == '\f')
                     temp[n++] = 'f';
                  else
                  {
                     temp[n++] = '0' + (v >> 6);
                     temp[n++] = '0' + ((v >> 3) & 7);
                     temp[n++] = '0' + (v & 7);
                  }
               } else if (v < 0x80)
                  temp[n++] = v;
               else if (v < 0x800)
               {
                  temp[n++] = (0xC0 + (v >> 6));
                  temp[n++] = (0x80 + (v & 0x3F));
               } else
               {
                  temp[n++] = (0xE0 + (v >> 12));
                  temp[n++] = (0x80 + ((v >> 6) & 0x3F));
                  temp[n++] = (0x80 + (v & 0x3F));
               }
            }
            temp[n] = 0;
            setenv ("ude", temp, 1);
            for (p = 0; p < udl && ud[p] < 0x100; p++);
            if (p == udl)
            {
               for (n = 0, p = 0; p < udl; p++)
               {
                  sprintf (temp + n, "%02X", ud[p]);
                  n += 2;
               }
               setenv ("ud8", temp, 1);
            }
            for (n = 0, p = 0; p < udl; p++)
            {
               sprintf (temp + n, "%04X", ud[p]);
               n += 4;
            }
            setenv ("ud16", temp, 1);
         }
         /* run the command */
         system (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 = (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", 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), 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, "%02X", ud[p]);
            }
         } else
         {                      /* use two byte hex */
            fprintf (f, "ud##");
            for (p = 0; p < udl; p++)
               fprintf (f, "%04X", 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;
}