Add %age area column to vacancies section.
[capital-apms-progress.git] / rplctn / download.pl
blob750352ebc2c4b8676128b14e57ec1d6606fcaf23
1 #! /perl/bin/perl -w
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 #---------------------------------------------------------------------------
18 use 5;
20 use Net::POP3; # Talk nicely to a POP server
21 use strict;
23 require "recvconf.ph"; # Configuration of Receiver
25 #---------------------------------------------------------------------------
27 # Mainline
29 my $seq = 0;
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);
39 if (! defined($pop))
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
50 my $i;
51 my $problems = 0;
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++;
61 my $filename;
62 do # Scan for a "unique" filename.
64 $seq++;
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.
81 if (close(MSG))
82 { $pop->delete($i) ||
83 warn "Error encountered marking message $i for deletion\n";
85 else
86 { warn "Errors closing file ($filename), message not deleted.\n";
87 $problems++
90 else
92 warn "Eeeek! Failed to write message $i to $filename\n";
93 $problems++;
96 else
98 warn "Problems downloading message $i -- skipping\n";
99 close(MSG);
100 unlink("$RecvConf::downloaded/$filename") ||
101 warn "Unable to remove partial message: $filename\n";
104 else
106 warn "Unable to open file to save message $i -- skipping ($filename)\n";
110 if ($problems)
111 { # Reset connection to try to leave messages on server.
112 warn "Problems during download: resetting connection.\n";
113 $pop -> reset();
116 # Leave nicely
117 $pop->quit();
119 if ($msgs > 0)
120 { exit $msgs; }
121 else
122 { exit 0; }