]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/channel.php
9cd16843d6051851399d5759aa0b11c0b51d63ff
[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('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 XMPPChannel extends Channel
51 {
52
53     var $conn = null;
54
55     function source()
56     {
57         return 'xmpp';
58     }
59
60     function __construct($conn)
61     {
62         $this->conn = $conn;
63     }
64
65     function on($user)
66     {
67         return $this->set_notify($user, 1);
68     }
69
70     function off($user)
71     {
72         return $this->set_notify($user, 0);
73     }
74
75     function output($user, $text)
76     {
77         $text = '['.common_config('site', 'name') . '] ' . $text;
78         jabber_send_message($user->jabber, $text);
79     }
80
81     function error($user, $text)
82     {
83         $text = '['.common_config('site', 'name') . '] ' . $text;
84         jabber_send_message($user->jabber, $text);
85     }
86
87     function set_notify(&$user, $notify)
88     {
89         $orig = clone($user);
90         $user->jabbernotify = $notify;
91         $result = $user->update($orig);
92         if (!$result) {
93             $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
94             common_log(LOG_ERR,
95                        'Could not set notify flag to ' . $notify .
96                        ' for user ' . common_log_objstring($user) .
97                        ': ' . $last_error->message);
98             return false;
99         } else {
100             common_log(LOG_INFO,
101                        'User ' . $user->nickname . ' set notify flag to ' . $notify);
102             return true;
103         }
104     }
105 }
106
107 class WebChannel extends Channel
108 {
109     var $out = null;
110
111     function __construct($out=null)
112     {
113         $this->out = $out;
114     }
115
116     function source()
117     {
118         return 'web';
119     }
120
121     function on($user)
122     {
123         return false;
124     }
125
126     function off($user)
127     {
128         return false;
129     }
130
131     function output($user, $text)
132     {
133         # XXX: buffer all output and send it at the end
134         # XXX: even better, redirect to appropriate page
135         #      depending on what command was run
136         $this->out->startHTML();
137         $this->out->elementStart('head');
138         $this->out->element('title', null, _('Command results'));
139         $this->out->elementEnd('head');
140         $this->out->elementStart('body');
141         $this->out->element('p', array('id' => 'command_result'), $text);
142         $this->out->elementEnd('body');
143         $this->out->endHTML();
144     }
145
146     function error($user, $text)
147     {
148         common_user_error($text);
149     }
150 }
151
152 class AjaxWebChannel extends WebChannel
153 {
154     function output($user, $text)
155     {
156         $this->out->startHTML('text/xml;charset=utf-8');
157         $this->out->elementStart('head');
158         $this->out->element('title', null, _('Command results'));
159         $this->out->elementEnd('head');
160         $this->out->elementStart('body');
161         $this->out->element('p', array('id' => 'command_result'), $text);
162         $this->out->elementEnd('body');
163         $this->out->endHTML();
164     }
165
166     function error($user, $text)
167     {
168         $this->out->startHTML('text/xml;charset=utf-8');
169         $this->out->elementStart('head');
170         $this->out->element('title', null, _('Ajax Error'));
171         $this->out->elementEnd('head');
172         $this->out->elementStart('body');
173         $this->out->element('p', array('id' => 'error'), $text);
174         $this->out->elementEnd('body');
175         $this->out->endHTML();
176     }
177 }
178
179 class MailChannel extends Channel
180 {
181
182     var $addr = null;
183
184     function source()
185     {
186         return 'mail';
187     }
188
189     function __construct($addr=null)
190     {
191         $this->addr = $addr;
192     }
193
194     function on($user)
195     {
196         return $this->set_notify($user, 1);
197     }
198
199     function off($user)
200     {
201         return $this->set_notify($user, 0);
202     }
203
204     function output($user, $text)
205     {
206
207         $headers['From'] = $user->incomingemail;
208         $headers['To'] = $this->addr;
209
210         $headers['Subject'] = _('Command complete');
211
212         return mail_send(array($this->addr), $headers, $text);
213     }
214
215     function error($user, $text)
216     {
217
218         $headers['From'] = $user->incomingemail;
219         $headers['To'] = $this->addr;
220
221         $headers['Subject'] = _('Command failed');
222
223         return mail_send(array($this->addr), $headers, $text);
224     }
225
226     function set_notify($user, $value)
227     {
228         $orig = clone($user);
229         $user->smsnotify = $value;
230         $result = $user->update($orig);
231         if (!$result) {
232             common_log_db_error($user, 'UPDATE', __FILE__);
233             return false;
234         }
235         return true;
236     }
237 }