]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - _darcs/pristine/scripts/maildaemon.php
add standard directories
[quix0rs-gnu-social.git] / _darcs / pristine / scripts / maildaemon.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * Laconica - a distributed open-source microblogging tool
5  * Copyright (C) 2008, Controlez-Vous, Inc.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
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";
24         exit();
25 }
26
27 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
28 define('LACONICA', true);
29
30 require_once(INSTALLDIR . '/lib/common.php');
31 require_once(INSTALLDIR . '/lib/mail.php');
32 require_once('Mail/mimeDecode.php');
33
34 # FIXME: we use both Mail_mimeDecode and mailparse
35 # Need to move everything to mailparse
36
37 class MailerDaemon {
38
39         function __construct() {
40         }
41
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, _('Could not parse message.'));
46                 }
47                 common_log(LOG_INFO, "Mail from $from to $to: " .substr($msg, 0, 20));
48                 $user = $this->user_from($from);
49                 if (!$user) {
50                         $this->error($from, _('Not a registered user.'));
51                         return false;
52                 }
53                 if (!$this->user_match_to($user, $to)) {
54                         $this->error($from, _('Sorry, that is not your incoming email address.'));
55                         return false;
56                 }
57                 if (!$user->emailpost) {
58                         $this->error($from, _('Sorry, no incoming email allowed.'));
59                         return false;
60                 }
61                 $response = $this->handle_command($user, $from, $msg);
62                 if ($response) {
63                         return true;
64                 }
65                 $msg = $this->cleanup_msg($msg);
66                 $this->add_notice($user, $msg);
67         }
68
69         function error($from, $msg) {
70                 file_put_contents("php://stderr", $msg . "\n");
71                 exit(1);
72         }
73
74         function user_from($from_hdr) {
75                 $froms = mailparse_rfc822_parse_addresses($from_hdr);
76                 if (!$froms) {
77                         return NULL;
78                 }
79                 $from = $froms[0];
80                 $addr = common_canonical_email($from['address']);
81                 $user = User::staticGet('email', $addr);
82                 if (!$user) {
83                         $user = User::staticGet('smsemail', $addr);
84                 }
85                 return $user;
86         }
87
88         function user_match_to($user, $to_hdr) {
89                 $incoming = $user->incomingemail;
90                 $tos = mailparse_rfc822_parse_addresses($to_hdr);
91                 foreach ($tos as $to) {
92                         if (strcasecmp($incoming, $to['address']) == 0) {
93                                 return true;
94                         }
95                 }
96                 return false;
97         }
98
99         function handle_command($user, $from, $msg) {
100                 $inter = new CommandInterpreter();
101                 $cmd = $inter->handle_command($user, $msg);
102                 if ($cmd) {
103                         $cmd->execute(new MailChannel($from));
104                         return true;
105                 }
106                 return false;
107         }
108
109         function respond($from, $to, $response) {
110
111                 $headers['From'] = $to;
112                 $headers['To'] = $from;
113                 $headers['Subject'] = "Command complete";
114
115                 return mail_send(array($from), $headers, $response);
116         }
117
118         function log($level, $msg) {
119                 common_log($level, 'MailDaemon: '.$msg);
120         }
121
122         function add_notice($user, $msg) {
123         // should test
124         // $msg_shortened = common_shorten_links($msg);
125         // if (mb_strlen($msg_shortened) > 140) ERROR and STOP
126                 $notice = Notice::saveNew($user->id, $msg, 'mail');
127                 if (is_string($notice)) {
128                         $this->log(LOG_ERR, $notice);
129                         return;
130                 }
131                 common_broadcast_notice($notice);
132                 $this->log(LOG_INFO,
133                                    'Added notice ' . $notice->id . ' from user ' . $user->nickname);
134         }
135
136         function parse_message($fname) {
137                 $contents = file_get_contents($fname);
138                 $parsed = Mail_mimeDecode::decode(array('input' => $contents,
139                                                                                                 'include_bodies' => true,
140                                                                                                 'decode_headers' => true,
141                                                                                                 'decode_bodies' => true));
142                 if (!$parsed) {
143                         return NULL;
144                 }
145
146                 $from = $parsed->headers['from'];
147
148                 $to = $parsed->headers['to'];
149
150                 $type = $parsed->ctype_primary . '/' . $parsed->ctype_secondary;
151
152                 if ($parsed->ctype_primary == 'multipart') {
153                         foreach ($parsed->parts as $part) {
154                                 if ($part->ctype_primary == 'text' &&
155                                         $part->ctype_secondary == 'plain') {
156                                         $msg = $part->body;
157                                         break;
158                                 }
159                         }
160                 } else if ($type == 'text/plain') {
161                         $msg = $parsed->body;
162                 } else {
163                         $this->unsupported_type($type);
164                 }
165
166                 return array($from, $to, $msg);
167         }
168
169         function unsupported_type($type) {
170                 $this->error(NULL, "Unsupported message type: " . $type);
171         }
172
173         function cleanup_msg($msg) {
174                 $lines = explode("\n", $msg);
175
176                 $output = '';
177
178                 foreach ($lines as $line) {
179                         // skip quotes
180                         if (preg_match('/^\s*>.*$/', $line)) {
181                                 continue;
182                         }
183                         // skip start of quote
184                         if (preg_match('/^\s*On.*wrote:\s*$/', $line)) {
185                                 continue;
186                         }
187                         // probably interesting to someone, not us
188                         if (preg_match('/^\s*Sent via/', $line)) {
189                                 continue;
190                         }
191                         // skip everything after a sig
192                         if (preg_match('/^\s*--+\s*$/', $line) ||
193                                 preg_match('/^\s*__+\s*$/', $line))
194                         {
195                                 break;
196                         }
197                         // skip everything after Outlook quote
198                         if (preg_match('/^\s*-+\s*Original Message\s*-+\s*$/', $line)) {
199                                 break;
200                         }
201                         // skip everything after weird forward
202                         if (preg_match('/^\s*Begin\s+forward/', $line)) {
203                                 break;
204                         }
205
206                         $output .= ' ' . $line;
207                 }
208
209                 preg_replace('/\s+/', ' ', $output);
210                 return trim($output);
211         }
212 }
213
214 $md = new MailerDaemon();
215 $md->handle_message('php://stdin');