rewrote parts of Msg.pm and client.c so that the messages no longer use
[spider.git] / src / client.c
1 /*
2  * C Client for the DX Spider cluster program
3  *
4  * Eventually this program will be a complete replacement
5  * for the perl version.
6  *
7  * This program provides the glue necessary to talk between
8  * an input (eg from telnet or ax25) and the perl DXSpider
9  * node.
10  *
11  * Currently, this program connects STDIN/STDOUT to the
12  * message system used by cluster.pl
13  *
14  * Copyright (c) 2000 Dirk Koopman G1TLH
15  *
16  * $Id$
17  */
18
19 #include <stdio.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <netdb.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <termios.h>
33 #include <regex.h>
34
35 #include "sel.h"
36 #include "cmsg.h"
37 #include "debug.h"
38
39 #define TEXT 1
40 #define MSG 2
41 #define MAXBUFL 1024
42
43 #ifndef MAXPATHLEN 
44 #define MAXPATHLEN 256
45 #endif
46
47 #define DEFPACLEN 128
48 #define MAXPACLEN 236
49 #define MAXCALLSIGN 9
50
51 #define DBUF 1
52 #define DMSG 2
53
54 typedef struct 
55 {
56         int cnum;                                       /* the connection number */
57         int sort;                                       /* the type of connection either text or msg */
58         cmsg_t *in;                                     /* current input message being built up */
59         cmsg_t *out;                            /* current output message being sent */
60         cmsg_t *obuf;                           /* current output being buffered */
61         reft *inq;                                      /* input queue */
62         reft *outq;                                     /* output queue */
63         sel_t *sp;                                      /* my select fcb address */
64         struct termios t;                       /* any termios associated with this cnum */
65         char echo;                                      /* echo characters back to this cnum */
66         char t_set;                                     /* the termios structure is valid */
67         char buffer_it;                         /* buffer outgoing packets for paclen */
68 } fcb_t;
69
70 typedef struct 
71 {
72         char *in;
73         regex_t *regex;
74 } myregex_t;
75
76
77 char *node_addr = "localhost";  /* the node tcp address, can be overridden by DXSPIDER_HOST */
78 int node_port = 27754;                  /* the tcp port of the node at the above address can be overidden by DXSPIDER_PORT*/
79 char *call;                                             /* the caller's callsign */
80 char *connsort;                                 /* the type of connection */
81 fcb_t *in;                                              /* the fcb of 'stdin' that I shall use */
82 fcb_t *node;                                    /* the fcb of the msg system */
83 char nl = '\n';                                 /* line end character */
84 char ending = 0;                                /* set this to end the program */
85 char send_Z = 1;                                /* set a Z record to the node on termination */
86 char echo = 1;                                  /* echo characters on stdout from stdin */
87 char int_tabs = 0;                              /* interpret tabs -> spaces */
88 char *root = "/spider";         /* root of data tree, can be overridden by DXSPIDER_ROOT  */
89 int timeout = 60;                               /* default timeout for logins and things */
90 int paclen = DEFPACLEN;                 /* default buffer size for outgoing packets */
91 int tabsize = 8;                                /* default tabsize for text messages */
92
93 myregex_t iscallreg[] = {               /* regexes to determine whether this is a reasonable callsign */
94         {
95                 "^[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0                /* G1TLH G1TLH1 */
96         },
97         {
98                 "^[0-9]+[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0          /* 2E0AAA 2E0AAA1 */
99         },
100         {
101                 "^[A-Z]+[0-9]+[A-Z]+-[1-9]$", 0                /* G1TLH-2 */
102         },
103         {
104                 "^[0-9]+[A-Z]+[0-9]+[A-Z]+-[1-9]$", 0          /* 2E0AAA-2 */
105         },
106         {
107                 "^[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0               /* G1TLH-11 */
108         },
109         {
110                 "^[0-9]+[A-Z]+[0-9]+[A-Z]+-1[0-5]$", 0         /* 2E0AAA-11 */
111         },
112         {
113                 0, 0
114         }
115 };
116
117 void terminate(int);
118
119 /*
120  * utility routines - various
121  */
122
123 void die(char *s, ...)
124 {
125         char buf[2000];
126         
127         va_list ap;
128         va_start(ap, s);
129         vsnprintf(buf, sizeof(buf)-1, s, ap);
130         va_end(ap);
131         fprintf(stderr,"%s\n", buf);
132         terminate(-1);
133 }
134
135 char *strupper(char *s)
136 {
137         char *d = malloc(strlen(s)+1);
138         char *p = d;
139         
140         if (!d)
141                 die("out of room in strupper");
142         while (*p++ = toupper(*s++)) ;
143         return d;
144 }
145
146 char *strlower(char *s)
147 {
148         char *d = malloc(strlen(s)+1);
149         char *p = d;
150         
151         if (!d)
152                 die("out of room in strlower");
153         while (*p++ = tolower(*s++)) ;
154         return d;
155 }
156
157 int eq(char *a, char *b)
158 {
159         return (strcmp(a, b) == 0);
160 }
161
162 int xopen(char *dir, char *name, int mode)
163 {
164         char fn[MAXPATHLEN+1];
165         snprintf(fn, MAXPATHLEN, "%s/%s/%s", root, dir, name);
166         return open(fn, mode);
167 }
168
169 int iscallsign(char *s)
170 {
171         myregex_t *rp;
172
173         if (strlen(s) > MAXCALLSIGN)
174                 return 0;
175         
176         for (rp = iscallreg; rp->in; ++rp) {
177                 if (regexec(rp->regex, s, 0, 0, 0) == 0)
178                         return 1;
179         }
180         return 0;
181 }
182
183 /*
184  * higher level send and receive routines
185  */
186
187 fcb_t *fcb_new(int cnum, int sort)
188 {
189         fcb_t *f = malloc(sizeof(fcb_t));
190         if (!f)
191                 die("no room in fcb_new");
192         memset (f, 0, sizeof(fcb_t));
193         f->cnum = cnum;
194         f->sort = sort;
195         f->inq = chain_new();
196         f->outq = chain_new();
197         return f;
198 }
199
200 void flush_text(fcb_t *f)
201 {
202         if (f->obuf) {
203                 cmsg_send(f->outq, f->obuf, 0);
204                 f->sp->flags |= SEL_OUTPUT;
205                 f->obuf = 0;
206         }
207 }
208
209 void send_text(fcb_t *f, char *s, int l)
210 {
211         cmsg_t *mp;
212         char *p;
213         
214         if (f->buffer_it && f->obuf) {
215                 mp = f->obuf;
216         } else {
217                 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
218         }
219
220         /* remove trailing spaces  */
221         while (l > 0 &&isspace(s[l-1]))
222                 --l;
223
224         for (p = s; p < s+l; ) {
225                 if (mp->inp >= mp->data + paclen) {
226                         flush_text(f);
227                         f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
228                 }
229                 *mp->inp++ = *p++;
230         }
231         if (mp->inp >= mp->data + paclen) {
232                 flush_text(f);
233                 f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
234         }
235         *mp->inp++ = nl;
236         if (!f->buffer_it)
237                 flush_text(f);
238 }
239
240 void send_msg(fcb_t *f, char let, unsigned char *s, int l)
241 {
242         cmsg_t *mp;
243         int ln;
244         int myl = strlen(call)+2+l;
245
246         mp = cmsg_new(myl+4+1, f->sort, f);
247         *mp->inp++ = let;
248         strcpy(mp->inp, call);
249         mp->inp += strlen(call);
250         *mp->inp++ = '|';
251         if (l > 0) {
252                 unsigned char *p;
253                 for (p = s; p < s+l; ++p) {
254                         if (mp->inp >= mp->data + (myl - 4)) {
255                                 int off = mp->inp - mp->data;
256                                 myl += 256;
257                                 mp = realloc(mp, myl);
258                                 mp->inp = mp->data + off;
259                         }
260                         
261                         if (*p < 0x20 || *p > 0x7e || *p == '%') {
262                                 sprintf(mp->inp, "%%%02X", *p & 0xff);
263                                 mp->inp += strlen(mp->inp);
264                         } else 
265                                 *mp->inp++ = *p;
266                 }
267         }
268         *mp->inp++ = '\n';
269         *mp->inp = 0;
270         cmsg_send(f->outq, mp, 0);
271         f->sp->flags |= SEL_OUTPUT;
272 }
273
274 /*
275  * the callback (called by sel_run) that handles all the inputs and outputs
276  */
277
278 int fcb_handler(sel_t *sp, int in, int out, int err)
279 {
280         fcb_t *f = sp->fcb;
281         cmsg_t *mp, *omp;
282         unsigned char c;
283         
284         /* input modes */
285         if (in) {
286                 char *p, buf[MAXBUFL];
287                 int r;
288
289                 /* read what we have into a buffer */
290                 r = read(f->cnum, buf, MAXBUFL);
291                 if (r < 0) {
292                         switch (errno) {
293                         case EINTR:
294                         case EINPROGRESS:
295                         case EAGAIN:
296                                 goto lout;
297                         default:
298                                 if (f->sort == MSG)
299                                         send_Z = 0;
300                                 ending++;
301                                 return 0;
302                         }
303                 } else if (r == 0) {
304                         if (f->sort == MSG)
305                                 send_Z = 0;
306                         ending++;
307                         return 0;
308                 }
309
310                 dbgdump(DBUF, "in ->", buf, r);
311                 
312                 /* create a new message buffer if required */
313                 if (!f->in)
314                         f->in = cmsg_new(MAXBUFL+1, f->sort, f);
315                 mp = f->in;
316
317                 switch (f->sort) {
318                 case TEXT:
319                         p = buf;
320                         if (f->echo)
321                                 omp = cmsg_new(3*r+1, f->sort, f);
322                         while (r > 0 && p < &buf[r]) {
323
324                                 /* echo processing */
325                                 if (f->echo) {
326                                         switch (*p) {
327                                         case '\b':
328                                         case 0x7f:
329                                                 strcpy(omp->inp, "\b \b");
330                                                 omp->inp += strlen(omp->inp);
331                                                 break;
332                                         default:
333                                                 *omp->inp++ = *p;
334                                         }
335                                 }
336                                 
337                                 /* character processing */
338                                 switch (*p) {
339                                 case '\t':
340                                         if (int_tabs) {
341                                                 memset(mp->inp, ' ', tabsize);
342                                                 mp->inp += tabsize;
343                                                 ++p;
344                                         } else {
345                                                 *mp->inp++ = *p++;
346                                         }
347                                         break;
348                                 case '\b':
349                                 case 0x7f:
350                                         if (mp->inp > mp->data)
351                                                 mp->inp--;
352                                         ++p;
353                                         break;
354                                 default:
355                                         if (nl == '\n' && *p == '\r') {   /* ignore \r in telnet mode (ugh) */
356                                                 p++;
357                                         } else if (*p == nl) {
358                                                 if (mp->inp == mp->data)
359                                                         *mp->inp++ = ' ';
360                                                 *mp->inp = 0;              /* zero terminate it, but don't include it in the length */
361                                                 dbgdump(DMSG, "QUEUE TEXT", mp->data, mp->inp-mp->data);
362                                                 cmsg_send(f->inq, mp, 0);
363                                                 f->in = mp = cmsg_new(MAXBUFL+1, f->sort, f);
364                                                 ++p;
365                                         } else {
366                                                 if (mp->inp < &mp->data[MAXBUFL-8])
367                                                         *mp->inp++ = *p++;
368                                                 else {
369                                                         mp->inp = mp->data;
370                                                 }
371                                         }
372                                 }
373                         }
374                         
375                         /* queue any echo text */
376                         if (f->echo) {
377                                 dbgdump(DMSG, "QUEUE ECHO TEXT", omp->data, omp->inp - omp->data);
378                                 cmsg_send(f->outq, omp, 0);
379                                 f->sp->flags |= SEL_OUTPUT;
380                         }
381                         
382                         break;
383
384                 case MSG:
385                         p = buf;
386                         while (r > 0 && p < &buf[r]) {
387                                 unsigned char ch = *p++;
388                                 
389                                 if (mp->inp >= mp->data + (MAXBUFL-1)) {
390                                         mp->state = 0;
391                                         mp->inp = mp->data;
392                                         dbg(DMSG, "Message longer than %d received", MAXBUFL);
393                                 }
394
395                                 switch (mp->state) {
396                                 case 0: 
397                                         if (ch == '%') {
398                                                 c = 0;
399                                                 mp->state = 1;
400                                         } else if (ch == '\n') {
401                                                 /* kick it upstairs */
402                                                 *mp->inp = 0;
403                                                 dbgdump(DMSG, "QUEUE MSG", mp->data, mp->inp - mp->data);
404                                                 cmsg_send(f->inq, mp, 0);
405                                                 mp = f->in = cmsg_new(MAXBUFL+1, f->sort, f);
406                                         } else if (ch < 0x20 || ch > 0x7e) {
407                                                 dbg(DMSG, "Illegal character (0x%02X) received", *p);
408                                                 mp->inp = mp->data;
409                                         } else {
410                                                 *mp->inp++ = ch;
411                                         }
412                                         break;
413
414                                 case 1:
415                                         mp->state = 2;
416                                         if (ch >= '0' && ch <= '9') 
417                                                 c = (ch - '0') << 4;
418                                         else if (ch >= 'A' && ch <= 'F')
419                                                 c = (ch - 'A' + 10) << 4;
420                                         else if (ch >= 'a' && ch <= 'a')
421                                                 c = (ch - 'a' + 10) << 4;
422                                         else {
423                                                 dbg(DMSG, "Illegal hex char (%c) received in state %d", ch, mp->state);
424                                                 mp->inp = mp->data;
425                                                 mp->state = 0;
426                                         }
427                                         break;
428                                         
429                                 case 2:
430                                         if (ch >= '0' && ch <= '9') 
431                                                 *mp->inp++ = c | (ch - '0');
432                                         else if (ch >= 'A' && ch <= 'F')
433                                                 *mp->inp++ = c | (ch - 'A' + 10);
434                                         else if (ch >= 'a' && ch <= 'a')
435                                                 *mp->inp++ = c | (ch - 'a' + 10);
436                                         else {
437                                                 dbg(DMSG, "Illegal hex char (%c) received in state %d", ch, mp->state);
438                                                 mp->inp = mp->data;
439                                         }
440                                         mp->state = 0;
441                                 }
442                         }
443                         break;
444                         
445                 default:
446                         die("invalid sort (%d) in input handler", f->sort);
447                 }
448         }
449         
450         /* output modes */
451 lout:;
452         if (out) {
453                 int l, r;
454                 
455                 if (!f->out) {
456                         mp = f->out = cmsg_next(f->outq);
457                         if (!mp) {
458                                 sp->flags &= ~SEL_OUTPUT;
459                                 return 0;
460                         }
461                         mp->inp = mp->data;
462                 }
463                 l = mp->size - (mp->inp - mp->data);
464                 if (l > 0) {
465                         
466                         dbgdump(DBUF, "<-out", mp->inp, l);
467                         
468                         r = write(f->cnum, mp->inp, l);
469                         if (r < 0) {
470                                 switch (errno) {
471                                 case EINTR:
472                                 case EINPROGRESS:
473                                 case EAGAIN:
474                                         goto lend;
475                                 default:
476                                         if (f->sort == MSG)
477                                                 send_Z = 0;
478                                         ending++;
479                                         return;
480                                 }
481                         } else if (r > 0) {
482                                 mp->inp += r;
483                         }
484                 } else if (l < 0) 
485                         die("got negative length in handler on node");
486                 if (mp->inp - mp->data >= mp->size) {
487                         cmsg_callback(mp, 0);
488                         f->out = 0;
489                 }
490         }
491 lend:;
492         return 0;
493 }
494
495 /*
496  * things to do with initialisation
497  */
498
499 void initargs(int argc, char *argv[])
500 {
501         int i, c, err = 0;
502
503         while ((c = getopt(argc, argv, "h:p:x:")) > 0) {
504                 switch (c) {
505                 case 'h':
506                         node_addr = optarg;
507                         break;
508                 case 'l':
509                         paclen = atoi(optarg);
510                         if (paclen < 80)
511                                 paclen = 80;
512                         if (paclen > MAXPACLEN)
513                                 paclen = MAXPACLEN;
514                         break;
515                 case 'p':
516                         node_port = atoi(optarg);
517                         break;
518                 case 'x':
519                         dbginit("client");
520                         dbgset(atoi(optarg));
521                         break;
522                 default:
523                         ++err;
524                         goto lerr;
525                 }
526         }
527
528 lerr:
529         if (err) {
530                 die("usage: client [-x n|-h<host>|-p<port>|-l<paclen>] <call>|login [local|telnet|ax25]");
531         }
532         
533         if (optind < argc) {
534                 call = strupper(argv[optind]);
535                 ++optind;
536         }
537         if (!call)
538                 die("Must have at least a callsign (for now)");
539
540         if (optind < argc) {
541                 connsort = strlower(argv[optind]);
542                 if (eq(connsort, "telnet") || eq(connsort, "local")) {
543                         nl = '\n';
544                         echo = 1;
545                 } else if (eq(connsort, "ax25")) {
546                         nl = '\r';
547                         echo = 0;
548                 } else {
549                         die("2nd argument must be \"telnet\" or \"ax25\" or \"local\"");
550                 }
551         } else {
552                 connsort = "local";
553                 nl = '\n';
554                 echo = 1;
555         }
556
557         /* this is kludgy, but hey so is the rest of this! */
558         if (!eq(connsort, "ax25") && paclen == DEFPACLEN) {
559                 paclen = MAXPACLEN;
560         }
561 }
562
563 void connect_to_node()
564 {
565         struct hostent *hp, *gethostbyname();
566         struct sockaddr_in server;
567         int nodef;
568         sel_t *sp;
569                                 
570         if ((hp = gethostbyname(node_addr)) == 0) 
571                 die("Unknown host tcp host %s for printer", node_addr);
572
573         memset(&server, 0, sizeof server);
574         server.sin_family = AF_INET;
575         memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
576         server.sin_port = htons(node_port);
577                                                 
578         nodef = socket(AF_INET, SOCK_STREAM, 0);
579         if (nodef < 0) 
580                 die("Can't open socket to %s port %d (%d)", node_addr, node_port, errno);
581
582         if (connect(nodef, (struct sockaddr *) &server, sizeof server) < 0) {
583                 die("Error on connect to %s port %d (%d)", node_addr, node_port, errno);
584         }
585         node = fcb_new(nodef, MSG);
586         node->sp = sel_open(nodef, node, "Msg System", fcb_handler, MSG, SEL_INPUT);
587         
588 }
589
590 /*
591  * things to do with going away
592  */
593
594 void term_timeout(int i)
595 {
596         /* none of this is going to be reused so don't bother cleaning up properly */
597         if (in && in->t_set)
598                 tcsetattr(0, TCSANOW, &in->t);
599         if (node) {
600                 close(node->cnum);
601         }
602         exit(i);
603 }
604
605 void terminate(int i)
606 {
607         if (node && send_Z && call) {
608                 send_msg(node, 'Z', "", 0);
609         }
610         
611         signal(SIGALRM, term_timeout);
612         alarm(10);
613         
614         while ((in && !is_chain_empty(in->outq)) ||
615                    (node && !is_chain_empty(node->outq))) {
616                 sel_run();
617         }
618         if (in && in->t_set)
619                 tcsetattr(0, TCSADRAIN, &in->t);
620         if (node) 
621                 close(node->cnum);
622         exit(i);
623 }
624
625 void login_timeout(int i)
626 {
627         write(0, "Timed Out", 10);
628         write(0, &nl, 1);
629         sel_run();                                      /* force a coordination */
630         if (in && in->t_set)
631                 tcsetattr(0, TCSANOW, &in->t);
632         exit(i);
633 }
634
635 /*
636  * things to do with ongoing processing of inputs
637  */
638
639 void process_stdin()
640 {
641         cmsg_t *mp = cmsg_next(in->inq);
642         if (mp) {
643                 dbg(DMSG, "MSG size: %d", mp->size);
644         
645                 if (mp->size > 0 && mp->inp > mp->data) {
646                         send_msg(node, 'I', mp->data, mp->size);
647                 }
648                 cmsg_callback(mp, 0);
649         }
650 }
651
652 void process_node()
653 {
654         cmsg_t *mp = cmsg_next(node->inq);
655         if (mp) {
656                 dbg(DMSG, "MSG size: %d", mp->size);
657         
658                 if (mp->size > 0 && mp->inp > mp->data) {
659                         char *p = strchr(mp->data, '|');
660                         if (p)
661                                 p++;
662                         switch (mp->data[0]) {
663                         case 'Z':
664                                 send_Z = 0;
665                                 ending++;
666                                 return;
667                         case 'E':
668                                 if (isdigit(*p))
669                                         in->echo = *p - '0';
670                                 break;
671                         case 'B':
672                                 if (isdigit(*p))
673                                         in->buffer_it = *p - '0';
674                                 break;
675                         case 'D':
676                                 if (p) {
677                                         int l = mp->inp - (unsigned char *) p;
678                                         send_text(in, p, l);
679                                 }
680                                 break;
681                         default:
682                                 break;
683                         }
684                 }
685                 cmsg_callback(mp, 0);
686         } else {
687                 flush_text(in);
688         }
689 }
690
691 /*
692  * the program itself....
693  */
694
695 main(int argc, char *argv[])
696 {
697         /* set up environment */
698         {
699                 char *p = getenv("DXSPIDER_ROOT");
700                 if (p)
701                         root = p;
702                 p = getenv("DXSPIDER_HOST");
703                 if (p)
704                         node_addr = p;
705                 p = getenv("DXSPIDER_PORT");
706                 if (p)
707                         node_port = atoi(p);
708                 p = getenv("DXSPIDER_PACLEN");
709                 if (p) {
710                         paclen = atoi(p);
711                         if (paclen < 80)
712                                 paclen = 80;
713                         if (paclen > MAXPACLEN)
714                                 paclen = MAXPACLEN;
715                 }
716         }
717         
718         /* get program arguments, initialise stuff */
719         initargs(argc, argv);
720         sel_init(10, 0, 10000);
721
722         /* trap signals */
723         signal(SIGHUP, SIG_IGN);
724         signal(SIGINT, terminate);
725         signal(SIGQUIT, terminate);
726         signal(SIGTERM, terminate);
727 #ifdef SIGPWR
728         signal(SIGPWR, terminate);
729 #endif
730
731         /* compile regexes for iscallsign */
732         {
733                 myregex_t *rp;
734                 for (rp = iscallreg; rp->in; ++rp) {
735                         regex_t reg;
736                         int r = regcomp(&reg, rp->in, REG_EXTENDED|REG_ICASE|REG_NOSUB);
737                         if (r)
738                                 die("regcomp returned %d for '%s'", r, rp->in);
739                         rp->regex = malloc(sizeof(regex_t));
740                         if (!rp->regex)
741                                 die("out of room - compiling regexes");
742                         *rp->regex = reg;
743                 }
744         }
745         
746         /* is this a login? */
747         if (eq(call, "LOGIN")) {
748                 char buf[MAXPACLEN+1];
749                 int r;
750                 int f = xopen("data", "issue", 0);
751                 if (f > 0) {
752                         while ((r = read(f, buf, paclen)) > 0) {
753                                 if (nl != '\n') {
754                                         char *p;
755                                         for (p = buf; p < &buf[r]; ++p) {
756                                                 if (*p == '\n')
757                                                         *p = nl;
758                                         }
759                                 }
760                                 write(0, buf, r);
761                         }
762                         close(f);
763                 }
764                 signal(SIGALRM, login_timeout);
765                 alarm(timeout);
766                 write(0, "login: ", 7);
767                 r = read(0, buf, 20);
768                 if (r <= 0)
769                         die("No login or error (%d)", errno);
770                 signal(SIGALRM, SIG_IGN);
771                 alarm(0);
772                 while (r > 0) {
773                         if (buf[r-1] == ' ' || buf[r-1] == '\r' || buf[r-1] == '\n')
774                                 --r;
775                         else
776                                 break;
777                 }
778                 buf[r] = 0;
779                 call = strupper(buf);
780         }
781
782         /* check the callsign */
783         if (!iscallsign(call)) {
784                 die("Sorry, %s isn't a valid callsign", call);
785         }
786         
787         /* connect up stdin */
788         in = fcb_new(0, TEXT);
789         in->sp = sel_open(0, in, "STDIN", fcb_handler, TEXT, SEL_INPUT);
790         if (tcgetattr(0, &in->t) < 0) {
791                 echo = 0;
792                 in->t_set = 0;
793         } else {
794                 struct termios t = in->t;
795                 t.c_lflag &= ~(ECHO|ECHONL|ICANON);
796                 if (tcsetattr(0, TCSANOW, &t) < 0) 
797                         die("tcsetattr (%d)", errno);
798                 in->echo = echo;
799                 in->t_set = 1;
800         }
801         in->buffer_it = 1;
802
803         /* connect up node */
804         connect_to_node();
805
806         /* tell the cluster who I am */
807         send_msg(node, 'A', connsort, strlen(connsort));
808         
809         /* main processing loop */
810         while (!ending) {
811                 sel_run();
812                 if (!ending) {
813                         process_stdin();
814                         process_node();
815                 }
816         }
817         terminate(0);
818 }
819
820
821
822
823
824