home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / POP2CLI.C < prev    next >
C/C++ Source or Header  |  1994-08-26  |  5KB  |  178 lines

  1. /* POP2 Client routines -- Not recommended.
  2.  *      Jan 92  William Allen Simpson
  3.  *              complete re-write to match new mailreader commands
  4.  *
  5.  *      partly based on a NNTP client design by Anders Klemets, SM0RGV
  6.  *      Originally authored by Mike Stockett (WA7DYX).
  7.  *
  8.  * History:
  9.  *      Support for Mail Index Files, June/July 1993, Johan. K. Reinalda, WG7J
  10.  *      Modified 12 May 1991 by Mark Edwards (WA6SMN) to use new timer
  11.  *      facilities in NOS0423.  Fixed type mismatches spotted by C++.
  12.  *      Modified 27 May 1990 by Allen Gwinn (N5CKP) for compatibility
  13.  *        with later releases (NOS0522).
  14.  *      Added into NOS by PA0GRI (and linted into "standard" C)
  15.  *      Modified 14 June 1987 by P. Karn for symbolic target addresses,
  16.  *        also rebuilt locking mechanism
  17.  *
  18.  *      Some code culled from previous releases of SMTP.
  19.  *      See that code for applicable copyright notices.
  20.  */
  21. #include <time.h>
  22. #include "global.h"
  23. #ifdef POP2CLIENT
  24. #include "timer.h"
  25. #include "proc.h"
  26. #include "netuser.h"
  27. #include "socket.h"
  28. #include "cmdparse.h"
  29. #include "files.h"
  30. #include "mailcli.h"
  31. #include "mailutil.h"
  32. #include "smtp.h"
  33.   
  34. extern char Badhost[];
  35.   
  36. /* Response string keys */
  37. static char *greeting_rsp  = "+ POP2 ";
  38.   
  39. void
  40. pop2_job(unused,v1,p2)
  41. int unused;
  42. void *v1;
  43. void *p2;
  44. {
  45.     struct mailservers *np = v1;
  46.     struct sockaddr_in fsocket;
  47.     char buf[TLINELEN];
  48.     char *cp;
  49.     FILE *wfp = NULLFILE;
  50.     int s = -1;
  51.     int folder_len;
  52.     int msg_num = 0;
  53.     time_t t;
  54.   
  55.     if ( mailbusy( np ) )
  56.         return;
  57.   
  58.     if ( (fsocket.sin_addr.s_addr = resolve(np->hostname)) == 0L ) {
  59.         /* No IP address found */
  60.         if (Mailtrace >= 1)
  61.             log(-1,"POP2 can't resolve host '%s'", np->hostname);
  62.         start_timer(&np->timer);
  63.         return;
  64.     }
  65.   
  66.     fsocket.sin_family = AF_INET;
  67.     fsocket.sin_port = IPPORT_POP2;
  68.   
  69.     s = socket(AF_INET,SOCK_STREAM,0);
  70.     sockmode(s,SOCK_ASCII);
  71.   
  72.     if (connect(s,(char *)&fsocket,SOCKSIZE) == -1) {
  73.         cp = sockerr(s);
  74.         if (Mailtrace >= 2)
  75.             log(s,"POP2 Connect failed: %s",
  76.             cp != NULLCHAR ? cp : "");
  77.         goto quit;
  78.     }
  79.   
  80.     log(s,"POP2 Connected to mailhost %s", np->hostname);
  81.   
  82.     if ( mailresponse( s, buf, "banner" ) == -1)
  83.         goto quit;
  84.   
  85.     if (strncmp(buf,greeting_rsp,strlen(greeting_rsp)) != 0)
  86.         goto quitcmd;
  87.   
  88.     (void)usprintf(s,"HELO %s %s\n",np->username,np->password);
  89.   
  90.     if ( mailresponse( s, buf, "HELO" ) == -1)
  91.         goto quit;
  92.   
  93.     if (buf[0] != '#'
  94.     || (folder_len = atoi(&(buf[1]))) == 0) {
  95.         /* If there is no mail (the only time we get a "+"
  96.          * response back at this stage of the game),
  97.          * then just close out the connection, because
  98.          * there is nothing more to do!! */
  99.         goto quitcmd;
  100.     }
  101.   
  102.     if ((wfp = tmpfile()) == NULLFILE) {
  103.         if ( Mailtrace >= 1 )
  104.             log(s,"POP2 Cannot create %s", "tmp file" );
  105.         goto quitcmd;
  106.     }
  107.   
  108.     (void)usputs(s,"READ\n");
  109.   
  110.     /* now, get the text */
  111.     while(TRUE) {
  112.         long msg_len;
  113.   
  114.         if ( mailresponse( s, buf, "read loop" ) == -1)
  115.             goto quit;
  116.   
  117.         if (buf[0] == '='
  118.         && (msg_len = atol(&(buf[1]))) > 0) {
  119.             (void)usputs(s,"RETR\n");
  120.   
  121.             /* for each message, add a 'Received by' line for our system.
  122.              * Thus USERLOG and mail-index code works properly - WG7J
  123.              */
  124.             time(&t);
  125.             fprintf( wfp, "From POP2@%s %s", np->hostname, ctime(&t));
  126.             fprintf( wfp, Hdrs[RECEIVED]);
  127.             fprintf( wfp, "by %s (%s) with POP2\n\tid AA%ld ; %s",
  128.             Hostname, shortversion, get_msgid(), ptime(&t));
  129.   
  130.   
  131.             while ( msg_len > 0 ) {
  132.                 if (recvline(s,buf,TLINELEN) == -1)
  133.                     goto quit;
  134.   
  135.                 rip(buf);
  136.   
  137.                 if(!strncmp(buf,"From ",5))
  138.                     fputc('>',wfp);
  139.   
  140.                 fprintf(wfp,"%s\n",buf);
  141.   
  142.                 msg_len -= (long)(strlen(buf)+2);/* Add CRLF */
  143.             }
  144.             (void)usputs(s,"ACKD\n");
  145.   
  146.             msg_num++;
  147.         } else {
  148.             break;
  149.         }
  150.     }
  151.   
  152.     if ( folder_len > 0 ) {
  153.         /* testing the result is pointless,
  154.          * since POP2 already deleted mail.
  155.          */
  156.         copymail( np->mailbox, buf, TLINELEN, wfp, Mailtrace );
  157.   
  158.         if (Mailtrace)
  159.             tprintf("New mail arrived for %s from mailhost %s%c\n",
  160.                 np->mailbox, np->hostname,
  161.                 Mailquiet ? ' ' : '\007');
  162.     }
  163.   
  164.     quitcmd:
  165.     (void)usputs(s,"QUIT\n");
  166.   
  167.     quit:
  168.     log(s,"POP2 daemon exiting");
  169.     (void) close_s(s);
  170.   
  171.     if (wfp != NULLFILE)
  172.         fclose(wfp);
  173.     start_timer(&np->timer);
  174. }
  175.   
  176. #endif
  177.   
  178.