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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 class ImapManager extends IoManager
32 protected $conn = null;
34 function __construct($plugin)
36 $this->plugin = $plugin;
40 * Fetch the singleton manager for the current site.
41 * @return mixed ImapManager, or false if unneeded
43 public static function get()
45 throw new Exception('ImapManager should be created using it\'s constructor, not the static get method');
49 * Lists the IM connection socket to allow i/o master to wake
50 * when input comes in here as well as from the queue source.
52 * @return array of resources
54 public function getSockets()
60 * Tell the i/o master we need one instance for each supporting site
61 * being handled in this process.
63 public static function multiSite()
65 return IoManager::INSTANCE_PER_SITE;
69 * Initialize connection to server.
70 * @return boolean true on success
72 public function start($master)
74 if(parent::start($master))
76 $this->conn = $this->connect();
83 public function handleInput($socket)
85 $this->check_mailbox();
89 public function poll()
91 return $this->check_mailbox() > 0;
94 function pollInterval()
96 return $this->plugin->poll_frequency;
99 protected function connect()
101 $this->conn = imap_open($this->plugin->mailbox, $this->plugin->user, $this->plugin->password);
103 common_log(LOG_INFO, "Connected");
106 common_log(LOG_INFO, "Failed to connect: " . imap_last_error());
111 protected function check_mailbox()
113 imap_ping($this->conn);
114 $count = imap_num_msg($this->conn);
115 common_log(LOG_INFO, "Found $count messages");
117 $handler = new IMAPMailHandler();
118 for($i=1; $i <= $count; $i++)
120 $rawmessage = imap_fetchheader($this->conn, $count, FT_PREFETCHTEXT) . imap_body($this->conn, $i);
121 $handler->handle_message($rawmessage);
122 imap_delete($this->conn, $i);
124 imap_expunge($this->conn);
125 common_log(LOG_INFO, "Finished processing messages");