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