/* Post Office Protocol (POP3) Client -- RFC1225
 * Copyright 1992 William Allen Simpson
 *      partly based on a NNTP client design by Anders Klemets, SM0RGV
 *      and POP2 Client by Mike Stockett, WA7DYX, et alia.
 *
 *      Support for Mail Index Files, June/July 1993, Johan. K. Reinalda, WG7J
 */
#include <time.h>
#include "global.h"
#ifdef POP3CLIENT
#include "timer.h"
#include "proc.h"
#include "netuser.h"
#include "socket.h"
#include "cmdparse.h"
#include "files.h"
#include "mailcli.h"
#include "mailutil.h"
#include "smtp.h"
#if defined(LZW)
#include "lzw.h"
extern int poplzw;
#endif
  
void
pop3_job(unused,v1,p2)
int unused;
void *v1;
void *p2;
{
    struct mailservers *np = v1;
    struct sockaddr_in fsocket;
    char buf[TLINELEN];
    char *cp;
    FILE *wfp = NULLFILE;
    time_t t;
    int s = -1;
    int bytes;
    int messages;
    int i;
#if defined(LZW)
    int lzwmode, lzwbits;
    extern int16 Lzwbits;
    extern int Lzwmode;
#endif
  
    if ( mailbusy( np ) )
        return;
  
    if ( (fsocket.sin_addr.s_addr = resolve(np->hostname)) == 0L ) {
        /* No IP address found */
        if (Mailtrace >= 1)
            log(-1,"POP3 can't resolve host '%s'", np->hostname);
        start_timer(&np->timer);
        return;
    }
  
    fsocket.sin_family = AF_INET;
    fsocket.sin_port = IPPORT_POP3;
  
    s = socket(AF_INET,SOCK_STREAM,0);
    sockmode(s,SOCK_ASCII);
  
    if (connect(s,(char *)&fsocket,SOCKSIZE) == -1) {
        cp = sockerr(s);
        if (Mailtrace >= 2)
            log(s,"POP3 Connect failed: %s",
            cp != NULLCHAR ? cp : "" );
        goto quit;
    }
  
    log(s,"POP3 Connected to mailhost %s", np->hostname);
  
    /* Eat the banner */
    if ( mailresponse( s, buf, "banner" ) == -1
    || buf[0] == '-' ) {
        goto quit;
    }
  
    usprintf(s,"USER %s\n", np->username);
    if ( mailresponse( s, buf, "USER" ) == -1
    || buf[0] == '-' ) {
        goto quit;
    }
  
    usprintf(s,"PASS %s\n", np->password);
    if ( mailresponse( s, buf, "PASS" ) == -1
    || buf[0] == '-' ) {
        goto quit;
    }
  
    usputs(s,"STAT\n" );
    if ( mailresponse( s, buf, "STAT" ) == -1
    || buf[0] == '-' ) {
        goto quit;
    }
  
    rip(buf);
    if ( Mailtrace >= 1 )
        log(s,"POP3 status %s",buf);
    sscanf(buf,"+OK %d %d",&messages,&bytes);
  
    if ((wfp = tmpfile()) == NULLFILE) {
        if ( Mailtrace >= 1 )
            log(s,"POP3 Cannot create %s", "tmp file" );
        goto quit;
    }
  
#if defined(LZW)
    if (poplzw && messages ) {
        usprintf(s,"XLZW %d %d\n",Lzwbits,Lzwmode);
        if ( mailresponse( s, buf, "XLZW" ) == 0
          && buf[0] != '-' ) {
            lzwmode = lzwbits = 0;
            sscanf(buf,"+OK lzw %d %d",&lzwbits,&lzwmode);
            if(lzwmode != Lzwmode || lzwbits != Lzwbits) {
                lzwmode = LZWCOMPACT;
                lzwbits = LZWBITS;
            }
            lzwinit(s,lzwbits,lzwmode);
        }
    }
#endif

    for ( i = 0; i++ < messages; ) {
        usprintf(s,"RETR %d\n", i);
        if ( mailresponse( s, buf, "RETR" ) == -1 )
            goto quit;
        if ( buf[0] == '-' ) {
            continue;
        }
  
        time(&t);
        fprintf( wfp, "From POP3@%s %s",
        np->hostname,
        ctime(&t));
        /* Add a 'Received by' line for our system, such that
         * USERLOG and mail-index code works properly - WG7J
         */
        fprintf(wfp,Hdrs[RECEIVED]);
        fprintf(wfp,"by %s (%s) with POP3\n\tid AA%ld ; %s",
        Hostname, shortversion, get_msgid(), ptime(&t));
  
  
        if ( recvmail(s, buf, TLINELEN, wfp, Mailtrace) == -1 ) {
            goto quit;
        }
  
        usprintf(s,"DELE %d\n", i);
        if ( mailresponse( s, buf, "DELE" ) == -1
        || buf[0] == '-' ) {
            goto quit;
        }
    }
  
    if ( messages == 0 ) {
        /* Quit for politeness sake */
        usputs(s,"QUIT\n" );
        mailresponse( s, buf, "QUIT" );
    } else if ( copymail( np->mailbox, buf, TLINELEN, wfp, Mailtrace ) != -1 ) {
        /* Quit command allows the deletions to complete */
        usputs(s,"QUIT\n" );
        mailresponse( s, buf, "QUIT" );
  
        if (Mailtrace)
            tprintf("New mail arrived for %s from mailhost %s%c\n",
                np->mailbox, np->hostname,
                Mailquiet ? ' ' : '\007');
    }
  
    quit:
    log(s,"POP3 daemon exiting" );
    close_s(s);
  
    if (wfp != NULLFILE)
        fclose(wfp);
    start_timer(&np->timer);
}
  
#endif
