Mail-IMAPTalk-4.01/0000755000000000000000000000000012553055514012363 5ustar rootrootMail-IMAPTalk-4.01/README0000644000000000000000000000134612553055213013243 0ustar rootrootMail::IMAPTalk =========================== This module communicates with an IMAP server. Each IMAP server command is mapped to a method of this object. INSTALLATION To install this module type the following: perl Makefile.PL make make test make install DEPENDENCIES Encode for automatic foldername/header decoding AUTHOR Rob Mueller DOCUMENTATION See the POD documentation. Viewable online at CPAN http://search.cpan.org/~robm/Mail-IMAPTalk/ Available on github at: L COPYRIGHT AND LICENCE Copyright (C) 2003-2015 by FastMail Pty Ltd This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Mail-IMAPTalk-4.01/MANIFEST.SKIP0000644000000000000000000000000712553051554014256 0ustar rootroot^\.git Mail-IMAPTalk-4.01/Changes0000644000000000000000000000653612553055170013666 0ustar rootrootRevision history for Perl extension Mail::IMAPTalk. 4.01 Mon Jul 20 12:15 2015 - Bump version and update copyright - Remove long command timer - Misc parser and fixes - Avoid / in generated message-id - More improvements to find_message function - Add cyrus IMAP extension commands - Support extended-list options to list command - Avoid undef warning on missing foldermode response - Handle unexpected capability response - Add ability to enable tracing when you "use" the module - Replace multigetannotation with multigetmetadata - Add IMAP myrights command - Add IMAP id command - Use release_socket to close connection - Update root folder and Inbox vs INBOX handling - Choose socket library to use, add SSL socket support - Better formatting of email strings from envelope structure 3.01 Tue Jul 10 11:00 2012 - Bumping major version again because of fetch response format changes (Content-Disposition and annotation changes) - Documentation updates. Update to SORT RFC, update to latest ACL RFC - Update get_response_code docs - Alert reponse is remainder of line, put that in the response code Hack around fact alert response is the remainder of the line after the ], it's not an argument inside the [...], but make it look that way because makes API easier to just call get_response_code - Deal with annoying ANSI_X3.4-1968 encoded headers - Handle servers which don't return any untagged sort/search results, but OK response - Add a Pedantic mode to the parser Will die if a line with an unexpected tag appears when parsing responses. Useful for test suite writers. - Fix parsing of server welcome banner - Always increase CmdId, and rethrow error in pedantic mode - Handle buggy multiple spaces in XIMAPPROXY response - Doc updates and misc fixes - Make Mail::IMAPTalk work on perl 5.8 - Add per-command parse mode, and parsing for annotations - Document unicode folders and remove old warnings - Improve find_message - Typo fixes, remove need for Carp - use RFC2047 not RFC1522 - Atoms can have } in them - Allow reporting errors for multigetannotation - Add IDLE command support - Break Content-Disposition into separate parts like Content-Type (NOTE: This changes the result of fetches and is backwards incompatible) - Improve find_message - Handle literals that fail better (eg no + go ahead response) - Strip newlines if server returns them (it should have unfolded) - Allow release_socket() to take an error flag - Show correct system error on system errors 2.01 Fri Jul 15 17:00 2011 - Pushing to CPAN. Updating major version number because of dozens of small incompatible changes 1.04 Mon Feb 8 13:55 2011 - hundreds of changes and improvements while being used internally by Fastmail - now making a project on github 1.03 Thu Sep 29 17:58 2005 - improved find_message() - added check/subscribe/unsubscribe (thanks Shriman Gurung) - documentation cleanup (thanks Shriman Gurung) - extra option to clear tracing variable before each command - fix weird perl memory/performance issue when parsing long ID lists 1.02 Tue Sep 2 10:48 2004 - fix literal continuation response check 1.01 Tue Aug 26 11:48 2004 - add utf-8 decoding support - add annotatemore support 1.00 Tue Feb 3 17:48 2004 - initial release Mail-IMAPTalk-4.01/Makefile.PL0000644000000000000000000000100412553051554014330 0ustar rootrootuse 5.006001; use ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. WriteMakefile( 'NAME' => 'Mail::IMAPTalk', 'VERSION_FROM' => 'IMAPTalk.pm', # finds $VERSION 'PREREQ_PM' => {}, # e.g., Module::Name => 1.1 ($] >= 5.005 ? ## Add these new keywords supported since 5.005 (ABSTRACT_FROM => 'IMAPTalk.pm', # retrieve abstract from module AUTHOR => 'Rob Mueller ') : ()), ); Mail-IMAPTalk-4.01/META.yml0000644000000000000000000000105212553055514013632 0ustar rootroot--- #YAML:1.0 name: Mail-IMAPTalk version: 4.01 abstract: IMAP client interface with lots of features author: - Rob Mueller license: unknown distribution_type: module configure_requires: ExtUtils::MakeMaker: 0 build_requires: ExtUtils::MakeMaker: 0 requires: {} no_index: directory: - t - inc generated_by: ExtUtils::MakeMaker version 6.57_05 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 Mail-IMAPTalk-4.01/IMAPTalk.pm0000755000000000000000000042670112553051727014302 0ustar rootrootpackage 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', ) || 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->{text}->{'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 generate_cid) ] ); Exporter::export_ok_tags('Default'); my $AlwaysTrace = 0; sub import { # Test for special case if need UTF8 support our $AlreadyLoadedEncode; my $Class = shift(@_); my %Parameters = map { $_ => 1 } @_; if (delete($Parameters{':utf8support'})) { if (!$AlreadyLoadedEncode) { eval "use Encode qw(decode decode_utf8);"; $AlreadyLoadedEncode = 1; } } if (delete($Parameters{':trace'})) { $AlwaysTrace = 1; } @_ = ($Class, keys(%Parameters)); goto &Exporter::import; } our $VERSION = '4.01'; # }}} # Use modules {{{ use Fcntl qw(:DEFAULT); use Socket; use IO::Select; use IO::Handle; use IO::Socket; use Digest; use Data::Dumper; # Choose the best socket class to use (all of these are sub-classes of IO::Socket) my $DefSocketClass; BEGIN { for (qw(IO::Socket::IP IO::Socket::INET6 IO::Socket::INET)) { if (eval "use $_; 1;") { $DefSocketClass = $_; last; } } } # Use Time::HiRes if available to handle select restarts eval 'use Time::HiRes qw(time);'; use strict; use warnings; # }}} =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 \"). undef is turned into NIL =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 string/data in the referenced 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. =item B Emits an opening bracket, and then each item in the array separated by a space, and finally a closing bracket. Each item in the array is processed by the same methods, so can be a scalar, file ref, scalar ref, another array ref, etc. =item B The hash reference should contain only 1 item. The key is a text string which specifies what to do with the value item of the hash. =over 4 =item * 'Literal' The string/data in the value is sent as an IMAP literal regardless of the actual data in the string/data. =item * 'Quote' The string/data in the value is sent as an IMAP quoted string regardless of the actual data in the string/data. =back 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 =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 RFC 3501 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"; use constant LBLEN => length(LB); # Regexps used to determine if header is MIME encoded (we remove . from # especials because of dumb ANSI_X3.4-1968 encoding) my $RFC2047Token = qr/[^\x00-\x1f\(\)\<\>\@\,\;\:\"\/\[\]\?\=\ ]+/; my $NeedDecodeUTF8Regexp = qr/=\?$RFC2047Token\?$RFC2047Token\?[^\?]*\?=/; # Known untagged responses my %UntaggedResponses = map { $_ => 1 } qw(exists expunge recent); # Default responses my %RespDefaults = ('annotation' => 'hash', 'metadata' => 'hash', 'fetch' => 'hash', 'list' => 'array', 'lsub' => 'array', 'sort' => 'array', 'search' => 'array'); # }}} =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 or 993 if not supplied and UseSSL is true. =item B If true, use an IO::Socket::SSL connection. All other SSL_* arguments are passed to the IO::Socket::SSL constructor. =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 For historical reasons, when reading from a socket, the module sets the socket to non-blocking and does a select(). If you're using an SSL socket that doesn't work, so you have to set UseBlocking to true to use blocking reads instead. =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. =item B For historical reasons, the special name "INBOX" is rewritten as Inbox because it looks nicer on the way out, and back on the way in. If you want to preserve the name INBOX on the outside, set this flag to true. =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. =back Examples: $imap = Mail::IMAPTalk->new( Server => 'foo.com', Port => 143, Username => 'joebloggs', Password => 'mypassword', Separator => '.', RootFolder => 'INBOX', ) || 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); # Empty buffer $Self->{ReadBuf} = ''; # Create new socket to server my $Socket; if ($Args{Server}) { # Set starting state $Self->state(Unconnected); my %SocketOpts; my $DefaultPort = 143; my $SocketClass = $DefSocketClass; if (my $SSLOpt = $Args{UseSSL}) { $SSLOpt = $SSLOpt eq '1' ? '' : " qw($SSLOpt)"; eval "use IO::Socket::SSL$SSLOpt; 1;" || return undef; $SocketClass = "IO::Socket::SSL"; $DefaultPort = 993; $SocketOpts{$_} = $Args{$_} for grep { /^SSL_/ } keys %Args; } $SocketOpts{PeerHost} = $Self->{Server} = $Args{Server} || die "No Server name given"; $SocketOpts{PeerPort} = $Self->{Port} = $Args{Port} || $DefaultPort; $Socket = ${SocketClass}->new(%SocketOpts) || 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); $Self->{UseBlocking} = $Args{UseBlocking}; $Self->{Pedantic} = $Args{Pedantic}; $Self->{PreserveINBOX} = $Args{PreserveINBOX}; # Do this now, so we trace greeting line as well $Self->set_tracing($AlwaysTrace); # 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(exists($Args{Uid}) ? $Args{Uid} : 1); $Self->parse_mode(Envelope => 1, BodyStructure => 1, Annotation => 1); $Self->{CurrentFolder} = ''; $Self->{CurrentFolderMode} = ''; # 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{AltRootRegexp}); 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 = { 'Quote' => $Pwd }; # Clear cached capability responses and the like delete $Self->{Cache}; # 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; # Callback to say we're switching folders $Self->cb_switch_folder($Self->{CurrentFolder}, ''); $Self->_imap_cmd('logout', 0, ''); # Returns the socket, which we immediately discard to close $Self->release_socket(1); 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 = $Self->_imap_cmd("capability", 0, "capability"); # Better be a hash-ref... ($Capability && ref($Capability) eq 'HASH') || return {}; # 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 Enabled the given imap extension =cut sub enable { my $Self = shift; my $Feature = shift; # If we've already executed the enable command once, just return the results return $Self->{Cache}->{enable}->{$Feature} if exists $Self->{Cache}->{enable}->{$Feature}; $Self->_require_capability($Feature) || return undef; my $Result = $Self->_imap_cmd("enable", 0, "enabled", $Feature); $Self->{Cache}->{enable} = $Result; return $Result && $Result->{$Feature}; } =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 "IMAPTalk: 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() if $Self->{Socket}; $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 "IMAPTalk: 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. Setting this affects all commands that take a folder argument. Basically if the foldername begins with root folder prefix, it's left as is, otherwise the root folder prefix and separator char are prefixed to the folder name. The AltRootRegexp is a regexp that if the start of the folder name matches, does not have $RootFolder preprended. You can use this to protect other namespaces in your IMAP server. Examples: # This is what cyrus uses $IMAP->set_root_folder('INBOX', '.', qr/^user/); # Selects 'Inbox' (because 'Inbox' eq 'inbox' case insensitive) $IMAP->select('Inbox'); # Selects 'INBOX.blah' $IMAP->select('blah'); # Selects 'INBOX.Inbox.fred' #IMAP->select('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, $AltRootRegexp) = @_; $RootFolder = '' if !defined($RootFolder); $Separator = '.' if !defined($Separator); # Strip off the Separator, if the IMAP-Server already appended it $RootFolder =~ s/\Q$Separator\E$//; $Self->{RootFolder} = $RootFolder; $Self->{AltRootRegexp} = $AltRootRegexp; $Self->{Separator} = $Separator; # We map canonical IMAP INBOX to nicer looking Inbox, # but have to be careful if the root is INBOX as well # INBOX -> Inbox (done in _fix_folder_name) # INBOX.blah -> blah # INBOX.inbox -> INBOX.inbox # INBOX.Inbox -> INBOX.Inbox # INBOX.inbox.inbox -> INBOX.inbox.inbox # INBOX.Inbox.blah -> Inbox.blah # user.xyz -> user.xyz # RootFolderMatch # If folder passed in doesn't match this, then prepend $RootFolder . $Separator # eg prepend inbox. if folder !/^inbox(\.inbox)*$|^user$|^user\./ # UnrootFolderMatch # If folder returned matches this, strip $RootFolder . $Separator # eg strip inbox. if folder /^inbox\.(?!inbox(\.inbox)*)/ my ($RootFolderMatch, $UnrootFolderMatch); if ($RootFolder) { # Note the /i on the end to make this case-insensitive $RootFolderMatch = qr/\Q${RootFolder}\E(?:\Q${Separator}${RootFolder}\E)*/i; $UnrootFolderMatch = qr/^\Q${RootFolder}${Separator}\E(?!${RootFolderMatch}$)/; if ($AltRootRegexp) { $RootFolderMatch = qr/^${RootFolderMatch}$|^(?:${AltRootRegexp}(?:\Q${Separator}\E|$))/; } else { $RootFolderMatch = qr/^${RootFolderMatch}$/; } } @$Self{qw(RootFolderMatch UnrootFolderMatch)} = ($RootFolderMatch, $UnrootFolderMatch); 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->{AltRootRegexp}); } =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 and the connection state is set to "Unconnected". This means you can't call any methods on the IMAPTalk object any more. If the socket is being released and being closed, then $Close is set to true. =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}; # Set into no connection state $Self->state(Mail::IMAPTalk::Unconnected); 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 last completion response to the tagged command. This is either the string "ok", "no" or "bad" (always lower case) =cut sub get_last_completion_response { my $Self = shift; return $Self->{LastRespCode}; } =item I Returns the extra response data generated by a previous call. This is most often used after calling B