]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Imap/imapdaemon.php
Add an IMAP daemon so StatusNet can process incoming user posts via catch-all mailbox...
[quix0rs-gnu-social.git] / plugins / Imap / imapdaemon.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - the distributed open-source microblogging tool
5  * Copyright (C) 2008, 2009, StatusNet, Inc.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../..'));
22
23 $shortoptions = 'fi::';
24 $longoptions = array('id::', 'foreground');
25
26 $helptext = <<<END_OF_IMAP_HELP
27 Daemon script for receiving new notices from users via a mail box (IMAP, POP3, etc)
28
29     -i --id           Identity (default none)
30     -f --foreground   Stay in the foreground (default background)
31
32 END_OF_IMAP_HELP;
33
34 require_once INSTALLDIR.'/scripts/commandline.inc';
35
36 require_once INSTALLDIR . '/lib/common.php';
37 require_once INSTALLDIR . '/lib/daemon.php';
38 require_once INSTALLDIR.'/lib/mailhandler.php';
39
40 class IMAPDaemon extends Daemon
41 {   
42     function __construct($resource=null, $daemonize=true, $attrs)
43     {
44         parent::__construct($daemonize);
45
46         foreach ($attrs as $attr=>$value)
47         {
48             $this->$attr = $value;
49         }
50
51         $this->log(LOG_INFO, "INITIALIZE IMAPDaemon {" . $this->name() . "}");
52     }
53
54     function name()
55     {
56         return strtolower('imapdaemon.'.$this->user.'.'.crc32($this->mailbox));
57     }
58
59     function run()
60     {
61         $this->connect();
62         while(true)
63         {
64             if(imap_ping($this->conn) || $this->connect())
65             {
66                 $this->check_mailbox();
67             }
68             sleep($this->poll_frequency);
69         }
70     }
71
72     function check_mailbox()
73     {
74         $count = imap_num_msg($this->conn);
75         $this->log(LOG_INFO, "Found $count messages");
76         if($count > 0){
77             $handler = new IMAPMailHandler();
78             for($i=1; $i <= $count; $i++)
79             {
80                 $rawmessage = imap_fetchheader($this->conn, $count, FT_PREFETCHTEXT) . imap_body($this->conn, $i);
81                 $handler->handle_message($rawmessage);
82                 imap_delete($this->conn, $i);
83             }
84             imap_expunge($this->conn);
85             $this->log(LOG_INFO, "Finished processing messages");
86         }
87     }
88
89     function log($level, $msg)
90     {
91         $text = $this->name() . ': '.$msg;
92         common_log($level, $text);
93         if (!$this->daemonize)
94         {
95             $line = common_log_line($level, $text);
96             echo $line;
97             echo "\n";
98         }
99     }
100
101     function connect()
102     {
103         $this->conn = imap_open($this->mailbox, $this->user, $this->password);
104         if($this->conn){
105             $this->log(LOG_INFO, "Connected");
106             return true;
107         }else{
108             $this->log(LOG_INFO, "Failed to connect: " . imap_last_error());
109             return false;
110         }
111     }
112 }
113
114 class IMAPMailHandler extends MailHandler
115 {
116     function error($from, $msg)
117     {
118         $this->log(LOG_INFO, "Error: $from $msg");
119         $headers['To'] = $from;
120         $headers['Subject'] = "Error";
121
122         return mail_send(array($from), $headers, $msg);
123     }
124 }
125
126 if (have_option('i', 'id')) {
127     $id = get_option_value('i', 'id');
128 } else if (count($args) > 0) {
129     $id = $args[0];
130 } else {
131     $id = null;
132 }
133
134 $foreground = have_option('f', 'foreground');
135
136 foreach(ImapPlugin::$instances as $pluginInstance){
137
138     $daemon = new IMAPDaemon($id, !$foreground, array(
139         'mailbox' => $pluginInstance->mailbox,
140         'user' => $pluginInstance->user,
141         'password' => $pluginInstance->password,
142         'poll_frequency' => $pluginInstance->poll_frequency
143     ));
144
145     $daemon->runOnce();
146
147 }