]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/AltNick.php
16d5f9b9ccb3df830beda3f31cb21fa81158f5a0
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / AltNick.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_Plugin_AltNick
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_Plugin_AltNick
20  */
21
22 /**
23  * Handles switching to alternate nicks in cases where the primary nick is 
24  * not available for use.
25  *
26  * @category Phergie 
27  * @package  Phergie_Plugin_AltNick
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_Plugin_AltNick
31  * @uses     extension spl 
32  */
33 class Phergie_Plugin_AltNick extends Phergie_Plugin_Abstract
34 {
35     /**
36      * Iterator for the alternate nick list
37      *
38      * @var ArrayIterator 
39      */
40     protected $iterator;
41
42     /**
43      * Initializes instance variables.
44      *
45      * @return void
46      */
47     public function onConnect()
48     {
49         if ($this->config['altnick.nicks']) {
50             if (is_string($this->config['altnick.nicks'])) {
51                 $this->config['altnick.nicks'] 
52                     = array($this->config['altnick.nicks']);
53             }
54             $this->iterator = new ArrayIterator($this->config['altnick.nicks']);
55         }
56     }
57
58     /**
59      * Switches to alternate nicks as needed when nick collisions occur.
60      *
61      * @return void
62      */
63     public function onResponse()
64     {
65         // If no alternate nick list was found, return
66         if (empty($this->iterator)) {
67             return;
68         }
69
70         // If the response event indicates that the nick set is in use...
71         $code = $this->getEvent()->getCode();
72         if ($code == Phergie_Event_Response::ERR_NICKNAMEINUSE) {
73
74             // Attempt to move to the next nick in the alternate nick list
75             $this->iterator->next();
76
77             // If another nick is available...
78             if ($this->iterator->valid()) {
79                 
80                 // Switch to the new nick
81                 $altNick = $this->iterator->current();
82                 $this->doNick($altNick);
83
84                 // Update the connection to reflect the nick change
85                 $this->getConnection()->setNick($altNick);
86
87             } else {
88                 // If no other nicks are available...
89
90                 // Terminate the connection
91                 $this->doQuit('All specified alternate nicks are in use');
92             }
93         }
94     }
95 }