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