]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/channel.php
Merge branch 'master' of git@gitorious.org:statusnet/mainline into testing
[quix0rs-gnu-social.git] / lib / channel.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 class Channel
23 {
24     function on($user)
25     {
26         return false;
27     }
28
29     function off($user)
30     {
31         return false;
32     }
33
34     function output($user, $text)
35     {
36         return false;
37     }
38
39     function error($user, $text)
40     {
41         return false;
42     }
43
44     function source()
45     {
46         return null;
47     }
48 }
49
50 class CLIChannel extends Channel
51 {
52     function source()
53     {
54         return 'cli';
55     }
56
57     function output($user, $text)
58     {
59         $site = common_config('site', 'name');
60         print "[{$user->nickname}@{$site}] $text\n";
61     }
62
63     function error($user, $text)
64     {
65         $this->output($user, $text);
66     }
67 }
68
69 class XMPPChannel extends Channel
70 {
71
72     var $conn = null;
73
74     function source()
75     {
76         return 'xmpp';
77     }
78
79     function __construct($conn)
80     {
81         $this->conn = $conn;
82     }
83
84     function on($user)
85     {
86         return $this->set_notify($user, 1);
87     }
88
89     function off($user)
90     {
91         return $this->set_notify($user, 0);
92     }
93
94     function output($user, $text)
95     {
96         $text = '['.common_config('site', 'name') . '] ' . $text;
97         jabber_send_message($user->jabber, $text);
98     }
99
100     function error($user, $text)
101     {
102         $text = '['.common_config('site', 'name') . '] ' . $text;
103         jabber_send_message($user->jabber, $text);
104     }
105
106     function set_notify(&$user, $notify)
107     {
108         $orig = clone($user);
109         $user->jabbernotify = $notify;
110         $result = $user->update($orig);
111         if (!$result) {
112             $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
113             common_log(LOG_ERR,
114                        'Could not set notify flag to ' . $notify .
115                        ' for user ' . common_log_objstring($user) .
116                        ': ' . $last_error->message);
117             return false;
118         } else {
119             common_log(LOG_INFO,
120                        'User ' . $user->nickname . ' set notify flag to ' . $notify);
121             return true;
122         }
123     }
124 }
125
126 class WebChannel extends Channel
127 {
128     var $out = null;
129
130     function __construct($out=null)
131     {
132         $this->out = $out;
133     }
134
135     function source()
136     {
137         return 'web';
138     }
139
140     function on($user)
141     {
142         return false;
143     }
144
145     function off($user)
146     {
147         return false;
148     }
149
150     function output($user, $text)
151     {
152         # XXX: buffer all output and send it at the end
153         # XXX: even better, redirect to appropriate page
154         #      depending on what command was run
155         $this->out->startHTML();
156         $this->out->elementStart('head');
157         $this->out->element('title', null, _('Command results'));
158         $this->out->elementEnd('head');
159         $this->out->elementStart('body');
160         $this->out->element('p', array('id' => 'command_result'), $text);
161         $this->out->elementEnd('body');
162         $this->out->endHTML();
163     }
164
165     function error($user, $text)
166     {
167         common_user_error($text);
168     }
169 }
170
171 class AjaxWebChannel extends WebChannel
172 {
173     function output($user, $text)
174     {
175         $this->out->startHTML('text/xml;charset=utf-8');
176         $this->out->elementStart('head');
177         $this->out->element('title', null, _('Command results'));
178         $this->out->elementEnd('head');
179         $this->out->elementStart('body');
180         $this->out->element('p', array('id' => 'command_result'), $text);
181         $this->out->elementEnd('body');
182         $this->out->endHTML();
183     }
184
185     function error($user, $text)
186     {
187         $this->out->startHTML('text/xml;charset=utf-8');
188         $this->out->elementStart('head');
189         $this->out->element('title', null, _('Ajax Error'));
190         $this->out->elementEnd('head');
191         $this->out->elementStart('body');
192         $this->out->element('p', array('id' => 'error'), $text);
193         $this->out->elementEnd('body');
194         $this->out->endHTML();
195     }
196 }
197
198 class MailChannel extends Channel
199 {
200
201     var $addr = null;
202
203     function source()
204     {
205         return 'mail';
206     }
207
208     function __construct($addr=null)
209     {
210         $this->addr = $addr;
211     }
212
213     function on($user)
214     {
215         return $this->set_notify($user, 1);
216     }
217
218     function off($user)
219     {
220         return $this->set_notify($user, 0);
221     }
222
223     function output($user, $text)
224     {
225
226         $headers['From'] = $user->incomingemail;
227         $headers['To'] = $this->addr;
228
229         $headers['Subject'] = _('Command complete');
230
231         return mail_send(array($this->addr), $headers, $text);
232     }
233
234     function error($user, $text)
235     {
236
237         $headers['From'] = $user->incomingemail;
238         $headers['To'] = $this->addr;
239
240         $headers['Subject'] = _('Command failed');
241
242         return mail_send(array($this->addr), $headers, $text);
243     }
244
245     function set_notify($user, $value)
246     {
247         $orig = clone($user);
248         $user->smsnotify = $value;
249         $result = $user->update($orig);
250         if (!$result) {
251             common_log_db_error($user, 'UPDATE', __FILE__);
252             return false;
253         }
254         return true;
255     }
256 }