]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/channel.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 1.0.x
[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 WebChannel extends Channel
73 {
74     var $out = null;
75
76     function __construct($out=null)
77     {
78         $this->out = $out;
79     }
80
81     function source()
82     {
83         return 'web';
84     }
85
86     function on($user)
87     {
88         return false;
89     }
90
91     function off($user)
92     {
93         return false;
94     }
95
96     function output($user, $text)
97     {
98         # XXX: buffer all output and send it at the end
99         # XXX: even better, redirect to appropriate page
100         #      depending on what command was run
101         $this->out->startHTML();
102         $this->out->elementStart('head');
103         // TRANS: Title for command results.
104         $this->out->element('title', null, _('Command results'));
105         $this->out->elementEnd('head');
106         $this->out->elementStart('body');
107         $this->out->element('p', array('id' => 'command_result'), $text);
108         $this->out->elementEnd('body');
109         $this->out->endHTML();
110     }
111
112     function error($user, $text)
113     {
114         common_user_error($text);
115     }
116 }
117
118 class AjaxWebChannel extends WebChannel
119 {
120     function output($user, $text)
121     {
122         $this->out->startHTML('text/xml;charset=utf-8');
123         $this->out->elementStart('head');
124         // TRANS: Title for command results.
125         $this->out->element('title', null, _('Command results'));
126         $this->out->elementEnd('head');
127         $this->out->elementStart('body');
128         $this->out->element('p', array('id' => 'command_result'), $text);
129         $this->out->elementEnd('body');
130         $this->out->endHTML();
131     }
132
133     function error($user, $text)
134     {
135         $this->out->startHTML('text/xml;charset=utf-8');
136         $this->out->elementStart('head');
137         // TRANS: Title for command results.
138         $this->out->element('title', null, _('AJAX error'));
139         $this->out->elementEnd('head');
140         $this->out->elementStart('body');
141         $this->out->element('p', array('id' => 'error'), $text);
142         $this->out->elementEnd('body');
143         $this->out->endHTML();
144     }
145 }
146
147 class MailChannel extends Channel
148 {
149     var $addr = null;
150
151     function source()
152     {
153         return 'mail';
154     }
155
156     function __construct($addr=null)
157     {
158         $this->addr = $addr;
159     }
160
161     function on($user)
162     {
163         return $this->setNotify($user, 1);
164     }
165
166     function off($user)
167     {
168         return $this->setNotify($user, 0);
169     }
170
171     function output($user, $text)
172     {
173         $headers['From'] = $user->incomingemail;
174         $headers['To'] = $this->addr;
175
176         // TRANS: E-mail subject when a command has completed.
177         $headers['Subject'] = _('Command complete');
178
179         return mail_send(array($this->addr), $headers, $text);
180     }
181
182     function error($user, $text)
183     {
184         $headers['From'] = $user->incomingemail;
185         $headers['To'] = $this->addr;
186
187         // TRANS: E-mail subject when a command has failed.
188         $headers['Subject'] = _('Command failed');
189
190         return mail_send(array($this->addr), $headers, $text);
191     }
192
193     function setNotify($user, $value)
194     {
195         $orig = clone($user);
196         $user->smsnotify = $value;
197         $result = $user->update($orig);
198         if (!$result) {
199             common_log_db_error($user, 'UPDATE', __FILE__);
200             return false;
201         }
202         return true;
203     }
204 }