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 globally.
61 * Since this is a plugin manager, the plugin class itself will
62 * create one instance per site. This prevents the IoMaster from
63 * making more instances.
65 public static function multiSite()
67 return IoManager::GLOBAL_SINGLE_ONLY;
71 * Initialize connection to server.
72 * @return boolean true on success
74 public function start($master)
76 if(parent::start($master))
78 $this->conn = $this->connect();
85 public function handleInput($socket)
87 $this->check_mailbox();
91 public function poll()
93 return $this->check_mailbox() > 0;
96 function pollInterval()
98 return $this->plugin->poll_frequency;
101 protected function connect()
103 $this->conn = imap_open($this->plugin->mailbox, $this->plugin->user, $this->plugin->password);
105 common_log(LOG_INFO, "Connected");
108 common_log(LOG_INFO, "Failed to connect: " . imap_last_error());
113 protected function check_mailbox()
115 imap_ping($this->conn);
116 $count = imap_num_msg($this->conn);
117 common_log(LOG_INFO, "Found $count messages");
119 $handler = new IMAPMailHandler();
120 for($i=1; $i <= $count; $i++)
122 $rawmessage = imap_fetchheader($this->conn, $count, FT_PREFETCHTEXT) . imap_body($this->conn, $i);
123 $handler->handle_message($rawmessage);
124 imap_delete($this->conn, $i);
126 imap_expunge($this->conn);
127 common_log(LOG_INFO, "Finished processing messages");