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