]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - maildaemon.php
better error msg
[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                 $user = User::staticGet('email', common_canonical_email($from));
45                 if (!$user) {
46                         $this->error($from, _('Not a registered user.'));
47                         return false;
48                 }
49                 if ($user->incomingemail != common_canonical_email($to)) {
50                         $this->error($from, _('Sorry, that is not your incoming email address.'));
51                 }
52                 $response = $this->handle_command($user, $msg);
53                 if ($response) {
54                         $this->respond($from, $to, $response);
55                 }
56                 $this->add_notice($user, $msg);
57         }
58
59         function error($from, $msg) {
60                 file_put_contents("php://stderr", $msg);
61                 exit(1);
62         }
63
64         function handle_command($user, $msg) {
65                 return false;
66         }
67         
68         function respond($from, $to, $response) {
69
70                 $headers['From'] = $to;
71                 $headers['To'] = $from;
72                 $headers['Subject'] = "Command complete";
73
74                 return mail_send(array($from), $headers, $response);
75         }
76         
77         function log($level, $msg) {
78                 common_log($level, 'MailDaemon: '.$msg);
79         }
80         
81         function add_notice($user, $msg) {
82                 $notice = new Notice();
83                 $notice->profile_id = $user->id;
84                 $notice->content = trim(substr($msg, 0, 140));
85                 $notice->rendered = common_render_content($notice->content, $notice);
86                 $notice->created = DB_DataObject_Cast::dateTime();
87                 $notice->query('BEGIN');
88                 $id = $notice->insert();
89                 if (!$id) {
90                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
91                         $this->log(LOG_ERROR,
92                                            'Could not insert ' . common_log_objstring($notice) .
93                                            ' for user ' . common_log_objstring($user) .
94                                            ': ' . $last_error->message);
95                         return;
96                 }
97                 $orig = clone($notice);
98                 $notice->uri = common_notice_uri($notice);
99                 $result = $notice->update($orig);
100                 if (!$result) {
101                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
102                         $this->log(LOG_ERROR,
103                                            'Could not add URI to ' . common_log_objstring($notice) .
104                                            ' for user ' . common_log_objstring($user) .
105                                            ': ' . $last_error->message);
106                         return;
107                 }
108                 $notice->query('COMMIT');
109         common_save_replies($notice);   
110                 common_real_broadcast($notice);
111                 $this->log(LOG_INFO,
112                                    'Added notice ' . $notice->id . ' from user ' . $user->nickname);
113         }
114         
115         function parse_message($fname) {
116                 $contents = file_get_contents($fname);
117                 $parsed = Mail_mimeDecode::decode(array('input' => $contents,
118                                                                                                 'include_bodies' => true,
119                                                                                                 'decode_headers' => true,
120                                                                                                 'decode_bodies' => true));
121                 if (!$parsed) {
122                         return NULL;
123                 }
124                 $from = $parsed->headers['from'];
125                 $to = $parsed->headers['to'];
126                 
127                 switch ($parsed->ctype_primary) {
128                  case 'multitype':
129                         # try and find a text/plain in the mix
130                         foreach ($parsed->parts as $part) {
131                                 if ($part->ctype_primary == 'text' &&
132                                         $part->ctype_secondary == 'plain') {
133                                         $msg = $part->body;
134                                         break;
135                                 }
136                         }
137                         break;
138                  case 'text':
139                         switch ($parsed->ctype_secondary) {
140                          case 'plain':
141                                 $msg = $parsed->body;
142                                 break;
143                          default:
144                                 $this->unsupported_type($parsed);
145                         }
146                  default:
147                         $this->unsupported_type($parsed);
148                 }
149                 
150                 return array($from, $to, $msg);
151         }
152         
153         function unsupported_type($parsed) {
154                 $this->error(NULL, "Unsupported message type: " . $parsed->ctype_primary . "/" . $parsed->ctype_secondary ."\n");
155         }
156 }
157
158 $md = new MailerDaemon();
159 $md->handle_message('php://stdin');