]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/channel.php
Merge branch 'anon-fave-plugin' of gitorious.org:~zcopley/statusnet/zcopleys-clone...
[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 /**
23  * @todo Needs documentation.
24  */
25 class Channel
26 {
27     function on($user)
28     {
29         return false;
30     }
31
32     function off($user)
33     {
34         return false;
35     }
36
37     function output($user, $text)
38     {
39         return false;
40     }
41
42     function error($user, $text)
43     {
44         return false;
45     }
46
47     function source()
48     {
49         return null;
50     }
51 }
52
53 class CLIChannel extends Channel
54 {
55     function source()
56     {
57         return 'cli';
58     }
59
60     function output($user, $text)
61     {
62         $site = common_config('site', 'name');
63         print "[{$user->nickname}@{$site}] $text\n";
64     }
65
66     function error($user, $text)
67     {
68         $this->output($user, $text);
69     }
70 }
71
72 class XMPPChannel extends Channel
73 {
74     var $conn = null;
75
76     function source()
77     {
78         return 'xmpp';
79     }
80
81     function __construct($conn)
82     {
83         $this->conn = $conn;
84     }
85
86     function on($user)
87     {
88         return $this->set_notify($user, 1);
89     }
90
91     function off($user)
92     {
93         return $this->set_notify($user, 0);
94     }
95
96     function output($user, $text)
97     {
98         $text = '['.common_config('site', 'name') . '] ' . $text;
99         jabber_send_message($user->jabber, $text);
100     }
101
102     function error($user, $text)
103     {
104         $text = '['.common_config('site', 'name') . '] ' . $text;
105         jabber_send_message($user->jabber, $text);
106     }
107
108     function set_notify(&$user, $notify)
109     {
110         $orig = clone($user);
111         $user->jabbernotify = $notify;
112         $result = $user->update($orig);
113         if (!$result) {
114             $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
115             common_log(LOG_ERR,
116                        'Could not set notify flag to ' . $notify .
117                        ' for user ' . common_log_objstring($user) .
118                        ': ' . $last_error->message);
119             return false;
120         } else {
121             common_log(LOG_INFO,
122                        'User ' . $user->nickname . ' set notify flag to ' . $notify);
123             return true;
124         }
125     }
126 }
127
128 class WebChannel extends Channel
129 {
130     var $out = null;
131
132     function __construct($out=null)
133     {
134         $this->out = $out;
135     }
136
137     function source()
138     {
139         return 'web';
140     }
141
142     function on($user)
143     {
144         return false;
145     }
146
147     function off($user)
148     {
149         return false;
150     }
151
152     function output($user, $text)
153     {
154         # XXX: buffer all output and send it at the end
155         # XXX: even better, redirect to appropriate page
156         #      depending on what command was run
157         $this->out->startHTML();
158         $this->out->elementStart('head');
159         // TRANS: Title for command results.
160         $this->out->element('title', null, _('Command results'));
161         $this->out->elementEnd('head');
162         $this->out->elementStart('body');
163         $this->out->element('p', array('id' => 'command_result'), $text);
164         $this->out->elementEnd('body');
165         $this->out->endHTML();
166     }
167
168     function error($user, $text)
169     {
170         common_user_error($text);
171     }
172 }
173
174 class AjaxWebChannel extends WebChannel
175 {
176     function output($user, $text)
177     {
178         $this->out->startHTML('text/xml;charset=utf-8');
179         $this->out->elementStart('head');
180         // TRANS: Title for command results.
181         $this->out->element('title', null, _('Command results'));
182         $this->out->elementEnd('head');
183         $this->out->elementStart('body');
184         $this->out->element('p', array('id' => 'command_result'), $text);
185         $this->out->elementEnd('body');
186         $this->out->endHTML();
187     }
188
189     function error($user, $text)
190     {
191         $this->out->startHTML('text/xml;charset=utf-8');
192         $this->out->elementStart('head');
193         // TRANS: Title for command results.
194         $this->out->element('title', null, _('AJAX error'));
195         $this->out->elementEnd('head');
196         $this->out->elementStart('body');
197         $this->out->element('p', array('id' => 'error'), $text);
198         $this->out->elementEnd('body');
199         $this->out->endHTML();
200     }
201 }
202
203 class MailChannel extends Channel
204 {
205     var $addr = null;
206
207     function source()
208     {
209         return 'mail';
210     }
211
212     function __construct($addr=null)
213     {
214         $this->addr = $addr;
215     }
216
217     function on($user)
218     {
219         return $this->set_notify($user, 1);
220     }
221
222     function off($user)
223     {
224         return $this->set_notify($user, 0);
225     }
226
227     function output($user, $text)
228     {
229         $headers['From'] = $user->incomingemail;
230         $headers['To'] = $this->addr;
231
232         // TRANS: E-mail subject when a command has completed.
233         $headers['Subject'] = _('Command complete');
234
235         return mail_send(array($this->addr), $headers, $text);
236     }
237
238     function error($user, $text)
239     {
240         $headers['From'] = $user->incomingemail;
241         $headers['To'] = $this->addr;
242
243         // TRANS: E-mail subject when a command has failed.
244         $headers['Subject'] = _('Command failed');
245
246         return mail_send(array($this->addr), $headers, $text);
247     }
248
249     function set_notify($user, $value)
250     {
251         $orig = clone($user);
252         $user->smsnotify = $value;
253         $result = $user->update($orig);
254         if (!$result) {
255             common_log_db_error($user, 'UPDATE', __FILE__);
256             return false;
257         }
258         return true;
259     }
260 }