]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Imap/imapmanager.php
Merge remote branch 'gitorious/1.0.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Imap / imapmanager.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * IMAP IO Manager
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
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/
30  */
31
32 class ImapManager extends IoManager
33 {
34     protected $conn = null;
35
36     function __construct($plugin)
37     {
38         $this->plugin = $plugin;
39     }
40
41     /**
42      * Fetch the singleton manager for the current site.
43      * @return mixed ImapManager, or false if unneeded
44      */
45     public static function get()
46     {
47         throw new Exception('ImapManager should be created using it\'s constructor, not the static get method');
48     }
49
50     /**
51      * Lists the IM connection socket to allow i/o master to wake
52      * when input comes in here as well as from the queue source.
53      *
54      * @return array of resources
55      */
56     public function getSockets()
57     {
58         return array();
59     }
60
61     /**
62      * Tell the i/o master we need one instance globally.
63      * Since this is a plugin manager, the plugin class itself will
64      * create one instance per site. This prevents the IoMaster from
65      * making more instances.
66      */
67     public static function multiSite()
68     {
69         return IoManager::GLOBAL_SINGLE_ONLY;
70     }
71
72     /**
73      * Initialize connection to server.
74      * @return boolean true on success
75      */
76     public function start($master)
77     {
78         if(parent::start($master))
79         {
80             $this->conn = $this->connect();
81             return true;
82         }else{
83             return false;
84         }
85     }
86
87     public function handleInput($socket)
88     {
89         $this->check_mailbox();
90         return true;
91     }
92
93     public function poll()
94     {
95         return $this->check_mailbox() > 0;
96     }
97     
98     function pollInterval()
99     {
100         return $this->plugin->poll_frequency;
101     }
102     
103     protected function connect()
104     {
105         $this->conn = imap_open($this->plugin->mailbox, $this->plugin->user, $this->plugin->password);
106         if($this->conn){
107             common_log(LOG_INFO, "Connected");
108             return $this->conn;
109         }else{
110             common_log(LOG_INFO, "Failed to connect: " . imap_last_error());
111             return $this->conn;
112         }
113     }
114
115     protected function check_mailbox()
116     {
117         imap_ping($this->conn);
118         $count = imap_num_msg($this->conn);
119         common_log(LOG_INFO, "Found $count messages");
120         if($count > 0){
121             $handler = new IMAPMailHandler();
122             for($i=1; $i <= $count; $i++)
123             {
124                 $rawmessage = imap_fetchheader($this->conn, $count, FT_PREFETCHTEXT) . imap_body($this->conn, $i);
125                 $handler->handle_message($rawmessage);
126                 imap_delete($this->conn, $i);
127             }
128             imap_expunge($this->conn);
129             common_log(LOG_INFO, "Finished processing messages");
130         }
131         return $count;
132     }
133 }