]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - _darcs/pristine/scripts/maildaemon.php
6100cd21beaa77a99d42115219e5f9dacb0a7694
[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
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         $this->add_notice($user, $msg);
69     }
70
71     function error($from, $msg)
72     {
73         file_put_contents("php://stderr", $msg . "\n");
74         exit(1);
75     }
76
77     function user_from($from_hdr)
78     {
79         $froms = mailparse_rfc822_parse_addresses($from_hdr);
80         if (!$froms) {
81             return null;
82         }
83         $from = $froms[0];
84         $addr = common_canonical_email($from['address']);
85         $user = User::staticGet('email', $addr);
86         if (!$user) {
87             $user = User::staticGet('smsemail', $addr);
88         }
89         return $user;
90     }
91
92     function user_match_to($user, $to_hdr)
93     {
94         $incoming = $user->incomingemail;
95         $tos = mailparse_rfc822_parse_addresses($to_hdr);
96         foreach ($tos as $to) {
97             if (strcasecmp($incoming, $to['address']) == 0) {
98                 return true;
99             }
100         }
101         return false;
102     }
103
104     function handle_command($user, $from, $msg)
105     {
106         $inter = new CommandInterpreter();
107         $cmd = $inter->handle_command($user, $msg);
108         if ($cmd) {
109             $cmd->execute(new MailChannel($from));
110             return true;
111         }
112         return false;
113     }
114
115     function respond($from, $to, $response)
116     {
117
118         $headers['From'] = $to;
119         $headers['To'] = $from;
120         $headers['Subject'] = "Command complete";
121
122         return mail_send(array($from), $headers, $response);
123     }
124
125     function log($level, $msg)
126     {
127         common_log($level, 'MailDaemon: '.$msg);
128     }
129
130     function add_notice($user, $msg)
131     {
132         // should test
133         // $msg_shortened = common_shorten_links($msg);
134         // if (mb_strlen($msg_shortened) > 140) ERROR and STOP
135         $notice = Notice::saveNew($user->id, $msg, 'mail');
136         if (is_string($notice)) {
137             $this->log(LOG_ERR, $notice);
138             return;
139         }
140         common_broadcast_notice($notice);
141         $this->log(LOG_INFO,
142                    'Added notice ' . $notice->id . ' from user ' . $user->nickname);
143     }
144
145     function parse_message($fname)
146     {
147         $contents = file_get_contents($fname);
148         $parsed = Mail_mimeDecode::decode(array('input' => $contents,
149                                                 'include_bodies' => true,
150                                                 'decode_headers' => true,
151                                                 'decode_bodies' => true));
152         if (!$parsed) {
153             return null;
154         }
155
156         $from = $parsed->headers['from'];
157
158         $to = $parsed->headers['to'];
159
160         $type = $parsed->ctype_primary . '/' . $parsed->ctype_secondary;
161
162         if ($parsed->ctype_primary == 'multipart') {
163             foreach ($parsed->parts as $part) {
164                 if ($part->ctype_primary == 'text' &&
165                     $part->ctype_secondary == 'plain') {
166                     $msg = $part->body;
167                     break;
168                 }
169             }
170         } else if ($type == 'text/plain') {
171             $msg = $parsed->body;
172         } else {
173             $this->unsupported_type($type);
174         }
175
176         return array($from, $to, $msg);
177     }
178
179     function unsupported_type($type)
180     {
181         $this->error(null, "Unsupported message type: " . $type);
182     }
183
184     function cleanup_msg($msg)
185     {
186         $lines = explode("\n", $msg);
187
188         $output = '';
189
190         foreach ($lines as $line) {
191             // skip quotes
192             if (preg_match('/^\s*>.*$/', $line)) {
193                 continue;
194             }
195             // skip start of quote
196             if (preg_match('/^\s*On.*wrote:\s*$/', $line)) {
197                 continue;
198             }
199             // probably interesting to someone, not us
200             if (preg_match('/^\s*Sent via/', $line)) {
201                 continue;
202             }
203             // skip everything after a sig
204             if (preg_match('/^\s*--+\s*$/', $line) ||
205                 preg_match('/^\s*__+\s*$/', $line))
206             {
207                 break;
208             }
209             // skip everything after Outlook quote
210             if (preg_match('/^\s*-+\s*Original Message\s*-+\s*$/', $line)) {
211                 break;
212             }
213             // skip everything after weird forward
214             if (preg_match('/^\s*Begin\s+forward/', $line)) {
215                 break;
216             }
217
218             $output .= ' ' . $line;
219         }
220
221         preg_replace('/\s+/', ' ', $output);
222         return trim($output);
223     }
224 }
225
226 $md = new MailerDaemon();
227 $md->handle_message('php://stdin');