9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie
23 * End-user interface that produces console output when running the bot from
28 * @author Phergie Development Team <team@phergie.org>
29 * @license http://phergie.org/license New BSD License
30 * @link http://pear.phergie.org/package/Phergie
32 class Phergie_Ui_Console extends Phergie_Ui_Abstract
35 * Flag that toggles all console output
42 * Format for timestamps included in console output
45 * @link http://php.net/date
50 * Constructor to initialize object properties.
54 public function __construct()
56 $this->enabled = true;
57 $this->format = 'H:i:s';
61 * Outputs a timestamped line to the console if console output is enabled.
63 * @param string $line Line to output
67 protected function console($line)
70 echo date($this->format), ' ', $line, PHP_EOL;
75 * Returns whether console output is enabled.
77 * @return bool TRUE if console output is enabled, FALSE otherwise
79 public function isEnabled()
81 return $this->enabled;
85 * Sets whether console output is enabled.
87 * @param bool $enabled TRUE to enable console output, FALSE otherwise,
90 * @return Phergie_Ui_Console Provides a fluent interface
92 public function setEnabled($enabled = true)
94 $this->enabled = (bool) $enabled;
99 * Returns the format used for timestamps in console output.
102 * @link http://php.net/date
104 public function getFormat()
106 return $this->format;
110 * Sets the format used for timestamps in console output, overwriting
111 * any previous format used.
113 * @param string $format Timestamp format
115 * @return Phergie_Ui_Console Provides a fluent interface
116 * @link http://php.net/date
118 public function setFormat($format)
120 $this->format = (string) $format;
125 * Outputs a prompt when a server connection is attempted.
127 * @param string $host Server hostname
131 public function onConnect($host)
133 $this->console('Connecting to ' . $host);
137 * Outputs a prompt when a plugin is loaded successfully.
139 * @param string $plugin Short name of the plugin
143 public function onPluginLoad($plugin)
145 $this->console('Loaded plugin ' . $plugin);
149 * Outputs a prompt when a plugin fails to load.
151 * @param string $plugin Short name of the plugin
152 * @param string $message Message describing the reason for the failure
156 public function onPluginFailure($plugin, $message)
158 $this->console('Unable to load plugin ' . $plugin . ' - ' . $message);
162 * Outputs a prompt when the bot receives an IRC event.
164 * @param Phergie_Event_Abstract $event Received event
165 * @param Phergie_Connection $connection Connection on which the
170 public function onEvent(Phergie_Event_Abstract $event,
171 Phergie_Connection $connection
173 $host = $connection->getHostmask()->getHost();
174 $this->console($host . ' <- ' . $event->getRawData());
178 * Outputs a prompt when the bot sends a command to a server.
180 * @param Phergie_Event_Command $event Event representing the
182 * @param Phergie_Connection $connection Connection on which the
183 * command is being sent
187 public function onCommand(Phergie_Event_Command $event,
188 Phergie_Connection $connection
190 $plugin = $event->getPlugin()->getName();
191 $host = $connection->getHostmask()->getHost();
192 $type = strtoupper($event->getType());
193 $args = implode(' ', $event->getArguments());
195 $plugin . ' plugin: ' .
196 $host . ' -> ' . $type . ' ' . $args
201 * Outputs a prompt when the bot terminates a connection to a server.
203 * @param Phergie_Connection $connection Terminated connection
207 public function onQuit(Phergie_Connection $connection)
209 $host = $connection->getHostmask()->getHost();
210 $this->console('Disconnecting from ' . $host);
214 * Outputs a prompt when the bot shuts down after terminating all server
219 public function onShutdown()
221 $this->console('Shutting down');