3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 require_once(INSTALLDIR . '/lib/mail.php');
21 require_once(INSTALLDIR . '/lib/mediafile.php');
22 require_once('Mail/mimeDecode.php');
24 // @todo FIXME: we use both Mail_mimeDecode and mailparse
25 // Need to move everything to mailparse
29 function __construct()
33 function handle_message($rawmessage)
35 list($from, $to, $msg, $attachments) = $this->parse_message($rawmessage);
36 if (!$from || !$to || !$msg) {
37 // TRANS: Error message in incoming mail handler used when an incoming e-mail cannot be processed.
38 $this->error(null, _('Could not parse message.'));
40 common_log(LOG_INFO, "Mail from $from to $to with ".count($attachments) .' attachment(s): ' .substr($msg, 0, 20));
41 $user = $this->user_from_header($from);
43 // TRANS: Error message in incoming mail handler used when an incoming e-mail is not from a registered user.
44 $this->error($from, _('Not a registered user.'));
47 if (!$this->user_match_to($user, $to)) {
48 // TRANS: Error message in incoming mail handler used when an incoming e-mail is not from a user's incoming e-mail address.
49 $this->error($from, _('Sorry, that is not your incoming email address.'));
52 if (!$user->emailpost) {
53 // TRANS: Error message in incoming mail handler used when no incoming e-mail is allowed.
54 $this->error($from, _('Sorry, no incoming email allowed.'));
57 $response = $this->handle_command($user, $from, $msg);
61 $msg = $this->cleanup_msg($msg);
62 $msg = $user->shortenLinks($msg);
63 if (Notice::contentTooLong($msg)) {
64 // TRANS: Error message in incoming mail handler used when an incoming e-mail contains too many characters.
65 $this->error($from, sprintf(_m('That\'s too long. Maximum notice size is %d character.',
66 'That\'s too long. Maximum notice size is %d characters.',
67 Notice::maxContent()),
68 Notice::maxContent()));
71 $mediafiles = array();
73 foreach($attachments as $attachment){
77 $mf = MediaFile::fromFileHandle($attachment, $user);
78 } catch(ClientException $ce) {
79 $this->error($from, $ce->getMessage());
82 $msg .= ' ' . $mf->shortUrl();
84 array_push($mediafiles, $mf);
88 $err = $this->add_notice($user, $msg, $mediafiles);
90 if (is_string($err)) {
91 $this->error($from, $err);
98 function error($from, $msg)
100 file_put_contents("php://stderr", $msg . "\n");
104 function user_from_header($from_hdr)
106 $froms = mailparse_rfc822_parse_addresses($from_hdr);
111 $addr = common_canonical_email($from['address']);
112 $user = User::staticGet('email', $addr);
114 $user = User::staticGet('smsemail', $addr);
119 function user_match_to($user, $to_hdr)
121 $incoming = $user->incomingemail;
122 $tos = mailparse_rfc822_parse_addresses($to_hdr);
123 foreach ($tos as $to) {
124 if (strcasecmp($incoming, $to['address']) == 0) {
131 function handle_command($user, $from, $msg)
133 $inter = new CommandInterpreter();
134 $cmd = $inter->handle_command($user, $msg);
136 $cmd->execute(new MailChannel($from));
142 function respond($from, $to, $response)
144 $headers['From'] = $to;
145 $headers['To'] = $from;
146 // TRANS: E-mail subject for reply to an e-mail command.
147 $headers['Subject'] = _('Command complete');
149 return mail_send(array($from), $headers, $response);
152 function log($level, $msg)
154 common_log($level, 'MailDaemon: '.$msg);
157 function add_notice($user, $msg, $mediafiles)
160 $notice = Notice::saveNew($user->id, $msg, 'mail');
161 } catch (Exception $e) {
162 $this->log(LOG_ERR, $e->getMessage());
163 return $e->getMessage();
165 foreach($mediafiles as $mf){
166 $mf->attachToNotice($notice);
170 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
174 function parse_message($contents)
176 $parsed = Mail_mimeDecode::decode(array('input' => $contents,
177 'include_bodies' => true,
178 'decode_headers' => true,
179 'decode_bodies' => true));
184 $from = $parsed->headers['from'];
186 $to = $parsed->headers['to'];
188 $type = $parsed->ctype_primary . '/' . $parsed->ctype_secondary;
190 $attachments = array();
192 $this->extract_part($parsed,$msg,$attachments);
194 return array($from, $to, $msg, $attachments);
197 function extract_part($parsed,&$msg,&$attachments){
198 if ($parsed->ctype_primary == 'multipart') {
199 if($parsed->ctype_secondary == 'alternative'){
200 $altmsg = $this->extract_msg_from_multipart_alternative_part($parsed);
201 if(!empty($altmsg)) $msg = $altmsg;
203 foreach($parsed->parts as $part){
204 $this->extract_part($part,$msg,$attachments);
207 } else if ($parsed->ctype_primary == 'text'
208 && $parsed->ctype_secondary=='plain') {
209 $msg = $parsed->body;
210 if(strtolower($parsed->ctype_parameters['charset']) != "utf-8"){
211 $msg = utf8_encode($msg);
213 }else if(!empty($parsed->body)){
214 if(common_config('attachments', 'uploads')){
215 //only save attachments if uploads are enabled
216 $attachment = tmpfile();
217 fwrite($attachment, $parsed->body);
218 $attachments[] = $attachment;
223 function extract_msg_from_multipart_alternative_part($parsed){
224 foreach ($parsed->parts as $part) {
225 $this->extract_part($part,$msg,$attachments);
227 //we don't want any attachments that are a result of this parsing
231 function unsupported_type($type)
233 // TRANS: Error message in incoming mail handler used when an incoming e-mail is of an unsupported type.
234 // TRANS: %s is the unsupported type.
235 $this->error(null, sprintf(_('Unsupported message type: %s.'), $type));
238 function cleanup_msg($msg)
240 $lines = explode("\n", $msg);
244 foreach ($lines as $line) {
246 if (preg_match('/^\s*>.*$/', $line)) {
249 // skip start of quote
250 if (preg_match('/^\s*On.*wrote:\s*$/', $line)) {
253 // probably interesting to someone, not us
254 if (preg_match('/^\s*Sent via/', $line)) {
257 if (preg_match('/^\s*Sent from my/', $line)) {
261 // skip everything after a sig
262 if (preg_match('/^\s*--+\s*$/', $line) ||
263 preg_match('/^\s*__+\s*$/', $line))
267 // skip everything after Outlook quote
268 if (preg_match('/^\s*-+\s*Original Message\s*-+\s*$/', $line)) {
271 // skip everything after weird forward
272 if (preg_match('/^\s*Begin\s+forward/', $line)) {
275 // skip everything after a blank line if we already have content
276 if ($output !== '' && $line === '') {
280 $output .= ' ' . $line;
283 preg_replace('/\s+/', ' ', $output);
284 return trim($output);