2008-07-17 14:25:33 +01:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Laconica - a distributed open-source microblogging tool
|
|
|
|
* Copyright (C) 2008, Controlez-Vous, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
# Abort if called from a web server
|
|
|
|
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
|
|
|
|
print "This script must be run from the command line\n";
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
define('INSTALLDIR', dirname(__FILE__));
|
|
|
|
define('LACONICA', true);
|
|
|
|
|
|
|
|
require_once(INSTALLDIR . '/lib/common.php');
|
|
|
|
require_once(INSTALLDIR . '/lib/mail.php');
|
|
|
|
require_once('Mail/mimeDecode.php');
|
|
|
|
|
2008-07-21 05:23:02 +01:00
|
|
|
# FIXME: we use both Mail_mimeDecode and mailparse
|
|
|
|
# Need to move everything to mailparse
|
|
|
|
|
2008-07-17 14:25:33 +01:00
|
|
|
class MailerDaemon {
|
2008-07-21 05:23:33 +01:00
|
|
|
|
2008-07-17 14:25:33 +01:00
|
|
|
function __construct() {
|
|
|
|
}
|
2008-07-21 05:23:33 +01:00
|
|
|
|
2008-07-18 19:09:51 +01:00
|
|
|
function handle_message($fname='php://stdin') {
|
2008-07-17 14:25:33 +01:00
|
|
|
list($from, $to, $msg) = $this->parse_message($fname);
|
|
|
|
if (!$from || !$to || !$msg) {
|
|
|
|
$this->error(NULL, _t('Could not parse message.'));
|
|
|
|
}
|
2008-07-18 21:27:02 +01:00
|
|
|
common_log(LOG_INFO, "Mail from $from to $to: " .substr($msg, 0, 20));
|
2008-07-19 15:20:58 +01:00
|
|
|
$user = $this->user_from($from);
|
2008-07-17 14:25:33 +01:00
|
|
|
if (!$user) {
|
|
|
|
$this->error($from, _('Not a registered user.'));
|
|
|
|
return false;
|
|
|
|
}
|
2008-07-19 15:20:58 +01:00
|
|
|
if (!$this->user_match_to($user, $to)) {
|
2008-07-17 14:25:33 +01:00
|
|
|
$this->error($from, _('Sorry, that is not your incoming email address.'));
|
2008-07-21 05:23:02 +01:00
|
|
|
return false;
|
2008-07-17 14:25:33 +01:00
|
|
|
}
|
2008-07-19 21:26:25 +01:00
|
|
|
if (!$user->emailpost) {
|
2008-07-21 05:23:02 +01:00
|
|
|
$this->error($from, _('Sorry, no incoming email allowed.'));
|
|
|
|
return false;
|
2008-07-19 21:26:25 +01:00
|
|
|
}
|
2008-07-17 14:25:33 +01:00
|
|
|
$response = $this->handle_command($user, $msg);
|
|
|
|
if ($response) {
|
|
|
|
$this->respond($from, $to, $response);
|
2008-07-21 05:23:02 +01:00
|
|
|
return true;
|
2008-07-17 14:25:33 +01:00
|
|
|
}
|
2008-07-19 15:20:58 +01:00
|
|
|
$msg = $this->cleanup_msg($msg);
|
2008-07-17 14:25:33 +01:00
|
|
|
$this->add_notice($user, $msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
function error($from, $msg) {
|
2008-07-19 15:20:58 +01:00
|
|
|
file_put_contents("php://stderr", $msg . "\n");
|
2008-07-17 14:25:33 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2008-07-19 15:20:58 +01:00
|
|
|
function user_from($from_hdr) {
|
|
|
|
$froms = mailparse_rfc822_parse_addresses($from_hdr);
|
|
|
|
if (!$froms) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
$from = $froms[0];
|
2008-07-21 05:23:02 +01:00
|
|
|
$addr = common_canonical_email($from['address']);
|
|
|
|
$user = User::staticGet('email', $addr);
|
|
|
|
if (!$user) {
|
|
|
|
$user = User::staticGet('smsemail', $addr);
|
|
|
|
}
|
|
|
|
return $user;
|
2008-07-19 15:20:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function user_match_to($user, $to_hdr) {
|
|
|
|
$incoming = $user->incomingemail;
|
|
|
|
$tos = mailparse_rfc822_parse_addresses($to_hdr);
|
|
|
|
foreach ($tos as $to) {
|
|
|
|
if (strcasecmp($incoming, $to['address']) == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-07-21 05:23:33 +01:00
|
|
|
|
2008-07-18 19:09:51 +01:00
|
|
|
function handle_command($user, $msg) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-07-21 05:23:33 +01:00
|
|
|
|
2008-07-17 14:25:33 +01:00
|
|
|
function respond($from, $to, $response) {
|
|
|
|
|
|
|
|
$headers['From'] = $to;
|
|
|
|
$headers['To'] = $from;
|
|
|
|
$headers['Subject'] = "Command complete";
|
|
|
|
|
|
|
|
return mail_send(array($from), $headers, $response);
|
|
|
|
}
|
2008-07-21 05:23:33 +01:00
|
|
|
|
2008-07-17 14:25:33 +01:00
|
|
|
function log($level, $msg) {
|
|
|
|
common_log($level, 'MailDaemon: '.$msg);
|
|
|
|
}
|
2008-07-21 05:23:33 +01:00
|
|
|
|
2008-07-17 14:25:33 +01:00
|
|
|
function add_notice($user, $msg) {
|
|
|
|
$notice = new Notice();
|
|
|
|
$notice->profile_id = $user->id;
|
|
|
|
$notice->content = trim(substr($msg, 0, 140));
|
|
|
|
$notice->rendered = common_render_content($notice->content, $notice);
|
|
|
|
$notice->created = DB_DataObject_Cast::dateTime();
|
|
|
|
$notice->query('BEGIN');
|
|
|
|
$id = $notice->insert();
|
|
|
|
if (!$id) {
|
|
|
|
$last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
|
2008-07-18 20:10:19 +01:00
|
|
|
$this->log(LOG_ERR,
|
2008-07-17 14:25:33 +01:00
|
|
|
'Could not insert ' . common_log_objstring($notice) .
|
|
|
|
' for user ' . common_log_objstring($user) .
|
|
|
|
': ' . $last_error->message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$orig = clone($notice);
|
|
|
|
$notice->uri = common_notice_uri($notice);
|
|
|
|
$result = $notice->update($orig);
|
|
|
|
if (!$result) {
|
|
|
|
$last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
|
2008-07-18 20:10:19 +01:00
|
|
|
$this->log(LOG_ERR,
|
2008-07-17 14:25:33 +01:00
|
|
|
'Could not add URI to ' . common_log_objstring($notice) .
|
|
|
|
' for user ' . common_log_objstring($user) .
|
|
|
|
': ' . $last_error->message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$notice->query('COMMIT');
|
2008-07-21 05:23:33 +01:00
|
|
|
common_save_replies($notice);
|
2008-07-17 14:25:33 +01:00
|
|
|
common_real_broadcast($notice);
|
|
|
|
$this->log(LOG_INFO,
|
|
|
|
'Added notice ' . $notice->id . ' from user ' . $user->nickname);
|
|
|
|
}
|
2008-07-21 05:23:33 +01:00
|
|
|
|
2008-07-17 14:25:33 +01:00
|
|
|
function parse_message($fname) {
|
|
|
|
$contents = file_get_contents($fname);
|
|
|
|
$parsed = Mail_mimeDecode::decode(array('input' => $contents,
|
|
|
|
'include_bodies' => true,
|
|
|
|
'decode_headers' => true,
|
|
|
|
'decode_bodies' => true));
|
|
|
|
if (!$parsed) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-07-21 05:23:33 +01:00
|
|
|
|
2008-07-17 14:25:33 +01:00
|
|
|
$from = $parsed->headers['from'];
|
2008-07-21 05:23:33 +01:00
|
|
|
|
2008-07-17 14:25:33 +01:00
|
|
|
$to = $parsed->headers['to'];
|
2008-07-18 21:20:52 +01:00
|
|
|
|
|
|
|
$type = $parsed->ctype_primary . '/' . $parsed->ctype_secondary;
|
2008-07-21 05:23:33 +01:00
|
|
|
|
2008-07-18 21:20:52 +01:00
|
|
|
if ($parsed->ctype_primary == 'multitype') {
|
2008-07-17 14:25:33 +01:00
|
|
|
foreach ($parsed->parts as $part) {
|
|
|
|
if ($part->ctype_primary == 'text' &&
|
|
|
|
$part->ctype_secondary == 'plain') {
|
|
|
|
$msg = $part->body;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-07-18 21:20:52 +01:00
|
|
|
} else if ($type == 'text/plain') {
|
|
|
|
$msg = $parsed->body;
|
|
|
|
} else {
|
2008-07-18 21:21:24 +01:00
|
|
|
$this->unsupported_type($type);
|
2008-07-17 14:25:33 +01:00
|
|
|
}
|
2008-07-21 05:23:33 +01:00
|
|
|
|
2008-07-17 14:25:33 +01:00
|
|
|
return array($from, $to, $msg);
|
|
|
|
}
|
2008-07-21 05:23:33 +01:00
|
|
|
|
2008-07-18 21:21:24 +01:00
|
|
|
function unsupported_type($type) {
|
|
|
|
$this->error(NULL, "Unsupported message type: " . $type);
|
2008-07-17 14:25:33 +01:00
|
|
|
}
|
2008-07-21 05:23:33 +01:00
|
|
|
|
2008-07-19 15:20:58 +01:00
|
|
|
function cleanup_msg($msg) {
|
|
|
|
# XXX: signatures
|
2008-07-21 05:23:33 +01:00
|
|
|
# XXX: quoting
|
2008-07-19 15:20:58 +01:00
|
|
|
preg_replace('/\s+/', ' ', $msg);
|
|
|
|
return $msg;
|
|
|
|
}
|
2008-07-17 14:25:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$md = new MailerDaemon();
|
|
|
|
$md->handle_message('php://stdin');
|