| 
									
										
										
										
											2008-07-17 09:25:33 -04: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(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-13 11:46:03 -04:00
										 |  |  | define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | define('LACONICA', true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require_once(INSTALLDIR . '/lib/common.php'); | 
					
						
							|  |  |  | require_once(INSTALLDIR . '/lib/mail.php'); | 
					
						
							|  |  |  | require_once('Mail/mimeDecode.php'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-21 00:23:02 -04:00
										 |  |  | # FIXME: we use both Mail_mimeDecode and mailparse
 | 
					
						
							|  |  |  | # Need to move everything to mailparse
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | class MailerDaemon { | 
					
						
							| 
									
										
										
										
											2008-07-21 00:23:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 	function __construct() { | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-07-21 00:23:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-18 14:09:51 -04:00
										 |  |  | 	function handle_message($fname='php://stdin') { | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 		list($from, $to, $msg) = $this->parse_message($fname); | 
					
						
							|  |  |  | 		if (!$from || !$to || !$msg) { | 
					
						
							|  |  |  | 			$this->error(NULL, _t('Could not parse message.')); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-07-18 16:27:02 -04:00
										 |  |  | 		common_log(LOG_INFO, "Mail from $from to $to: " .substr($msg, 0, 20)); | 
					
						
							| 
									
										
										
										
											2008-07-19 10:20:58 -04:00
										 |  |  | 		$user = $this->user_from($from); | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 		if (!$user) { | 
					
						
							|  |  |  | 			$this->error($from, _('Not a registered user.')); | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-07-19 10:20:58 -04:00
										 |  |  | 		if (!$this->user_match_to($user, $to)) { | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 			$this->error($from, _('Sorry, that is not your incoming email address.')); | 
					
						
							| 
									
										
										
										
											2008-07-21 00:23:02 -04:00
										 |  |  | 			return false; | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-07-19 16:26:25 -04:00
										 |  |  | 		if (!$user->emailpost) { | 
					
						
							| 
									
										
										
										
											2008-07-21 00:23:02 -04:00
										 |  |  | 			$this->error($from, _('Sorry, no incoming email allowed.')); | 
					
						
							|  |  |  | 			return false; | 
					
						
							| 
									
										
										
										
											2008-07-19 16:26:25 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 		$response = $this->handle_command($user, $msg); | 
					
						
							|  |  |  | 		if ($response) { | 
					
						
							|  |  |  | 			$this->respond($from, $to, $response); | 
					
						
							| 
									
										
										
										
											2008-07-21 00:23:02 -04:00
										 |  |  | 			return true; | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-07-19 10:20:58 -04:00
										 |  |  | 		$msg = $this->cleanup_msg($msg); | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 		$this->add_notice($user, $msg); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function error($from, $msg) { | 
					
						
							| 
									
										
										
										
											2008-07-19 10:20:58 -04:00
										 |  |  | 		file_put_contents("php://stderr", $msg . "\n"); | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 		exit(1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-19 10:20:58 -04:00
										 |  |  | 	function user_from($from_hdr) { | 
					
						
							|  |  |  | 		$froms = mailparse_rfc822_parse_addresses($from_hdr); | 
					
						
							|  |  |  | 		if (!$froms) { | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		$from = $froms[0]; | 
					
						
							| 
									
										
										
										
											2008-07-21 00:23:02 -04:00
										 |  |  | 		$addr = common_canonical_email($from['address']); | 
					
						
							|  |  |  | 		$user = User::staticGet('email', $addr); | 
					
						
							|  |  |  | 		if (!$user) { | 
					
						
							|  |  |  | 			$user = User::staticGet('smsemail', $addr); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return $user; | 
					
						
							| 
									
										
										
										
											2008-07-19 10:20:58 -04: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 00:23:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-18 14:09:51 -04:00
										 |  |  | 	function handle_command($user, $msg) { | 
					
						
							| 
									
										
										
										
											2008-08-04 15:07:49 -04:00
										 |  |  | 		$cmd = trim(strtolower($msg)); | 
					
						
							|  |  |  | 		switch ($cmd) { | 
					
						
							|  |  |  | 		 case 'off': | 
					
						
							|  |  |  | 			$this->set_notify($user, false); | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		 case 'on': | 
					
						
							|  |  |  | 			$this->set_notify($user, true); | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		 default: | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-07-18 14:09:51 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-07-21 00:23:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-04 15:07:49 -04:00
										 |  |  | 	function set_notify($user, $value) { | 
					
						
							|  |  |  | 		$orig = clone($user); | 
					
						
							|  |  |  | 		$user->smsnotify = $value; | 
					
						
							|  |  |  | 		$result = $user->update($orig); | 
					
						
							|  |  |  | 		if (!$result) { | 
					
						
							|  |  |  | 			common_log_db_error($user, 'UPDATE', __FILE__); | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04: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 00:23:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 	function log($level, $msg) { | 
					
						
							|  |  |  | 		common_log($level, 'MailDaemon: '.$msg); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-07-21 00:23:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 	function add_notice($user, $msg) { | 
					
						
							| 
									
										
										
										
											2008-07-29 22:28:56 -04:00
										 |  |  | 		$notice = Notice::saveNew($user->id, $msg, 'mail'); | 
					
						
							|  |  |  | 		if (is_string($notice)) { | 
					
						
							|  |  |  | 			$this->log(LOG_ERR, $notice); | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-07-21 09:33:37 -04:00
										 |  |  | 		common_broadcast_notice($notice); | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 		$this->log(LOG_INFO, | 
					
						
							|  |  |  | 				   'Added notice ' . $notice->id . ' from user ' . $user->nickname); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-07-21 00:23:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04: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 00:23:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 		$from = $parsed->headers['from']; | 
					
						
							| 
									
										
										
										
											2008-07-21 00:23:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 		$to = $parsed->headers['to']; | 
					
						
							| 
									
										
										
										
											2008-07-18 16:20:52 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		$type = $parsed->ctype_primary . '/' . $parsed->ctype_secondary; | 
					
						
							| 
									
										
										
										
											2008-07-21 00:23:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-21 00:51:19 -04:00
										 |  |  | 		if ($parsed->ctype_primary == 'multipart') { | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 			foreach ($parsed->parts as $part) { | 
					
						
							|  |  |  | 				if ($part->ctype_primary == 'text' && | 
					
						
							|  |  |  | 					$part->ctype_secondary == 'plain') { | 
					
						
							|  |  |  | 					$msg = $part->body; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2008-07-18 16:20:52 -04:00
										 |  |  | 		} else if ($type == 'text/plain') { | 
					
						
							|  |  |  | 			$msg = $parsed->body; | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2008-07-18 16:21:24 -04:00
										 |  |  | 			$this->unsupported_type($type); | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-07-21 00:23:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 		return array($from, $to, $msg); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-07-21 00:23:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-18 16:21:24 -04:00
										 |  |  | 	function unsupported_type($type) { | 
					
						
							|  |  |  | 		$this->error(NULL, "Unsupported message type: " . $type); | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-07-21 00:23:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-19 10:20:58 -04:00
										 |  |  | 	function cleanup_msg($msg) { | 
					
						
							| 
									
										
										
										
											2008-07-21 00:40:15 -04:00
										 |  |  | 		$lines = explode("\n", $msg); | 
					
						
							| 
									
										
										
										
											2008-07-21 00:37:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		$output = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		foreach ($lines as $line) { | 
					
						
							|  |  |  | 			// skip quotes
 | 
					
						
							|  |  |  | 			if (preg_match('/^\s*>.*$/', $line)) { | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			// skip start of quote
 | 
					
						
							|  |  |  | 			if (preg_match('/^\s*On.*wrote:\s*$/', $line)) { | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2008-07-21 01:05:08 -04:00
										 |  |  | 			// probably interesting to someone, not us
 | 
					
						
							|  |  |  | 			if (preg_match('/^\s*Sent via/', $line)) { | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2008-07-21 00:37:34 -04:00
										 |  |  | 			// skip everything after a sig
 | 
					
						
							|  |  |  | 			if (preg_match('/^\s*--+\s*$/', $line) || | 
					
						
							|  |  |  | 				preg_match('/^\s*__+\s*$/', $line)) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2008-07-21 01:05:08 -04:00
										 |  |  | 			// skip everything after Outlook quote
 | 
					
						
							|  |  |  | 			if (preg_match('/^\s*-+\s*Original Message\s*-+\s*$/', $line)) { | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			// skip everything after weird forward
 | 
					
						
							|  |  |  | 			if (preg_match('/^\s*Begin\s+forward/', $line)) { | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-21 00:41:35 -04:00
										 |  |  | 			$output .= ' ' . $line; | 
					
						
							| 
									
										
										
										
											2008-07-21 00:37:34 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		preg_replace('/\s+/', ' ', $output); | 
					
						
							| 
									
										
										
										
											2008-07-21 00:41:35 -04:00
										 |  |  | 		return trim($output); | 
					
						
							| 
									
										
										
										
											2008-07-19 10:20:58 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-07-17 09:25:33 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $md = new MailerDaemon(); | 
					
						
							|  |  |  | $md->handle_message('php://stdin'); |