]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Imap/imapmanager.php
Merge branch '0.9.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  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 class ImapManager extends IoManager
31 {
32     protected $conn = null;
33
34     function __construct($plugin)
35     {
36         $this->plugin = $plugin;
37     }
38
39     /**
40      * Fetch the singleton manager for the current site.
41      * @return mixed ImapManager, or false if unneeded
42      */
43     public static function get()
44     {
45         throw new Exception('ImapManager should be created using it\'s constructor, not the static get method');
46     }
47
48     /**
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.
51      *
52      * @return array of resources
53      */
54     public function getSockets()
55     {
56         return array();
57     }
58
59     /**
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.
64      */
65     public static function multiSite()
66     {
67         return IoManager::GLOBAL_SINGLE_ONLY;
68     }
69
70     /**
71      * Initialize connection to server.
72      * @return boolean true on success
73      */
74     public function start($master)
75     {
76         if(parent::start($master))
77         {
78             $this->conn = $this->connect();
79             return true;
80         }else{
81             return false;
82         }
83     }
84
85     public function handleInput($socket)
86     {
87         $this->check_mailbox();
88         return true;
89     }
90
91     public function poll()
92     {
93         return $this->check_mailbox() > 0;
94     }
95     
96     function pollInterval()
97     {
98         return $this->plugin->poll_frequency;
99     }
100     
101     protected function connect()
102     {
103         $this->conn = imap_open($this->plugin->mailbox, $this->plugin->user, $this->plugin->password);
104         if($this->conn){
105             common_log(LOG_INFO, "Connected");
106             return $this->conn;
107         }else{
108             common_log(LOG_INFO, "Failed to connect: " . imap_last_error());
109             return $this->conn;
110         }
111     }
112
113     protected function check_mailbox()
114     {
115         imap_ping($this->conn);
116         $count = imap_num_msg($this->conn);
117         common_log(LOG_INFO, "Found $count messages");
118         if($count > 0){
119             $handler = new IMAPMailHandler();
120             for($i=1; $i <= $count; $i++)
121             {
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);
125             }
126             imap_expunge($this->conn);
127             common_log(LOG_INFO, "Finished processing messages");
128         }
129         return $count;
130     }
131 }