#!/usr/bin/perl -w package Mail::IMAPTalk; =head1 NAME Mail::IMAPTalk - IMAP client interface with lots of features =head1 SYNOPSIS use Mail::IMAPTalk; $IMAP = Mail::IMAPTalk->new( Server => $IMAPServer, Username => 'foo', Password => 'bar', Uid => 1 ) || die "Failed to connect/login to IMAP server"; # Append message to folder open(my $F, 'rfc822msg.txt'); $IMAP->append($FolderName, $F) || dir $@; close($F); # Select folder and get first unseen message $IMAP->select($FolderName) || die $@; $MsgId = $IMAP->search('not', 'seen')->[0]; # Get message envelope and print some details $MsgEV = $IMAP->fetch($MsgId, 'envelope')->{$MsgId}->{envelope}; print "From: " . $MsgEv->{From}; print "To: " . $MsgEv->{To}; print "Subject: " . $MsgEv->{Subject}; # Get message body structure $MsgBS = $IMAP->fetch($MsgId, 'bodystructure')->{$MsgId}->{bodystructure}; # Find imap part number of text part of message $MsgTxtHash = Mail::IMAPTalk::find_message($MsgBS); $MsgPart = $MsgTxtHash->{plain}->{'IMAP-Partnum'}; # Retrieve message text body $MsgTxt = $IMAP->fetch($MsgId, "body[$MsgPart]")->{$MsgId}->{body}; $IMAP->logout(); =head1 DESCRIPTION This module communicates with an IMAP server. Each IMAP server command is mapped to a method of this object. Although other IMAP modules exist on CPAN, this has several advantages over other modules. =over 4 =item * It parses the more complex IMAP structures like envelopes and body structures into nice Perl data structures. =item * It correctly supports atoms, quoted strings and literals at any point. Some parsers in other modules aren't fully IMAP compatiable and may break at odd times with certain messages on some servers. =item * It allows large return values (eg. attachments on a message) to be read directly into a file, rather than into memory. =item * It includes some helper functions to find the actual text/plain or text/html part of a message out of a complex MIME structure. It also can find a list of attachements, and CID links for HTML messages with attached images. =item * It supports decoding of MIME headers to Perl utf-8 strings automatically, so you don't have to deal with MIME encoded headers (enabled optionally). =back While the IMAP protocol does allow for asynchronous running of commands, this module is designed to be used in a synchronous manner. That is, you issue a command by calling a method, and the command will block until the appropriate response is returned. The method will then return the parsed results from the given command. =cut # Export {{{ require Exporter; @ISA = qw(Exporter); %EXPORT_TAGS = ( Default => [ qw(get_body_part find_message build_cid_map) ] ); Exporter::export_ok_tags('Default'); sub import { # Test for special case if need UTF8 support our $AlreadyLoadedEncode; if (@_>1 && $_[1] && $_[1] eq ':utf8support') { splice @_, 1, 1; if (!$AlreadyLoadedEncode) { eval "use Encode qw(decode);"; $AlreadyLoadedEncode = 1; } } goto &Exporter::import; } our $VERSION = '1.03'; # }}} # Use modules {{{ use Fcntl qw(:DEFAULT); use Socket; use IO::Select; use IO::Handle; use IO::Socket; use Data::Dumper; use strict; # }}} =head1 CLASS OVERVIEW The object methods have been broken in several sections. =head2 Sections =over 4 =item CONSTANTS Lists the available constants the class uses. =item CONSTRUCTOR Explains all the options available when constructing a new instance of the C class. =item CONNECTION CONTROL METHODS These are methods which control the overall IMAP connection object, such as logging in and logging out, how results are parsed, how folder names and message id's are treated, etc. =item IMAP FOLDER COMMAND METHODS These are methods to inspect, add, delete and rename IMAP folders on the server. =item IMAP MESSAGE COMMAND METHODS These are methods to retrieve, delete, move and add messages to/from IMAP folders. =item HELPER METHODS These are extra methods that users of this class might find useful. They generally do extra parsing on returned structures to provide higher level functionality. =item INTERNAL METHODS These are methods used internally by the C object to get work done. They may be useful if you need to extend the class yourself. Note that internal methods will always 'die' if they encounter any errors. =item INTERNAL SOCKET FUNCTIONS These are functions used internally by the C object to read/write data to/from the IMAP connection socket. The class does its own buffering so if you want to read/write to the IMAP socket, you should use these functions. =item INTERNAL PARSING FUNCTIONS These are functions used to parse the results returned from the IMAP server into Perl style data structures. =back =head2 Method results All methods return undef on failure. There are four main modes of failure: =over 4 =item 1. An error occurred reading/writing to a socket. Maybe the server closed it, or you're not connected to any server. =item 2. An error occurred parsing the response of an IMAP command. This is usually only a problem if your IMAP server returns invalid data. =item 3. An IMAP command didn't return an 'OK' response. =item 4. The socket read operation timed out waiting for a response from the server. =back In each case, some readable form of error text is placed in $@, or you can call the C method. For commands which return responses (e.g. fetch, getacl, etc), the result is returned. See each command for details of the response result. For commands with no response but which succeed (e.g. setacl, rename, etc) the result 'ok' is generally returned. =head2 Method parameters All methods which send data to the IMAP server (e.g. C, C, etc) have their arguments processed before they are sent. Arguments may be specified in several ways: =over 4 =item B The value is first checked and quoted if required. Values containing [\000\012\015] are turned into literals, values containing [\000-\040\{\} \%\*\"] are quoted by surrounding with a "..." pair (any " themselves are turned into \"). =item B The contents of the file is sent as an IMAP literal. Note that because IMAPTalk has to know the length of the file being sent, this must be a true file reference that can be seeked and not just some stream. The entire file will be sent regardless of the current seek point. =item B The array reference should contain only 2 items. The first item is a text string which specifies what to do with the second item of the array ref. =over 4 =item * 'Literal' The string/data in the second item should be sent as an IMAP literal regardless of the actually data in the string/data. =item * 'NoQuote' The string/data in the second item should be sent as is, no quoting will occur, and the data won't be sent as quoted or as a literal regardless of the contents of the string/data. Examples: # Password is automatically quoted to "nasty%*\"passwd" $IMAP->login("joe", 'nasty%*"passwd'); # Append $MsgTxt as string $IMAP->append("inbox", [ 'Literal', $MsgTxt ]) # Append MSGFILE contents as new message $IMAP->append("inbox", \*MSGFILE ]) =back =back =cut =head1 CONSTANTS These constants relate to the standard 4 states that an IMAP connection can be in. They are passed and returned from the C method. See RFC2060 for more details about IMAP connection states. =over 4 =item I Current not connected to any server. =item I Connected to a server, but not logged in. =item I Connected and logged into a server, but not current folder. =item I Connected, logged in and have 'select'ed a current folder. =back =cut # Constants for the possible states the connection can be in {{{ # Object not connected use constant Unconnected => 0; # connected; not logged in use constant Connected => 1; # logged in; no mailbox selected use constant Authenticated => 2; # mailbox selected use constant Selected => 3; # What a link break is on the network connection use constant LB => "\015\012"; # Regexps used to determine if header is MIME encoded my $RFC1522Token = qr/[^\x00-\x1f\(\)\<\>\@\,\;\:\"\/\[\]\?\.\=\ ]+/; my $NeedDecodeUTF8Regexp = qr/=\?$RFC1522Token\?$RFC1522Token\?[^\?]*\?=/; # }}} =head1 CONSTRUCTOR =over 4 =cut =item Inew(%Options)> Creates new Mail::IMAPTalk object. The following options are supported. =item B =over 4 =item B The hostname or IP address to connect to. This must be supplied unless the B option is supplied. =item B The port number on the host to connect to. Defaults to 143 if not supplied. =item B An existing socket to use as the connection to the IMAP server. If you supply the B option, you should not supply a B or B option. This is useful if you want to create an SSL socket connection using IO::Socket::SSL and then pass in the connected socket to the new() call. It's also useful in conjunction with the C method described below for reusing the same socket beyond the lifetime of the IMAPTalk object. See a description in the section C method for more information. You must have write flushing enabled for any socket you pass in here so that commands will actually be sent, and responses received, rather than just waiting and eventually timing out. you can do this using the Perl C call and $| ($AUTOFLUSH) variable as shown below. my $ofh = select($Socket); $| = 1; select ($ofh); =item B If you supply a C option, you can specify the IMAP state the socket is currently in, namely one of 'Unconnected', 'Connected', 'Authenticated' or 'Selected'. This defaults to 'Connected' if not supplied and the C option is supplied. =item B If supplied and true, and a socket is supplied via the C option, checks that a greeting line is supplied by the server and reads the greeting line. =back =item B =over 4 =item B The username to connect to the IMAP server as. If not supplied, no login is attempted and the IMAP object is left in the B state. If supplied, you must also supply the B option and a login is attempted. If the login fails, the connection is closed and B is returned. If you want to do something with a connection even if the login fails, don't pass a B option, but instead use the B method described below. =item B The password to use to login to the account. =back =item B =over 4 =item B Control whether message ids are message uids or not. This is 1 (on) by default because generally that's how most people want to use it. This affects most commands that require/use/return message ids (e.g. B, B, B, etc) =item B If supplied, sets the root folder prefix. This is the same as calling C with the value passed. If no value is supplied, C is called with no value. See the C method for more details. =item B If supplied, sets the folder name text string separator character. Passed as the second parameter to the C method. =item B If supplied, passed along with RootFolder to the C method. =item B If supplied, passed along with RootFolder to the C method. =back Examples: $imap = Mail::IMAPTalk->new( Server => 'foo.com', Port => 143, Username => 'joebloggs', Password => 'mypassword', Separator => '.', RootFolder => 'inbox', CaseInsensitive => 1) || die "Connection to foo.com failed. Reason: $@"; $imap = Mail::IMAPTalk->new( Socket => $SSLSocket, State => Mail::IMAPTalk::Authenticated, Uid => 0) || die "Could not query on existing socket. Reason: $@"; =cut sub new { my $Proto = shift; my $Class = ref($Proto) || $Proto; my %Args = @_; # Two main possible new() modes. Either connect to server # or use existing socket passed $Args{Server} || $Args{Socket} || die "No 'Server' or 'Socket' specified"; $Args{Server} && $Args{Socket} && die "Can not specify 'Server' and 'Socket' simultaneously"; # Set ourself to empty to start with my $Self = {}; bless ($Self, $Class); # Create new socket to server my $Socket; if ($Args{Server}) { # Set starting state $Self->state(Unconnected); my $Server = $Self->{Server} = $Args{Server} || die "No Server name given"; my $Port = $Self->{Port} = $Args{Port} || 143; # Create a new socket and connect to IMAP server socket($Socket, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || return undef; my $paddr = sockaddr_in($Port, inet_aton($Server)); connect($Socket, $paddr) || return undef; # Force flushing after every write to the socket my $ofh = select($Socket); $| = 1; select ($ofh); # Set to connected state $Self->state(Connected); } # We have an existing socket else { # Copy socket $Socket = $Args{Socket}; delete $Args{Socket}; # Set state $Self->state(exists $Args{State} ? $Args{State} : Connected); } $Self->{Socket} = $Socket; # Save socket for later use and create IO::Select $Self->{Select} = IO::Select->new(); $Self->{Select}->add($Socket); $Self->{LocalFD} = fileno($Socket); # Process greeting if ($Args{Server} || $Args{ExpectGreeting}) { $Self->{CmdId} = "*"; my ($CompletionResp, $DataResp) = $Self->_parse_response(''); return undef if $CompletionResp !~ /^ok/i; } # Start counter when sending commands $Self->{CmdId} = 1; # Set base modes $Self->uid($Args{Uid}); $Self->parse_mode(Envelope => 1, BodyStructure => 1); $Self->set_tracing(0); # Login first if specified if ($Args{Username}) { # If login fails, just return undef $Self->login(@Args{'Username', 'Password'}) || return undef; } # Set root folder and separator (if supplied) $Self->set_root_folder( $Args{RootFolder}, $Args{Separator}, $Args{CaseInsensitive}, $Args{AltRootFolder}); return $Self; } =back =cut =head1 CONNECTION CONTROL METHODS =over 4 =cut =item I Attempt to login user specified username and password. Currently there is only plain text password login support. If someone can give me a hand implementing others (like DIGEST-MD5, CRAM-MD5, etc) please contact me (see details below). =cut sub login { my $Self = shift; my ($User, $Pwd) = @_; my $PwdArr = ['DoQuote', $Pwd]; # Call standard command. Return undef if login failed $Self->_imap_cmd("login", 0, "", $User, $PwdArr) || return undef; # Set to authenticated if successful $Self->state(Authenticated); return 1; } =item I Log out of IMAP server. This usually closes the servers connection as well. =cut sub logout { my $Self = shift; $Self->_imap_cmd('logout', 0, ''); $Self->state(Unconnected); return 1; } =item I Set/get the current IMAP connection state. Returned or passed value should be one of the constants (Unconnected, Connected, Authenticated, Selected). =cut sub state { my $Self = shift; $Self->{State} = $_[0] if defined $_[0]; return (defined($Self->{State}) ? $Self->{State} : ''); } =item I Get/set the UID status of all UID possible IMAP commands. If set to 1, all commands that can take a UID are set to 'UID Mode', where any ID sent to IMAPTalk is assumed to be a UID. =cut sub uid { $_[0]->{Uid} = $_[1]; return 1; } =item I This method returns the IMAP servers capability command results. The result is a hash reference of (lc(Capability) => 1) key value pairs. This means you can do things like: if ($IMAP->capability()->{quota}) { ... } to test if the server has the QUOTA capability. If you just want a list of capabilities, use the Perl 'keys' function to get a list of keys from the returned hash reference. =cut sub capability { my $Self = shift; # If we've already executed the capability command once, just return the results return $Self->{Cache}->{capability} if exists $Self->{Cache}->{capability}; # Otherwise execute capability command my %Capability = map { lc($_), 1 } ($Self->_imap_cmd("capability", 0, "capability")); # Save for any future queries and return return ($Self->{Cache}->{capability} = \%Capability); } =item I Returns the result of the IMAP servers namespace command. =cut sub namespace { my $Self = shift; # If we've already executed the capability command once, just return the results return $Self->{Cache}->{namespace} if exists $Self->{Cache}->{namespace}; $Self->_require_capability('namespace') || return undef; # Otherwise execute capability command my $Namespace = $Self->_imap_cmd("namespace", 0, "namespace"); # Save for any future queries and return return ($Self->{Cache}->{namespace} = $Namespace); } =item I Perform the standard IMAP 'noop' command which does nothing. =cut sub noop { my $Self = shift; return $Self->_imap_cmd("noop", 0, "", @_); } =item I Returns true if the current socket connection is still open (e.g. the socket hasn't been closed this end or the other end due to a timeout). =cut sub is_open { my $Self = shift; $Self->_trace("A: is_open test\n") if $Self->{Trace}; while (1) { # Ensure no data was left in our own read buffer if ($Self->{ReadLine}) { $Self->_trace("A: unexpected data in read buffer - '" .$Self->{ReadLine}. "'\n") if $Self->{Trace}; die "Unexpected data in read buffer '" . $Self->{ReadLine} . "'"; } $Self->{ReadLine} = undef; # See if there's any data to read local $Self->{Timeout} = 0; # If no sockets with data, must be blocked, so must be connected my $Atom = eval { $Self->_next_atom(); }; # If a timeout, socket is still connected and open if ($@ && ($@ =~ /timed out/)) { $Self->_trace("A: is_open test received timeout, still open\n") if $Self->{Trace}; return 1; } # Other error, assume it's closed if ($@) { $Self->_trace("A: is_open test received error - $@\n") if $Self->{Trace}; $Self->{Socket}->close(); $Self->{Socket} = undef; $Self->state(Unconnected); return undef; } # There was something, find what it was $Atom = $Self->_remaining_line(); $Self->_trace("A: is_open test returned data - '$Atom'\n") if $Self->{Trace}; $Atom || die "Unexpected response while checking connection - $Atom"; # If it's a bye, we're being closed if ($Atom =~ /^bye/i) { $Self->_trace("A: is_open test received 'bye' response\n") if $Self->{Trace}; $Self->{Socket}->close(); $Self->{Socket} = undef; $Self->state(Unconnected); return undef; } # Otherwise it was probably some sort of alert, # check again } } =item I Change the root folder prefix. Some IMAP servers require that all user folders/mailboxes live under a root folder prefix (current versions of B for example use 'INBOX' for personal folders and 'user' for other users folders). If no value is specified, it sets it to ''. You might want to use the B method to find out what roots are available. The $CaseInsensitive argument is a flag that determines whether the root folder should be matched in a case sensitive or insensitive way. See below. Setting this affects all commands that take a folder argument. Basically if the foldername begins with root folder prefix (case sensitive or insensitive based on the second argument), it's left as is, otherwise the root folder prefix and separator char are prefixed to the folder name. Examples: # This is what cyrus uses $IMAP->set_root_folder('inbox', '.', 1, 'user'); # Selects 'Inbox' (because 'Inbox' eq 'inbox' case insensitive) $IMAP->select('Inbox'); # Selects 'inbox.blah' $IMAP->select('blah'); # Selects 'INBOX.fred' (because 'INBOX' eq 'inbox' case insensitive) #IMAP->select('INBOX.fred'); # Selects 'INBOX.fred' # Selects 'user.john' (because 'user' is alt root) #IMAP->select('user.john'); # Selects 'user.john' =cut sub set_root_folder { my ($Self, $RootFolder, $Separator, $CaseInsensitive, $AltRootFolder) = @_; $RootFolder = '' if !defined($RootFolder); $Separator = '' if !defined($Separator); $AltRootFolder = '' if !defined($AltRootFolder); # Strip of the Separator, if the IMAP-Server already appended it $RootFolder =~ s/\Q$Separator\E$//; $Self->{RootFolder} = $RootFolder; $Self->{AltRootFolder} = $AltRootFolder; $Self->{Separator} = $Separator; $Self->{RootPrefix} = $RootFolder . $Separator; $Self->{CaseInsensitive} = $CaseInsensitive; my $RootPrefix = $RootFolder . $Separator; if ($RootFolder) { # Quote any special chars $RootFolder =~ s/([^\w])/\\$1/g; $Separator =~ s/([^\w])/\\$1/g; $AltRootFolder =~ s/([^\w])/\\$1/g; # Make folder name search RootFolder|AltRootFolder $AltRootFolder = '|^(?:' . $AltRootFolder . "(?:\\z|$Separator))" if $AltRootFolder; # Make sure we match these forms: # inbox # inbox. # inbox.blah # And not these forms # inbo # inboxen if ($CaseInsensitive) { $Self->{RootFolderMatch} = qr/^(?:${RootFolder}\z)${AltRootFolder}/i; $Self->{RootFolderMatch2} = qr/^${RootFolder}${Separator}/i; } else { $Self->{RootFolderMatch} = qr/^(?:${RootFolder})${AltRootFolder}/; $Self->{RootFolderMatch2} = qr/^${RootFolder}${Separator}/; } } else { $Self->{RootFolderMatch} = undef; $Self->{RootFolderMatch2} = undef; } return 1; } =item I<_set_separator($Separator)> Checks if the given separator is the same as the one we used before. If not, it calls set_root_folder to recreate the settings with the new Separator. =cut sub _set_separator { my ($Self,$Separator) = @_; #Nothing to do, if we have the same Separator as before return 1 if (defined($Separator) && ($Self->{Separator} eq $Separator)); return $Self->set_root_folder($Self->{RootFolder}, $Separator, $Self->{CaseInsensitive}, $Self->{AltRootFolder}); } =item I Sets the mode whether to read literals as file handles or scalars. You should pass a filehandle here that any literal will be read into. To turn off literal reads into a file handle, pass a 0. Examples: # Read rfc822 text of message 3 into file # (note that the file will have /r/n line terminators) open(F, ">messagebody.txt"); $IMAP->literal_handle_control(\*F); $IMAP->fetch(3, 'rfc822'); $IMAP->literal_handle_control(0); =cut sub literal_handle_control { my $Self = shift; $Self->{LiteralControl} = $_[0] if defined $_[0]; return $Self->{LiteralControl} ? 1 : 0; } =item I Release IMAPTalk's ownership of the current socket it's using so it's not disconnected on DESTROY. This returns the socket, and makes sure that the IMAPTalk object doesn't hold a reference to it any more. This means you can't call any methods on the IMAPTalk object any more. =cut sub release_socket { my $Self = shift; # Remove from the select object $Self->{Select}->remove($Self->{Socket}) if ref($Self->{Select}); my $Socket = $Self->{Socket}; # Delete any knowledge of the socket in our instance delete $Self->{Socket}; delete $Self->{Select}; $Self->_trace("A: Release socket, fileno=" . fileno($Socket) . "\n") if $Self->{Trace}; return $Socket; } =item I Returns a text string which describes the last error that occurred. =cut sub get_last_error { my $Self = shift; return $Self->{LastError}; } =item I Returns the extra response data generated by a previous call. This is most often used after calling B