[22831] | 1 | #!/usr/bin/perl -w |
---|
[20552] | 2 | |
---|
[20593] | 3 | # $Id: mitmailrename.pl,v 1.2 2004-07-29 19:11:53 rbasch Exp $ |
---|
[20552] | 4 | |
---|
| 5 | # Rename 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 close_connection(); |
---|
| 15 | sub errorout($); |
---|
| 16 | |
---|
| 17 | my $prog = $0; |
---|
| 18 | |
---|
| 19 | sub usage(;$) { |
---|
| 20 | print STDERR "$prog: $_[0]\n" if ($_[0] && $_[0] ne "help"); |
---|
| 21 | print STDERR <<EOF; |
---|
| 22 | Usage: $prog [<options>] <existing-mailbox> <new-mailbox> |
---|
| 23 | Options: |
---|
| 24 | --debug turn on debugging |
---|
| 25 | --help print this usage information |
---|
| 26 | --host=<name> query host <name> instead of default POBOX server |
---|
| 27 | EOF |
---|
| 28 | exit 1; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | # Parse the command line arguments. |
---|
| 32 | use vars qw($opt_debug $opt_host); |
---|
| 33 | |
---|
| 34 | GetOptions("debug", "help" => \&usage, "host=s") || usage; |
---|
| 35 | |
---|
| 36 | usage "Please specify an existing and new mailbox names" if @ARGV != 2; |
---|
| 37 | |
---|
[23700] | 38 | my $username = $ENV{'ATHENA_USER'} || $ENV{'USER'} || getlogin || (getpwuid($<))[0] || |
---|
[20552] | 39 | errorout "Cannot determine user name"; |
---|
| 40 | |
---|
| 41 | unless ($opt_host) { |
---|
| 42 | $opt_host = (split(" ", `hesinfo $username pobox`))[1] || |
---|
| 43 | errorout "Cannot find Post Office server for $username"; |
---|
| 44 | } |
---|
[23913] | 45 | errorout "Exchange accounts are not supported yet. Try http://owa.mit.edu/." if $opt_host =~ /EXCHANGE/; |
---|
[20552] | 46 | |
---|
| 47 | # Connect to the IMAP server, and authenticate. |
---|
| 48 | my $client = Cyrus::IMAP->new($opt_host) || |
---|
| 49 | errorout "Cannot connect to IMAP server on $opt_host"; |
---|
| 50 | unless ($client->authenticate(-authz => $username)) { |
---|
| 51 | close_connection(); |
---|
| 52 | errorout "Cannot authenticate to $opt_host"; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | # Rename the mailbox. |
---|
| 56 | send_command "RENAME \"$ARGV[0]\" \"$ARGV[1]\""; |
---|
| 57 | |
---|
| 58 | # We are done talking to the IMAP server, close down the connection. |
---|
| 59 | close_connection(); |
---|
| 60 | |
---|
| 61 | # Subroutine to send a command to the IMAP server, and wait for the |
---|
| 62 | # response; any defined callbacks for the response are invoked. |
---|
| 63 | # If the server response indicates failure, we error out. |
---|
| 64 | sub send_command($) { |
---|
| 65 | print "Sending: $_[0]\n" if $opt_debug; |
---|
| 66 | my ($status, $text) = $client->send('', '', $_[0]); |
---|
| 67 | print "Response: status $status, text $text\n" if $opt_debug; |
---|
| 68 | errorout "Premature end-of-file on IMAP connection to $opt_host" |
---|
| 69 | if $status eq 'EOF'; |
---|
| 70 | if ($status ne 'OK') { |
---|
| 71 | close_connection(); |
---|
| 72 | errorout "IMAP error on $opt_host: $text" |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | # Logout from the IMAP server, and close the connection. |
---|
| 77 | sub close_connection() { |
---|
| 78 | $client->send('', '', "LOGOUT"); |
---|
[20593] | 79 | # Set the client reference to undef, so that perl invokes the |
---|
| 80 | # destructor, which closes the connection. Note that if we invoke |
---|
| 81 | # the destructor explicitly here, then perl will still invoke it |
---|
| 82 | # again when the program exits, thus touching memory which has |
---|
| 83 | # already been freed. |
---|
| 84 | $client = undef; |
---|
[20552] | 85 | } |
---|
| 86 | |
---|
| 87 | sub errorout($) { |
---|
| 88 | print STDERR "$prog: $_[0]\n"; |
---|
| 89 | exit 1; |
---|
| 90 | } |
---|