home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 3 / hamradioversion3.0examsandprograms1992.iso / packet / n17jsrc / popcli.c < prev    next >
C/C++ Source or Header  |  1991-05-14  |  10KB  |  477 lines

  1. /*
  2.  *    POP2 Client routines.  Originally authored by Mike Stockett
  3.  *      (WA7DYX).
  4.  *    Modified 27 May 1990 by Allen Gwinn (N5CKP) for compatibility
  5.  *      with later releases (NOS0522).
  6.  *    Added into NOS by PA0GRI (and linted into "standard" C)
  7.  *
  8.  *    Some code culled from previous releases of SMTP.
  9.  *
  10.  *    Client routines for Simple Mail Transfer Protocol ala RFC821
  11.  *    A.D. Barksdale Garbee II, aka Bdale, N3EUA
  12.  *    Copyright 1986 Bdale Garbee, All Rights Reserved.
  13.  *    Permission granted for non-commercial copying and use, provided
  14.  *      this notice is retained.
  15.  *     Modified 14 June 1987 by P. Karn for symbolic target addresses,
  16.  *      also rebuilt locking mechanism
  17.  *    Copyright 1987 1988 David Trulli, All Rights Reserved.
  18.  *    Permission granted for non-commercial copying and use, provided
  19.  *    this notice is retained.
  20.  */
  21. #include <stdio.h>
  22. #include <fcntl.h>
  23. #include <time.h>
  24. #include <setjmp.h>
  25. #ifdef UNIX
  26. #include <sys/types.h>
  27. #endif
  28. #ifdef    __TURBOC__
  29. #include <dir.h>
  30. #include <io.h>
  31. #endif
  32. #include "global.h"
  33. #ifdef    ANSIPROTO
  34. #include <stdarg.h>
  35. #endif
  36. #include "mbuf.h"
  37. #include "cmdparse.h"
  38. #include "proc.h"
  39. #include "socket.h"
  40. #include "timer.h"
  41. #include "netuser.h"
  42. #include "dirutil.h"
  43. #include "files.h"
  44.  
  45. extern char Badhost[];
  46.  
  47. #define BUF_LEN        257
  48.  
  49. /* POP client control block */
  50.  
  51. struct pop_ccb {
  52.     int    socket;        /* socket for this connection */
  53.     char    state;        /* client state */
  54. #define       CALL        0
  55. #define       NMBR        3
  56. #define       SIZE        5
  57. #define       XFER        8
  58. #define       EXIT        10
  59.     char    buf[BUF_LEN],    /* tcp input buffer */
  60.         count;        /* input buffer length */
  61.     int    folder_len;    /* number of msgs in current folder */
  62.     long    msg_len;    /* length of current msg */
  63.     int    msg_num;    /* current message number */
  64. } *ccb;
  65.  
  66. #define NULLCCB        (struct pop_ccb *)0
  67.  
  68. static int Popquiet = 0;
  69.  
  70. static struct timer  popcli_t;
  71. static int32 mailhost;
  72. static char    mailbox_name[10],
  73.         mailbox_pathname[BUF_LEN],
  74.         username[20],
  75.         password[20],
  76.         Workfile_name[] ="mbox.pop";
  77.  
  78. static int domailbox __ARGS((int argc,char *argv[],void *p));
  79. static int domailhost __ARGS((int argc,char *argv[],void *p));
  80. static int douserdata __ARGS((int argc,char *argv[],void *p));
  81. static int doquiet __ARGS((int argc,char *argv[],void *p));
  82. static int dotimer __ARGS((int argc,char *argv[],void *p));
  83. static struct pop_ccb     *new_ccb __ARGS((void));
  84. static void delete_ccb __ARGS((void));
  85. static void pop_send __ARGS((int unused,void *cb1,void *p));
  86. static int popkick __ARGS((int argc,char *argv[],void *p));
  87.  
  88. static struct cmds Popcmds[] = {
  89.     "mailbox",    domailbox,    0,    0,    NULLCHAR,
  90.     "mailhost",    domailhost,    0,    0,    NULLCHAR,
  91.     "kick",        popkick,    0,    0,    NULLCHAR,
  92.     "quiet",    doquiet,    0,    0,    NULLCHAR,
  93.     "timer",    dotimer,    0,    0,    NULLCHAR,
  94.     "userdata",    douserdata,    0,    0,    NULLCHAR,
  95.     NULLCHAR,
  96. };
  97.  
  98.  
  99. /* Command string specifications */
  100.  
  101. static char ackd_cmd[] = "ACKD\n",
  102. #ifdef POP_FOLDERS
  103.     fold_cmd[] = "FOLD %s\n",
  104. #endif
  105.     login_cmd[] = "HELO %s %s\n",
  106.     /* nack_cmd[]      = "NACK\n",     /* Not implemented */
  107.     quit_cmd[]      = "QUIT\n",
  108.     read_cur_cmd[]  = "READ\n",
  109.     retr_cmd[]      = "RETR\n";
  110.  
  111. /* Response string keys */
  112.  
  113. static char *greeting_rsp  = "+ POP2 ";
  114.  
  115. FILE    *fd;
  116. #define    NULLFILE    (FILE *)0
  117.  
  118. int
  119. dopop(argc,argv,p)
  120. int     argc;
  121. char     *argv[];
  122. void     *p;
  123. {
  124.     return subcmd(Popcmds,argc,argv,p);
  125. }
  126.  
  127. static int
  128. domailbox(argc,argv,p) 
  129. int argc;
  130. char *argv[];
  131. void *p;
  132. {
  133.     if(argc < 2) {
  134.         if(mailbox_name[0] == '\0')
  135.             tprintf("maibox name not set yet\n");
  136.         else
  137.             tprintf("%s\n",mailbox_name);
  138.     } else {
  139.         strncpy(mailbox_name,argv[1],10);
  140.     }
  141.  
  142.     return 0;
  143. }
  144.  
  145. static int
  146. domailhost(argc,argv,p)
  147. int argc;
  148. char *argv[];
  149. void *p;
  150. {
  151.     int32 n;
  152.  
  153.     if(argc < 2) {
  154.         tprintf("%s\n",inet_ntoa(mailhost));
  155.     } else
  156.         if((n = resolve(argv[1])) == 0) {
  157.             tprintf(Badhost,argv[1]);
  158.             return 1;
  159.         } else
  160.             mailhost = n;
  161.  
  162.     return 0;
  163. }
  164.  
  165. static int
  166. doquiet(argc,argv,p)
  167. int argc;
  168. char *argv[];
  169. void *p;
  170. {
  171.     return setbool(&Popquiet,"POP quiet",argc,argv);
  172. }
  173.  
  174. static int
  175. douserdata(argc,argv,p)
  176. int argc;
  177. char *argv[];
  178. void *p;
  179. {
  180.     if (argc < 2)
  181.         tprintf("%s\n",username);
  182.     else if (argc != 3) {
  183.         tprintf("Usage: pop userdata <username> <password>\n");
  184.         return 1;
  185.     } else {
  186.         sscanf(argv[1],"%18s",username);
  187.         sscanf(argv[2],"%18s",password);
  188.     }
  189.  
  190.     return 0;
  191. }
  192.  
  193. /* Set scan interval */
  194.  
  195. static int
  196. dotimer(argc,argv,p)
  197. int argc;
  198. char *argv[];
  199. void *p;
  200. {
  201.     int poptick();
  202.  
  203.  
  204.     if(argc < 2) {
  205.         tprintf("%lu/%lu\n",
  206.                read_timer(&popcli_t)/1000L,
  207.                dur_timer(&popcli_t)/1000L);
  208.         return 0;
  209.     }
  210.  
  211.     popcli_t.func  = (void (*)())poptick;          /* what to call on timeout */
  212.     popcli_t.arg   = NULL;                /* dummy value */
  213.     set_timer(&popcli_t,atol(argv[1])*1000L);    /* set timer duration */
  214.     start_timer(&popcli_t);                /* and fire it up */
  215.     return 0;
  216. }
  217.  
  218. static int
  219. popkick(argc,argv,p)
  220. int argc;
  221. char *argv[];
  222. void *p;
  223. {
  224.     poptick(NULL);
  225.     return 0;
  226. }
  227.  
  228. int
  229. poptick()
  230. {
  231.     if (ccb == NULLCCB) {
  232.  
  233.         /* Don't start if any of the required parameters have not been specified */
  234.  
  235.         if (mailhost == 0) {
  236.             tprintf("mailhost not defined yet.(pop mailhost <host>)\n");
  237.             return 0;
  238.         }
  239.  
  240.         if (mailbox_name[0] == '\0') {
  241.             tprintf("mailbox name not defined yet.(pop mailbox <name>)\n");
  242.             return 0;
  243.         }
  244.  
  245.         if (username[0] == '\0') {
  246.             tprintf("username not defined yet. (pop user <name> <pass>)\n");
  247.             return 0;
  248.         }
  249.  
  250.         if (password[0] == '\0') {
  251.             tprintf(" Unknown password\n");
  252.             return 0;
  253.         }
  254.  
  255.         if ((ccb = new_ccb()) == NULLCCB) {
  256.             fprintf(stderr,"*** Unable to allocate CCB");
  257.             return 0;
  258.         }
  259.  
  260.         newproc("Auto-POP Client",1024,pop_send,0,ccb,NULL,0);
  261.     }
  262.  
  263.     /* Restart timer */
  264.  
  265.     start_timer(&popcli_t);
  266.     return 0;
  267. }
  268.  
  269. /* this is the master state machine that handles a single SMTP transaction */
  270. /* it is called with a queue of jobs for a particular host. */
  271.  
  272. static void
  273. pop_send(unused,cb1,p) 
  274. int unused;
  275. void *cb1;
  276. void *p;
  277. {
  278.     char *cp;
  279.     struct sockaddr_in fsocket;
  280.     struct pop_ccb    *ccb;
  281.     void pop_csm(struct pop_ccb *);
  282.     void quit_session(struct pop_ccb *);
  283.  
  284.     ccb = (struct pop_ccb *)cb1;
  285.     fsocket.sin_family = AF_INET;
  286.     fsocket.sin_addr.s_addr = mailhost;
  287.     fsocket.sin_port = IPPORT_POP;
  288.  
  289.     ccb->socket = socket(AF_INET,SOCK_STREAM,0);
  290.  
  291.     ccb->state = CALL;
  292.  
  293.     if (connect(ccb->socket,(char *)&fsocket,SOCKSIZE) == 0) {
  294.         log(ccb->socket,"Connected to mailhost %s", inet_ntoa(mailhost));
  295.     } else {
  296.         cp = sockerr(ccb->socket);
  297.         log(ccb->socket,"Connect to mailhost %s failed: %s", inet_ntoa(mailhost),
  298.             (cp != NULLCHAR)? cp: "");
  299.     }
  300.  
  301.     while(1) {
  302.         if (recvline(ccb->socket,ccb->buf,BUF_LEN) == -1)
  303.             goto quit;
  304.  
  305.         rip(ccb->buf);
  306.         pop_csm(ccb);
  307.         if (ccb->state == EXIT)
  308.             goto quit;
  309.     }
  310. quit:
  311.     log(ccb->socket,"Connection closed to mailhost %s", inet_ntoa(mailhost));
  312.     (void) close_s(ccb->socket);
  313.     if (fd != NULLFILE)
  314.         fclose(fd);
  315.     delete_ccb();
  316. }
  317.  
  318. /* free the message struct and data */
  319.  
  320. static void
  321. delete_ccb()
  322. {
  323.     if (ccb == NULLCCB)
  324.         return;
  325.  
  326.     free((char *)ccb);
  327.     ccb = NULLCCB;
  328. }
  329.  
  330. /* create a new  pop control block */
  331.  
  332. static struct
  333. pop_ccb *new_ccb()
  334. {
  335.     register struct pop_ccb *ccb;
  336.  
  337.     if ((ccb = (struct pop_ccb *) callocw(1,sizeof(struct pop_ccb))) == NULLCCB)
  338.         return(NULLCCB);
  339.     return(ccb);
  340. }
  341.  
  342. /* ---------------------- pop client code starts here --------------------- */
  343.  
  344. void
  345. pop_csm(ccb)
  346. struct pop_ccb    *ccb;
  347. {
  348.     FILE *mf;
  349.  
  350.     int mlock (char *,char *);
  351.     int rmlock (char * ,char *);
  352.     void quit_session __ARGS((struct pop_ccb *));
  353.     /* int mlock __ARGS((char *dir,char *id));   */
  354.     /* int rmlock __ARGS((char *dir,char *id));   */
  355.  
  356.  
  357.     switch(ccb->state) {
  358.     case CALL:
  359.         if (strncmp(ccb->buf,greeting_rsp,strlen(greeting_rsp)) == 0) {
  360.              (void)usprintf(ccb->socket,login_cmd,username,password);
  361.             ccb->state = NMBR;
  362.         } else
  363.             (void) quit_session(ccb);
  364.         break;
  365.  
  366.     case NMBR:
  367.  
  368.         switch (ccb->buf[0]) {
  369.         case '#':
  370.             if ((fd = fopen(Workfile_name,"a+")) == NULLFILE) {
  371.                 perror("Unable to open work file");
  372.                 quit_session(ccb);
  373.                 return;
  374.             }
  375.  
  376.             fseek(fd,0,SEEK_SET);
  377.             ccb->folder_len = atoi(&(ccb->buf[1]));
  378.             (void)usprintf(ccb->socket,read_cur_cmd);
  379.             ccb->state = SIZE;
  380.             break;
  381.  
  382.         case '+':
  383.  
  384.             /* If there is no mail (the only time we get a "+"
  385.              * response back at this stage of the game),
  386.              * then just close out the connection, because
  387.              * there is nothing more to do!! */
  388.  
  389.         default:
  390.             quit_session(ccb);
  391.             break;
  392.         }
  393.     break;
  394.  
  395.     case SIZE:
  396.         if (ccb->buf[0] == '=') {
  397.             ccb->msg_len = atol(&(ccb->buf[1]));
  398.             if (ccb->msg_len > 0) {
  399.                 (void)usprintf(ccb->socket,retr_cmd);
  400.  
  401.                 ccb->state = XFER;
  402.             } else {
  403.                 log(ccb->socket,"POP client retrieved %d messages",
  404.                         ccb->folder_len);
  405.  
  406.                 /* All done, so do local cleanup */
  407.  
  408.                 if (mlock(Mailspool,mailbox_name)) {
  409.                     tprintf("\n*** Local mailbox locked, new mail in file %s\n",
  410.                          Workfile_name);
  411.                     quit_session(ccb);
  412.                     return;
  413.                 }
  414.  
  415.                 sprintf(mailbox_pathname,"%s/%s.txt",Mailspool,
  416.                     mailbox_name);
  417.                 if ((mf = fopen(mailbox_pathname,"a+")) == NULL) {
  418.                     tprintf("\n*** Unable to open local mailbox, new mail in file %s\n",
  419.                            Workfile_name);
  420.                     quit_session(ccb);
  421.                     return;
  422.                 }
  423.  
  424.                 fseek(fd,0,SEEK_SET);
  425.  
  426.                 while (!feof(fd)) {
  427.                     if(fgets(ccb->buf,BUF_LEN,fd) != NULLCHAR) {
  428.                         fputs(ccb->buf,mf);
  429.                     }
  430.                 }
  431.                 fclose(mf);
  432.                 fclose(fd);
  433.                 fd = NULL;
  434.                 tprintf("New mail arrived for %s from mailhost <%s>%c\n",
  435.                     mailbox_name, inet_ntoa(mailhost),
  436.                     Popquiet ? ' ' : '\007');
  437.                 rmlock(Mailspool,mailbox_name);
  438.                 unlink(Workfile_name);
  439.                 quit_session(ccb);
  440.             }
  441.         } else
  442.             quit_session(ccb);
  443.         break;
  444.  
  445.         case XFER:
  446.             fprintf(fd,"%s\n",ccb->buf);
  447.  
  448.             ccb->msg_len -= (long)(strlen(ccb->buf)+2);    /* Add CRLF */
  449.  
  450.             if (ccb->msg_len > 0)
  451.                 return;
  452.  
  453.             (void)usprintf(ccb->socket,ackd_cmd);
  454.  
  455.             ccb->msg_num++;
  456.             ccb->state = SIZE;
  457.             break;
  458.  
  459.         case EXIT:
  460.             if (fd != NULLFILE)
  461.                 fclose(fd);
  462.             break;
  463.  
  464.         default:
  465.             break;
  466.     }
  467. }
  468.  
  469. void
  470. quit_session(ccb)
  471. struct pop_ccb    *ccb;
  472. {
  473.     (void)usprintf(ccb->socket,quit_cmd);
  474.  
  475.     ccb->state  = EXIT;
  476. }
  477.