Author: as
Date: Tue Jul  3 14:46:22 2007
New Revision: 5677

Log:
- Implemented feature request #10459: Added the searchMailbox() method to the
  IMAP transport. Based on a patch from Sinisa Dukaric.

Modified:
    trunk/Mail/ChangeLog
    trunk/Mail/docs/tutorial/tutorial_imap.php
    trunk/Mail/docs/tutorial/tutorial_imap_extra.php
    trunk/Mail/src/transports/imap/imap_transport.php
    trunk/Mail/tests/transports/transport_imap_test.php

Modified: trunk/Mail/ChangeLog
==============================================================================
--- trunk/Mail/ChangeLog [iso-8859-1] (original)
+++ trunk/Mail/ChangeLog [iso-8859-1] Tue Jul  3 14:46:22 2007
@@ -3,6 +3,8 @@
 
 - Implemented feature request #11061: Added missing conditions for SMTP
   methods.
+- Implemented feature request #10459: Added the searchMailbox() method to the
+  IMAP transport. Based on a patch from Sinisa Dukaric.
 
 
 1.3 - Monday 02 July 2007

Modified: trunk/Mail/docs/tutorial/tutorial_imap.php
==============================================================================
--- trunk/Mail/docs/tutorial/tutorial_imap.php [iso-8859-1] (original)
+++ trunk/Mail/docs/tutorial/tutorial_imap.php [iso-8859-1] Tue Jul  3 14:46:22 
2007
@@ -25,7 +25,7 @@
 // where the key is an message number and the value is the message unique id
     $messages = $imap->listUniqueIdentifiers();
 
-// Usually you will call one of these 5 fetch functions:
+// Usually you will call one of these fetch functions:
 
     // Fetch all messages on the server
     $set = $imap->fetchAll();
@@ -45,6 +45,13 @@
     // See the function description for a list of criterias and for how to 
sort ascending or descending
     $set = $imap->sortFromOffset( 1, 10, "Date" );
 
+    // Fetch messages which match the specified criteria.
+    // See the section 6.4.4. of RFC 1730 or 2060 for a list of criterias
+    // (http://www.faqs.org/rfcs/rfc1730.html)
+    // The following example returns the messages flagged as SEEN and with
+    // 'release' in their Subject
+    $set = $imap->searchMailbox( 'SEEN SUBJECT "release"' );
+
 // Delete a message from the server (message is not physically deleted, but 
it's
 // list of flags get the "Deleted" flag.
     $imap->delete( 1 );

Modified: trunk/Mail/docs/tutorial/tutorial_imap_extra.php
==============================================================================
--- trunk/Mail/docs/tutorial/tutorial_imap_extra.php [iso-8859-1] (original)
+++ trunk/Mail/docs/tutorial/tutorial_imap_extra.php [iso-8859-1] Tue Jul  3 
14:46:22 2007
@@ -34,7 +34,7 @@
 
 // Clears a flag from messages
 // See the function description for a list of supported flags
-    $imap->setFlag( "1,2,4", "SEEN" );
+    $imap->clearFlag( "1,2,4", "SEEN" );
 
 // Append a message to a mailbox. $mail must contain the mail as text
 // Use this with a "Sent" or "Drafts" mailbox

