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', realpath(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');
34 # FIXME: we use both Mail_mimeDecode and mailparse
35 # Need to move everything to mailparse
39 function __construct() {
42 function handle_message($fname='php://stdin') {
43 list($from, $to, $msg) = $this->parse_message($fname);
44 if (!$from || !$to || !$msg) {
45 $this->error(NULL, _t('Could not parse message.'));
47 common_log(LOG_INFO, "Mail from $from to $to: " .substr($msg, 0, 20));
48 $user = $this->user_from($from);
50 $this->error($from, _('Not a registered user.'));
53 if (!$this->user_match_to($user, $to)) {
54 $this->error($from, _('Sorry, that is not your incoming email address.'));
57 if (!$user->emailpost) {
58 $this->error($from, _('Sorry, no incoming email allowed.'));
61 $response = $this->handle_command($user, $msg);
63 $this->respond($from, $to, $response);
66 $msg = $this->cleanup_msg($msg);
67 $this->add_notice($user, $msg);
70 function error($from, $msg) {
71 file_put_contents("php://stderr", $msg . "\n");
75 function user_from($from_hdr) {
76 $froms = mailparse_rfc822_parse_addresses($from_hdr);
81 $addr = common_canonical_email($from['address']);
82 $user = User::staticGet('email', $addr);
84 $user = User::staticGet('smsemail', $addr);
89 function user_match_to($user, $to_hdr) {
90 $incoming = $user->incomingemail;
91 $tos = mailparse_rfc822_parse_addresses($to_hdr);
92 foreach ($tos as $to) {
93 if (strcasecmp($incoming, $to['address']) == 0) {
100 function handle_command($user, $msg) {
101 $cmd = trim(strtolower($msg));
104 $this->set_notify($user, false);
107 $this->set_notify($user, true);
114 function set_notify($user, $value) {
115 $orig = clone($user);
116 $user->smsnotify = $value;
117 $result = $user->update($orig);
119 common_log_db_error($user, 'UPDATE', __FILE__);
125 function respond($from, $to, $response) {
127 $headers['From'] = $to;
128 $headers['To'] = $from;
129 $headers['Subject'] = "Command complete";
131 return mail_send(array($from), $headers, $response);
134 function log($level, $msg) {
135 common_log($level, 'MailDaemon: '.$msg);
138 function add_notice($user, $msg) {
139 $notice = Notice::saveNew($user->id, $msg, 'mail');
140 if (is_string($notice)) {
141 $this->log(LOG_ERR, $notice);
144 common_broadcast_notice($notice);
146 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
149 function parse_message($fname) {
150 $contents = file_get_contents($fname);
151 $parsed = Mail_mimeDecode::decode(array('input' => $contents,
152 'include_bodies' => true,
153 'decode_headers' => true,
154 'decode_bodies' => true));
159 $from = $parsed->headers['from'];
161 $to = $parsed->headers['to'];
163 $type = $parsed->ctype_primary . '/' . $parsed->ctype_secondary;
165 if ($parsed->ctype_primary == 'multipart') {
166 foreach ($parsed->parts as $part) {
167 if ($part->ctype_primary == 'text' &&
168 $part->ctype_secondary == 'plain') {
173 } else if ($type == 'text/plain') {
174 $msg = $parsed->body;
176 $this->unsupported_type($type);
179 return array($from, $to, $msg);
182 function unsupported_type($type) {
183 $this->error(NULL, "Unsupported message type: " . $type);
186 function cleanup_msg($msg) {
187 $lines = explode("\n", $msg);
191 foreach ($lines as $line) {
193 if (preg_match('/^\s*>.*$/', $line)) {
196 // skip start of quote
197 if (preg_match('/^\s*On.*wrote:\s*$/', $line)) {
200 // probably interesting to someone, not us
201 if (preg_match('/^\s*Sent via/', $line)) {
204 // skip everything after a sig
205 if (preg_match('/^\s*--+\s*$/', $line) ||
206 preg_match('/^\s*__+\s*$/', $line))
210 // skip everything after Outlook quote
211 if (preg_match('/^\s*-+\s*Original Message\s*-+\s*$/', $line)) {
214 // skip everything after weird forward
215 if (preg_match('/^\s*Begin\s+forward/', $line)) {
219 $output .= ' ' . $line;
222 preg_replace('/\s+/', ' ', $output);
223 return trim($output);
227 $md = new MailerDaemon();
228 $md->handle_message('php://stdin');