summaryrefslogtreecommitdiff
path: root/plugins/Irc/extlib/phergie/Phergie/Ui/Console.php
blob: a0a528b3f26d81786c140f1690145b635fa7fd80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
 * Phergie 
 *
 * PHP version 5
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.
 * It is also available through the world-wide-web at this URL:
 * http://phergie.org/license
 *
 * @category  Phergie 
 * @package   Phergie
 * @author    Phergie Development Team <team@phergie.org>
 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
 * @license   http://phergie.org/license New BSD License
 * @link      http://pear.phergie.org/package/Phergie
 */

/**
 * End-user interface that produces console output when running the bot from 
 * a shell.
 *
 * @category Phergie 
 * @package  Phergie
 * @author   Phergie Development Team <team@phergie.org>
 * @license  http://phergie.org/license New BSD License
 * @link     http://pear.phergie.org/package/Phergie
 */
class Phergie_Ui_Console extends Phergie_Ui_Abstract
{
    /**
     * Flag that toggles all console output
     *
     * @var bool
     */
    protected $enabled;

    /**
     * Format for timestamps included in console output
     *
     * @var string
     * @link http://php.net/date
     */
    protected $format;

    /**
     * Constructor to initialize object properties.
     *
     * @return void
     */
    public function __construct()
    {
        $this->enabled = true;
        $this->format = 'H:i:s';
    }

    /** 
     * Outputs a timestamped line to the console if console output is enabled.
     *
     * @param string $line Line to output
     *
     * @return void
     */
    protected function console($line)
    {
        if ($this->enabled) {
            echo date($this->format), ' ', $line, PHP_EOL;
        }
    }

    /**
     * Returns whether console output is enabled.
     *
     * @return bool TRUE if console output is enabled, FALSE otherwise
     */
    public function isEnabled()
    {
        return $this->enabled;
    }

    /**
     * Sets whether console output is enabled.
     *
     * @param bool $enabled TRUE to enable console output, FALSE otherwise, 
     *        defaults to TRUE
     *
     * @return Phergie_Ui_Console Provides a fluent interface
     */
    public function setEnabled($enabled = true)
    {
        $this->enabled = (bool) $enabled;
        return $this;
    }

    /**
     * Returns the format used for timestamps in console output.
     *
     * @return string
     * @link http://php.net/date
     */
    public function getFormat()
    {
        return $this->format;
    }

    /**
     * Sets the format used for timestamps in console output, overwriting 
     * any previous format used.
     *
     * @param string $format Timestamp format
     *
     * @return Phergie_Ui_Console Provides a fluent interface
     * @link http://php.net/date
     */
    public function setFormat($format)
    {
        $this->format = (string) $format;
        return $this;
    }

    /**
     * Outputs a prompt when a server connection is attempted.
     *
     * @param string $host Server hostname
     *
     * @return void 
     */
    public function onConnect($host)
    {
        $this->console('Connecting to ' . $host);
    }

    /**
     * Outputs a prompt when a plugin is loaded successfully. 
     *
     * @param string $plugin Short name of the plugin
     *
     * @return void 
     */
    public function onPluginLoad($plugin)
    {
        $this->console('Loaded plugin ' . $plugin);
    }

    /**
     * Outputs a prompt when a plugin fails to load.
     *
     * @param string $plugin  Short name of the plugin
     * @param string $message Message describing the reason for the failure
     *
     * @return void 
     */
    public function onPluginFailure($plugin, $message)
    {
        $this->console('Unable to load plugin ' . $plugin . ' - ' . $message);
    }

    /**
     * Outputs a prompt when the bot receives an IRC event. 
     *
     * @param Phergie_Event_Abstract $event      Received event
     * @param Phergie_Connection     $connection Connection on which the 
     *        event was received
     *
     * @return void
     */
    public function onEvent(Phergie_Event_Abstract $event, 
        Phergie_Connection $connection
    ) {
        $host = $connection->getHostmask()->getHost();
        $this->console($host . ' <- ' . $event->getRawData());
    }

    /**
     * Outputs a prompt when the bot sends a command to a server.
     *
     * @param Phergie_Event_Command $event      Event representing the 
     *        command being sent
     * @param Phergie_Connection    $connection Connection on which the 
     *        command is being sent 
     *
     * @return void
     */
    public function onCommand(Phergie_Event_Command $event, 
        Phergie_Connection $connection
    ) {
        $plugin = $event->getPlugin()->getName();
        $host = $connection->getHostmask()->getHost();
        $type = strtoupper($event->getType());
        $args = implode(' ', $event->getArguments());
        $this->console(
            $plugin . ' plugin: ' . 
            $host . ' -> ' . $type . ' ' . $args
        ); 
    }

    /**
     * Outputs a prompt when the bot terminates a connection to a server.
     *
     * @param Phergie_Connection $connection Terminated connection 
     *
     * @return void
     */
    public function onQuit(Phergie_Connection $connection)
    {
        $host = $connection->getHostmask()->getHost();
        $this->console('Disconnecting from ' . $host);
    }

    /**
     * Outputs a prompt when the bot shuts down after terminating all server 
     * connections.
     *
     * @return void
     */
    public function onShutdown()
    {
        $this->console('Shutting down');
    }
}