3 * StatusNet, the distributed open-source microblogging tool
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Craig Andrews <candrews@integralblue.com>
25 * @copyright 2009-2010 StatusNet, Inc.
26 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
27 * @maintainer Craig Andrews <candrews@integralblue.com>
28 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29 * @link http://status.net/
32 if (!defined('STATUSNET')) {
36 class ImapManager extends IoManager
38 protected $conn = null;
40 function __construct($plugin)
42 $this->plugin = $plugin;
46 * Fetch the singleton manager for the current site.
47 * @return mixed ImapManager, or false if unneeded
49 public static function get()
51 throw new Exception(_m('ImapManager should be created using its constructor, not the using the static get method.'));
55 * Lists the IM connection socket to allow i/o master to wake
56 * when input comes in here as well as from the queue source.
58 * @return array of resources
60 public function getSockets()
66 * Tell the i/o master we need one instance globally.
67 * Since this is a plugin manager, the plugin class itself will
68 * create one instance per site. This prevents the IoMaster from
69 * making more instances.
71 public static function multiSite()
73 return IoManager::GLOBAL_SINGLE_ONLY;
77 * Initialize connection to server.
78 * @return boolean true on success
80 public function start($master)
82 if(parent::start($master))
84 $this->conn = $this->connect();
91 public function handleInput($socket)
93 $this->check_mailbox();
97 public function poll()
99 return $this->check_mailbox() > 0;
102 function pollInterval()
104 return $this->plugin->poll_frequency;
107 protected function connect()
109 $this->conn = imap_open($this->plugin->mailbox, $this->plugin->user, $this->plugin->password);
111 common_log(LOG_INFO, "Connected");
114 common_log(LOG_INFO, "Failed to connect: " . imap_last_error());
119 protected function check_mailbox()
121 imap_ping($this->conn);
122 $count = imap_num_msg($this->conn);
123 common_log(LOG_INFO, "Found $count messages");
125 $handler = new IMAPMailHandler();
126 for($i=1; $i <= $count; $i++)
128 $rawmessage = imap_fetchheader($this->conn, $count, FT_PREFETCHTEXT) . imap_body($this->conn, $i);
129 $handler->handle_message($rawmessage);
130 imap_delete($this->conn, $i);
132 imap_expunge($this->conn);
133 common_log(LOG_INFO, "Finished processing messages");