source: trunk/athena/bin/mitmailrename/mitmailrename.pl @ 22831

Revision 22831, 2.6 KB checked in by tabbott, 16 years ago (diff)
In mitmailrename: * Merged quilt patches into mainline Athena tree
Line 
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
7use strict;
8use warnings FATAL => 'all';
9use Cyrus::IMAP;
10use Getopt::Long;
11
12sub usage(;$);
13sub send_command($);
14sub close_connection();
15sub errorout($);
16
17my $prog = $0;
18
19sub usage(;$) {
20    print STDERR "$prog: $_[0]\n" if ($_[0] && $_[0] ne "help");
21    print STDERR <<EOF;
22Usage: $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
27EOF
28    exit 1;
29}
30
31# Parse the command line arguments.
32use vars qw($opt_debug $opt_host);
33
34GetOptions("debug", "help" => \&usage, "host=s") || usage;
35
36usage "Please specify an existing and new mailbox names" if @ARGV != 2;
37
38my $username = $ENV{'USER'} || getlogin || (getpwuid($<))[0] ||
39    errorout "Cannot determine user name";
40
41unless ($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.
47my $client = Cyrus::IMAP->new($opt_host) ||
48    errorout "Cannot connect to IMAP server on $opt_host";
49unless ($client->authenticate(-authz => $username)) {
50    close_connection();
51    errorout "Cannot authenticate to $opt_host";
52}
53
54# Rename the mailbox.
55send_command "RENAME \"$ARGV[0]\" \"$ARGV[1]\"";
56
57# We are done talking to the IMAP server, close down the connection.
58close_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.
63sub 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.
76sub 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
86sub errorout($) {
87    print STDERR "$prog: $_[0]\n";
88    exit 1;
89}
Note: See TracBrowser for help on using the repository browser.