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