4 * Laconica - a distributed open-source microblogging tool
5 * Copyright (C) 2008, Controlez-Vous, 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 # Abort if called from a web server
22 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
23 print "This script must be run from the command line\n";
27 define('INSTALLDIR', dirname(__FILE__));
28 define('LACONICA', true);
30 require_once(INSTALLDIR . '/lib/common.php');
31 require_once(INSTALLDIR . '/lib/mail.php');
32 require_once('Mail/mimeDecode.php');
36 function __construct() {
39 function handle_message($fname='php://stdin') {
40 list($from, $to, $msg) = $this->parse_message($fname);
41 if (!$from || !$to || !$msg) {
42 $this->error(NULL, _t('Could not parse message.'));
44 common_log(LOG_INFO, "Mail from $from to $to: " .substr($msg, 0, 20));
45 $user = $this->user_from($from);
47 $this->error($from, _('Not a registered user.'));
50 if (!$this->user_match_to($user, $to)) {
51 $this->error($from, _('Sorry, that is not your incoming email address.'));
53 $response = $this->handle_command($user, $msg);
55 $this->respond($from, $to, $response);
57 $msg = $this->cleanup_msg($msg);
58 $this->add_notice($user, $msg);
61 function error($from, $msg) {
62 file_put_contents("php://stderr", $msg . "\n");
66 function user_from($from_hdr) {
67 $froms = mailparse_rfc822_parse_addresses($from_hdr);
72 return User::staticGet('email', common_canonical_email($from['address']));
75 function user_match_to($user, $to_hdr) {
76 $incoming = $user->incomingemail;
77 $tos = mailparse_rfc822_parse_addresses($to_hdr);
78 foreach ($tos as $to) {
79 if (strcasecmp($incoming, $to['address']) == 0) {
86 function handle_command($user, $msg) {
90 function respond($from, $to, $response) {
92 $headers['From'] = $to;
93 $headers['To'] = $from;
94 $headers['Subject'] = "Command complete";
96 return mail_send(array($from), $headers, $response);
99 function log($level, $msg) {
100 common_log($level, 'MailDaemon: '.$msg);
103 function add_notice($user, $msg) {
104 $notice = new Notice();
105 $notice->profile_id = $user->id;
106 $notice->content = trim(substr($msg, 0, 140));
107 $notice->rendered = common_render_content($notice->content, $notice);
108 $notice->created = DB_DataObject_Cast::dateTime();
109 $notice->query('BEGIN');
110 $id = $notice->insert();
112 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
114 'Could not insert ' . common_log_objstring($notice) .
115 ' for user ' . common_log_objstring($user) .
116 ': ' . $last_error->message);
119 $orig = clone($notice);
120 $notice->uri = common_notice_uri($notice);
121 $result = $notice->update($orig);
123 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
125 'Could not add URI to ' . common_log_objstring($notice) .
126 ' for user ' . common_log_objstring($user) .
127 ': ' . $last_error->message);
130 $notice->query('COMMIT');
131 common_save_replies($notice);
132 common_real_broadcast($notice);
134 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
137 function parse_message($fname) {
138 $contents = file_get_contents($fname);
139 $parsed = Mail_mimeDecode::decode(array('input' => $contents,
140 'include_bodies' => true,
141 'decode_headers' => true,
142 'decode_bodies' => true));
147 $from = $parsed->headers['from'];
149 $to = $parsed->headers['to'];
151 $type = $parsed->ctype_primary . '/' . $parsed->ctype_secondary;
153 if ($parsed->ctype_primary == 'multitype') {
154 foreach ($parsed->parts as $part) {
155 if ($part->ctype_primary == 'text' &&
156 $part->ctype_secondary == 'plain') {
161 } else if ($type == 'text/plain') {
162 $msg = $parsed->body;
164 $this->unsupported_type($type);
167 return array($from, $to, $msg);
170 function unsupported_type($type) {
171 $this->error(NULL, "Unsupported message type: " . $type);
174 function cleanup_msg($msg) {
177 preg_replace('/\s+/', ' ', $msg);
182 $md = new MailerDaemon();
183 $md->handle_message('php://stdin');