]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - maildaemon.php
stop overwriting links.ini
[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 class MailerDaemon {
35         
36         function __construct() {
37         }
38         
39         function handle_message($fname='php://stdin') {
40                 list($from, $to, $msg) = $this->parse_message($fname);
41                 if (!$from || !$to || !$msg) {
42                         $this->error(NULL, _t('Could not parse message.'));
43                 }
44                 common_log(LOG_INFO, "Mail from $from to $to: " .substr($msg, 0, 20));
45                 $user = $this->user_from($from);
46                 if (!$user) {
47                         $this->error($from, _('Not a registered user.'));
48                         return false;
49                 }
50                 if (!$this->user_match_to($user, $to)) {
51                         $this->error($from, _('Sorry, that is not your incoming email address.'));
52                 }
53                 if (!$user->emailpost) {
54                 }
55                 $response = $this->handle_command($user, $msg);
56                 if ($response) {
57                         $this->respond($from, $to, $response);
58                 }
59                 $msg = $this->cleanup_msg($msg);
60                 $this->add_notice($user, $msg);
61         }
62
63         function error($from, $msg) {
64                 file_put_contents("php://stderr", $msg . "\n");
65                 exit(1);
66         }
67
68         function user_from($from_hdr) {
69                 $froms = mailparse_rfc822_parse_addresses($from_hdr);
70                 if (!$froms) {
71                         return NULL;
72                 }
73                 $from = $froms[0];
74                 return User::staticGet('email', common_canonical_email($from['address']));
75         }
76
77         function user_match_to($user, $to_hdr) {
78                 $incoming = $user->incomingemail;
79                 $tos = mailparse_rfc822_parse_addresses($to_hdr);
80                 foreach ($tos as $to) {
81                         if (strcasecmp($incoming, $to['address']) == 0) {
82                                 return true;
83                         }
84                 }
85                 return false;
86         }
87         
88         function handle_command($user, $msg) {
89                 return false;
90         }
91         
92         function respond($from, $to, $response) {
93
94                 $headers['From'] = $to;
95                 $headers['To'] = $from;
96                 $headers['Subject'] = "Command complete";
97
98                 return mail_send(array($from), $headers, $response);
99         }
100         
101         function log($level, $msg) {
102                 common_log($level, 'MailDaemon: '.$msg);
103         }
104         
105         function add_notice($user, $msg) {
106                 $notice = new Notice();
107                 $notice->profile_id = $user->id;
108                 $notice->content = trim(substr($msg, 0, 140));
109                 $notice->rendered = common_render_content($notice->content, $notice);
110                 $notice->created = DB_DataObject_Cast::dateTime();
111                 $notice->query('BEGIN');
112                 $id = $notice->insert();
113                 if (!$id) {
114                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
115                         $this->log(LOG_ERR,
116                                            'Could not insert ' . common_log_objstring($notice) .
117                                            ' for user ' . common_log_objstring($user) .
118                                            ': ' . $last_error->message);
119                         return;
120                 }
121                 $orig = clone($notice);
122                 $notice->uri = common_notice_uri($notice);
123                 $result = $notice->update($orig);
124                 if (!$result) {
125                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
126                         $this->log(LOG_ERR,
127                                            'Could not add URI to ' . common_log_objstring($notice) .
128                                            ' for user ' . common_log_objstring($user) .
129                                            ': ' . $last_error->message);
130                         return;
131                 }
132                 $notice->query('COMMIT');
133         common_save_replies($notice);   
134                 common_real_broadcast($notice);
135                 $this->log(LOG_INFO,
136                                    'Added notice ' . $notice->id . ' from user ' . $user->nickname);
137         }
138         
139         function parse_message($fname) {
140                 $contents = file_get_contents($fname);
141                 $parsed = Mail_mimeDecode::decode(array('input' => $contents,
142                                                                                                 'include_bodies' => true,
143                                                                                                 'decode_headers' => true,
144                                                                                                 'decode_bodies' => true));
145                 if (!$parsed) {
146                         return NULL;
147                 }
148                 
149                 $from = $parsed->headers['from'];
150                 
151                 $to = $parsed->headers['to'];
152
153                 $type = $parsed->ctype_primary . '/' . $parsed->ctype_secondary;
154                 
155                 if ($parsed->ctype_primary == 'multitype') {
156                         foreach ($parsed->parts as $part) {
157                                 if ($part->ctype_primary == 'text' &&
158                                         $part->ctype_secondary == 'plain') {
159                                         $msg = $part->body;
160                                         break;
161                                 }
162                         }
163                 } else if ($type == 'text/plain') {
164                         $msg = $parsed->body;
165                 } else {
166                         $this->unsupported_type($type);
167                 }
168                 
169                 return array($from, $to, $msg);
170         }
171         
172         function unsupported_type($type) {
173                 $this->error(NULL, "Unsupported message type: " . $type);
174         }
175         
176         function cleanup_msg($msg) {
177                 # XXX: signatures
178                 # XXX: quoting 
179                 preg_replace('/\s+/', ' ', $msg);
180                 return $msg;
181         }
182 }
183
184 $md = new MailerDaemon();
185 $md->handle_message('php://stdin');