Modified: trunk/Mail/src/transports/imap/imap_transport.php
==============================================================================
--- trunk/Mail/src/transports/imap/imap_transport.php [iso-8859-1] (original)
+++ trunk/Mail/src/transports/imap/imap_transport.php [iso-8859-1] Tue Jul  3 
14:46:22 2007
@@ -980,6 +980,79 @@
     }
 
     /**
+     * Returns an ezcMailImapSet containing the messages which match the
+     * provided $criteria.
+     *
+     * See [EMAIL PROTECTED] http://www.faqs.org/rfcs/rfc1730.html} - 6.4.4. 
(or
+     * [EMAIL PROTECTED] http://www.faqs.org/rfcs/rfc1730.html} - 6.4.4.) for 
criterias
+     * which can be used for searching. The criterias can be combined in the
+     * same search string (separate the criterias with spaces).
+     *
+     * If $criteria is null or empty then it will default to 'ALL' (returns all
+     * messages in the mailbox).
+     *
+     * Examples:
+     * <code>
+     * $imap = new ezcMailImapTransport( 'imap.example.com' );
+     * $imap->authenticate( 'username', 'password' );
+     * $imap->selectMailbox( 'mailbox' ); // Inbox or another mailbox
+     *
+     * // return an ezcMailImapSet containing all messages flagged as 'SEEN'
+     * $set = $imap->searchMailbox( 'SEEN' );
+     *
+     * // return an ezcMailImapSet containing messages with 'release' in their 
Subject
+     * $set = $imap->searchMailbox( 'SUBJECT "release"' );
+     *
+     * // criterias can be combined:
+     * // return an ezcMailImapSet containing messages flagged as 'SEEN' and
+     * // with 'release' in their Subject
+     * $set = $imap->searchMailbox( 'SEEN SUBJECT "release"' );
+     * </code>
+     *
+     * @throws ezcMailTransportException
+     *         if a mailbox is not selected
+     *         or if the server sent a negative response
+     * @param string $criteria
+     * @return ezcMailImapSet
+     */
+    public function searchMailbox( $criteria = null )
+    {
+        if ( $this->state != self::STATE_SELECTED &&
+             $this->state != self::STATE_SELECTED_READONLY )
+        {
+            throw new ezcMailTransportException( "Can't call searchMailbox() 
on the IMAP transport when a mailbox is not selected." );
+        }
+
+        $criteria = trim( $criteria );
+        if ( empty( $criteria ) )
+        {
+            $criteria = 'ALL';
+        }
+
+        $matchingMessages = array();
+        $tag = $this->getNextTag();
+        $this->connection->sendData( "{$tag} SEARCH {$criteria}" );
+
+        $response = $this->getResponse( '* SEARCH' );
+        if ( strpos( $response, '* SEARCH' ) !== false )
+        {
+            $ids = substr( trim( $response ), 9 );
+            if ( trim( $ids ) !== "" )
+            {
+                $matchingMessages = explode( ' ', $ids );
+            }
+        }
+
+        $response = trim( $this->getResponse( $tag, $response ) );
+        if ( $this->responseType( $response ) != self::RESPONSE_OK )
+        {
+            throw new ezcMailTransportException( "The IMAP server could not 
search the messages by the specified criteria: {$response}." );
+        }
+
+        return new ezcMailImapSet( $this->connection, array_values( 
$matchingMessages ) );
+    }
+
+    /**
      * Fetches $count messages from $offset sorted by $sortCriteria.
      *
      * Fetches $count messages starting from the $offset and returns them as a

Modified: trunk/Mail/tests/transports/transport_imap_test.php
==============================================================================
--- trunk/Mail/tests/transports/transport_imap_test.php [iso-8859-1] (original)
+++ trunk/Mail/tests/transports/transport_imap_test.php [iso-8859-1] Tue Jul  3 
14:46:22 2007
@@ -135,6 +135,31 @@
         catch ( ezcMailTransportException $e )
         {
             $this->assertEquals( "An error occured while sending or receiving 
mail. The IMAP server could not fetch the unique identifiers: XXXXX BAD 
XXXXX.", $e->getMessage() );
+        }
+        $imap->setStatus( ezcMailImapTransport::STATE_NOT_CONNECTED );
+    }
+
+    public function testWrapperMockSearchMailboxFail()
+    {
+        $imap = $this->getMock( 'ezcMailImapTransportWrapper', array( 
'getResponse' ), array( self::$server, self::$port ) );
+        $imap->expects( $this->any() )
+             ->method( 'getResponse' )
+             ->will( $this->onConsecutiveCalls(
+                        $this->returnValue( 'XXXXX OK XXXXX' ),
+                        $this->returnValue( 'XXXXX * SEARCH completed' ),
+                        $this->returnValue( 'XXXXX BAD XXXXX' )
+                   ) );
+        $imap->authenticate( self::$user, self::$password );
+        $imap->selectMailbox( 'inbox' );
+
+        try
+        {
+            $imap->searchMailbox();
+            $this->fail( 'Expected exception was not thrown.' );
+        }
+        catch ( ezcMailTransportException $e )
+        {
+            $this->assertEquals( "An error occured while sending or receiving 
mail. The IMAP server could not search the messages by the specified criteria: 
XXXXX BAD XXXXX.", $e->getMessage() );
         }
         $imap->setStatus( ezcMailImapTransport::STATE_NOT_CONNECTED );
     }
@@ -1834,6 +1859,101 @@
         $imap->deleteMailbox( "Guybrush" );
     }
 
+    public function testSearchMailboxEmpty()
+    {
+        $imap = new ezcMailImapTransport( self::$server );
+        $imap->authenticate( self::$user, self::$password );
+        $imap->selectMailbox( 'inbox' );
+
+        $set = $imap->searchMailbox();
+        $this->assertEquals( array( 1, 2, 3, 4 ), $set->getMessageNumbers() );
+        $parser = new ezcMailParser();
+        $mails = $parser->parseMail( $set );
+        $this->assertEquals( 4, count( $mails ) );
+
+        $set = $imap->searchMailbox( ' ' );
+        $this->assertEquals( array( 1, 2, 3, 4 ), $set->getMessageNumbers() );
+        $parser = new ezcMailParser();
+        $mails = $parser->parseMail( $set );
+        $this->assertEquals( 4, count( $mails ) );
+    }
+
+    public function testSearchMailboxFlagged()
+    {
+        $imap = new ezcMailImapTransport( self::$server );
+        $imap->authenticate( self::$user, self::$password );
+        $imap->selectMailbox( 'inbox' );
+        $set = $imap->searchMailbox( 'FLAGGED' );
+        $this->assertEquals( array(), $set->getMessageNumbers() );
+        $parser = new ezcMailParser();
+        $mails = $parser->parseMail( $set );
+        $this->assertEquals( 0, count( $mails ) );
+    }
+
+    public function testSearchMailboxSeen()
+    {
+        $imap = new ezcMailImapTransport( self::$server );
+        $imap->authenticate( self::$user, self::$password );
+        $imap->selectMailbox( 'inbox' );
+        $set = $imap->searchMailbox( 'SEEN' );
+        $this->assertEquals( array( 1, 2, 3, 4 ), $set->getMessageNumbers() );
+        $parser = new ezcMailParser();
+        $mails = $parser->parseMail( $set );
+        $this->assertEquals( 4, count( $mails ) );
+    }
+
+    public function testSearchMailboxSubject()
+    {
+        $imap = new ezcMailImapTransport( self::$server );
+        $imap->authenticate( self::$user, self::$password );
+        $imap->selectMailbox( 'inbox' );
+        $set = $imap->searchMailbox( 'SUBJECT "norwegian"' );
+        $this->assertEquals( array( 1, 3 ), $set->getMessageNumbers() );
+        $parser = new ezcMailParser();
+        $mails = $parser->parseMail( $set );
+        $this->assertEquals( 2, count( $mails ) );
+    }
+
+    public function testSearchMailboxCombineSeenSubject()
+    {
+        $imap = new ezcMailImapTransport( self::$server );
+        $imap->authenticate( self::$user, self::$password );
+        $imap->selectMailbox( 'inbox' );
+        $set = $imap->searchMailbox( 'SEEN SUBJECT "norwegian"' );
+        $this->assertEquals( array( 1, 3 ), $set->getMessageNumbers() );
+        $parser = new ezcMailParser();
+        $mails = $parser->parseMail( $set );
+        $this->assertEquals( 2, count( $mails ) );
+    }
+
+    public function testSearchMailboxCombineFlaggedSubject()
+    {
+        $imap = new ezcMailImapTransport( self::$server );
+        $imap->authenticate( self::$user, self::$password );
+        $imap->selectMailbox( 'inbox' );
+        $set = $imap->searchMailbox( 'FLAGGED SUBJECT "norwegian"' );
+        $this->assertEquals( array(), $set->getMessageNumbers() );
+        $parser = new ezcMailParser();
+        $mails = $parser->parseMail( $set );
+        $this->assertEquals( 0, count( $mails ) );
+    }
+
+    public function testSearchMailboxFail()
+    {
+        $imap = new ezcMailImapTransport( self::$server );
+        $imap->authenticate( self::$user, self::$password );
+
+        try
+        {
+            $set = $imap->searchMailbox( 'SUBJECT "pine"' );
+            $this->fail( 'Expected exception was not thrown.' );
+        }
+        catch ( ezcMailTransportException $e )
+        {
+            $this->assertEquals( "An error occured while sending or receiving 
mail. Can't call searchMailbox() on the IMAP transport when a mailbox is not 
selected.", $e->getMessage() );
+        }
+    }
+
     public function testTransportOptionsSetNotExistent()
     {
         $options = new ezcMailImapTransportOptions();


-- 
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to