[22829] | 1 | #!/usr/bin/perl -w |
---|
[20055] | 2 | |
---|
[20593] | 3 | # $Id: mitmailexp.pl,v 1.3 2004-07-29 19:11:53 rbasch Exp $ |
---|
[20055] | 4 | |
---|
| 5 | # Expunge an IMAP folder. |
---|
| 6 | |
---|
| 7 | use strict; |
---|
| 8 | use warnings FATAL => 'all'; |
---|
| 9 | use Cyrus::IMAP; |
---|
| 10 | use Getopt::Long; |
---|
| 11 | |
---|
| 12 | sub usage(;$); |
---|
| 13 | sub send_command($); |
---|
| 14 | sub fetch_callback(@); |
---|
| 15 | sub number_callback(@); |
---|
| 16 | sub make_msgspecs(@); |
---|
| 17 | sub close_connection(); |
---|
| 18 | sub errorout($); |
---|
| 19 | |
---|
| 20 | my $prog = $0; |
---|
| 21 | |
---|
| 22 | sub usage(;$) { |
---|
| 23 | print STDERR "$prog: $_[0]\n" if ($_[0] && $_[0] ne "help"); |
---|
| 24 | print STDERR <<EOF; |
---|
| 25 | Usage: $prog [<options>] |
---|
| 26 | Options: |
---|
| 27 | --debug turn on debugging |
---|
| 28 | --help print this usage information |
---|
| 29 | --host=<name> query host <name> instead of default POBOX server |
---|
| 30 | --mailbox=<name> examine mailbox <name> instead of INBOX |
---|
| 31 | EOF |
---|
| 32 | exit 1; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | # Parse the command line arguments. |
---|
| 36 | use vars qw($opt_debug $opt_host $opt_mailbox); |
---|
| 37 | |
---|
| 38 | GetOptions("debug", |
---|
| 39 | "help" => \&usage, |
---|
| 40 | "host=s", |
---|
| 41 | "mailbox=s") || usage; |
---|
| 42 | |
---|
| 43 | usage "Too many arguments" if @ARGV > 0; |
---|
| 44 | |
---|
| 45 | $opt_mailbox = 'INBOX' unless $opt_mailbox; |
---|
| 46 | |
---|
[23698] | 47 | my $username = $ENV{'ATHENA_USER'} || $ENV{'USER'} || getlogin || (getpwuid($<))[0] || |
---|
[20055] | 48 | errorout "Cannot determine user name"; |
---|
| 49 | |
---|
| 50 | unless ($opt_host) { |
---|
[24300] | 51 | $opt_host = (gethostbyname("$username.mail.mit.edu"))[0]; |
---|
| 52 | errorout "Cannot find Post Office server for $username" unless $opt_host; |
---|
[20055] | 53 | } |
---|
[23913] | 54 | errorout "Exchange accounts are not supported yet. Try http://owa.mit.edu/." if $opt_host =~ /EXCHANGE/; |
---|
[20055] | 55 | |
---|
| 56 | # Connect to the IMAP server, and authenticate. |
---|
| 57 | my $client = Cyrus::IMAP->new($opt_host) || |
---|
| 58 | errorout "Cannot connect to IMAP server on $opt_host"; |
---|
[24300] | 59 | unless ($client->authenticate(-authz => $username, -maxssf => 0)) { |
---|
[20055] | 60 | close_connection(); |
---|
| 61 | errorout "Cannot authenticate to $opt_host"; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | # Access the mailbox. Make sure we have read-write access. |
---|
[20505] | 65 | send_command "SELECT \"$opt_mailbox\""; |
---|
[20055] | 66 | |
---|
| 67 | # Close the mailbox, thereby expunging deleted messages. |
---|
| 68 | send_command "CLOSE"; |
---|
| 69 | |
---|
| 70 | # We are done talking to the IMAP server, close down the connection. |
---|
| 71 | close_connection(); |
---|
| 72 | |
---|
| 73 | # Subroutine to send a command to the IMAP server, and wait for the |
---|
| 74 | # response; any defined callbacks for the response are invoked. |
---|
| 75 | # If the server response indicates failure, we error out. |
---|
| 76 | sub send_command($) { |
---|
| 77 | print "Sending: $_[0]\n" if $opt_debug; |
---|
| 78 | my ($status, $text) = $client->send('', '', $_[0]); |
---|
| 79 | print "Response: status $status, text $text\n" if $opt_debug; |
---|
| 80 | errorout "Premature end-of-file on IMAP connection to $opt_host" |
---|
| 81 | if $status eq 'EOF'; |
---|
| 82 | if ($status ne 'OK') { |
---|
| 83 | close_connection(); |
---|
| 84 | errorout "IMAP error for $opt_mailbox on $opt_host: $text" |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | # Logout from the IMAP server, and close the connection. |
---|
| 89 | sub close_connection() { |
---|
| 90 | $client->send('', '', "LOGOUT"); |
---|
[20593] | 91 | # Set the client reference to undef, so that perl invokes the |
---|
| 92 | # destructor, which closes the connection. Note that if we invoke |
---|
| 93 | # the destructor explicitly here, then perl will still invoke it |
---|
| 94 | # again when the program exits, thus touching memory which has |
---|
| 95 | # already been freed. |
---|
| 96 | $client = undef; |
---|
[20055] | 97 | } |
---|
| 98 | |
---|
| 99 | sub errorout($) { |
---|
| 100 | print STDERR "$prog: $_[0]\n"; |
---|
| 101 | exit 1; |
---|
| 102 | } |
---|