home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 3 / hamradioversion3.0examsandprograms1992.iso / packet / n17jsrc / mailbox.c < prev    next >
C/C++ Source or Header  |  1991-10-15  |  47KB  |  1,886 lines

  1. /* There are only two functions in this mailbox code that depend on the
  2.  * underlying protocol, namely mbx_getname() and dochat(). All the other
  3.  * functions can hopefully be used without modification on other stream
  4.  * oriented protocols than AX.25 or NET/ROM.
  5.  *
  6.  * SM0RGV 890506, most work done previously by W9NK
  7.  *
  8.  *** Changed 900114 by KA9Q to use newline mapping features in stream socket
  9.  *    interface code; everything here uses C eol convention (\n)
  10.  *
  11.  *    Numerous new commands and other changes by SM0RGV, 900120
  12.  */
  13. #include <stdio.h>
  14. #include <time.h>
  15. #include <ctype.h>
  16. #ifdef    UNIX
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #endif
  20. #include "global.h"
  21. #include "config.h"
  22. #include "timer.h"
  23. #include "proc.h"
  24. #include "socket.h"
  25. #include "usock.h"
  26. #include "session.h"
  27. #include "smtp.h"
  28. #include "dirutil.h"
  29. #include "telnet.h"
  30. #include "ftp.h"
  31. #include "ftpserv.h"
  32. #include "commands.h"
  33. #include "netuser.h"
  34. #include "files.h"
  35. #include "bm.h"
  36. #include "mailbox.h"
  37. #include "ax25mail.h"
  38. #include "nr4mail.h"
  39. #include "cmdparse.h"
  40.  
  41. /*
  42. #define MBDEBUG
  43. */
  44.  
  45. struct mbx *Mbox[NUMMBX];
  46. static char *MMotd = NULLCHAR;
  47. static int MAttended = TRUE;    /* default to attended mode */
  48. unsigned Maxlet = BM_NLET;
  49. int Mtrace = 0;
  50.  
  51. char Noperm[] = "Permission denied.\n";
  52. char Nosock[] = "Can't create socket\n";
  53.  
  54. static char Mbbanner[] = "[NET-H$]\nWelcome %s to the %s TCP/IP Mailbox (%s)\n%s%s\n";
  55.  
  56. static char Mbwarning[] = "This mailbox does not permit Third Party mail to be sent.\n";
  57.  
  58. static char Mbmenu[] = "Current msg# %d : ?,A,B,C,D,E,F,G,H,I,J,K,L,N,R,S,T,U,V,W,Z >\n";
  59.  
  60. static char Longmenu1[] = "(?)help    (A)rea     (B)ye      (C)hat     (D)ownload (E)scape   (F)inger\n";
  61. static char Longmenu2[] = "(G)ateway  (H)elp     (I)nfo     (J)heard   (K)ill     (L)ist     (N)etrom\n";
  62. static char Longmenu3[] = "(R)ead     (S)end     (T)elnet   (U)pload   (V)erbose  (W)hat     (Z)ap\n";
  63. static char Loginbanner[] = "\nKA9Q NOS (%s)\n\n";
  64. static char Howtoend[] = "Terminate with /EX or ^Z in first column (^A aborts):\n";
  65. static int doattend __ARGS((int argc,char *argv[],void *p));
  66. static int domaxmsg __ARGS((int argc,char *argv[],void *p));
  67. static int domotd __ARGS((int argc,char *argv[],void *p));
  68. static int dotimeout __ARGS((int argc,char *argv[],void *p));
  69. static int dombtrace __ARGS((int argc,char *argv[],void *p));
  70. static int doarea __ARGS((int argc,char *argv[],void *p));
  71.  
  72. /* mbox subcommand table */
  73. static struct cmds Mbtab[] = {
  74.     "attend",    doattend,    0, 0, NULLCHAR,
  75. #ifdef    AX25
  76.     "kick",        dombkick,    0, 0, NULLCHAR,
  77. #endif
  78.     "maxmsg",    domaxmsg,    0, 0, NULLCHAR,
  79.     "motd",        domotd,        0, 0, NULLCHAR,
  80.     "status",    domboxdisplay,    0, 0, NULLCHAR,
  81. #ifdef    AX25
  82.     "timer",    dombtimer,    0, 0, NULLCHAR,
  83. #endif
  84.      "tiptimeout",    dotimeout,    0, 0, NULLCHAR,
  85.     "trace",    dombtrace,    0, 0, NULLCHAR,
  86.     NULLCHAR,
  87. };
  88. int
  89. dombox(argc,argv,p)
  90. int argc;
  91. char *argv[];
  92. void *p;
  93. {
  94.     if(argc == 1)
  95.         return domboxdisplay(argc,argv,p);
  96.     return subcmd(Mbtab,argc,argv,p);
  97. }
  98.  
  99. /* if unattended mode is set, ax25, telnet and maybe other sessions will
  100.  * be restricted.
  101.  */
  102. static int
  103. doattend(argc,argv,p)
  104. int argc;
  105. char *argv[];
  106. void *p;
  107. {
  108.     return setbool(&MAttended,"Mailbox Attended flag",argc,argv);
  109. }
  110.  
  111. static int
  112. domaxmsg(argc,argv,p)
  113. int argc;
  114. char *argv[];
  115. void *p;
  116. {
  117.     return setuns(&Maxlet,"Maximum messages per area",argc,argv);
  118. }
  119.  
  120. static int
  121. domotd(argc,argv,p)
  122. int argc;
  123. char *argv[];
  124. void *p;
  125. {
  126.     if(argc > 2) {
  127.         tprintf("Usage: mbox motd \"<your message>\"\n");
  128.         return 0;
  129.     }
  130.  
  131.     if(argc < 2) {
  132.         if(MMotd != NULLCHAR)
  133.             tputs(MMotd);
  134.     }
  135.     else {
  136.         if(MMotd != NULLCHAR){
  137.             free(MMotd);
  138.             MMotd = NULLCHAR;    /* reset the pointer */
  139.         }
  140.         if(!strlen(argv[1]))
  141.             return 0;        /* clearing the buffer */
  142.         MMotd = mallocw(strlen(argv[1])+5);/* allow for the EOL char */
  143.         strcpy(MMotd, argv[1]);
  144.         strcat(MMotd, "\n");        /* add the EOL char */
  145.     }
  146.     return 0;
  147. }
  148.  
  149. int
  150. domboxdisplay(argc,argv,p)
  151. int argc;
  152. char *argv[];
  153. void *p;
  154. {
  155.     int i, j, len;
  156.     struct mbx *m;
  157.     char fsocket[MAXSOCKSIZE];
  158.     static char *states[] = {"LOGIN","CMD","SUBJ","DATA","REVFWD",
  159.                 "TRYING","FORWARD"};
  160.  
  161.     tprintf("User       State    S#  Where\n");
  162.  
  163.     for (i = 0; i < NUMMBX; i++){
  164.         if((m = Mbox[i]) != NULLMBX){
  165.             len = MAXSOCKSIZE;
  166.             j = getpeername(m->user,fsocket,&len);
  167.             if(tprintf("%-11s%-9s%-4u%s\n",m->name,
  168.                    states[m->state],m->user,
  169.                    j != -1 ? psocket(fsocket): "") == EOF)
  170.                 break;
  171.         }
  172.     }
  173.     return 0;
  174. }
  175.  
  176. static int
  177. dotimeout(argc,argv,p)
  178. int argc;
  179. char *argv[];
  180. void *p;
  181. {
  182.     return setuns(&Tiptimeout,"Tip connection timeout",argc,argv);
  183. }
  184.  
  185. static int
  186. dombtrace(argc,argv,p)
  187. int argc;
  188. char *argv[];
  189. void *p;
  190. {
  191.     return setbool(&Mtrace,"Mailbox trace flag",argc,argv);
  192. }
  193.  
  194. /**********************************************************************/
  195.  
  196. void
  197. listusers(s)
  198. int s;
  199. {
  200.     int outsave;
  201.  
  202.     usprintf(s,"\nCurrent remote users:\n");
  203.     outsave = Curproc->output;
  204.     Curproc->output = s;
  205.     domboxdisplay(0,NULLCHARP,NULL);
  206.     Curproc->output = outsave;
  207. }
  208. struct mbx *
  209. newmbx()
  210. {
  211.     int i;
  212.     struct mbx *m;
  213.  
  214.     for(i = 0; i < NUMMBX; i++){
  215.         if(Mbox[i] == NULLMBX){
  216.             m = Mbox[i] = (struct mbx *)callocw(1,sizeof(struct mbx));
  217.             m->mbnum = i;
  218.             return m;
  219.         }
  220.     }
  221.     /* If we get here, there are no free mailbox sessions */
  222.     return NULLMBX;
  223. }
  224.  
  225. static int
  226. mbx_getname(m)
  227. struct mbx *m;
  228. {
  229. #ifdef    AX25
  230.     char *cp;
  231. #endif
  232.     union sp sp;
  233.     char tmp[MAXSOCKSIZE];
  234.     char buf[MBXLINE];
  235.     int len = MAXSOCKSIZE;
  236.     int anony = 0;
  237.     int oldmode;
  238.  
  239.     sp.p = tmp;
  240.     sp.sa->sa_family = AF_LOCAL;    /* default to AF_LOCAL */
  241.     getpeername(m->user,tmp,&len);
  242.     m->path = mallocw(MBXLINE);
  243.     /* This is one of the two parts of the mbox code that depends on the
  244.      * underlying protocol. We have to figure out the name of the
  245.      * calling station. This is only practical when AX.25 or NET/ROM is
  246.      * used. Telnet users have to identify themselves by a login procedure.
  247.      */
  248.     switch(sp.sa->sa_family){
  249. #ifdef    AX25
  250.     case AF_NETROM:
  251.     case AF_AX25:
  252.         /* NETROM and AX25 socket address structures are "compatible" */
  253.         pax25(m->name,sp.ax->ax25_addr);
  254.         cp = strchr(m->name,'-');
  255.         if(cp != NULLCHAR)            /* get rid of SSID */
  256.             *cp = '\0';
  257.         /* SMTP wants the name to be in lower case */
  258.         cp = m->name;
  259.         while(*cp){
  260.             if(isupper(*cp))
  261.                 *cp = tolower(*cp);
  262.             ++cp;
  263.         }
  264.         anony = 1;
  265.         /* Try to find the privileges of this user from the userfile */
  266.         if((m->privs = userlogin(m->name,buf,&m->path,MBXLINE,&anony)) == -1)
  267.             if((m->privs = userlogin("bbs",buf,&m->path,MBXLINE,&anony)) == -1)
  268.                 if((m->privs = userlogin("anonymous",buf,&m->path,MBXLINE,
  269.                      &anony)) == -1){
  270.                         m->privs = 0;
  271.                         free(m->path);
  272.                         m->path = NULLCHAR;
  273.                 }
  274.         if(m->privs & EXCLUDED_CMD)
  275.             return -1;
  276.         return 0;
  277. #endif
  278.     case AF_LOCAL:
  279.     case AF_INET:
  280.         m->state = MBX_LOGIN;
  281.         tprintf(Loginbanner,Hostname);
  282.         for(;;){
  283.             tputs("login: ");
  284.             usflush(m->user);
  285.             if(mbxrecvline(m->user,m->name,sizeof(m->name),-1) == EOF)
  286.                 return -1;
  287.             if(*m->name == '\0')
  288.                 continue;
  289.             tprintf("Password: %c%c%c",IAC,WILL,TN_ECHO);
  290.             usflush(m->user);
  291.             oldmode = sockmode(m->user,SOCK_BINARY);
  292.             if(mbxrecvline(m->user,buf,MBXLINE,-1) == EOF)
  293.                 return -1;
  294.             tprintf("%c%c%c",IAC,WONT,TN_ECHO);
  295.             sockmode(m->user,oldmode);
  296.             tputc('\n');
  297.             usflush(m->user);
  298.             /* This is needed if the password was send before the
  299.              * telnet no-echo options were receied. We neeed to
  300.              * flush the eold sequence from the input buffers, sigh
  301.              */
  302.             if(socklen(m->user,0))/* discard any remaining input */
  303.                 recv_mbuf(m->user,NULL,0,NULLCHAR,0);
  304.             if((m->privs = userlogin(m->name,buf,&m->path,MBXLINE,&anony))
  305.              != -1){
  306.                 if(anony)
  307.                     log(m->user,"MBOX login: %s Password: %s",m->name,buf);
  308.                 else
  309.                     log(m->user,"MBOX login: %s",m->name);
  310.                 if(m->privs & EXCLUDED_CMD)
  311.                     return -1;
  312.                 return 0;
  313.             }
  314.             tprintf("Login incorrect\n");
  315.             *m->name = '\0';    /* wipe any garbage */
  316.         }
  317.     }
  318.     return 0;
  319. }
  320.  
  321. /* Incoming mailbox session */
  322. void
  323. mbx_incom(s,t,p)
  324. int s;
  325. void *t;
  326. void *p;
  327. {
  328.     struct mbx *m;
  329.     struct usock *up;
  330.     char *buf[3];
  331.     int rval;
  332.  
  333.     sockmode(s,SOCK_ASCII);
  334.     sockowner(s,Curproc);    /* We own it now */
  335.     /* Secede from the parent's sockets, and use the network socket that
  336.      * was passed to us for both input and output. The reference
  337.      * count on this socket will still be 1; this allows the domboxbye()
  338.      * command to work by closing that socket with a single call.
  339.      * If we return, the socket will be closed automatically.
  340.      */
  341.     close_s(Curproc->output);
  342.     close_s(Curproc->input);
  343.     Curproc->output = Curproc->input = s;
  344.  
  345.     /* We'll do our own flushing right before we read input */
  346.     setflush(s,-1);
  347.  
  348.     log(s,"open MBOX");
  349.     if((m = newmbx()) == NULLMBX){
  350.         tprintf("Too many mailbox sessions\n");
  351.         return;
  352.     }
  353.     m->user = s;
  354.     m->escape = 24;        /* default escape character is Ctrl-X */
  355.     m->type = (int) t;
  356.     /* get the name of the remote station */
  357.  
  358.     if(mbx_getname(m) == -1) {
  359.         exitbbs(m);
  360.         return;
  361.     }
  362.  
  363.     m->state = MBX_CMD;    /* start in command state */
  364.  
  365.     /* Now say hi */
  366.     tprintf(Mbbanner,m->name,Hostname,Version,ThirdParty ? "" : Mbwarning,(MMotd != NULLCHAR) ? MMotd : "");
  367.     /* Enable our local message area */
  368.     buf[1] = m->name;
  369.     doarea(2,buf,m);
  370.     tprintf(Mbmenu,m->current);
  371.     while(mbxrecvline(s,m->line,MBXLINE,-1) != EOF){
  372.         if((rval = mbx_parse(m)) == -2)
  373.             break;
  374.         if(rval == 1)
  375.             tprintf("Bad syntax.\n");
  376.         if(!(m->sid & MBX_SID) && isnewprivmail(m) > 0L)
  377.             tprintf("You have new mail.\n");
  378.         scanmail(m);
  379.         tprintf((m->sid & MBX_SID) ? ">\n" : Mbmenu, m->current);
  380.         m->state = MBX_CMD;
  381.     }
  382.     exitbbs(m);
  383.     /* nasty hack! we may have screwed up reference count */
  384.     /* by invoking newproc("smtp_send",....); Fudge it!   */
  385.     if((up = itop(Curproc->output)) != NULLUSOCK)
  386.         up->refcnt = 1;
  387.     close_s(Curproc->output);
  388. }
  389.  
  390. void
  391. exitbbs(m)
  392. struct mbx *m;
  393. {
  394.     closenotes(m);
  395.     free(m->to);
  396.     free(m->tofrom);
  397.     free(m->origto);
  398.     free(m->tomsgid);
  399.     free(m->path);
  400.     if(m->stdinbuf != NULLCHAR)
  401.         free(m->stdinbuf);
  402.     if(m->stdoutbuf != NULLCHAR)
  403.         free(m->stdoutbuf);
  404.     free((char *)m->mbox);
  405.     Mbox[m->mbnum] = NULLMBX;
  406.     free((char *)m);
  407. }
  408.  
  409. /**********************************************************************/
  410.  
  411. static int dochat __ARGS((int argc,char *argv[],void *p));
  412. static int dodownload __ARGS((int argc,char *argv[],void *p));
  413. static int dombupload __ARGS((int argc,char *argv[],void *p));
  414. static int dowhat __ARGS((int argc,char *argv[],void *p));
  415. static int dozap __ARGS((int argc,char *argv[],void *p));
  416. static int dosend __ARGS((int argc,char *argv[],void *p));
  417. static int dosid __ARGS((int argc,char *argv[],void *p));
  418. static int dosysop __ARGS((int argc,char *argv[],void *p));
  419. static int dostars __ARGS((int argc,char *argv[],void *p));
  420. static int dombhelp __ARGS((int argc,char *argv[],void *p));
  421. static int dombtelnet __ARGS((int argc,char *argv[],void *p));
  422. static int dombfinger __ARGS((int argc,char *argv[],void *p));
  423. static void gw_alarm __ARGS((void *p));
  424. static void gw_input __ARGS((int s,void *notused,void *p));
  425. static void gw_superv __ARGS((int null,void *proc,void *p));
  426. static int mbx_to __ARGS((int argc,char *argv[],void *p));
  427. static int mbx_data __ARGS((struct mbx *m,struct list *cclist,char *extra));
  428. static int msgidcheck __ARGS((char *string));
  429. static int uuencode __ARGS((FILE *infile,int s,char *infilename));
  430. static int thirdparty __ARGS((struct mbx *m));
  431.  
  432. static struct cmds Mbcmds[] = {
  433.     "",        doreadnext,    0, 0, NULLCHAR,
  434.     "area",        doarea,        0, 0, NULLCHAR,
  435.     "send",        dosend,        0, 0, NULLCHAR,
  436.     "read",        doreadmsg,    0, 2, "R numbers",
  437.     "verbose",    doreadmsg,    0, 2, "V numbers",
  438. #ifdef    AX25
  439.     "jheard",    doaxheard,    0, 0, NULLCHAR,
  440. #endif
  441.     "kill",        dodelmsg,    0, 2, "K numbers",
  442.     "list",        dolistnotes,    0, 0, NULLCHAR,
  443.     "escape",    dombescape,    0, 0, NULLCHAR,
  444.     "download",    dodownload,    0, 2, "D[U] filename",
  445.     "upload",    dombupload,    0, 2, "U filename",
  446.     "what",        dowhat,        0, 0, NULLCHAR,
  447.     "zap",        dozap,        0, 2, "Z filename",
  448. #ifdef AX25
  449.     "gateway",    dogateway,    0, 3, "G interface callsigns",
  450. #endif
  451.     "telnet",    dombtelnet,    0, 2, "T hostname",
  452.     "finger",    dombfinger,    0, 0, NULLCHAR,
  453. #ifdef    NETROM
  454.     "netrom",    dombnetrom,    0, 0, NULLCHAR,
  455. #endif
  456.     "chat",        dochat,        0, 0, NULLCHAR,
  457.     "bye",        domboxbye,    0, 0, NULLCHAR,
  458.     "help",        dombhelp,    0, 0, NULLCHAR,
  459.     "info",        dombhelp,    0, 0, NULLCHAR,
  460.     "?",        dombhelp,    0, 0, NULLCHAR,
  461.     "[",        dosid,        0, 0, NULLCHAR,
  462. #ifdef    AX25
  463.     "f>",        dorevfwd,    0, 0, NULLCHAR,
  464. #endif
  465.     "@",        dosysop,    0, 0, NULLCHAR,
  466.     "***",        dostars,    0, 0, NULLCHAR,
  467.     NULLCHAR,    NULLFP,        0, 0, "Huh?",
  468. };
  469.  
  470. /* "twocmds" defines the MBL/RLI two-letter commands, eg. "SB", "SP" and so on.
  471.  * They have to be treated specially since cmdparse() wants a space between
  472.  * the actual command and its arguments.
  473.  * "SP FOO" is converted to "s  foo" and the second command letter is saved
  474.  * in m->stype. Longer commands like "SEND" are unaffected, except for
  475.  * commands starting with "[", i.e. the SID, since we don't know what it will
  476.  * look like.
  477.  */
  478. static char twocmds[] = "slrd[";    /* S,L,R,D are two-letter commands */
  479. int
  480. mbx_parse(m)
  481. struct mbx *m;
  482. {
  483.     char *cp;
  484.     int i;
  485.     char *newargv[2];
  486.     /* Translate entire buffer to lower case */
  487.     for (cp = m->line; *cp != '\0'; ++cp)
  488.         if(isupper(*cp))
  489.             *cp = tolower(*cp);
  490.     /* Skip any spaces at the begining */
  491.     for(cp = m->line;isspace(*cp);++cp)
  492.         ;
  493.     m->stype = ' ';
  494.     if(*cp != '\0' && *(cp+1) != '\0')
  495.     for(i=0; i<strlen(twocmds); ++i){
  496.         if(*cp == twocmds[i] && (isspace(*(cp+2)) || *(cp+2) == '\0'
  497.          || *cp == '[')){
  498.             if(islower(*(++cp)))
  499.                 m->stype = toupper(*cp); /* Save the second character */
  500.             else
  501.                 m->stype = *cp;
  502.             *cp = ' ';
  503.             break;
  504.         }
  505.     }
  506.     /* See if the input line consists solely of digits */
  507.     cp = m->line;
  508.     for(cp = m->line;isspace(*cp);++cp)
  509.         ;
  510.     newargv[1] = cp;
  511.     for(;*cp != '\0' && isdigit(*cp);++cp)
  512.         ;
  513.     if(*cp == '\0' && strlen(newargv[1]) > 0) {
  514.         newargv[0] = "read";
  515.         return doreadmsg(2,newargv,(void *)m);
  516.     }
  517.     else
  518.         return cmdparse(Mbcmds,m->line,(void *)m);
  519. }
  520.  
  521. /* This works like recvline(), but telnet options are answered and the
  522.  * terminating newline character is not put into the buffer. If the
  523.  * incoming character equals the value of escape, any queued input is
  524.  * flushed and -2 returned.
  525.  */
  526. int
  527. mbxrecvline(s,buf,len,escape)
  528. int s;
  529. char *buf;
  530. int len;
  531. int escape;
  532. {
  533.     int c, cnt = 0, opt;
  534.     if(buf == NULLCHAR)
  535.         return 0;
  536.     usflush(Curproc->output);
  537.     while((c = recvchar(s)) != EOF){
  538.         if(c == IAC){        /* Telnet command escape */
  539.             if((c = recvchar(s)) == EOF)
  540.                 break;
  541.             if(c > 250 && c < 255 && (opt = recvchar(s)) != EOF){
  542. #ifdef    foo
  543.                 switch(c){
  544.                 case WILL:
  545.                     tprintf("%c%c%c",IAC,DONT,opt);
  546.                     break;
  547.                 case WONT:
  548.                     tprintf("%c%c%c",IAC,DONT,opt);
  549.                     break;
  550.                 case DO:
  551.                     tprintf("%c%c%c",IAC,WONT,opt);
  552.                     break;
  553.                 case DONT:
  554.                     tprintf("%c%c%c",IAC,WONT,opt);
  555.                 }
  556. #endif
  557. /* to be fixed             usflush(Curproc->output);*/
  558.                 continue;
  559.             }
  560.             if(c != IAC && (c = recvchar(s)) == EOF)
  561.                 break;
  562.         }
  563.         /* ordinary character */
  564.         if(c == '\r' || c == '\n')
  565.             break;
  566.         if(uchar(c) == escape){
  567.             if(socklen(s,0)) /* discard any remaining input */
  568.                 recv_mbuf(s,NULL,0,NULLCHAR,0);
  569.             cnt = -2;
  570.             break;
  571.         }
  572.         *buf++ = c;
  573.         ++cnt;
  574.         if(cnt == len - 1)
  575.             break;
  576.     }
  577.     if(c == EOF && cnt == 0)
  578.         return -1;
  579.     *buf = '\0';
  580.     return cnt;
  581. }
  582.  
  583. int
  584. domboxbye(argc,argv,p)
  585. int argc;
  586. char *argv[];
  587. void *p;
  588. {
  589.     struct mbx *m;
  590.  
  591.     m = (struct mbx *)p;
  592.     /* Now say goodbye */
  593.     tprintf("Thank you %s, for calling the %s NOS Tcp/Ip Mailbox.\n",m->name,
  594.         Hostname);
  595.     if(m->type == TIP)
  596.         tprintf("Please hang up now.\n");
  597.  
  598.     return -2;    /* signal that exitbbs() should be called */
  599. }
  600.  
  601. static int
  602. dombhelp(argc,argv,p)
  603. int argc;
  604. char *argv[];
  605. void *p;
  606. {
  607.     char buf[255];
  608.     int i;
  609.     FILE *fp;
  610.     if(*argv[0] == '?') {
  611.         tputs(Longmenu1);
  612.         tputs(Longmenu2);
  613.         tputs(Longmenu3);
  614.         return 0;
  615.     }
  616.     buf[0] = '\0';
  617.     if(argc > 1)
  618.         for(i=0; Mbcmds[i].name != NULLCHAR; ++i)
  619.             if(!strncmp(Mbcmds[i].name,argv[1],strlen(argv[1]))) {
  620.                 sprintf(buf,"%s/%s.hlp",Helpdir,Mbcmds[i].name);
  621.                 break;
  622.             }
  623.     if(buf[0] == '\0')
  624.         if(*argv[0] == 'i')            /* INFO command */
  625.             sprintf(buf,"%s/info.hlp",Helpdir);
  626.         else
  627.             sprintf(buf,"%s/help.hlp",Helpdir);
  628.     if((fp = fopen(buf,READ_TEXT)) != NULLFILE) {
  629.         sendfile(fp,Curproc->output,ASCII_TYPE,0);
  630.         fclose(fp);
  631.     }
  632.     else
  633.         tprintf("No help available. (%s not found)\n",buf);
  634.     return 0;
  635. }
  636.  
  637. static int
  638. dochat(argc,argv,p)
  639. int argc;
  640. char *argv[];
  641. void *p;
  642. {
  643.     char buf[8], *newargv[3];
  644.  
  645.     if(MAttended){
  646.         newargv[0] = "telnet";
  647.         newargv[1] = Hostname;
  648.         sprintf(buf,"%d",IPPORT_TTYLINK);
  649.         newargv[2] = buf;
  650.         return dombtelnet(3,newargv,p);
  651.     }
  652.     else {
  653.         tprintf("Sorry - the system is unattended.\007\n");
  654.     }
  655.     /* It returns only after a disconnect or refusal */
  656.     return 0;
  657. }
  658.  
  659. static int
  660. dosend(argc,argv,p)
  661. int argc;
  662. char *argv[];
  663. void *p;
  664. {
  665.     int cccnt = 0, fail = 0;
  666.     char *host, *cp, fullfrom[MBXLINE], sigwork[LINELEN], *rhdr = NULLCHAR;
  667.     struct list *ap, *cclist = NULLLIST;
  668.     struct mbx *m;
  669.     FILE *fp;
  670.  
  671.     m = (struct mbx *)p;
  672.     if((m->stype != 'R' || (m->sid & MBX_SID)) && mbx_to(argc,argv,m)
  673.        == -1){
  674.         if(m->sid & MBX_SID)
  675.             tprintf("NO - syntax error\n");
  676.         else {
  677.             tprintf("S command syntax error - format is:\n");
  678.             tprintf("  S[F] name [@ host] [< from_addr] [$bulletin_id]\n");
  679.             tprintf("  SR [number]\n");
  680.         }
  681.         return 0;
  682.     }
  683.     if(m->stype != 'R' && msgidcheck(m->tomsgid)) {
  684.         if(m->sid & MBX_SID)
  685.             tputs("NO - ");
  686.         tprintf("Already have %s\n",m->tomsgid);
  687.         return 0;
  688.     }
  689.     if(m->stype == 'R' && !(m->sid & MBX_SID) &&
  690.        mbx_reply(argc,argv,m,&cclist,&rhdr) == -1)
  691.         return 0;
  692.     if((cp = rewrite_address(m->to)) != NULLCHAR)
  693.          if(strcmp(m->to,cp) != 0){
  694.           m->origto = m->to;
  695.           m->to = cp;
  696.          }
  697.          else
  698.           free(cp);
  699.  
  700.     if(!ThirdParty && !(m->privs & SYSOP_CMD))
  701.         if(thirdparty(m) == 0){
  702.             tprintf("Third party mail not permitted!\n");
  703.             return 0;
  704.         }
  705.  
  706.     if((m->origto != NULLCHAR || m->stype == 'R') && !(m->sid & MBX_SID))
  707.         tprintf("To: %s\n", m->to);
  708.     if(validate_address(m->to) == 0){
  709.         if(m->sid & MBX_SID)
  710.             tprintf("NO - bad address\n");
  711.         else
  712.             tprintf("Bad user or host name\n");
  713.         free(rhdr);
  714.         del_list(cclist);
  715.         /* We don't free any more buffers here. They are freed upon
  716.          * the next call to mbx_to() or to domboxbye()
  717.          */
  718.         return 0;
  719.     }
  720.     /* Display the Cc: line (during SR command) */
  721.     for(ap = cclist; ap != NULLLIST; ap = ap->next) {
  722.         if(cccnt == 0){
  723.             tprintf("%s",Hdrs[CC]);
  724.             cccnt = 4;
  725.         }
  726.         else {
  727.             tputs(", ");
  728.             cccnt += 2;
  729.         }
  730.         if(cccnt + strlen(ap->val) > 80 - 3) {
  731.             tputs("\n    ");
  732.             cccnt = 4;
  733.         }
  734.         tputs(ap->val);
  735.         cccnt += strlen(ap->val);
  736.     }
  737.     if(cccnt)
  738.         tputc('\n');
  739.     m->state = MBX_SUBJ;
  740.     if(m->stype != 'R' || (m->sid & MBX_SID) != 0) {
  741.         tprintf((m->sid & MBX_SID) ? "OK\n" : "Subject: ");
  742.         if(mbxrecvline(m->user,m->line,MBXLINE,-1) == -1)
  743.             return 0;
  744.     }
  745.     else                /* Replying to a message */
  746.         tprintf("Subject: %s\n",m->line);
  747.     if(mbx_data(m,cclist,rhdr) == -1){
  748.         free(rhdr);
  749.         del_list(cclist);
  750.         tputs("Can't create temp file for mail\n");
  751.         return 0;
  752.     }
  753.     free(rhdr);
  754.     m->state = MBX_DATA;
  755.     if((m->sid & MBX_SID) == 0 && m->stype != 'F')
  756.         tprintf("Enter message.  %s",Howtoend);
  757.  
  758.     if(m->stype != 'F' || (m->sid & MBX_SID) != 0)
  759.         while(mbxrecvline(m->user,m->line,MBXLINE,-1) != -1){
  760.             if(m->line[0] == 0x01){  /* CTRL-A */
  761.                 fclose(m->tfile);
  762.                 tputs("Aborted.\n");
  763.                 del_list(cclist);
  764.                 return 0;
  765.             }
  766.             if(m->line[0] != CTLZ && stricmp(m->line, "/ex"))
  767.                 fprintf(m->tfile,"%s\n",m->line);
  768.             else
  769.                 break;    /* all done */
  770.         }
  771.     else {
  772.         fprintf(m->tfile,"----- Forwarded message -----\n\n");
  773.         msgtofile(m,m->current,m->tfile,0);
  774.         fprintf(m->tfile,"----- End of forwarded message -----\n");
  775.     }
  776.  
  777.     /* Insert customised signature if one is found */
  778.     if(!(m->sid & MBX_SID)) {    /* not a forwarding BBS */
  779.          sprintf(sigwork,"%s/%s.sig",Signature,
  780.              m->tofrom ? m->tofrom : m->name);
  781.          if((fp = fopen(sigwork,READ_TEXT)) != NULLFILE){
  782.           while(fgets(sigwork,LINELEN,fp) != NULLCHAR)
  783.             fputs(sigwork,m->tfile);
  784.           fclose(fp);
  785.          }
  786.     }
  787.  
  788.     if((host = strrchr(m->to,'@')) == NULLCHAR) {
  789.         host = Hostname;    /* use our hostname */
  790.         if(m->origto != NULLCHAR) {
  791.             /* rewrite_address() will be called again by our
  792.              * SMTP server, so revert to the original address.
  793.              */
  794.              free(m->to);
  795.             m->to = m->origto;
  796.             m->origto = NULLCHAR;
  797.         }
  798.     }
  799.     else
  800.         host++;    /* use the host part of address */
  801.  
  802.     /* make up full from name for work file */
  803.     if(m->tofrom != NULLCHAR)
  804.         sprintf(fullfrom,"%s%%%s@%s",m->tofrom, m->name, Hostname);
  805.     else
  806.         sprintf(fullfrom,"%s@%s",m->name,Hostname);
  807.     if(cclist != NULLLIST && stricmp(host,Hostname) != 0) {
  808.         fseek(m->tfile,0L,0);    /* reset to beginning */
  809.         fail = queuejob(m->tfile,Hostname,cclist,fullfrom);
  810.         del_list(cclist);
  811.         cclist = NULLLIST;
  812.     }
  813.     addlist(&cclist,m->to,0);
  814.     fseek(m->tfile,0L,0);
  815.     fail += queuejob(m->tfile,host,cclist,fullfrom);
  816.     del_list(cclist);
  817.     fclose(m->tfile);
  818.     if(fail)
  819.          tputs("Couldn't queue message for delivery\n");
  820.     else
  821.          if(m->tomsgid != NULLCHAR &&
  822.         (fp = fopen(Historyfile,APPEND_TEXT)) != NULLFILE) {
  823.           fprintf(fp,"%s\n",m->tomsgid); /* Save BID in history file */
  824.           fclose(fp);
  825.          }
  826.     smtptick(NULL);        /* wake SMTP to send that mail */
  827.     return 0;
  828. }
  829.  
  830. static int
  831. dosid(argc,argv,p)
  832. int argc;
  833. char *argv[];
  834. void *p;
  835. {
  836.     struct mbx *m;
  837.     char *cp;
  838.  
  839.     m = (struct mbx *)p;
  840.     if(argc == 1)
  841.         return 1;
  842.     if(argv[1][strlen(argv[1]) - 1] != ']') /* must be an SID */
  843.         return 1;
  844.     m->sid = MBX_SID;
  845.     /* Now check to see if this is an RLI board.
  846.      * As usual, Hank does it a bit differently from
  847.      * the rest of the world.
  848.      */
  849.     if(m->stype == 'R' && strncmp(argv[1],"li",2) == 0)/* [RLI] at a minimum */
  850.         m->sid |= MBX_RLI_SID;
  851.     /* Check to see if the BBS supports a kludge called "hierarchical
  852.      * routing designators."
  853.      *
  854.      * No need to check for ']' -- it must be there or this is not
  855.      * a valid mbox id -- it is checked earlier (fix de OH3LKU)
  856.      */
  857.     if((cp = strchr(argv[1],'-')) != NULLCHAR
  858.      && (cp=strchr(cp+1,'h')) != NULLCHAR
  859.      && strchr(cp+1,'$'))
  860.         m->sid |= MBX_HIER_SID;
  861.     return 0;
  862. }
  863.  
  864. int
  865. dombescape(argc,argv,p)
  866. int argc;
  867. char *argv[];
  868. void *p;
  869. {
  870.     struct mbx *m;
  871.  
  872.     m = (struct mbx *)p;
  873.     if(argc < 2){
  874.         tprintf("The escape character is: ");
  875.         if(m->escape < 32)
  876.             tprintf("CTRL-%c\n",m->escape+'A'-1);
  877.         else
  878.             tprintf("'%c'\n",m->escape);
  879.         return 0;
  880.     }
  881.     if(strlen(argv[1]) > 1)
  882.         if(isdigit(*argv[1]))
  883.             m->escape = (char) atoi(argv[1]);
  884.         else
  885.             return 1;
  886.     else
  887.         m->escape = *argv[1];
  888.     return 0;
  889. }
  890.  
  891. static int
  892. dodownload(argc,argv,p)
  893. int argc;
  894. char *argv[];
  895. void *p;
  896. {
  897.     struct mbx *m;
  898.     FILE *fp;
  899.     char *file;
  900.  
  901.     m = (struct mbx *)p;
  902.     file = pathname(m->path,argv[1]);
  903.     if(!permcheck(m->path,m->privs,RETR_CMD,file)){
  904.         tprintf(Noperm);
  905.         return 0;
  906.     }
  907.     if((fp = fopen(file,READ_TEXT)) == NULLFILE)
  908.         tprintf("Can't open \"%s\": %s\n",file,sys_errlist[errno]);
  909.     else
  910.         if(m->stype == 'U'){            /* uuencode ? */
  911.             fclose(fp);
  912.             fp = fopen(file,READ_BINARY);    /* assume non-ascii */
  913.             uuencode(fp,m->user,file);
  914.         } else
  915.             sendfile(fp,m->user,ASCII_TYPE,0);
  916.     free(file);
  917.     fclose(fp);
  918.     return 0;
  919. }
  920.  
  921. static int
  922. dombupload(argc,argv,p)
  923. int argc;
  924. char *argv[];
  925. void *p;
  926. {
  927.     struct mbx *m;
  928.     FILE *fp;
  929.     char *file, buf[LINELEN];
  930.  
  931.     m = (struct mbx *)p;
  932.     file = pathname(m->path,argv[1]);
  933.     if(!permcheck(m->path,m->privs,STOR_CMD,file)){
  934.         tprintf(Noperm);
  935.         return 0;
  936.     }
  937.     if((fp = fopen(file,WRITE_TEXT)) == NULLFILE){
  938.         tprintf("Can't create \"%s\": %s\n",file,sys_errlist[errno]);
  939.         free(file);
  940.         return 0;
  941.     }
  942.     log(m->user,"MBOX upload: %s",file);
  943.     tprintf("Send file,  %s",Howtoend);
  944.     for(;;){
  945.         if(mbxrecvline(m->user,buf,LINELEN,-1) == -1){
  946.             unlink(file);
  947.             break;
  948.         }
  949.         if(buf[0] == 0x01){  /* CTRL-A */
  950.             unlink(file);
  951.             tprintf("Aborted.\n");
  952.             break;
  953.         }
  954.         if(buf[0] == CTLZ || !stricmp("/ex",buf))
  955.             break;
  956.         fputs(buf,fp);
  957. #if !defined(UNIX) && !defined(__TURBOC__) && !defined(AMIGA)
  958.         /* Needed only if the OS uses a CR/LF
  959.          * convention and putc doesn't do
  960.          * an automatic translation
  961.          */
  962.         if(putc('\r',fp) == EOF)
  963.             break;
  964. #endif
  965.         if(putc('\n',fp) == EOF)
  966.             break;
  967.     }
  968.     free(file);
  969.     fclose(fp);
  970.     return 0;
  971. }
  972.  
  973. static int
  974. dowhat(argc,argv,p)
  975. int argc;
  976. char *argv[];
  977. void *p;
  978. {
  979.     struct mbx *m;
  980.     FILE *fp;
  981.     char *file;
  982.  
  983.     m = (struct mbx *)p;
  984.     if(argc < 2)
  985.         file = strdup(m->path);
  986.     else
  987.         file = pathname(m->path,argv[1]);
  988.     if(!permcheck(m->path,m->privs,RETR_CMD,file)){
  989.         tprintf(Noperm);
  990.         return 0;
  991.     }
  992.     if((fp = dir(file,1)) == NULLFILE)
  993.         tprintf("Can't read directory: \"%s\": %s\n",file,sys_errlist[errno]);
  994.     else
  995.         sendfile(fp,m->user,ASCII_TYPE,0);
  996.     free(file);
  997.     fclose(fp);
  998.     return 0;
  999. }
  1000.  
  1001. static int
  1002. dozap(argc,argv,p)
  1003. int argc;
  1004. char *argv[];
  1005. void *p;
  1006. {
  1007.     struct mbx *m;
  1008.     char *file;
  1009.  
  1010.     m = (struct mbx *)p;
  1011.     file = pathname(m->path,argv[1]);
  1012.     if(!permcheck(m->path,m->privs,DELE_CMD,file)){
  1013.         tprintf(Noperm);
  1014.         return 0;
  1015.     }
  1016.     if(unlink(file))
  1017.         tprintf("Zap failed: %s\n",sys_errlist[errno]);
  1018.     log(m->user,"MBOX Zap: %s",file);
  1019.     free(file);
  1020.     return 0;
  1021. }
  1022.  
  1023. static int
  1024. dosysop(argc,argv,p)
  1025. int argc;
  1026. char *argv[];
  1027. void *p;
  1028. {
  1029.     struct mbx *m;
  1030.     int c;
  1031.     extern struct cmds Cmds[];
  1032.  
  1033.     m = (struct mbx *) p;
  1034.     if(!(m->privs & SYSOP_CMD)){
  1035.         tprintf(Noperm);
  1036.         return 0;
  1037.     }
  1038.     dombescape(1,NULLCHARP,p);
  1039.  
  1040.     tprintf("\nBe VERY carefull!!\n");
  1041.  
  1042.     for(;;){
  1043.         tprintf("Net> ");
  1044.         usflush(Curproc->output);
  1045.         c = mbxrecvline(Curproc->input,m->line,MBXLINE,m->escape);
  1046.         if(c == EOF || c == -2)
  1047.             break;
  1048.         log(m->user,"MBOX sysop: %s",m->line);
  1049.         cmdparse(Cmds,m->line,NULL);
  1050.     }
  1051.     return 0;
  1052. }
  1053.  
  1054. /* Handle the "*** Done" command when reverse forwarding ends or the
  1055.  * "*** LINKED to" command.
  1056.  */
  1057. static int
  1058. dostars(argc,argv,p)
  1059. int argc;
  1060. char *argv[];
  1061. void *p;
  1062. {
  1063.     struct mbx *m;
  1064.     int anony = 1;
  1065.     m = (struct mbx *)p;
  1066.     /* The "*** LINKED to" command is only allowed to stations with
  1067.      * SYSOP privileges to prevent others from obtaining the same.
  1068.      */
  1069.     if((m->privs & SYSOP_CMD) && argc == 4 && !strcmp(argv[1],"linked")) {
  1070.         strcpy(m->name,argv[3]);
  1071.         /* Try to find the privileges of this user from the userfile */
  1072.         if((m->privs = userlogin(m->name,NULLCHAR,&m->path,MBXLINE,
  1073.                      &anony)) == -1)
  1074.              if((m->privs = userlogin("bbs",NULLCHAR,&m->path,
  1075.                       MBXLINE,&anony)) == -1)
  1076.               if((m->privs = userlogin("anonymous",NULLCHAR,
  1077.                        &m->path,MBXLINE,&anony)) == -1){
  1078.                         m->privs = 0;
  1079.                         free(m->path);
  1080.                         m->path = NULLCHAR;
  1081.               }
  1082.         tprintf("Oh, hello %s.\n",m->name);
  1083.         if(m->privs & EXCLUDED_CMD)
  1084.             return domboxbye(0,NULLCHARP,p);
  1085.         changearea(m,m->name);
  1086.         return 0;
  1087.     }
  1088.     if(argc > 1 && (m->sid & MBX_SID))    /* "*** Done" or similar */
  1089.         return 2;
  1090.     return -1;
  1091. }
  1092.  
  1093. static int
  1094. doarea(argc,argv,p)
  1095. int argc;
  1096. char *argv[];
  1097. void *p;
  1098. {
  1099.     struct mbx *m;
  1100.     char *cp, *area;
  1101.     FILE *fp;
  1102.  
  1103.     m = (struct mbx *) p;
  1104.     if(argc < 2){
  1105.         area = strdup(m->area);
  1106.         while((cp = strchr(area,'/')) != NULLCHAR)
  1107.             *cp = '.';
  1108.         tprintf("Current message area is: %s\n",area);
  1109.         free(area);
  1110.  
  1111.         tprintf("Available areas are:\n%-15s  Your private mail area\n",
  1112.           m->name);
  1113.         if((fp = fopen(Arealist,READ_TEXT)) == NULLFILE)
  1114.             return 0;
  1115.         sendfile(fp,m->user,ASCII_TYPE,0);
  1116.         fclose(fp);
  1117.         return 0;
  1118.     }
  1119.     if((m->privs & SYSOP_CMD) || strcmp(m->name,argv[1]) == 0){
  1120.         changearea(m,argv[1]);
  1121.         if(m->nmsgs){
  1122.             if(!strcmp(m->name,m->area))
  1123.                 tprintf("You have ");
  1124.             else {
  1125.                 area = strdup(m->area);
  1126.                 while((cp = strchr(area,'/')) != NULLCHAR)
  1127.                     *cp = '.';
  1128.                 tprintf("%s: ",area);
  1129.                 free(area);
  1130.             }
  1131.             tprintf("%d message%s -  %d new.\n", m->nmsgs,
  1132.               m->nmsgs == 1 ? " " : "s ", m->newmsgs);
  1133.         }
  1134.         return 0;
  1135.     }
  1136.     if(isarea(argv[1])) {
  1137.         changearea(m,argv[1]);
  1138.         area = strdup(m->area);
  1139.         while((cp = strchr(area,'/')) != NULLCHAR)
  1140.             *cp = '.';
  1141.         tprintf("%s: %d message%s.\n", area, m->nmsgs,
  1142.           m->nmsgs == 1 ? "" : "s");
  1143.         free(area);
  1144.     }
  1145.     else
  1146.         tprintf("No such message area: %s\n",argv[1]);
  1147.     return 0;
  1148. }
  1149.  
  1150. /* subroutine to do the actual switch from one area to another */
  1151. void
  1152. changearea(m,area)
  1153. struct mbx *m;
  1154. char *area;
  1155. {
  1156.     char *cp;
  1157.  
  1158.     closenotes(m);
  1159.     m->nmsgs = m->newmsgs = m->current = 0;
  1160.     strcpy(m->area,area);
  1161.     while((cp = strchr(m->area,'.')) != NULLCHAR)
  1162.         *cp = '/';
  1163.     while((cp = strchr(m->area,'\\')) != NULLCHAR)
  1164.         *cp = '/';
  1165.     scanmail(m);
  1166. }
  1167.  
  1168. static int
  1169. dombtelnet(argc,argv,p)
  1170. int argc;
  1171. char *argv[];
  1172. void *p;
  1173. {
  1174.     struct mbx *m;
  1175.     int s, len, i;
  1176.     char dsocket[MAXSOCKSIZE];
  1177.     struct sockaddr_in fsocket;
  1178.  
  1179.     m = (struct mbx *) p;
  1180.     fsocket.sin_family = AF_INET;
  1181.     if(argc < 3)
  1182.         fsocket.sin_port = IPPORT_TELNET;
  1183.     else
  1184.         fsocket.sin_port = atoi(argv[2]);
  1185.  
  1186.     if((fsocket.sin_addr.s_addr = resolve(argv[1])) == 0){
  1187.         tprintf(Badhost,argv[1]);
  1188.         return 0;
  1189.     }
  1190.     /* Only local telnets are are allowed to the unprivileged user */
  1191.     if(!(m->privs & TELNET_CMD) && !ismyaddr(fsocket.sin_addr.s_addr)){
  1192.         tprintf(Noperm);
  1193.         return 0;
  1194.     }
  1195.     if((s = socket(AF_INET,SOCK_STREAM,0)) == -1){
  1196.         tprintf(Nosock);
  1197.         return 0;
  1198.     }
  1199.     if(fsocket.sin_port == IPPORT_TTYLINK) {
  1200.         m->startmsg = mallocw(80);
  1201.         len = MAXSOCKSIZE;
  1202.         i = getpeername(m->user,dsocket,&len);
  1203.         sprintf(m->startmsg,"*** Incoming call from %s@%s ***\n",
  1204.             m->name,i != -1 ? psocket(dsocket): Hostname);
  1205.     }
  1206.     return gw_connect(m,s,(char *)&fsocket,SOCKSIZE);
  1207. }
  1208.  
  1209. static int
  1210. dombfinger(argc,argv,p)
  1211. int argc;
  1212. char *argv[];
  1213. void *p;
  1214. {
  1215.     struct mbx *m;
  1216.     char *host, *user = NULLCHAR, buf[8], *newargv[3];
  1217.  
  1218.     if(argc > 2){
  1219.         tprintf("Usage: F user@host  or  F @host  or  F user.\n");
  1220.         return 0;
  1221.     }
  1222.     host = Hostname;
  1223.     if(argc == 2){
  1224.         if((host = strchr(argv[1], '@')) != NULLCHAR){
  1225.             *host = '\0';
  1226.             host++;
  1227.         } else
  1228.             host = Hostname;
  1229.         user = argv[1];
  1230.     }
  1231.     m = (struct mbx *) p;
  1232.     m->startmsg = mallocw(80);
  1233.     if(user != NULLCHAR)
  1234.         sprintf(m->startmsg,"%s\n",user);
  1235.     else
  1236.         strcpy(m->startmsg,"\n");
  1237.     newargv[0] = "telnet";
  1238.     newargv[1] = host;
  1239.     sprintf(buf,"%d",IPPORT_FINGER);
  1240.     newargv[2] = buf;
  1241.     return dombtelnet(3,newargv,p);
  1242. }
  1243.  
  1244. /* Generic mbox gateway code. It sends and frees the contents of m->startmsg
  1245.  * when the connection has been established unless it a null pointer.
  1246.  */
  1247. int
  1248. gw_connect(m,s,fsocket,len)
  1249. struct mbx *m;
  1250. int s;
  1251. char *fsocket;
  1252. int len;
  1253. {
  1254.     int c;
  1255.     char *cp;
  1256.     struct proc *child;
  1257.     struct gwalarm *gwa;
  1258.  
  1259.     sockmode(s,SOCK_ASCII);
  1260.     child = newproc("gateway supervisor",256,gw_superv,0,Curproc,m,0);
  1261.     tprintf("Trying %s...  ",psocket((struct sockaddr *)fsocket));
  1262.     dombescape(0,NULLCHARP,(void *)m);
  1263.     usflush(Curproc->output);
  1264.     if(connect(s,fsocket,len) == -1){
  1265.         cp = sockerr(s);
  1266.         tprintf("Connection failed: ");
  1267.         if(cp != NULLCHAR)
  1268.             tprintf("%s errno %d\n",cp,errno);
  1269.         else
  1270.             tprintf("Escape character sent.\n");
  1271.         free(m->startmsg);
  1272.         m->startmsg = NULLCHAR;
  1273.         shutdown(s,2);    /* HB9RWM suggestion */
  1274.         close_s(s);
  1275.         killproc(child);
  1276.         return 0;
  1277.     }
  1278.     /* The user did not type the escape character */
  1279.     killproc(child);
  1280.     tputs("Connected.\n");
  1281.     
  1282.     if(m->startmsg != NULLCHAR){
  1283.         usputs(s,m->startmsg);
  1284.         free(m->startmsg);
  1285.         m->startmsg = NULLCHAR;
  1286.     }
  1287.     /* Since NOS does not flush the output socket after a certain
  1288.      * period of time, we have to arrange that ourselves.
  1289.      */
  1290.     gwa = (struct gwalarm *) mallocw(sizeof(struct gwalarm));
  1291.     gwa->s1 = Curproc->output;
  1292.     gwa->s2 = s;
  1293.     set_timer(&gwa->t,2*1000L);  /* was 240*1000l */
  1294.     gwa->t.func = gw_alarm;
  1295.     gwa->t.arg = (void *) gwa;
  1296.     start_timer(&gwa->t);
  1297.     /* Fork off the receive process */
  1298.     child = newproc("Gateway in",1024,gw_input,s,NULL,Curproc,0);
  1299.     
  1300.     for(;;){
  1301.         if((c = recvchar(Curproc->input)) == EOF)
  1302.             break;
  1303.         if(c == m->escape){
  1304.             tputs("Disconnecting.");
  1305.             if(socklen(Curproc->input,0))
  1306.                 recv_mbuf(Curproc->input,NULL,0,NULLCHAR,0);
  1307.             break;
  1308.         }
  1309.         if(usputc(s,(char)c) == EOF)
  1310.             break;
  1311.     }
  1312.     stop_timer(&gwa->t);
  1313.     free((char *)gwa);
  1314.     close_s(s);
  1315.     killproc(child); /* get rid of the receive process */
  1316.     tprintf("%c%c%c\n",IAC,WONT,TN_ECHO);
  1317.     return 0;
  1318. }
  1319.  
  1320. static void
  1321. gw_input(s,notused,p)
  1322. int s;
  1323. void *notused;
  1324. void *p;
  1325. {
  1326.     int c;
  1327.     char *cp;
  1328.     struct proc *parent;
  1329.  
  1330.     parent = (struct proc *) p;
  1331.     while((c = recvchar(s)) != EOF)
  1332.         tputc((char)c);
  1333.     tprintf("\nDisconnected.\n");
  1334.     cp = sockerr(s);
  1335.     if(cp != NULLCHAR)
  1336.         tputs(cp);
  1337.     /* Tell the parent that we are no longer connected */
  1338.     alert(parent,ENOTCONN);
  1339.     pwait(Curproc); /* Now wait to be killed */
  1340. }
  1341.  
  1342. /* Check if the escape character is typed while the parent process is busy
  1343.  * doing other things. 
  1344.  */
  1345. static void
  1346. gw_superv(null,proc,p)
  1347. int null;
  1348. void *proc;
  1349. void *p;
  1350. {
  1351.     struct proc *parent;
  1352.     struct mbx *m;
  1353.     int c;
  1354.     parent = (struct proc *) proc;
  1355.     m = (struct mbx *) p;
  1356.     while((c = recvchar(Curproc->input)) != EOF)
  1357.         if(c == m->escape){
  1358.             /* flush anything in the input queue */
  1359.             if(socklen(Curproc->input,0))
  1360.                 recv_mbuf(Curproc->input,NULL,0,NULLCHAR,0);
  1361.             break;
  1362.         }
  1363.     alert(parent,EINTR);     /* Tell the parent to quit */
  1364.     pwait(Curproc);         /* Please kill me */
  1365. }
  1366.  
  1367. static void
  1368. gw_alarm(p)
  1369. void *p;
  1370. {
  1371.     struct gwalarm *gwa = (struct gwalarm *)p;
  1372.     char oldbl;
  1373.     struct usock *up;
  1374.  
  1375.     /* Flush sockets s1 and s2, but first make sure that the socket
  1376.      * is set to non-blocking mode, to prevent the flush from blocking
  1377.      * if the high water mark has been reached.
  1378.      */
  1379.     if((up = itop(gwa->s1)) != NULLUSOCK) {
  1380.         oldbl = up->noblock;
  1381.         up->noblock = 1;
  1382.         usflush(gwa->s1);
  1383.         up->noblock = oldbl;
  1384.     }
  1385.     if((up = itop(gwa->s2)) != NULLUSOCK) {
  1386.         oldbl = up->noblock;
  1387.         up->noblock = 1;
  1388.         usflush(gwa->s2);
  1389.         up->noblock = oldbl;
  1390.     }
  1391.     start_timer(&gwa->t);
  1392. }
  1393.  
  1394. /* States for send line parser state machine */
  1395. #define        LOOK_FOR_USER        2
  1396. #define        IN_USER            3
  1397. #define        AFTER_USER        4
  1398. #define        LOOK_FOR_HOST        5
  1399. #define        IN_HOST            6
  1400. #define        AFTER_HOST        7
  1401. #define        LOOK_FOR_FROM        8
  1402. #define        IN_FROM            9
  1403. #define        AFTER_FROM        10
  1404. #define        LOOK_FOR_MSGID        11
  1405. #define        IN_MSGID        12
  1406. #define        FINAL_STATE        13
  1407. #define        ERROR_STATE        14
  1408.  
  1409. /* Prepare the addressee.  If the address is bad, return -1, otherwise
  1410.  * return 0
  1411.  */
  1412. static int
  1413. mbx_to(argc,argv,p)
  1414. int argc;
  1415. char *argv[];
  1416. void *p;
  1417. {
  1418.     register char *cp;
  1419.     int state, i;
  1420.     char *user, *host, *from, *msgid;
  1421.     int userlen = 0, hostlen = 0, fromlen = 0, msgidlen = 0;
  1422.     struct mbx *m;
  1423.  
  1424.     m = (struct mbx *)p;
  1425.     /* Free anything that might be allocated
  1426.      * since the last call to mbx_to() or mbx_reply()
  1427.      */
  1428.     free(m->to);
  1429.     m->to = NULLCHAR;
  1430.     free(m->tofrom);
  1431.     m->tofrom = NULLCHAR;
  1432.     free(m->tomsgid);
  1433.     m->tomsgid = NULLCHAR;
  1434.     free(m->origto);
  1435.     m->origto = NULLCHAR;
  1436.  
  1437.     if(argc == 1)
  1438.         return -1;
  1439.     i = 1;
  1440.     cp = argv[i];
  1441.     state = LOOK_FOR_USER;
  1442.     while(state < FINAL_STATE){
  1443. #ifdef MBDEBUG
  1444.         tprintf("State is %d, char is %c\n", state, *cp);
  1445. #endif
  1446.         switch(state){
  1447.         case LOOK_FOR_USER:
  1448.             if(*cp == '@' || *cp == '<' || *cp == '$'){
  1449.                 state = ERROR_STATE;        /* no user */
  1450.             } else {
  1451.                 user = cp;            /* point at start */
  1452.                 userlen++;            /* start counting */
  1453.                 state = IN_USER;
  1454.             }
  1455.             break;
  1456.         case IN_USER:
  1457.             switch(*cp){
  1458.             case '\0':
  1459.                 state = AFTER_USER;        /* done with username */
  1460.                 break;
  1461.             case '@':
  1462.                 state = LOOK_FOR_HOST;        /* hostname should follow */
  1463.                 break;
  1464.             case '<':
  1465.                 state = LOOK_FOR_FROM;        /* from name should follow */
  1466.                 break;
  1467.             case '$':
  1468.                 state = LOOK_FOR_MSGID;    /* message id should follow */
  1469.                 break;
  1470.             default:
  1471.                 userlen++;            /* part of username */
  1472.             }
  1473.             break;
  1474.         case AFTER_USER:
  1475.             switch(*cp){
  1476.             case '@':
  1477.                 state = LOOK_FOR_HOST;        /* hostname follows */
  1478.                 break;
  1479.             case '<':
  1480.                 state = LOOK_FOR_FROM;        /* fromname follows */
  1481.                 break;
  1482.             case '$':
  1483.             state = LOOK_FOR_MSGID;    /* message id follows */
  1484.                 break;
  1485.             default:
  1486.                 state = ERROR_STATE;
  1487.             }
  1488.             break;
  1489.         case LOOK_FOR_HOST:
  1490.             if(*cp == '@' || *cp == '<' || *cp == '$'){
  1491.                 state = ERROR_STATE;
  1492.                 break;
  1493.             }
  1494.             if(*cp == '\0')
  1495.                 break;
  1496.             host = cp;
  1497.             hostlen++;
  1498.             state = IN_HOST;
  1499.             break;
  1500.         case IN_HOST:
  1501.             switch(*cp){
  1502.             case '\0':
  1503.                 state = AFTER_HOST;        /* found user@host */
  1504.                 break;
  1505.             case '@':
  1506.                 state = ERROR_STATE;        /* user@host@? */
  1507.                 break;
  1508.             case '<':
  1509.                 state = LOOK_FOR_FROM;        /* fromname follows */
  1510.                 break;
  1511.             case '$':
  1512.                 state = LOOK_FOR_MSGID;    /* message id follows */
  1513.                 break;
  1514.             default:
  1515.                 hostlen++;
  1516.             }
  1517.             break;
  1518.         case AFTER_HOST:
  1519.             switch(*cp){
  1520.             case '@':
  1521.                 state = ERROR_STATE;        /* user@host @ */
  1522.                 break;
  1523.             case '<':
  1524.                 state = LOOK_FOR_FROM;        /* user@host < */
  1525.                 break;
  1526.             case '$':
  1527.                 state = LOOK_FOR_MSGID;    /* user@host $ */
  1528.                 break;
  1529.             default:
  1530.                 state = ERROR_STATE;        /* user@host foo */
  1531.             }
  1532.             break;
  1533.         case LOOK_FOR_FROM:
  1534.             if(*cp == '@' || *cp == '<' || *cp == '$'){
  1535.                 state = ERROR_STATE;
  1536.                 break;
  1537.             }
  1538.             if(*cp == '\0')
  1539.                 break;
  1540.             from = cp;
  1541.             fromlen++;
  1542.             state = IN_FROM;
  1543.             break;
  1544.         case IN_FROM:
  1545.             switch(*cp){
  1546.             case '\0':
  1547.                 state = AFTER_FROM;        /* user@host <foo */
  1548.                 break;
  1549.             case '<':
  1550.                 state = ERROR_STATE;        /* user@host <foo< */
  1551.                 break;
  1552.             case '$':
  1553.                 state = LOOK_FOR_MSGID;    /* message id follows */
  1554.                 break;
  1555.             default:
  1556.                 fromlen++;
  1557.             }
  1558.             break;
  1559.         case AFTER_FROM:
  1560.             switch(*cp){
  1561.             case '@':                /* user@host <foo @ */
  1562.             case '<':                /* user@host <foo < */
  1563.                 state = ERROR_STATE;
  1564.                 break;
  1565.             case '$':
  1566.                 state = LOOK_FOR_MSGID;    /* user@host <foo $ */
  1567.                 break;
  1568.             default:
  1569.                 state = ERROR_STATE;        /* user@host foo */
  1570.             }
  1571.             break;
  1572.         case LOOK_FOR_MSGID:
  1573.             if(*cp == '\0')
  1574.                 break;
  1575.             msgid = cp;
  1576.             msgidlen++;
  1577.             state = IN_MSGID;
  1578.             break;
  1579.         case IN_MSGID:
  1580.             if(*cp == '\0')
  1581.                 state = FINAL_STATE;
  1582.             else
  1583.                 msgidlen++;
  1584.             break;
  1585.         default:
  1586.             /* what are we doing in this state? */
  1587.             state = ERROR_STATE;
  1588.         }
  1589.         if(*(cp) == '\0'){
  1590.             ++i;
  1591.             if(i < argc)
  1592.             cp = argv[i];
  1593.             else break;
  1594.         } else
  1595.             ++cp;
  1596.     }
  1597.     if(state == ERROR_STATE || state == LOOK_FOR_HOST
  1598.      || state == LOOK_FOR_FROM || state == LOOK_FOR_MSGID)
  1599.         return -1;        /* syntax error */
  1600.  
  1601.     m->to = mallocw(userlen + hostlen + 2);
  1602.  
  1603.     strncpy(m->to, user, userlen);
  1604.     m->to[userlen] = '\0';
  1605.  
  1606.     if(hostlen){
  1607.         m->to[userlen] = '@';
  1608.         strncpy(m->to + userlen + 1, host, hostlen);
  1609.         m->to[userlen + hostlen + 1] = '\0';
  1610.     }
  1611.     if(fromlen){
  1612.         m->tofrom = mallocw(fromlen + 1);
  1613.         strncpy(m->tofrom, from, fromlen);
  1614.         m->tofrom[fromlen] = '\0';
  1615.     }
  1616.     if(msgidlen){
  1617.         m->tomsgid = mallocw(msgidlen + 1);
  1618.         strncpy(m->tomsgid, msgid, msgidlen);
  1619.         m->tomsgid[msgidlen] = '\0';
  1620.     }
  1621.     return 0;
  1622. }
  1623.  
  1624. /* This opens the data file and writes the mail header into it.
  1625.  * Returns 0 if OK, and -1 if not.
  1626.  */
  1627. static int
  1628. mbx_data(m,cclist,extra)
  1629. struct mbx *m;
  1630. struct list *cclist;    /* list of carbon copy recipients */
  1631. char *extra;        /* optional extra header lines */
  1632. {
  1633.     time_t t;
  1634.     struct list *ap;
  1635.     int cccnt = 0;
  1636.     
  1637.     if((m->tfile = tmpfile()) == NULLFILE)
  1638.         return -1;
  1639.     time(&t);
  1640.     fprintf(m->tfile,Hdrs[RECEIVED]);
  1641.     if(m->tofrom != NULLCHAR)
  1642.         fprintf(m->tfile,"from %s ",m->name);
  1643.     fprintf(m->tfile,"by %s (%s)\n\tid AA%ld ; %s",
  1644.         Hostname, Version, get_msgid(), ptime(&t));
  1645.     fprintf(m->tfile,"%s%s",Hdrs[DATE],ptime(&t));
  1646.     fprintf(m->tfile,Hdrs[MSGID]);
  1647.     if(m->tomsgid)
  1648.         fprintf(m->tfile,"<%s@%s>\n", m->tomsgid, m->name);
  1649.     else
  1650.         fprintf(m->tfile,"<%ld@%s>\n",get_msgid(), Hostname);
  1651.     fprintf(m->tfile,Hdrs[FROM]);
  1652.     if(m->tofrom)
  1653.         fprintf(m->tfile,"%s%%%s@%s\n",
  1654.             m->tofrom, m->name, Hostname);
  1655.     else
  1656.         fprintf(m->tfile,"%s@%s\n", m->name, Hostname);
  1657.     fprintf(m->tfile,"%s%s\n",Hdrs[TO],m->origto != NULLCHAR ? m->origto : m->to);
  1658.     /* Write Cc: line */
  1659.     for(ap = cclist; ap != NULLLIST; ap = ap->next) {
  1660.         if(cccnt == 0){
  1661.             fprintf(m->tfile,"%s",Hdrs[CC]);
  1662.             cccnt = 4;
  1663.         }
  1664.         else {
  1665.                fprintf(m->tfile,", ");
  1666.                cccnt += 2;
  1667.         }
  1668.         if(cccnt + strlen(ap->val) > 80 - 3) {
  1669.                fprintf(m->tfile,"\n    ");
  1670.                cccnt = 4;
  1671.         }
  1672.         fputs(ap->val,m->tfile);
  1673.         cccnt += strlen(ap->val);
  1674.     }
  1675.     if(cccnt)
  1676.         fputc('\n',m->tfile);
  1677.     fprintf(m->tfile,"%s%s\n",Hdrs[SUBJECT],m->line);
  1678.     if(!isspace(m->stype) && ((m->stype != 'R' && m->stype != 'F') ||
  1679.       (m->sid & MBX_SID) !=0))
  1680.           fprintf(m->tfile,"%s%c\n", Hdrs[BBSTYPE],m->stype);
  1681.     if(extra != NULLCHAR)
  1682.         fprintf(m->tfile,extra);
  1683.     fprintf(m->tfile,"\n");
  1684.  
  1685.     return 0;
  1686. }
  1687.  
  1688. /* Returns true if string is in history file or if string appears to be a
  1689.  * message id generated by our system.
  1690.  */
  1691. static int
  1692. msgidcheck(string)
  1693. char *string;
  1694. {
  1695.      FILE *fp;
  1696.      char buf[LINELEN], *cp;
  1697.      if(string == NULLCHAR)
  1698.       return 0;
  1699.      /* BID's that we have generated ourselves are not kept in the history
  1700.       * file. Such BID's are in the nnnn_hhhh form, where hhhh is a part of
  1701.       * our hostname, truncated so that the BID is no longer than 11
  1702.       * characters.
  1703.       */
  1704.      if((cp = strchr(string,'_')) != NULLCHAR && *(cp+1) != '\0' && 
  1705.     strnicmp(cp+1,Hostname,strlen(cp+1)) == 0)
  1706.       return 1;
  1707.  
  1708.      if((fp = fopen(Historyfile,READ_TEXT)) == NULLFILE)
  1709.       return 0;
  1710.      while(fgets(buf,LINELEN,fp) != NULLCHAR) {
  1711.       rip(buf);
  1712.       if(stricmp(string,buf) == 0) {    /* found */
  1713.            fclose(fp);
  1714.            return 1;
  1715.       }
  1716.      }
  1717.      fclose(fp);
  1718.      return 0;
  1719. }
  1720.      
  1721. /* Read the rewrite file for lines where the first word is a regular
  1722.  * expression and the second word are rewriting rules. The special
  1723.  * character '$' followed by a digit denotes the string that matched
  1724.  * a '*' character. The '*' characters are numbered from 1 to 9.
  1725.  * Example: the line "*@*.* $2@$1.ampr.org" would rewrite the address
  1726.  * "foo@bar.xxx" to "bar@foo.ampr.org".
  1727.  * $H is replaced by our hostname, and $$ is an escaped $ character.
  1728.  * If the third word on the line has an 'r' character in it, the function
  1729.  * will recurse with the new address.
  1730.  */
  1731. char *
  1732. rewrite_address(addr)
  1733. char *addr;
  1734. {
  1735.     char *argv[10], buf[MBXLINE], *cp, *cp2, *retstr;
  1736.     int cnt;
  1737.     FILE *fp;
  1738.     if ((fp = fopen(Rewritefile,READ_TEXT)) == NULLFILE)
  1739.         return NULLCHAR;
  1740.     memset((char *)argv,0,10*sizeof(char *));
  1741.     while(fgets(buf,MBXLINE,fp) != NULLCHAR) {
  1742.         if(*buf == '#')        /* skip commented lines */
  1743.             continue;
  1744.         if((cp = strchr(buf,' ')) == NULLCHAR) /* get the first word */
  1745.             if((cp = strchr(buf,'\t')) == NULLCHAR)
  1746.                 continue;
  1747.         *cp = '\0';
  1748.         if((cp2 = strchr(buf,'\t')) != NULLCHAR){
  1749.             *cp = ' ';
  1750.             cp = cp2;
  1751.             *cp = '\0';
  1752.         }
  1753.         if(!wildmat(addr,buf,argv))
  1754.             continue;        /* no match */
  1755.         rip(++cp);
  1756.         cp2 = retstr = (char *) callocw(1,MBXLINE);
  1757.         while(*cp != '\0' && *cp != ' ' && *cp != '\t')
  1758.             if(*cp == '$') {
  1759.                 if(isdigit(*(++cp)))
  1760.                     if(argv[*cp - '0'-1] != '\0')
  1761.                         strcat(cp2,argv[*cp - '0'-1]);
  1762.                 if(*cp == 'h' || *cp == 'H') /* Our hostname */
  1763.                     strcat(cp2,Hostname);
  1764.                 if(*cp == '$')    /* Escaped $ character */
  1765.                     strcat(cp2,"$");
  1766.                 cp2 = retstr + strlen(retstr);
  1767.                 cp++;
  1768.             }
  1769.             else
  1770.                 *cp2++ = *cp++;
  1771.         for(cnt=0; argv[cnt] != NULLCHAR; ++cnt)
  1772.             free(argv[cnt]);
  1773.         fclose(fp);
  1774.         /* If there remains an 'r' character on the line, repeat
  1775.          * everything by recursing.
  1776.          */
  1777.         if(strchr(cp,'r') != NULLCHAR || strchr(cp,'R') != NULLCHAR) {
  1778.             if((cp2 = rewrite_address(retstr)) != NULLCHAR) {
  1779.                 free(retstr);
  1780.                 return cp2;
  1781.             }
  1782.         }
  1783.         return retstr;
  1784.     }
  1785.     fclose(fp);
  1786.     return NULLCHAR;
  1787. }
  1788.  
  1789. /* uuencode a file -- translated from C++; both versions copyright 1990
  1790.    by David R. Evans, G4AMJ/NQ0I
  1791. */
  1792.  
  1793. static int
  1794. uuencode(infile,s,infilename)
  1795. FILE *infile;
  1796. int s;            /* output socket */
  1797. char *infilename;
  1798. {
  1799.   int n_read_so_far = 0, n_written_so_far = 0, in_chars, n, mode = 0755;
  1800.   unsigned int32 cnt = 0;
  1801.   unsigned char in[3], out[4], line[100];
  1802. #ifdef UNIX
  1803.   struct stat stb;
  1804.   
  1805.   if(stat(infilename,&stb) != -1)
  1806.        mode = stb.st_mode & 0777;    /* get real file protection mode */
  1807. #endif
  1808.   usprintf(s, "begin %03o %s\n", mode, infilename);
  1809.  
  1810.   /* do the encode */
  1811.  for(;;){    
  1812.     in_chars = fread(in, 1, 3, infile);
  1813.     out[0] = in[0] >> 2;
  1814.     out[1] = in[0] << 6;
  1815.     out[1] = out[1] >> 2;
  1816.     out[1] = out[1] | (in[1] >> 4);
  1817.     out[2] = in[1] << 4;
  1818.     out[2] = out[2] >> 2;
  1819.     out[2] = out[2] | (in[2] >> 6);
  1820.     out[3] = in[2] << 2;
  1821.     out[3] = out[3] >> 2;
  1822.     for (n = 0; n < 4; n++)
  1823.       out[n] += ' ';
  1824.     n_read_so_far += in_chars;
  1825.     for (n = 0; n < 4; n++)
  1826.       line[n_written_so_far++] = out[n];
  1827.     if (((in_chars != 3) || (n_written_so_far == 60)) && n_read_so_far > 0) {
  1828.       line[(n_read_so_far + 2) / 3 * 4] = '\0';
  1829.       
  1830.       usprintf(s,"%c%s\n",n_read_so_far + ' ', line);
  1831.       cnt += n_read_so_far;
  1832.       n_read_so_far = 0;
  1833.       n_written_so_far = 0;
  1834.     }
  1835.     if (in_chars == 0)
  1836.       break;
  1837.   }
  1838.   if (usprintf(s," \nend\nsize %lu\n", cnt) == EOF)
  1839.     return 1;
  1840.   return 0;
  1841. }
  1842.  
  1843. /* Attempt to determine if this is third-pary mail. */
  1844. static int 
  1845. thirdparty(m)
  1846. struct mbx *m;
  1847. {
  1848.     char buf[MBXLINE], *cp, *rp;
  1849.     FILE *fp;
  1850.  
  1851.     if(strchr(m->to,'@') != NULLCHAR || strchr(m->to,'%') != NULLCHAR
  1852.             || strchr(m->to,'!') != NULLCHAR)
  1853.         return 0;
  1854.  
  1855.     rp = strdup(Hostname);
  1856.  
  1857.     if((cp = strchr(rp, '.')) != NULLCHAR)
  1858.         *cp = '\0';
  1859.  
  1860.     if(stricmp(m->to,rp) == 0){
  1861.         free(rp);
  1862.         return -1;
  1863.     }
  1864.     free(rp);
  1865.  
  1866.     if(stricmp(m->to,"sysop") == 0)
  1867.         return -1;
  1868.  
  1869.     if((fp = fopen(Arealist,READ_TEXT)) == NULLFILE)
  1870.         return 0;
  1871.  
  1872.     while(fgets(buf,MBXLINE,fp) != NULLCHAR){
  1873.         /* The first word on each line is all that matters */
  1874.         if((cp = strchr(buf, ' ')) != NULLCHAR)
  1875.             *cp = '\0';
  1876.         if((cp = strchr(buf,'\t')) != NULLCHAR)
  1877.             *cp = '\0';
  1878.         if(stricmp(m->to,buf) == 0){
  1879.             fclose(fp);
  1880.             return -1;
  1881.         }
  1882.     }
  1883.     fclose(fp);
  1884.     return 0;
  1885. }
  1886.