channel.php 4.72 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 23 24
/**
 * @todo Needs documentation.
 */
25
class Channel
26
{
27 28
    function on($user)
    {
29 30 31
        return false;
    }

32 33
    function off($user)
    {
34 35 36
        return false;
    }

37 38
    function output($user, $text)
    {
39 40
        return false;
    }
41

42 43
    function error($user, $text)
    {
44 45
        return false;
    }
46

47 48
    function source()
    {
Evan Prodromou's avatar
Evan Prodromou committed
49
        return null;
50
    }
51 52
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
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);
    }
}

72 73
class WebChannel extends Channel
{
74 75 76 77 78 79
    var $out = null;

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

81 82
    function source()
    {
83 84
        return 'web';
    }
85

86 87
    function on($user)
    {
88 89
        return false;
    }
90

91 92
    function off($user)
    {
93 94 95
        return false;
    }

96 97
    function output($user, $text)
    {
98 99 100
        // XXX: buffer all output and send it at the end
        // XXX: even better, redirect to appropriate page
        //      depending on what command was run
101 102
        $this->out->startHTML();
        $this->out->elementStart('head');
103
        // TRANS: Title for command results.
104 105 106 107 108 109
        $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();
110
    }
111

112 113
    function error($user, $text)
    {
114 115
        common_user_error($text);
    }
116 117
}

118 119
class AjaxWebChannel extends WebChannel
{
120 121
    function output($user, $text)
    {
122 123
        $this->out->startHTML('text/xml;charset=utf-8');
        $this->out->elementStart('head');
124
        // TRANS: Title for command results.
125 126 127 128 129 130
        $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();
131 132
    }

133 134
    function error($user, $text)
    {
135 136
        $this->out->startHTML('text/xml;charset=utf-8');
        $this->out->elementStart('head');
137 138
        // TRANS: Title for command results.
        $this->out->element('title', null, _('AJAX error'));
139 140 141 142 143
        $this->out->elementEnd('head');
        $this->out->elementStart('body');
        $this->out->element('p', array('id' => 'error'), $text);
        $this->out->elementEnd('body');
        $this->out->endHTML();
144
    }
145 146
}

147 148
class MailChannel extends Channel
{
Evan Prodromou's avatar
Evan Prodromou committed
149
    var $addr = null;
150

151 152
    function source()
    {
153 154
        return 'mail';
    }
155

156 157
    function __construct($addr=null)
    {
158 159
        $this->addr = $addr;
    }
160

161 162
    function on($user)
    {
Craig Andrews's avatar
Craig Andrews committed
163
        return $this->setNotify($user, 1);
164
    }
165

166 167
    function off($user)
    {
Craig Andrews's avatar
Craig Andrews committed
168
        return $this->setNotify($user, 0);
169 170
    }

171 172
    function output($user, $text)
    {
173 174
        $headers['From'] = $user->incomingemail;
        $headers['To'] = $this->addr;
175

176
        // TRANS: E-mail subject when a command has completed.
177 178 179 180
        $headers['Subject'] = _('Command complete');

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

182 183
    function error($user, $text)
    {
184 185
        $headers['From'] = $user->incomingemail;
        $headers['To'] = $this->addr;
186

187
        // TRANS: E-mail subject when a command has failed.
188 189 190 191
        $headers['Subject'] = _('Command failed');

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

Craig Andrews's avatar
Craig Andrews committed
193
    function setNotify($user, $value)
194
    {
195 196 197 198 199 200 201 202 203
        $orig = clone($user);
        $user->smsnotify = $value;
        $result = $user->update($orig);
        if (!$result) {
            common_log_db_error($user, 'UPDATE', __FILE__);
            return false;
        }
        return true;
    }
204
}