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