1 | #!/usr/bin/perl -w |
---|
2 | |
---|
3 | # $Id: mitmailrename.pl,v 1.2 2004-07-29 19:11:53 rbasch Exp $ |
---|
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 | |
---|
38 | my $username = $ENV{'ATHENA_USER'} || $ENV{'USER'} || getlogin || (getpwuid($<))[0] || |
---|
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 | } |
---|
45 | |
---|
46 | # Connect to the IMAP server, and authenticate. |
---|
47 | my $client = Cyrus::IMAP->new($opt_host) || |
---|
48 | errorout "Cannot connect to IMAP server on $opt_host"; |
---|
49 | unless ($client->authenticate(-authz => $username)) { |
---|
50 | close_connection(); |
---|
51 | errorout "Cannot authenticate to $opt_host"; |
---|
52 | } |
---|
53 | |
---|
54 | # Rename the mailbox. |
---|
55 | send_command "RENAME \"$ARGV[0]\" \"$ARGV[1]\""; |
---|
56 | |
---|
57 | # We are done talking to the IMAP server, close down the connection. |
---|
58 | close_connection(); |
---|
59 | |
---|
60 | # Subroutine to send a command to the IMAP server, and wait for the |
---|
61 | # response; any defined callbacks for the response are invoked. |
---|
62 | # If the server response indicates failure, we error out. |
---|
63 | sub send_command($) { |
---|
64 | print "Sending: $_[0]\n" if $opt_debug; |
---|
65 | my ($status, $text) = $client->send('', '', $_[0]); |
---|
66 | print "Response: status $status, text $text\n" if $opt_debug; |
---|
67 | errorout "Premature end-of-file on IMAP connection to $opt_host" |
---|
68 | if $status eq 'EOF'; |
---|
69 | if ($status ne 'OK') { |
---|
70 | close_connection(); |
---|
71 | errorout "IMAP error on $opt_host: $text" |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | # Logout from the IMAP server, and close the connection. |
---|
76 | sub close_connection() { |
---|
77 | $client->send('', '', "LOGOUT"); |
---|
78 | # Set the client reference to undef, so that perl invokes the |
---|
79 | # destructor, which closes the connection. Note that if we invoke |
---|
80 | # the destructor explicitly here, then perl will still invoke it |
---|
81 | # again when the program exits, thus touching memory which has |
---|
82 | # already been freed. |
---|
83 | $client = undef; |
---|
84 | } |
---|
85 | |
---|
86 | sub errorout($) { |
---|
87 | print STDERR "$prog: $_[0]\n"; |
---|
88 | exit 1; |
---|
89 | } |
---|