]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Imap/imapmanager.php
Merge commit 'mainline/0.9.x' into 0.9.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 for each supporting site
61      * being handled in this process.
62      */
63     public static function multiSite()
64     {
65         return IoManager::INSTANCE_PER_SITE;
66     }
67
68     /**
69      * Initialize connection to server.
70      * @return boolean true on success
71      */
72     public function start($master)
73     {
74         if(parent::start($master))
75         {
76             $this->conn = $this->connect();
77             return true;
78         }else{
79             return false;
80         }
81     }
82
83     public function handleInput($socket)
84     {
85         $this->check_mailbox();
86         return true;
87     }
88
89     public function poll()
90     {
91         return $this->check_mailbox() > 0;
92     }
93     
94     function pollInterval()
95     {
96         return $this->plugin->poll_frequency;
97     }
98     
99     protected function connect()
100     {
101         $this->conn = imap_open($this->plugin->mailbox, $this->plugin->user, $this->plugin->password);
102         if($this->conn){
103             common_log(LOG_INFO, "Connected");
104             return $this->conn;
105         }else{
106             common_log(LOG_INFO, "Failed to connect: " . imap_last_error());
107             return $this->conn;
108         }
109     }
110
111     protected function check_mailbox()
112     {
113         imap_ping($this->conn);
114         $count = imap_num_msg($this->conn);
115         common_log(LOG_INFO, "Found $count messages");
116         if($count > 0){
117             $handler = new IMAPMailHandler();
118             for($i=1; $i <= $count; $i++)
119             {
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);
123             }
124             imap_expunge($this->conn);
125             common_log(LOG_INFO, "Finished processing messages");
126         }
127         return $count;
128     }
129 }