4 * StatusNet - the distributed open-source microblogging tool
5 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../..'));
23 $shortoptions = 'fi::';
24 $longoptions = array('id::', 'foreground');
26 $helptext = <<<END_OF_IMAP_HELP
27 Daemon script for receiving new notices from users via a mail box (IMAP, POP3, etc)
29 -i --id Identity (default none)
30 -f --foreground Stay in the foreground (default background)
34 require_once INSTALLDIR.'/scripts/commandline.inc';
36 require_once INSTALLDIR . '/lib/common.php';
37 require_once INSTALLDIR . '/lib/daemon.php';
38 require_once INSTALLDIR.'/lib/mailhandler.php';
40 class IMAPDaemon extends Daemon
42 function __construct($resource=null, $daemonize=true, $attrs)
44 parent::__construct($daemonize);
46 foreach ($attrs as $attr=>$value)
48 $this->$attr = $value;
51 $this->log(LOG_INFO, "INITIALIZE IMAPDaemon {" . $this->name() . "}");
56 return strtolower('imapdaemon.'.$this->user.'.'.crc32($this->mailbox));
64 if(imap_ping($this->conn) || $this->connect())
66 $this->check_mailbox();
68 sleep($this->poll_frequency);
72 function check_mailbox()
74 $count = imap_num_msg($this->conn);
75 $this->log(LOG_INFO, "Found $count messages");
77 $handler = new IMAPMailHandler();
78 for($i=1; $i <= $count; $i++)
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);
84 imap_expunge($this->conn);
85 $this->log(LOG_INFO, "Finished processing messages");
89 function log($level, $msg)
91 $text = $this->name() . ': '.$msg;
92 common_log($level, $text);
93 if (!$this->daemonize)
95 $line = common_log_line($level, $text);
103 $this->conn = imap_open($this->mailbox, $this->user, $this->password);
105 $this->log(LOG_INFO, "Connected");
108 $this->log(LOG_INFO, "Failed to connect: " . imap_last_error());
114 class IMAPMailHandler extends MailHandler
116 function error($from, $msg)
118 $this->log(LOG_INFO, "Error: $from $msg");
119 $headers['To'] = $from;
120 $headers['Subject'] = "Error";
122 return mail_send(array($from), $headers, $msg);
126 if (have_option('i', 'id')) {
127 $id = get_option_value('i', 'id');
128 } else if (count($args) > 0) {
134 $foreground = have_option('f', 'foreground');
136 foreach(ImapPlugin::$instances as $pluginInstance){
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