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 $helptext = <<<END_OF_HELP
24 Script for converting mail messages into notices. Takes message body
29 require_once INSTALLDIR.'/scripts/commandline.inc';
31 require_once(INSTALLDIR . '/lib/mail.php');
32 require_once(INSTALLDIR . '/lib/mediafile.php');
33 require_once('Mail/mimeDecode.php');
35 # FIXME: we use both Mail_mimeDecode and mailparse
36 # Need to move everything to mailparse
40 function __construct()
44 function handle_message($fname='php://stdin')
46 list($from, $to, $msg, $attachments) = $this->parse_message($fname);
47 if (!$from || !$to || !$msg) {
48 $this->error(null, _('Could not parse message.'));
50 common_log(LOG_INFO, "Mail from $from to $to with ".count($attachments) .' attachment(s): ' .substr($msg, 0, 20));
51 $user = $this->user_from($from);
53 $this->error($from, _('Not a registered user.'));
56 if (!$this->user_match_to($user, $to)) {
57 $this->error($from, _('Sorry, that is not your incoming email address.'));
60 if (!$user->emailpost) {
61 $this->error($from, _('Sorry, no incoming email allowed.'));
64 $response = $this->handle_command($user, $from, $msg);
68 $msg = $this->cleanup_msg($msg);
69 $msg = common_shorten_links($msg);
70 if (Notice::contentTooLong($msg)) {
71 $this->error($from, sprintf(_('That\'s too long. '.
72 'Max notice size is %d chars.'),
73 Notice::maxContent()));
76 $mediafiles = array();
78 foreach($attachments as $attachment){
83 $mf = MediaFile::fromFileHandle($attachment, $user);
84 } catch(ClientException $ce) {
85 $this->error($from, $ce->getMessage());
88 $msg .= ' ' . $mf->shortUrl();
90 array_push($mediafiles, $mf);
94 $err = $this->add_notice($user, $msg, $mediafiles);
96 if (is_string($err)) {
97 $this->error($from, $err);
104 function error($from, $msg)
106 file_put_contents("php://stderr", $msg . "\n");
110 function user_from($from_hdr)
112 $froms = mailparse_rfc822_parse_addresses($from_hdr);
117 $addr = common_canonical_email($from['address']);
118 $user = User::staticGet('email', $addr);
120 $user = User::staticGet('smsemail', $addr);
125 function user_match_to($user, $to_hdr)
127 $incoming = $user->incomingemail;
128 $tos = mailparse_rfc822_parse_addresses($to_hdr);
129 foreach ($tos as $to) {
130 if (strcasecmp($incoming, $to['address']) == 0) {
137 function handle_command($user, $from, $msg)
139 $inter = new CommandInterpreter();
140 $cmd = $inter->handle_command($user, $msg);
142 $cmd->execute(new MailChannel($from));
148 function respond($from, $to, $response)
151 $headers['From'] = $to;
152 $headers['To'] = $from;
153 $headers['Subject'] = "Command complete";
155 return mail_send(array($from), $headers, $response);
158 function log($level, $msg)
160 common_log($level, 'MailDaemon: '.$msg);
163 function add_notice($user, $msg, $mediafiles)
166 $notice = Notice::saveNew($user->id, $msg, 'mail');
167 } catch (Exception $e) {
168 $this->log(LOG_ERR, $e->getMessage());
169 return $e->getMessage();
171 foreach($mediafiles as $mf){
172 $mf->attachToNotice($notice);
174 common_broadcast_notice($notice);
176 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
180 function parse_message($fname)
182 $contents = file_get_contents($fname);
183 $parsed = Mail_mimeDecode::decode(array('input' => $contents,
184 'include_bodies' => true,
185 'decode_headers' => true,
186 'decode_bodies' => true));
191 $from = $parsed->headers['from'];
193 $to = $parsed->headers['to'];
195 $type = $parsed->ctype_primary . '/' . $parsed->ctype_secondary;
197 $attachments = array();
199 $this->extract_part($parsed,$msg,$attachments);
201 return array($from, $to, $msg, $attachments);
204 function extract_part($parsed,&$msg,&$attachments){
205 if ($parsed->ctype_primary == 'multipart') {
206 if($parsed->ctype_secondary == 'alternative'){
207 $altmsg = $this->extract_msg_from_multipart_alternative_part($parsed);
208 if(!empty($altmsg)) $msg = $altmsg;
210 foreach($parsed->parts as $part){
211 $this->extract_part($part,$msg,$attachments);
214 } else if ($parsed->ctype_primary == 'text'
215 && $parsed->ctype_secondary=='plain') {
216 $msg = $parsed->body;
217 if(strtolower($parsed->ctype_parameters['charset']) != "utf-8"){
218 $msg = utf8_encode($msg);
220 }else if(!empty($parsed->body)){
221 if(common_config('attachments', 'uploads')){
222 //only save attachments if uploads are enabled
223 $attachment = tmpfile();
224 fwrite($attachment, $parsed->body);
225 $attachments[] = $attachment;
230 function extract_msg_from_multipart_alternative_part($parsed){
231 foreach ($parsed->parts as $part) {
232 $this->extract_part($part,$msg,$attachments);
234 //we don't want any attachments that are a result of this parsing
238 function unsupported_type($type)
240 $this->error(null, "Unsupported message type: " . $type);
243 function cleanup_msg($msg)
245 $lines = explode("\n", $msg);
249 foreach ($lines as $line) {
251 if (preg_match('/^\s*>.*$/', $line)) {
254 // skip start of quote
255 if (preg_match('/^\s*On.*wrote:\s*$/', $line)) {
258 // probably interesting to someone, not us
259 if (preg_match('/^\s*Sent via/', $line)) {
262 if (preg_match('/^\s*Sent from my/', $line)) {
266 // skip everything after a sig
267 if (preg_match('/^\s*--+\s*$/', $line) ||
268 preg_match('/^\s*__+\s*$/', $line))
272 // skip everything after Outlook quote
273 if (preg_match('/^\s*-+\s*Original Message\s*-+\s*$/', $line)) {
276 // skip everything after weird forward
277 if (preg_match('/^\s*Begin\s+forward/', $line)) {
281 $output .= ' ' . $line;
284 preg_replace('/\s+/', ' ', $output);
285 return trim($output);
289 if (common_config('emailpost', 'enabled')) {
290 $md = new MailerDaemon();
291 $md->handle_message('php://stdin');