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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
<?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
*/
/**
* Base class for plugins to provide event handler stubs and commonly needed
* functionality.
*
* @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
*/
abstract class Phergie_Plugin_Abstract
{
/**
* Current configuration handler
*
* @var Phergie_Config
*/
protected $config;
/**
* Plugin handler used to provide access to other plugins
*
* @var Phergie_Plugin_Handler
*/
protected $plugins;
/**
* Current event handler instance for outgoing events
*
* @var Phergie_Event_Handler
*/
protected $events;
/**
* Current connection instance
*
* @var Phergie_Connection
*/
protected $connection;
/**
* Current incoming event being handled
*
* @var Phergie_Event_Request|Phergie_Event_Response
*/
protected $event;
/**
* Plugin short name
*
* @var string
*/
protected $name;
/**
* Returns the short name for the plugin based on its class name.
*
* @return string
*/
public function getName()
{
if (empty($this->name)) {
$this->name = substr(strrchr(get_class($this), '_'), 1);
}
return $this->name;
}
/**
* Sets the short name for the plugin.
*
* @param string $name Plugin short name
*
* @return Phergie_Plugin_Abstract Provides a fluent interface
*/
public function setName($name)
{
$this->name = (string) $name;
return $this;
}
/**
* Indicates that the plugin failed to load due to an unsatisfied
* runtime requirement, such as a missing dependency.
*
* @param string $message Error message to provide more information
* about the reason for the failure
*
* @return Phergie_Plugin_Abstract Provides a fluent interface
* @throws Phergie_Plugin_Exception Always
*/
protected function fail($message)
{
throw new Phergie_Plugin_Exception(
$message,
Phergie_Plugin_Exception::ERR_REQUIREMENT_UNSATISFIED
);
}
/**
* Sets the current configuration handler.
*
* @param Phergie_Config $config Configuration handler
*
* @return Phergie_Plugin_Abstract Provides a fluent interface
*/
public function setConfig(Phergie_Config $config)
{
$this->config = $config;
return $this;
}
/**
* Returns the current configuration handler or the value of a single
* setting from it.
*
* @param string $name Optional name of a setting for which the value
* should be returned instead of the entire configuration handler
* @param mixed $default Optional default value to return if no value
* is set for the setting indicated by $name
*
* @return Phergie_Config|mixed Configuration handler or value of the
* setting specified by $name
* @throws Phergie_Plugin_Exception No configuration handler has been set
*/
public function getConfig($name = null, $default = null)
{
if (empty($this->config)) {
throw new Phergie_Plugin_Exception(
'Configuration handler cannot be accessed before one is set',
Phergie_Plugin_Exception::ERR_NO_CONFIG_HANDLER
);
}
if (!is_null($name)) {
if (!isset($this->config[$name])) {
return $default;
}
return $this->config[$name];
}
return $this->config;
}
/**
* Sets the current plugin handler.
*
* @param Phergie_Plugin_Handler $handler Plugin handler
*
* @return Phergie_Plugin_Abstract Provides a fluent interface
*/
public function setPluginHandler(Phergie_Plugin_Handler $handler)
{
$this->plugins = $handler;
return $this;
}
/**
* Returns the current plugin handler.
*
* @return Phergie_Plugin_Handler
* @throws Phergie_Plugin_Exception No plugin handler has been set
*/
public function getPluginHandler()
{
if (empty($this->plugins)) {
throw new Phergie_Plugin_Exception(
'Plugin handler cannot be accessed before one is set',
Phergie_Plugin_Exception::ERR_NO_PLUGIN_HANDLER
);
}
return $this->plugins;
}
/**
* Sets the current event handler.
*
* @param Phergie_Event_Handler $handler Event handler
*
* @return Phergie_Plugin_Abstract Provides a fluent interface
*/
public function setEventHandler(Phergie_Event_Handler $handler)
{
$this->events = $handler;
return $this;
}
/**
* Returns the current event handler.
*
* @return Phergie_Event_Handler
* @throws Phergie_Plugin_Exception No event handler has been set
*/
public function getEventHandler()
{
if (empty($this->events)) {
throw new Phergie_Plugin_Exception(
'Event handler cannot be accessed before one is set',
Phergie_Plugin_Exception::ERR_NO_EVENT_HANDLER
);
}
return $this->events;
}
/**
* Sets the current connection.
*
* @param Phergie_Connection $connection Connection
*
* @return Phergie_Plugin_Abstract Provides a fluent interface
*/
public function setConnection(Phergie_Connection $connection)
{
$this->connection = $connection;
return $this;
}
/**
* Returns the current event connection.
*
* @return Phergie_Connection
* @throws Phergie_Plugin_Exception No connection has been set
*/
public function getConnection()
{
if (empty($this->connection)) {
throw new Phergie_Plugin_Exception(
'Connection cannot be accessed before one is set',
Phergie_Plugin_Exception::ERR_NO_CONNECTION
);
}
return $this->connection;
}
/**
* Sets the current incoming event to be handled.
*
* @param Phergie_Event_Request|Phergie_Event_Response $event Event
*
* @return Phergie_Plugin_Abstract Provides a fluent interface
*/
public function setEvent($event)
{
$this->event = $event;
return $this;
}
/**
* Returns the current incoming event to be handled.
*
* @return Phergie_Event_Request|Phergie_Event_Response
*/
public function getEvent()
{
if (empty($this->connection)) {
throw new Phergie_Plugin_Exception(
'Event cannot be accessed before one is set',
Phergie_Plugin_Exception::ERR_NO_EVENT
);
}
return $this->event;
}
/**
* Provides do* methods with signatures identical to those of
* Phergie_Driver_Abstract but that queue up events to be dispatched
* later.
*
* @param string $name Name of the method called
* @param array $args Arguments passed in the call
*
* @return mixed
*/
public function __call($name, array $args)
{
$subcmd = substr($name, 0, 2);
if ($subcmd == 'do') {
$type = strtolower(substr($name, 2));
$this->getEventHandler()->addEvent($this, $type, $args);
} else if ($subcmd != 'on') {
throw new Phergie_Plugin_Exception(
'Called invalid method ' . $name . ' in ' . get_class($this),
Phergie_Plugin_Exception::ERR_INVALID_CALL
);
}
}
/**
* Handler for when the plugin is initially loaded - useful for checking
* runtime dependencies or performing any setup necessary for the plugin
* to function properly such as initializing a database.
*
* @return void
*/
public function onLoad()
{
}
/**
* Handler for when the bot initially connects to a server.
*
* @return void
*/
public function onConnect()
{
}
/**
* Handler for each tick, a single iteration of the continuous loop
* executed by the bot to receive, handle, and send events - useful for
* repeated execution of tasks on a time interval.
*
* @return void
*/
public function onTick()
{
}
/**
* Handler for when any event is received but has not yet been dispatched
* to the plugin handler method specific to its event type.
*
* @return bool|null|void FALSE to short-circuit further event
* processing, TRUE or NULL otherwise
*/
public function preEvent()
{
}
/**
* Handler for after plugin processing of an event has concluded but
* before any events triggered in response by plugins are sent to the
* server - useful for modifying outgoing events before they are sent.
*
* @return void
*/
public function preDispatch()
{
}
/**
* Handler for after any events triggered by plugins in response to a
* received event are sent to the server.
*
* @return void
*/
public function postDispatch()
{
}
/**
* Handler for when the server prompts the client for a nick.
*
* @return void
* @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_1_2
*/
public function onNick()
{
}
/**
* Handler for when a user obtains operator privileges.
*
* @return void
* @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_1_5
*/
public function onOper()
{
}
/**
* Handler for when the client session is about to be terminated.
*
* @return void
* @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_1_6
*/
public function onQuit()
{
}
/**
* Handler for when a user joins a channel.
*
* @return void
* @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_2_1
*/
public function onJoin()
{
}
/**
* Handler for when a user leaves a channel.
*
* @return void
* @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_2_2
*/
public function onPart()
{
}
/**
* Handler for when a user or channel mode is changed.
*
* @return void
* @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_2_3
*/
public function onMode()
{
}
/**
* Handler for when a channel topic is viewed or changed.
*
* @return void
* @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_2_4
*/
public function onTopic()
{
}
/**
* Handler for when a message is received from a channel or user.
*
* @return void
* @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_4_1
*/
public function onPrivmsg()
{
}
/**
* Handler for when the bot receives a CTCP ACTION request.
*
* @return void
* @link http://www.invlogic.com/irc/ctcp.html#4.4
*/
public function onAction()
{
}
/**
* Handler for when a notice is received.
*
* @return void
* @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_4_2
*/
public function onNotice()
{
}
/**
* Handler for when a user is kicked from a channel.
*
* @return void
* @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_2_8
*/
public function onKick()
{
}
/**
* Handler for when the bot receives a ping event from a server, at
* which point it is expected to respond with a pong request within
* a short period else the server may terminate its connection.
*
* @return void
* @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_6_2
*/
public function onPing()
{
}
/**
* Handler for when the bot receives a CTCP TIME request.
*
* @return void
* @link http://www.invlogic.com/irc/ctcp.html#4.6
*/
public function onTime()
{
}
/**
* Handler for when the bot receives a CTCP VERSION request.
*
* @return void
* @link http://www.invlogic.com/irc/ctcp.html#4.1
*/
public function onVersion()
{
}
/**
* Handler for when the bot receives a CTCP PING request.
*
* @return void
* @link http://www.invlogic.com/irc/ctcp.html#4.2
*/
public function onCtcpPing()
{
}
/**
* Handler for when the bot receives a CTCP request of an unknown type.
*
* @return void
* @link http://www.invlogic.com/irc/ctcp.html
*/
public function onCtcp()
{
}
/**
* Handler for when a reply is received for a CTCP PING request sent by
* the bot.
*
* @return void
* @link http://www.invlogic.com/irc/ctcp.html#4.2
*/
public function onPingReply()
{
}
/**
* Handler for when a reply is received for a CTCP TIME request sent by
* the bot.
*
* @return void
* @link http://www.invlogic.com/irc/ctcp.html#4.6
*/
public function onTimeReply()
{
}
/**
* Handler for when a reply is received for a CTCP VERSION request sent
* by the bot.
*
* @return void
* @link http://www.invlogic.com/irc/ctcp.html#4.1
*/
public function onVersionReply()
{
}
/**
* Handler for when a reply received for a CTCP request of an unknown
* type.
*
* @return void
* @link http://www.invlogic.com/irc/ctcp.html
*/
public function onCtcpReply()
{
}
/**
* Handler for when the bot receives a kill request from a server.
*
* @return void
* @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_6_1
*/
public function onKill()
{
}
/**
* Handler for when the bot receives an invitation to join a channel.
*
* @return void
* @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_2_7
*/
public function onInvite()
{
}
/**
* Handler for when a server response is received to a command issued by
* the bot.
*
* @return void
* @link http://irchelp.org/irchelp/rfc/chapter6.html
*/
public function onResponse()
{
}
}
|