--- pop3mail.pl.orig	Sun May 25 18:42:52 2003
+++ pop3mail.pl	Sun May 25 18:41:55 2003
@@ -48,16 +48,20 @@
    $remote_sock->autoflush(1);
    $_=<$remote_sock>;
    return -12 if (/^\-/);		# server not ready
+log_time("connect $pop3host");
 
    # try if server supports auth login(base64 encoding) first
    print $remote_sock "auth login\r\n";
    $_=<$remote_sock>;
    if (/^\+/) {
+log_time("support auth login");
       print $remote_sock &encode_base64($pop3user);
       $_=<$remote_sock>;
       (close($remote_sock) && return -13) if (/^\-/);		# username error
       print $remote_sock &encode_base64($pop3passwd);
       $_=<$remote_sock>;
+   } else {
+log_time("not support auth login");
    }
 
    if (! /^\+/) {	# not supporting auth login or auth login failed
@@ -83,27 +87,34 @@
       close($remote_sock);
       return 0;
    }
+log_time("$mailcount msg on server");
 
    # use 'uidl' to find the msg being retrieved last time
    print $remote_sock "uidl 1\r\n";
    $_ = <$remote_sock>;
 
    if (/^\-/) {	# pop3d not support uidl, try last command
+log_time("not support uidl");
       # use 'last' to find the msg being retrieved last time
       print $remote_sock "last\r\n";
       $_ = <$remote_sock>; s/^\s+//;
       if (/^\+/) { # server does support last
+log_time("support last");
          $last=(split(/\s/))[1];		# +OK N
+log_time("last = $last");
          if ($last eq $mailcount) {
+log_time("quit, because last = $mailcount");
             print $remote_sock "quit\r\n";
             close($remote_sock);
             return 0;
          }
       } else {
+log_time("quit, because not support uidl or last");
          return -16;	# both uid and last not supported
       }
 
    } else {	# pop3d does support uidl
+log_time("support uidl");
       $uidl_support=1;
       if (/^\+/) {
          $uidl_field=2;
@@ -112,11 +123,13 @@
       }
       if (!$config{'dbmopen_haslock'}) {
          if (!filelock("$uidldb$config{'dbm_ext'}", LOCK_EX|LOCK_NB)) {
+log_time("uidl db lock error");
             close($remote_sock);
             return -1;
          }
       }
       if (!dbmopen(%UIDLDB, "$uidldb$config{'dbmopen_ext'}", 0600)) {
+log_time("uidl db open error");
          filelock("$uidldb$config{'dbm_ext'}", LOCK_UN) if (!$config{'dbmopen_haslock'});
          close($remote_sock);
          return -2;
@@ -124,6 +137,7 @@
    }
 
    # retr messages
+log_time("retr loop start from msg ", $last+1);
    for (my $i=$last+1; $i<=$mailcount; $i++) {
       my ($msgcontent, $msgfrom, $msgdate)=("", "", "");
 
@@ -132,15 +146,18 @@
          $_ = <$remote_sock>;
          $uidl=(split(/\s/))[$uidl_field];
          if ( defined($UIDLDB{$uidl}) ) {		# already fetched before
+log_time("skip msg $i because uidl($uidl) found in uidldb");
             $uidldb{$uidl}=1; next;
          }
       }
           
       print $remote_sock "retr ".$i."\r\n";
+log_time("retr msg $i");
       while (<$remote_sock>) {	# use loop to filter out verbose output
          if ( /^\+/ ) {
             next;
          } elsif (/^\-/) {
+log_time("quit, because msg $i retr error");
             if ($uidl_support) {
                @UIDLDB{keys %uidldb}=values %uidldb if ($retr_total>0);
                dbmclose(%UIDLDB);
@@ -187,6 +204,7 @@
             $msgdate=$1;
          }
       }
+log_time("msg $i, $_") if (/^Subject:\s/i);
 
       my $dateserial=datefield2dateserial($msgdate);
       my $gmserial=gmtime2dateserial();
@@ -204,6 +222,7 @@
       my $append=0;;
       if (filelock($spoolfile, LOCK_EX)) {
          if (open(IN,">>$spoolfile")) {
+log_time("append msg $i to spool");
             print IN "From $msgfrom $msgdate\n";
             print IN $msgcontent;
             print IN "\n";		# mark mail end
@@ -213,6 +232,7 @@
          filelock($spoolfile, LOCK_UN);
       } 
       if (!$append) {
+log_time("quit, because spool write error for msg $i");
          if ($uidl_support) {
             @UIDLDB{keys %uidldb}=values %uidldb if ($retr_total>0);
             dbmclose(%UIDLDB);
@@ -223,9 +243,11 @@
       }
 
       if ($pop3del == 1) {
+log_time("del msg $i") if (!/^\+/);
          print $remote_sock "dele $i\r\n";
          $_=<$remote_sock>;
          $uidldb{$uidl}=1 if ($uidl_support && !/^\+/);
+log_time("del msg $i on pop3 failed") if (!/^\+/);
       } else {
          $uidldb{$uidl}=1 if ($uidl_support);
       }
@@ -239,6 +261,9 @@
       %UIDLDB=%uidldb if ($retr_total>0);
       dbmclose(%UIDLDB);
       filelock("$uidldb$config{'dbm_ext'}", LOCK_UN) if (!$config{'dbmopen_haslock'});
+if ($retr_total>0) {
+   log_time("rewrite uidldb with:\n".join("\n", sort keys %uidldb));
+}
    }
 
    # return number of fetched mail
