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

  1. /* Post Office Protocol (POP3) Client -- RFC1225
  2.  * Copyright 1992 William Allen Simpson
  3.  *      partly based on a NNTP client design by Anders Klemets, SM0RGV
  4.  *      and POP2 Client by Mike Stockett, WA7DYX, et alia.
  5.  *
  6.  *      Support for Mail Index Files, June/July 1993, Johan. K. Reinalda, WG7J
  7.  */
  8. #include <time.h>
  9. #include "global.h"
  10. #ifdef POP3CLIENT
  11. #include "timer.h"
  12. #include "proc.h"
  13. #include "netuser.h"
  14. #include "socket.h"
  15. #include "cmdparse.h"
  16. #include "files.h"
  17. #include "mailcli.h"
  18. #include "mailutil.h"
  19. #include "smtp.h"
  20. #if defined(LZW)
  21. #include "lzw.h"
  22. extern int poplzw;
  23. #endif
  24.   
  25. void
  26. pop3_job(unused,v1,p2)
  27. int unused;
  28. void *v1;
  29. void *p2;
  30. {
  31.     struct mailservers *np = v1;
  32.     struct sockaddr_in fsocket;
  33.     char buf[TLINELEN];
  34.     char *cp;
  35.     FILE *wfp = NULLFILE;
  36.     time_t t;
  37.     int s = -1;
  38.     int bytes;
  39.     int messages;
  40.     int i;
  41. #if defined(LZW)
  42.     int lzwmode, lzwbits;
  43.     extern int16 Lzwbits;
  44.     extern int Lzwmode;
  45. #endif
  46.   
  47.     if ( mailbusy( np ) )
  48.         return;
  49.   
  50.     if ( (fsocket.sin_addr.s_addr = resolve(np->hostname)) == 0L ) {
  51.         /* No IP address found */
  52.         if (Mailtrace >= 1)
  53.             log(-1,"POP3 can't resolve host '%s'", np->hostname);
  54.         start_timer(&np->timer);
  55.         return;
  56.     }
  57.   
  58.     fsocket.sin_family = AF_INET;
  59.     fsocket.sin_port = IPPORT_POP3;
  60.   
  61.     s = socket(AF_INET,SOCK_STREAM,0);
  62.     sockmode(s,SOCK_ASCII);
  63.   
  64.     if (connect(s,(char *)&fsocket,SOCKSIZE) == -1) {
  65.         cp = sockerr(s);
  66.         if (Mailtrace >= 2)
  67.             log(s,"POP3 Connect failed: %s",
  68.             cp != NULLCHAR ? cp : "" );
  69.         goto quit;
  70.     }
  71.   
  72.     log(s,"POP3 Connected to mailhost %s", np->hostname);
  73.   
  74.     /* Eat the banner */
  75.     if ( mailresponse( s, buf, "banner" ) == -1
  76.     || buf[0] == '-' ) {
  77.         goto quit;
  78.     }
  79.   
  80.     usprintf(s,"USER %s\n", np->username);
  81.     if ( mailresponse( s, buf, "USER" ) == -1
  82.     || buf[0] == '-' ) {
  83.         goto quit;
  84.     }
  85.   
  86.     usprintf(s,"PASS %s\n", np->password);
  87.     if ( mailresponse( s, buf, "PASS" ) == -1
  88.     || buf[0] == '-' ) {
  89.         goto quit;
  90.     }
  91.   
  92.     usputs(s,"STAT\n" );
  93.     if ( mailresponse( s, buf, "STAT" ) == -1
  94.     || buf[0] == '-' ) {
  95.         goto quit;
  96.     }
  97.   
  98.     rip(buf);
  99.     if ( Mailtrace >= 1 )
  100.         log(s,"POP3 status %s",buf);
  101.     sscanf(buf,"+OK %d %d",&messages,&bytes);
  102.   
  103.     if ((wfp = tmpfile()) == NULLFILE) {
  104.         if ( Mailtrace >= 1 )
  105.             log(s,"POP3 Cannot create %s", "tmp file" );
  106.         goto quit;
  107.     }
  108.   
  109. #if defined(LZW)
  110.     if (poplzw && messages ) {
  111.         usprintf(s,"XLZW %d %d\n",Lzwbits,Lzwmode);
  112.         if ( mailresponse( s, buf, "XLZW" ) == 0
  113.           && buf[0] != '-' ) {
  114.             lzwmode = lzwbits = 0;
  115.             sscanf(buf,"+OK lzw %d %d",&lzwbits,&lzwmode);
  116.             if(lzwmode != Lzwmode || lzwbits != Lzwbits) {
  117.                 lzwmode = LZWCOMPACT;
  118.                 lzwbits = LZWBITS;
  119.             }
  120.             lzwinit(s,lzwbits,lzwmode);
  121.         }
  122.     }
  123. #endif
  124.  
  125.     for ( i = 0; i++ < messages; ) {
  126.         usprintf(s,"RETR %d\n", i);
  127.         if ( mailresponse( s, buf, "RETR" ) == -1 )
  128.             goto quit;
  129.         if ( buf[0] == '-' ) {
  130.             continue;
  131.         }
  132.   
  133.         time(&t);
  134.         fprintf( wfp, "From POP3@%s %s",
  135.         np->hostname,
  136.         ctime(&t));
  137.         /* Add a 'Received by' line for our system, such that
  138.          * USERLOG and mail-index code works properly - WG7J
  139.          */
  140.         fprintf(wfp,Hdrs[RECEIVED]);
  141.         fprintf(wfp,"by %s (%s) with POP3\n\tid AA%ld ; %s",
  142.         Hostname, shortversion, get_msgid(), ptime(&t));
  143.   
  144.   
  145.         if ( recvmail(s, buf, TLINELEN, wfp, Mailtrace) == -1 ) {
  146.             goto quit;
  147.         }
  148.   
  149.         usprintf(s,"DELE %d\n", i);
  150.         if ( mailresponse( s, buf, "DELE" ) == -1
  151.         || buf[0] == '-' ) {
  152.             goto quit;
  153.         }
  154.     }
  155.   
  156.     if ( messages == 0 ) {
  157.         /* Quit for politeness sake */
  158.         usputs(s,"QUIT\n" );
  159.         mailresponse( s, buf, "QUIT" );
  160.     } else if ( copymail( np->mailbox, buf, TLINELEN, wfp, Mailtrace ) != -1 ) {
  161.         /* Quit command allows the deletions to complete */
  162.         usputs(s,"QUIT\n" );
  163.         mailresponse( s, buf, "QUIT" );
  164.   
  165.         if (Mailtrace)
  166.             tprintf("New mail arrived for %s from mailhost %s%c\n",
  167.                 np->mailbox, np->hostname,
  168.                 Mailquiet ? ' ' : '\007');
  169.     }
  170.   
  171.     quit:
  172.     log(s,"POP3 daemon exiting" );
  173.     close_s(s);
  174.   
  175.     if (wfp != NULLFILE)
  176.         fclose(wfp);
  177.     start_timer(&np->timer);
  178. }
  179.   
  180. #endif
  181.