source: trunk/athena/bin/mitmailappend/mitmailappend.pl @ 22826

Revision 22826, 4.2 KB checked in by tabbott, 16 years ago (diff)
In mitmailappend: * Merged quilt patches into mainline Athena tree
Line 
1#!/usr/bin/perl -w
2
3# $Id: mitmailappend.pl,v 1.1 2004-09-03 20:44:43 rbasch Exp $
4
5# Append a message to an IMAP folder.
6
7use strict;
8use warnings FATAL => 'all';
9use Cyrus::IMAP;
10use Getopt::Long;
11
12sub usage(;$);
13sub read_file($);
14sub send_command(@);
15sub close_and_errorout($);
16sub close_connection();
17sub errorout($);
18
19my $prog = $0;
20
21sub usage(;$) {
22    print STDERR "$prog: $_[0]\n" if ($_[0] && $_[0] ne "help");
23    print STDERR <<EOF;
24Usage: $prog [<options>]
25  Options:
26    --debug                turn on debugging
27    --file=<path>          read message from <path> instead of standard input
28    --help                 print this usage information
29    --host=<name>          query host <name> instead of default POBOX server
30    --mailbox=<name>       access mailbox <name> instead of INBOX
31    --no-create            do not create the target mailbox automatically
32EOF
33    exit 1;
34}
35
36# Parse the command line arguments.
37use vars qw($opt_debug $opt_file $opt_host $opt_mailbox $opt_no_create);
38
39GetOptions("debug",
40           "file=s",
41           "help" => \&usage,
42           "host=s",
43           "mailbox=s",
44           "no-create") || usage;
45
46usage unless @ARGV == 0;
47
48$opt_mailbox = "INBOX" unless $opt_mailbox;
49
50# By default we read the message from standard input.
51$opt_file = "-" unless $opt_file;
52
53my $username = $ENV{'USER'} || getlogin || (getpwuid($<))[0] ||
54    errorout "Cannot determine user name";
55
56unless ($opt_host) {
57    $opt_host = (split(" ", `hesinfo $username pobox`))[1] ||
58        errorout "Cannot find Post Office server for $username";
59}
60
61# Read the entire message file into a string.
62my $msg = read_file($opt_file);
63
64# Convert LF -> CRLF if necessary.
65unless ($msg =~ m/\r\n/os) {
66    print "Converting LF to CRLF...\n" if $opt_debug;
67    $msg =~ s/\n/\r\n/gos;
68}
69
70# Connect to the IMAP server, and authenticate.
71my $client = Cyrus::IMAP->new($opt_host) ||
72    errorout "Cannot connect to IMAP server on $opt_host";
73$client->authenticate(-authz => $username) ||
74    close_and_errorout "Cannot authenticate to $opt_host";
75
76# Try the APPEND command.  If the server returns an error,
77# check for "TRYCREATE" in the response text; this is a hint that
78# the target mailbox does not exist, but that it can be created.
79my ($status, $text) = send_command("APPEND %s %s", $opt_mailbox, $msg);
80if ($status ne 'OK') {
81    if ($text =~ m/\bTRYCREATE\b/io) {
82        close_and_errorout "Mailbox $opt_mailbox does not exist"
83            if $opt_no_create;
84        print "Creating $opt_mailbox\n" if $opt_debug;
85        # send_command will error out if the CREATE fails.
86        send_command("CREATE %s", $opt_mailbox);
87        ($status, $text) = send_command("APPEND %s %s", $opt_mailbox, $msg);
88    }
89    close_and_errorout "IMAP error from $opt_host: $text"
90        if ($status ne 'OK');
91}
92
93# We are done talking to the IMAP server, close down the connection.
94close_connection();
95
96exit 0;
97
98# Read the given file's entire contents, returning it as a scalar.
99sub read_file($) {
100    my $file = $_[0];
101    local $/ = undef;
102    open(FILE, $file) || errorout "Cannot open $file: $!";
103    my $contents = <FILE>;
104    close(FILE);
105    return $contents;
106}
107
108# Subroutine to send a command to the IMAP server, and wait for the
109# response.  If the response status indicates failure (i.e. is not
110# "OK"), we error out.
111sub send_command(@) {
112    my ($fmt, @args) = @_;
113    printf("Sending: $fmt\n", @args) if $opt_debug;
114    my ($status, $text) = $client->send('', '', $fmt, @args);
115    errorout "Premature end-of-file on IMAP connection to $opt_host"
116        if $status eq 'EOF';
117    print "Response: status $status, text $text\n" if $opt_debug;
118    return ($status, $text) if wantarray;
119    close_and_errorout "IMAP error from $opt_host: $text"
120        if $status ne 'OK';
121}
122
123# Close the connection to the IMAP server, and error out.
124sub close_and_errorout($) {
125    close_connection();
126    errorout $_[0];
127}
128
129# Logout from the IMAP server, and close the connection.
130sub close_connection() {
131    $client->send('', '', "LOGOUT");
132    # Set the client reference to undef, so that perl invokes the
133    # destructor, which closes the connection.  Note that if we invoke
134    # the destructor explicitly here, then perl will still invoke it
135    # again when the program exits, thus touching memory which has
136    # already been freed.
137    $client = undef;
138}
139
140sub errorout($) {
141    print STDERR "$prog: $_[0]\n";
142    exit 1;
143}
Note: See TracBrowser for help on using the repository browser.