On Fri, Jun 06, 2003 at 11:15:14AM -0400, Courier User wrote:
> On Fri, Jun 06, 2003 at 10:22:16AM -0400, Sam Varshavchik wrote:
> > Courier User writes:
> > >
> > > [ ... ]
> > >
> > >But if user "me" wants the message to be sent to the INBOX.foobar
> > >folder of user "him", is there any way to cause this to happen using
> > >"to", or any of the other standard maildrop commands?
> > 
> > No.

Given that maildrop doesn't offer a command for doing this, I wrote
my own.  The Perl source code of a quick-and-dirty, preliminary
version of this utility is attached.

This utility requires the Mail::IMAPClient perl module.  Assuming
that the utility is called 'toimap', it's invoked as follows:

  /path/to/toimap user [ folder ]

... where 'user' is the imap user ID, with an optional folder
    name appended, as follows:  '[EMAIL PROTECTED]'.  In the absence of
    a '@host' suffix, 'localhost' is used.

... and where 'folder' is an optional folder name.  In its
    absence, it will default to 'INBOX'.  If the folder name
    is specified as 'INBOX.whatever', then it will be used
    verbatim.  If it's specified as 'whatever', then the folder
    will be 'INBOX.whatever'.

The message is assumed to be available on STDIN.

There is an empty routine in this Perl script called 'getpass'.  I
left it as an exercise to the reader to implement this routine. It
takes a user and a host, and it returns a password.  I recommend
this be written in a very secure fashion.

To use this utility inside of maildrop, do the following:

  cc "|/path/to/toimap [EMAIL PROTECTED] [ folder ]"

or

  to "|/path/to/toimap [EMAIL PROTECTED] [ folder ]"

-- 
 Courier User
 [EMAIL PROTECTED]
#!/usr/bin/perl
# -*- perl -*-

use Mail::IMAPClient;

$0 =~ s:^.*/::;
my $program = $0;

my $imapInbox = 'INBOX';
my $host      = 'localhost';
my $errorRC   = 77;

unless (scalar(@ARGV) > 0) {
  stop("usage: $program [EMAIL PROTECTED] [ folder ]\n");
  # notreached
}

my $user   = shift(@ARGV);
my $folder = shift(@ARGV);

if (!isset($folder)) {
  $folder = $imapInbox;
}
elsif ($folder !~ m/^$imapInbox\./) {
  $folder = "$imapInbox.$folder";
}

if ($user =~ m/^(.+?)\@(.+)$/) {
  $user = $1;
  $host = $2;
}

my $imap = Mail::IMAPClient->new();

unless (defined($imap)) {
  stop("$program: unable to instantiate Mail::IMAPClient object\n");
  # notreached
}

unless ($imap->Server($host) &&
        $imap->connect(User => $user, Password => getpass($user, $host))) {
  stop("unable to connect: [EMAIL PROTECTED]");
  # notreached
}

unless ($imap->IsConnected() && $imap->IsAuthenticated()) {
  stop("connection failed: [EMAIL PROTECTED]");
  # notreached
}

unless ($imap->append($folder, join('', <STDIN>))) {
  stop("failed to append: [EMAIL PROTECTED]");
  # notreached
}

quit(0);
# notreached

sub quit {
  my $code = shift;
  unless (defined($code)) {
    $code = 0;
  }
  if ($imap) {
    if ($imap->IsConnected() || $imap->IsAuthenticated()) {
      $imap->logout();
    }
    $imap = undef;
  }
  exit($code);
}

sub handler {
  quit($errorRC);
  # notreached
}

sub stop {
  my $message = shift;
  if (defined($message)) {
    $message =~ s:\r+::g;
    $message =~ s:\n+$::g;
    if ($message =~ m/\S/) {
      print "$message\n";
    }
  }
  quit($errorRC);
  # notreached
}

sub isset {
  my $item = shift;
  return (defined($item) && $item =~ m/\S/);
}

#
# Return the password associated with a given user on
# a given host.  The implementation of this is left as
# an exercise to the reader.
#
sub getpass {

  my $user = shift;
  my $host = shift;

  # Get the password, given the user and the host.
  my $pswd = undef;

  return ($pswd);
}

__END__

Reply via email to