]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Ui/Console.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Ui / Console.php
1 <?php
2 /**
3  * Phergie 
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
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
13  *
14  * @category  Phergie 
15  * @package   Phergie
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
20  */
21
22 /**
23  * End-user interface that produces console output when running the bot from 
24  * a shell.
25  *
26  * @category Phergie 
27  * @package  Phergie
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
31  */
32 class Phergie_Ui_Console extends Phergie_Ui_Abstract
33 {
34     /**
35      * Flag that toggles all console output
36      *
37      * @var bool
38      */
39     protected $enabled;
40
41     /**
42      * Format for timestamps included in console output
43      *
44      * @var string
45      * @link http://php.net/date
46      */
47     protected $format;
48
49     /**
50      * Constructor to initialize object properties.
51      *
52      * @return void
53      */
54     public function __construct()
55     {
56         $this->enabled = true;
57         $this->format = 'H:i:s';
58     }
59
60     /** 
61      * Outputs a timestamped line to the console if console output is enabled.
62      *
63      * @param string $line Line to output
64      *
65      * @return void
66      */
67     protected function console($line)
68     {
69         if ($this->enabled) {
70             echo date($this->format), ' ', $line, PHP_EOL;
71         }
72     }
73
74     /**
75      * Returns whether console output is enabled.
76      *
77      * @return bool TRUE if console output is enabled, FALSE otherwise
78      */
79     public function isEnabled()
80     {
81         return $this->enabled;
82     }
83
84     /**
85      * Sets whether console output is enabled.
86      *
87      * @param bool $enabled TRUE to enable console output, FALSE otherwise, 
88      *        defaults to TRUE
89      *
90      * @return Phergie_Ui_Console Provides a fluent interface
91      */
92     public function setEnabled($enabled = true)
93     {
94         $this->enabled = (bool) $enabled;
95         return $this;
96     }
97
98     /**
99      * Returns the format used for timestamps in console output.
100      *
101      * @return string
102      * @link http://php.net/date
103      */
104     public function getFormat()
105     {
106         return $this->format;
107     }
108
109     /**
110      * Sets the format used for timestamps in console output, overwriting 
111      * any previous format used.
112      *
113      * @param string $format Timestamp format
114      *
115      * @return Phergie_Ui_Console Provides a fluent interface
116      * @link http://php.net/date
117      */
118     public function setFormat($format)
119     {
120         $this->format = (string) $format;
121         return $this;
122     }
123
124     /**
125      * Outputs a prompt when a server connection is attempted.
126      *
127      * @param string $host Server hostname
128      *
129      * @return void 
130      */
131     public function onConnect($host)
132     {
133         $this->console('Connecting to ' . $host);
134     }
135
136     /**
137      * Outputs a prompt when a plugin is loaded successfully. 
138      *
139      * @param string $plugin Short name of the plugin
140      *
141      * @return void 
142      */
143     public function onPluginLoad($plugin)
144     {
145         $this->console('Loaded plugin ' . $plugin);
146     }
147
148     /**
149      * Outputs a prompt when a plugin fails to load.
150      *
151      * @param string $plugin  Short name of the plugin
152      * @param string $message Message describing the reason for the failure
153      *
154      * @return void 
155      */
156     public function onPluginFailure($plugin, $message)
157     {
158         $this->console('Unable to load plugin ' . $plugin . ' - ' . $message);
159     }
160
161     /**
162      * Outputs a prompt when the bot receives an IRC event. 
163      *
164      * @param Phergie_Event_Abstract $event      Received event
165      * @param Phergie_Connection     $connection Connection on which the 
166      *        event was received
167      *
168      * @return void
169      */
170     public function onEvent(Phergie_Event_Abstract $event, 
171         Phergie_Connection $connection
172     ) {
173         $host = $connection->getHostmask()->getHost();
174         $this->console($host . ' <- ' . $event->getRawData());
175     }
176
177     /**
178      * Outputs a prompt when the bot sends a command to a server.
179      *
180      * @param Phergie_Event_Command $event      Event representing the 
181      *        command being sent
182      * @param Phergie_Connection    $connection Connection on which the 
183      *        command is being sent 
184      *
185      * @return void
186      */
187     public function onCommand(Phergie_Event_Command $event, 
188         Phergie_Connection $connection
189     ) {
190         $plugin = $event->getPlugin()->getName();
191         $host = $connection->getHostmask()->getHost();
192         $type = strtoupper($event->getType());
193         $args = implode(' ', $event->getArguments());
194         $this->console(
195             $plugin . ' plugin: ' . 
196             $host . ' -> ' . $type . ' ' . $args
197         ); 
198     }
199
200     /**
201      * Outputs a prompt when the bot terminates a connection to a server.
202      *
203      * @param Phergie_Connection $connection Terminated connection 
204      *
205      * @return void
206      */
207     public function onQuit(Phergie_Connection $connection)
208     {
209         $host = $connection->getHostmask()->getHost();
210         $this->console('Disconnecting from ' . $host);
211     }
212
213     /**
214      * Outputs a prompt when the bot shuts down after terminating all server 
215      * connections.
216      *
217      * @return void
218      */
219     public function onShutdown()
220     {
221         $this->console('Shutting down');
222     }
223 }