We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

channel.php 5.77 KB
Newer Older
1 2
<?php
/*
3
 * StatusNet - the distributed open-source microblogging tool
4
 * Copyright (C) 2008, 2009, StatusNet, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

20
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21

22
class Channel
23
{
24 25
    function on($user)
    {
26 27 28
        return false;
    }

29 30
    function off($user)
    {
31 32 33
        return false;
    }

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()
    {
Evan Prodromou's avatar
Evan Prodromou committed
46
        return null;
47
    }
48 49
}

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
class CLIChannel extends Channel
{
    function source()
    {
        return 'cli';
    }

    function output($user, $text)
    {
        $site = common_config('site', 'name');
        print "[{$user->nickname}@{$site}] $text\n";
    }

    function error($user, $text)
    {
        $this->output($user, $text);
    }
}

69 70
class XMPPChannel extends Channel
{
71

Evan Prodromou's avatar
Evan Prodromou committed
72
    var $conn = null;
73

74 75
    function source()
    {
76 77
        return 'xmpp';
    }
78

79 80
    function __construct($conn)
    {
81 82
        $this->conn = $conn;
    }
83

84 85
    function on($user)
    {
86 87
        return $this->set_notify($user, 1);
    }
88

89 90
    function off($user)
    {
91 92 93
        return $this->set_notify($user, 0);
    }

94 95
    function output($user, $text)
    {
96 97 98
        $text = '['.common_config('site', 'name') . '] ' . $text;
        jabber_send_message($user->jabber, $text);
    }
99

100 101
    function error($user, $text)
    {
102 103 104
        $text = '['.common_config('site', 'name') . '] ' . $text;
        jabber_send_message($user->jabber, $text);
    }
105

106 107
    function set_notify(&$user, $notify)
    {
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        $orig = clone($user);
        $user->jabbernotify = $notify;
        $result = $user->update($orig);
        if (!$result) {
            $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
            common_log(LOG_ERR,
                       'Could not set notify flag to ' . $notify .
                       ' for user ' . common_log_objstring($user) .
                       ': ' . $last_error->message);
            return false;
        } else {
            common_log(LOG_INFO,
                       'User ' . $user->nickname . ' set notify flag to ' . $notify);
            return true;
        }
    }
124 125
}

126 127
class WebChannel extends Channel
{
128 129 130 131 132 133
    var $out = null;

    function __construct($out=null)
    {
        $this->out = $out;
    }
134

135 136
    function source()
    {
137 138
        return 'web';
    }
139

140 141
    function on($user)
    {
142 143
        return false;
    }
144

145 146
    function off($user)
    {
147 148 149
        return false;
    }

150 151
    function output($user, $text)
    {
152 153 154
        # XXX: buffer all output and send it at the end
        # XXX: even better, redirect to appropriate page
        #      depending on what command was run
155 156 157 158 159 160 161 162
        $this->out->startHTML();
        $this->out->elementStart('head');
        $this->out->element('title', null, _('Command results'));
        $this->out->elementEnd('head');
        $this->out->elementStart('body');
        $this->out->element('p', array('id' => 'command_result'), $text);
        $this->out->elementEnd('body');
        $this->out->endHTML();
163
    }
164

165 166
    function error($user, $text)
    {
167 168
        common_user_error($text);
    }
169 170
}

171 172
class AjaxWebChannel extends WebChannel
{
173 174
    function output($user, $text)
    {
175 176 177 178 179 180 181 182
        $this->out->startHTML('text/xml;charset=utf-8');
        $this->out->elementStart('head');
        $this->out->element('title', null, _('Command results'));
        $this->out->elementEnd('head');
        $this->out->elementStart('body');
        $this->out->element('p', array('id' => 'command_result'), $text);
        $this->out->elementEnd('body');
        $this->out->endHTML();
183 184
    }

185 186
    function error($user, $text)
    {
187 188 189 190 191 192 193 194
        $this->out->startHTML('text/xml;charset=utf-8');
        $this->out->elementStart('head');
        $this->out->element('title', null, _('Ajax Error'));
        $this->out->elementEnd('head');
        $this->out->elementStart('body');
        $this->out->element('p', array('id' => 'error'), $text);
        $this->out->elementEnd('body');
        $this->out->endHTML();
195
    }
196 197
}

198 199
class MailChannel extends Channel
{
200

Evan Prodromou's avatar
Evan Prodromou committed
201
    var $addr = null;
202

203 204
    function source()
    {
205 206
        return 'mail';
    }
207

208 209
    function __construct($addr=null)
    {
210 211
        $this->addr = $addr;
    }
212

213 214
    function on($user)
    {
215 216
        return $this->set_notify($user, 1);
    }
217

218 219
    function off($user)
    {
220 221 222
        return $this->set_notify($user, 0);
    }

223 224
    function output($user, $text)
    {
225 226 227

        $headers['From'] = $user->incomingemail;
        $headers['To'] = $this->addr;
228

229 230 231 232
        $headers['Subject'] = _('Command complete');

        return mail_send(array($this->addr), $headers, $text);
    }
233

234 235
    function error($user, $text)
    {
236

237 238
        $headers['From'] = $user->incomingemail;
        $headers['To'] = $this->addr;
239

240 241 242 243
        $headers['Subject'] = _('Command failed');

        return mail_send(array($this->addr), $headers, $text);
    }
244

245 246
    function set_notify($user, $value)
    {
247 248 249 250 251 252 253 254 255
        $orig = clone($user);
        $user->smsnotify = $value;
        $result = $user->update($orig);
        if (!$result) {
            common_log_db_error($user, 'UPDATE', __FILE__);
            return false;
        }
        return true;
    }
256
}