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