Fetchmail fixes.
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 12 Dec 2008 06:43:14 +0000 (23:43 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 12 Dec 2008 06:43:14 +0000 (23:43 -0700)
imp/lib/Fetchmail.php
imp/lib/Fetchmail/Account.php
imp/lib/Fetchmail/imap.php

index d26ba65..f6871c5 100644 (file)
@@ -159,7 +159,7 @@ abstract class IMP_Fetchmail
      *
      * @param array $params  The configuration parameter array.
      */
-    function __construct($params)
+    public function __construct($params)
     {
         /* Check for missing params. */
         $paramlist = $this->getParameterList();
@@ -198,33 +198,6 @@ abstract class IMP_Fetchmail
     abstract public function getMail();
 
     /**
-     * Processes a single mail message by calling any user defined functions,
-     * stripping bare newlines, and adding color information to the headers.
-     *
-     * @param string $header  The message header text.
-     * @param string $body    The message body text.
-     *
-     * @return string  The complete message.
-     */
-    protected function _processMailMessage($header, $body)
-    {
-        $msg = rtrim($header);
-
-        if (empty($this->_params['acctcolor'])) {
-            $msg .= "\nX-color: " . $this->_params['acctcolor'];
-        }
-        $msg .= "\n\n" . $body;
-
-        /* If there is a user defined function, call it with the current
-         * message as an argument. */
-        if ($GLOBALS['conf']['hooks']['fetchmail_filter']) {
-            $msg = Horde::callHook('_imp_hook_fetchmail_filter', array($msg), 'imp');
-        }
-
-        return $msg;
-    }
-
-    /**
      * Checks the message size to see if it exceeds the maximum value
      * allowable in the configuration file.
      *
@@ -246,14 +219,29 @@ abstract class IMP_Fetchmail
     }
 
     /**
-     * Add the message to the requested local mailbox.
+     * Add the message to the requested local mailbox, performing any
+     * necessary processing.
      *
-     * @param string $msg  The message text.
+     * @param string $header  The message header text.
+     * @param string $body    The message body text.
      *
      * @return boolean  True on success, false on failure.
      */
-    protected function _addMessage($msg)
+    protected function _addMessage($header, $body)
     {
+        $msg = rtrim($header);
+
+        if (empty($this->_params['acctcolor'])) {
+            $msg .= "\nX-color: " . $this->_params['acctcolor'];
+        }
+        $msg .= "\n\n" . $body;
+
+        /* If there is a user defined function, call it with the current
+         * message as an argument. */
+        if ($GLOBALS['conf']['hooks']['fetchmail_filter']) {
+            $msg = Horde::callHook('_imp_hook_fetchmail_filter', array($msg), 'imp');
+        }
+
         try {
             $GLOBALS['imp_imap']->ob->append($this->_params['lmailbox'], array(array('data' => $msg)));
             return true;
index 3221881..277853e 100644 (file)
@@ -18,7 +18,7 @@ class IMP_Fetchmail_Account
     /**
      * Constructor.
      */
-    function __construct()
+    public function __construct()
     {
         /* Read all the user's accounts from the prefs object or build
          * a new account from the standard values given in prefs.php. */
@@ -85,7 +85,9 @@ class IMP_Fetchmail_Account
      */
     public function getValue($key, $account)
     {
-        return (isset($this->_accounts[$account][$key])) ? $this->_accounts[$account][$key] : false;
+        return isset($this->_accounts[$account][$key])
+            ? $this->_accounts[$account][$key]
+            : false;
     }
 
     /**
@@ -97,7 +99,9 @@ class IMP_Fetchmail_Account
      */
     public function getAllValues($account)
     {
-        return (isset($this->_accounts[$account])) ? $this->_accounts[$account] : false;
+        return isset($this->_accounts[$account])
+            ? $this->_accounts[$account]
+            : false;
     }
 
     /**
@@ -110,6 +114,7 @@ class IMP_Fetchmail_Account
     public function getAll($key)
     {
         $list = array();
+
         foreach (array_keys($this->_accounts) as $account) {
             $list[$account] = $this->getValue($key, $account);
         }
@@ -129,15 +134,11 @@ class IMP_Fetchmail_Account
         /* These parameters are checkbox items - make sure they are stored
          * as boolean values. */
         $list = array('del', 'onlynew', 'markseen', 'loginfetch');
-        if (in_array($key, $list) && !is_bool($val)) {
-            if (($val == 'yes') || (intval($val) != 0)) {
-                $val = true;
-            } else {
-                $val = false;
-            }
-        }
 
-        $this->_accounts[$account][$key] = $val;
+        $this->_accounts[$account][$key] =
+            (in_array($key, $list) && !is_bool($val) &&
+             (($val == 'yes') || (intval($val) != 0)));
+
         $this->_save();
     }
 
index 1f9cbe1..a473dd0 100644 (file)
@@ -60,50 +60,48 @@ class IMP_Fetchmail_imap extends IMP_Fetchmail
                 'name' => _("POP3"),
                 'string' => 'pop3',
                 'port' => 110,
-                'base' => 'POP3'
+                'base' => 'POP3',
+                'secure' => false
             ),
-            'pop3sslvalid' => array(
+            'pop3tls' => array(
+                'name' => _("POP3 over TLS"),
+                'string' => 'pop3',
+                'port' => 110,
+                'base' => 'POP3',
+                'secure' => 'tls'
+            ),
+            'pop3ssl' => array(
                 'name' => _("POP3 over SSL"),
                 'string' => 'pop3',
                 'port' => 995,
-                'base' => 'POP3'
+                'base' => 'POP3',
+                'secure' => 'ssl'
             ),
             'imap' => array(
                 'name' => _("IMAP"),
                 'string' => 'imap',
                 'port' => 143,
-                'base' => 'IMAP'
+                'base' => 'IMAP',
+                'secure' => false
+            ),
+            'imaptls' => array(
+                'name' => _("IMAP"),
+                'string' => 'imap over TLS',
+                'port' => 143,
+                'base' => 'IMAP',
+                'secure' => 'tls'
             ),
             'imapsslvalid' => array(
                 'name' => _("IMAP over SSL"),
                 'string' => 'imap',
                 'port' => 993,
-                'base' => 'IMAP'
+                'base' => 'IMAP',
+                'secure' => 'ssl'
             )
         );
     }
 
     /**
-     * Checks if the remote mailbox exists.
-     *
-     * @return boolean  Does the remote mailbox exist?
-     */
-    protected function _remoteMboxExists($mbox)
-    {
-        if (strcasecmp($mbox, 'INBOX') === 0) {
-            /* INBOX always exists and is a special case. */
-            return true;
-        }
-
-        try {
-            $res = $this->_ob->listMailboxes($mbox, array('flat' => true));
-            return (bool)count($res);
-        } catch (Horde_Imap_Client_Exception $e) {
-            return false;
-        }
-    }
-
-    /**
      * Attempts to connect to the mail server
      *
      * @return mixed  Returns true on success or PEAR_Error on failure.
@@ -121,8 +119,8 @@ class IMP_Fetchmail_imap extends IMP_Fetchmail
             'hostspec' => $this->_params['server'],
             'password' => $this->_params['password'],
             'port' => $protocols[$this->_params['protocol']]['port'],
-            'username' => $this->_params['username']
-            // TODO: secure
+            'username' => $this->_params['username'],
+            'secure' => $protocols[$this->_params['protocol']]['secure']
         );
 
         try {
@@ -140,7 +138,7 @@ class IMP_Fetchmail_imap extends IMP_Fetchmail
      */
     public function getMail()
     {
-        $flags = $to_store = array();
+        $to_store = array();
         $numMsgs = 0;
 
         $stream = $this->_connect();
@@ -150,7 +148,20 @@ class IMP_Fetchmail_imap extends IMP_Fetchmail
 
         /* Check to see if remote mailbox exists. */
         $mbox = $this->_params['rmailbox'];
-        if (!$mbox || !$this->_remoteMboxExists($mbox)) {
+
+        /* INBOX always exists and is a special case. */
+        if ($mbox && strcasecmp($mbox, 'INBOX') !== 0) {
+            try {
+                $res = $this->_ob->listMailboxes($mbox, array('flat' => true));
+                if (!count($res)) {
+                    $mbox = false;
+                }
+            } catch (Horde_Imap_Client_Exception $e) {
+                $mbox = false;
+            }
+        }
+
+        if (!$mbox) {
             return PEAR::raiseError(_("Invalid Remote Mailbox"));
         }
 
@@ -161,11 +172,11 @@ class IMP_Fetchmail_imap extends IMP_Fetchmail
         }
 
         try {
-            $search_res = $GLOBALS['imp_imap']->ob->search($mbox, $query);
+            $search_res = $this->_ob->search($mbox, $query);
             if (empty($search_res['match'])) {
                 return 0;
             }
-            $fetch_res = $GLOBALS['imp_imap']->ob->fetch($mbox, array(
+            $fetch_res = $this->_ob->fetch($mbox, array(
                 Horde_Imap_Client::FETCH_ENVELOPE => true,
                 Horde_Imap_Client::FETCH_SIZE => true
 
@@ -174,6 +185,9 @@ class IMP_Fetchmail_imap extends IMP_Fetchmail
             return 0;
         }
 
+        /* Mark message seen if 'markseen' is set. */
+        $peek = !$this->_params['markseen'];
+
         reset($fetch_res);
         while (list($id, $ob) = each($fetch_res)) {
             /* Check message size. */
@@ -182,38 +196,26 @@ class IMP_Fetchmail_imap extends IMP_Fetchmail
             }
 
             try {
-                $res = $GLOBALS['imp_imap']->ob->fetch($this->_mailbox, array(
-                    Horde_Imap_Client::FETCH_HEADERTEXT => array(array('peek' => true)),
+                $res = $this->_ob->fetch($mbox, array(
+                    Horde_Imap_Client::FETCH_HEADERTEXT => array(array('peek' => $peek)),
                     Horde_Imap_Client::FETCH_BODYTEXT => array(array('peek' => true))
-                ), array('ids' => array($this->_index)));
-                $mail_source = $this->_processMailMessage($res[$this->_index]['headertext'][0], $res[$this->_index]['bodytext'][0]);
+                ), array('ids' => array($id)));
             } catch (Horde_Imap_Client_Exception $e) {
                 continue;
             }
 
             /* Append to the server. */
-            if ($this->_addMessage($mail_source)) {
+            if ($this->_addMessage($res[$id]['headertext'][0], $res[$id]['bodytext'][0])) {
                 ++$numMsgs;
                 $to_store[] = $id;
             }
         }
 
         /* Remove the mail if 'del' is set. */
-        if ($this->_params['del']) {
-            $flags[] = '\\deleted';
-        }
-
-        /* Mark message seen if 'markseen' is set. */
-        if ($this->_params['markseen']) {
-            $flags[] = '\\seen';
-        }
-
-        if (!empty($flags)) {
+        if ($numMsgs && $this->_params['del']) {
             try {
-                $imp_imap->ob->store($mbox, array('add' => $flags, 'ids' => $to_store));
-                if ($this->_params['del']) {
-                    $imp_imap->ob->expunge($mbox, array('ids' => $to_store));
-                }
+                $imp_imap->ob->store($mbox, array('add' => array('\\deleted'), 'ids' => $to_store));
+                $imp_imap->ob->expunge($mbox, array('ids' => $to_store));
             } catch (Horde_Imap_Client_Exception $e) {}
         }