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 * Base class for plugins to provide event handler stubs and commonly needed
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 abstract class Phergie_Plugin_Abstract
35 * Current configuration handler
42 * Plugin handler used to provide access to other plugins
44 * @var Phergie_Plugin_Handler
49 * Current event handler instance for outgoing events
51 * @var Phergie_Event_Handler
56 * Current connection instance
58 * @var Phergie_Connection
60 protected $connection;
63 * Current incoming event being handled
65 * @var Phergie_Event_Request|Phergie_Event_Response
70 * Returns the short name for the plugin based on its class name.
74 public function getName()
76 return substr(strrchr(get_class($this), '_'), 1);
80 * Indicates that the plugin failed to load due to an unsatisfied
81 * runtime requirement, such as a missing dependency.
83 * @param string $message Error message to provide more information
84 * about the reason for the failure
86 * @return Phergie_Plugin_Abstract Provides a fluent interface
87 * @throws Phergie_Plugin_Exception Always
89 protected function fail($message)
91 throw new Phergie_Plugin_Exception(
93 Phergie_Plugin_Exception::ERR_REQUIREMENT_UNSATISFIED
98 * Sets the current configuration handler.
100 * @param Phergie_Config $config Configuration handler
102 * @return Phergie_Plugin_Abstract Provides a fluent interface
104 public function setConfig(Phergie_Config $config)
106 $this->config = $config;
111 * Returns the current configuration handler or the value of a single
114 * @param string $name Optional name of a setting for which the value
115 * should be returned instead of the entire configuration handler
116 * @param mixed $default Optional default value to return if no value
117 * is set for the setting indicated by $name
119 * @return Phergie_Config|mixed Configuration handler or value of the
120 * setting specified by $name
121 * @throws Phergie_Plugin_Exception No configuration handler has been set
123 public function getConfig($name = null, $default = null)
125 if (empty($this->config)) {
126 throw new Phergie_Plugin_Exception(
127 'Configuration handler cannot be accessed before one is set',
128 Phergie_Plugin_Exception::ERR_NO_CONFIG_HANDLER
131 if (!is_null($name)) {
132 if (!isset($this->config[$name])) {
135 return $this->config[$name];
137 return $this->config;
141 * Sets the current plugin handler.
143 * @param Phergie_Plugin_Handler $handler Plugin handler
145 * @return Phergie_Plugin_Abstract Provides a fluent interface
147 public function setPluginHandler(Phergie_Plugin_Handler $handler)
149 $this->plugins = $handler;
154 * Returns the current plugin handler.
156 * @return Phergie_Plugin_Handler
157 * @throws Phergie_Plugin_Exception No plugin handler has been set
159 public function getPluginHandler()
161 if (empty($this->plugins)) {
162 throw new Phergie_Plugin_Exception(
163 'Plugin handler cannot be accessed before one is set',
164 Phergie_Plugin_Exception::ERR_NO_PLUGIN_HANDLER
167 return $this->plugins;
171 * Sets the current event handler.
173 * @param Phergie_Event_Handler $handler Event handler
175 * @return Phergie_Plugin_Abstract Provides a fluent interface
177 public function setEventHandler(Phergie_Event_Handler $handler)
179 $this->events = $handler;
184 * Returns the current event handler.
186 * @return Phergie_Event_Handler
187 * @throws Phergie_Plugin_Exception No event handler has been set
189 public function getEventHandler()
191 if (empty($this->events)) {
192 throw new Phergie_Plugin_Exception(
193 'Event handler cannot be accessed before one is set',
194 Phergie_Plugin_Exception::ERR_NO_EVENT_HANDLER
197 return $this->events;
201 * Sets the current connection.
203 * @param Phergie_Connection $connection Connection
205 * @return Phergie_Plugin_Abstract Provides a fluent interface
207 public function setConnection(Phergie_Connection $connection)
209 $this->connection = $connection;
214 * Returns the current event connection.
216 * @return Phergie_Connection
217 * @throws Phergie_Plugin_Exception No connection has been set
219 public function getConnection()
221 if (empty($this->connection)) {
222 throw new Phergie_Plugin_Exception(
223 'Connection cannot be accessed before one is set',
224 Phergie_Plugin_Exception::ERR_NO_CONNECTION
227 return $this->connection;
231 * Sets the current incoming event to be handled.
233 * @param Phergie_Event_Request|Phergie_Event_Response $event Event
235 * @return Phergie_Plugin_Abstract Provides a fluent interface
237 public function setEvent($event)
239 $this->event = $event;
244 * Returns the current incoming event to be handled.
246 * @return Phergie_Event_Request|Phergie_Event_Response
248 public function getEvent()
250 if (empty($this->connection)) {
251 throw new Phergie_Plugin_Exception(
252 'Event cannot be accessed before one is set',
253 Phergie_Plugin_Exception::ERR_NO_EVENT
260 * Provides do* methods with signatures identical to those of
261 * Phergie_Driver_Abstract but that queue up events to be dispatched
264 * @param string $name Name of the method called
265 * @param array $args Arguments passed in the call
269 public function __call($name, array $args)
271 $subcmd = substr($name, 0, 2);
272 if ($subcmd == 'do') {
273 $type = strtolower(substr($name, 2));
274 $this->getEventHandler()->addEvent($this, $type, $args);
275 } else if ($subcmd != 'on') {
276 throw new Phergie_Plugin_Exception(
277 'Called invalid method ' . $name . ' in ' . get_class($this),
278 Phergie_Plugin_Exception::ERR_INVALID_CALL
284 * Handler for when the plugin is initially loaded - useful for checking
285 * runtime dependencies or performing any setup necessary for the plugin
286 * to function properly such as initializing a database.
290 public function onLoad()
295 * Handler for when the bot initially connects to a server.
299 public function onConnect()
304 * Handler for each tick, a single iteration of the continuous loop
305 * executed by the bot to receive, handle, and send events - useful for
306 * repeated execution of tasks on a time interval.
310 public function onTick()
315 * Handler for when any event is received but has not yet been dispatched
316 * to the plugin handler method specific to its event type.
318 * @return bool|null|void FALSE to short-circuit further event
319 * processing, TRUE or NULL otherwise
321 public function preEvent()
326 * Handler for after plugin processing of an event has concluded but
327 * before any events triggered in response by plugins are sent to the
328 * server - useful for modifying outgoing events before they are sent.
332 public function preDispatch()
337 * Handler for after any events triggered by plugins in response to a
338 * received event are sent to the server.
342 public function postDispatch()
347 * Handler for when the server prompts the client for a nick.
350 * @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_1_2
352 public function onNick()
357 * Handler for when a user obtains operator privileges.
360 * @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_1_5
362 public function onOper()
367 * Handler for when the client session is about to be terminated.
370 * @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_1_6
372 public function onQuit()
377 * Handler for when a user joins a channel.
380 * @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_2_1
382 public function onJoin()
387 * Handler for when a user leaves a channel.
390 * @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_2_2
392 public function onPart()
397 * Handler for when a user or channel mode is changed.
400 * @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_2_3
402 public function onMode()
407 * Handler for when a channel topic is viewed or changed.
410 * @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_2_4
412 public function onTopic()
417 * Handler for when a message is received from a channel or user.
420 * @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_4_1
422 public function onPrivmsg()
427 * Handler for when the bot receives a CTCP ACTION request.
430 * @link http://www.invlogic.com/irc/ctcp.html#4.4
432 public function onAction()
437 * Handler for when a notice is received.
440 * @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_4_2
442 public function onNotice()
447 * Handler for when a user is kicked from a channel.
450 * @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_2_8
452 public function onKick()
457 * Handler for when the bot receives a ping event from a server, at
458 * which point it is expected to respond with a pong request within
459 * a short period else the server may terminate its connection.
462 * @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_6_2
464 public function onPing()
469 * Handler for when the bot receives a CTCP TIME request.
472 * @link http://www.invlogic.com/irc/ctcp.html#4.6
474 public function onTime()
479 * Handler for when the bot receives a CTCP VERSION request.
482 * @link http://www.invlogic.com/irc/ctcp.html#4.1
484 public function onVersion()
489 * Handler for when the bot receives a CTCP PING request.
492 * @link http://www.invlogic.com/irc/ctcp.html#4.2
494 public function onCtcpPing()
499 * Handler for when the bot receives a CTCP request of an unknown type.
502 * @link http://www.invlogic.com/irc/ctcp.html
504 public function onCtcp()
509 * Handler for when a reply is received for a CTCP PING request sent by
513 * @link http://www.invlogic.com/irc/ctcp.html#4.2
515 public function onPingReply()
520 * Handler for when a reply is received for a CTCP TIME request sent by
524 * @link http://www.invlogic.com/irc/ctcp.html#4.6
526 public function onTimeReply()
531 * Handler for when a reply is received for a CTCP VERSION request sent
535 * @link http://www.invlogic.com/irc/ctcp.html#4.1
537 public function onVersionReply()
542 * Handler for when a reply received for a CTCP request of an unknown
546 * @link http://www.invlogic.com/irc/ctcp.html
548 public function onCtcpReply()
553 * Handler for when the bot receives a kill request from a server.
556 * @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_6_1
558 public function onKill()
563 * Handler for when the bot receives an invitation to join a channel.
566 * @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_2_7
568 public function onInvite()
573 * Handler for when a server response is received to a command issued by
577 * @link http://irchelp.org/irchelp/rfc/chapter6.html
579 public function onResponse()