remove trailing spaces properly!
[spider.git] / src / client.c
index c6be0cb80ba7afff42cb16ad2d6dd096953cb761..fb7e5c137ccd12711061bf054882eb8f193a4e05 100644 (file)
@@ -30,6 +30,7 @@
 #include <signal.h>
 #include <string.h>
 #include <termios.h>
+#include <regex.h>
 
 #include "sel.h"
 #include "cmsg.h"
@@ -43,7 +44,9 @@
 #define MAXPATHLEN 256
 #endif
 
+#define DEFPACLEN 128
 #define MAXPACLEN 236
+
 #define DBUF 1
 #define DMSG 2
 
@@ -63,6 +66,13 @@ typedef struct
        char buffer_it;                         /* buffer outgoing packets for paclen */
 } fcb_t;
 
+typedef struct 
+{
+       char *in;
+       regex_t *regex;
+} myregex_t;
+
+
 char *node_addr = "localhost"; /* the node tcp address, can be overridden by DXSPIDER_HOST */
 int node_port = 27754;                 /* the tcp port of the node at the above address can be overidden by DXSPIDER_PORT*/
 char *call;                                            /* the caller's callsign */
@@ -76,9 +86,33 @@ char echo = 1;                                       /* echo characters on stdout from stdin */
 char int_tabs = 0;                             /* interpret tabs -> spaces */
 char *root = "/spider";         /* root of data tree, can be overridden by DXSPIDER_ROOT  */
 int timeout = 60;                              /* default timeout for logins and things */
-int paclen = 128;                              /* default buffer size for outgoing packets */
+int paclen = DEFPACLEN;                        /* default buffer size for outgoing packets */
 int tabsize = 8;                               /* default tabsize for text messages */
 
+myregex_t iscallreg[] = {              /* regexes to determine whether this is a reasonable callsign */
+       {
+               "^[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0
+       },
+       {
+               "^[0-9]+[A-Z]+[0-9]+[A-Z]+[1-9]?$", 0
+       },
+       {
+               "^[A-Z]+[0-9]+[A-Z]+[1-9]?-[1-9]$", 0
+       },
+       {
+               "^[0-9]+[A-Z]+[0-9]+[A-Z]+[1-9]?-[1-9]$", 0
+       },
+       {
+               "^[A-Z]+[0-9]+[A-Z]+[1-9]?-1[0-5]$", 0
+       },
+       {
+               "^[0-9]+[A-Z]+[0-9]+[A-Z]+[1-9]?-1[0-5]$", 0
+       },
+       {
+               0, 0
+       }
+};
+
 void terminate(int);
 
 /*
@@ -133,33 +167,12 @@ int xopen(char *dir, char *name, int mode)
 
 int iscallsign(char *s)
 {
-       char *p, ch, state = 0;
-       
-       ch = *p;
-       while (ch) {
-               switch (state) {
-               case 0:                                 /* initial numerics */
-                       if (isalpha(ch)) {
-                               if (p == s) 
-                                       state = 10;
-                               else
-                                       state = 1;
-                       } if (isdigit(ch)) {
-                               ;
-                       } else 
-                               goto lend;
-                       break;
-               case 1:                                 /* letter(s) */
-               case 10:                                /* had an initial character */
-                       if (isdigit(ch))
-                               state = 11;
-                       
-               }
-               ch = ++*p;
+       myregex_t *rp;
+       for (rp = iscallreg; rp->in; ++rp) {
+               if (regexec(rp->regex, s, 0, 0, 0) == 0)
+                       return 1;
        }
-
-lend:
-       return 1;
+       return 0;
 }
 
 /*
@@ -199,6 +212,10 @@ void send_text(fcb_t *f, char *s, int l)
                f->obuf = mp = cmsg_new(paclen+1, f->sort, f);
        }
 
+       /* ignore trailing spaces  */
+       while (l > 0 &&isspace(s[l-1]))
+               --l;
+
        for (p = s; p < s+l; ) {
                if (mp->inp >= mp->data + paclen) {
                        flush_text(f);
@@ -489,6 +506,11 @@ lerr:
                nl = '\n';
                echo = 1;
        }
+
+       /* this is kludgy, but hey so is the rest of this! */
+       if (!eq(connsort, "ax25") && paclen == DEFPACLEN) {
+               paclen = MAXPACLEN;
+       }
 }
 
 void connect_to_node()
@@ -535,7 +557,7 @@ void term_timeout(int i)
 
 void terminate(int i)
 {
-       if (send_Z && call) {
+       if (node && send_Z && call) {
                send_msg(node, 'Z', "", 0);
        }
        
@@ -659,6 +681,21 @@ main(int argc, char *argv[])
        signal(SIGPWR, terminate);
 #endif
 
+       /* compile regexes for iscallsign */
+       {
+               myregex_t *rp;
+               for (rp = iscallreg; rp->in; ++rp) {
+                       regex_t reg;
+                       int r = regcomp(&reg, rp->in, REG_EXTENDED|REG_ICASE|REG_NOSUB);
+                       if (r)
+                               die("regcomp returned %d for '%s'", r, rp->in);
+                       rp->regex = malloc(sizeof(regex_t));
+                       if (!rp->regex)
+                               die("out of room - compiling regexes");
+                       *rp->regex = reg;
+               }
+       }
+       
        /* is this a login? */
        if (eq(call, "LOGIN")) {
                char buf[MAXPACLEN+1];
@@ -693,9 +730,11 @@ main(int argc, char *argv[])
                }
                buf[r] = 0;
                call = strupper(buf);
-               if (!iscallsign(call)) {
-                       die("Sorry, %s isn't a valid callsign", buf);
-               }
+       }
+
+       /* check the callsign */
+       if (!iscallsign(call)) {
+               die("Sorry, %s isn't a valid callsign", call);
        }
        
        /* connect up stdin */