3 # Downloader: download RFC822 format messages from a POP server.
5 # Written by Ewen McNeill, 19/11/1997.
7 # All messages at the POP server are downloaded and stored in files
8 # named by date, time, and sequence number (intended to give a unique
9 # filename). WARNING: filenames will be longer than 8.3.
11 # As each message is successfully downloaded it is marked for deletion.
12 # If all messages are successfully downloaded a QUIT is done, which will
13 # delete all the messages marked for deletion; otherwise the connection
14 # is reset (leaving all messages intact).
16 #---------------------------------------------------------------------------
20 use Net
::POP3
; # Talk nicely to a POP server
23 require "recvconf.ph"; # Configuration of Receiver
25 #---------------------------------------------------------------------------
31 die "No inbound directory" if (! defined($RecvConf::downloaded
));
32 die "No POP server" if (! defined($RecvConf::POPserver
));
33 die "No POP user" if (! defined($RecvConf::login
));
34 die "No POP password" if (! defined($RecvConf::password
));
36 # Connect to the POP server
37 my $pop = Net
::POP3
->new($RecvConf::POPserver
);
41 die "Unable to establish connection to POP server \"$RecvConf::POPserver\": $!\n";
44 # Login to the POP server
46 my $msgs = $pop -> login
($RecvConf::login
, $RecvConf::password
);
48 # Cycle through the messages (if any) downloading them
52 for ($i = 1; $i <= $msgs; $i++)
54 # Figure out a filename to save this message in.
56 # WARNING: "long" filename; WARNING: race condition!
58 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
59 $year += 1900; $mon++;
62 do # Scan for a "unique" filename.
65 $filename = sprintf("%04d-%02d-%02d-%02d-%02d-%03d.msg",
66 $year, $mon, $mday, $hour, $min, $seq);
67 } while (-r
"$RecvConf::downloaded/$filename" ||
68 -f
"$RecvConf::downloaded/$filename" ||
69 -d
"$RecvConf::downloaded/$filename");
71 if (open(MSG
, ">$RecvConf::downloaded/$filename"))
73 # Grab a reference to the whole message.
74 my $messageref = $pop->get($i);
76 # And save it out to our file.
77 if (defined($messageref))
79 if (print MSG @
$messageref)
80 { # Message saved away, we can mark it for deletion.
83 warn "Error encountered marking message $i for deletion\n";
86 { warn "Errors closing file ($filename), message not deleted.\n";
92 warn "Eeeek! Failed to write message $i to $filename\n";
98 warn "Problems downloading message $i -- skipping\n";
100 unlink("$RecvConf::downloaded/$filename") ||
101 warn "Unable to remove partial message: $filename\n";
106 warn "Unable to open file to save message $i -- skipping ($filename)\n";
111 { # Reset connection to try to leave messages on server.
112 warn "Problems during download: resetting connection.\n";