]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/NickServ.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / NickServ.php
1 <?php\r
2 /**\r
3  * Phergie\r
4  *\r
5  * PHP version 5\r
6  *\r
7  * LICENSE\r
8  *\r
9  * This source file is subject to the new BSD license that is bundled\r
10  * with this package in the file LICENSE.\r
11  * It is also available through the world-wide-web at this URL:\r
12  * http://phergie.org/license\r
13  *\r
14  * @category  Phergie\r
15  * @package   Phergie_Plugin_NickServ\r
16  * @author    Phergie Development Team <team@phergie.org>\r
17  * @copyright 2008-2010 Phergie Development Team (http://phergie.org)\r
18  * @license   http://phergie.org/license New BSD License\r
19  * @link      http://pear.phergie.org/package/Phergie_Plugin_NickServ\r
20  */\r
21 \r
22 /**\r
23  * Intercepts and responds to messages from the NickServ agent requesting that\r
24  * the bot authenticate its identify.\r
25  *\r
26  * The password configuration setting should contain the password registered\r
27  * with NickServ for the nick used by the bot.\r
28  *\r
29  * @category Phergie\r
30  * @package  Phergie_Plugin_NickServ\r
31  * @author   Phergie Development Team <team@phergie.org>\r
32  * @license  http://phergie.org/license New BSD License\r
33  * @link     http://pear.phergie.org/package/Phergie_Plugin_NickServ\r
34  * @uses     Phergie_Plugin_Command pear.phergie.org\r
35  */\r
36 class Phergie_Plugin_NickServ extends Phergie_Plugin_Abstract\r
37 {\r
38     /**\r
39      * Nick of the NickServ bot\r
40      *\r
41      * @var string\r
42      */\r
43     protected $botNick;\r
44 \r
45     /**\r
46     * Identify message\r
47     */\r
48     protected $identifyMessage;\r
49 \r
50     /**\r
51      * Checks for dependencies and required configuration settings.\r
52      *\r
53      * @return void\r
54      */\r
55     public function onLoad()\r
56     {\r
57         $this->getPluginHandler()->getPlugin('Command');\r
58 \r
59         // Get the name of the NickServ bot, defaults to NickServ\r
60         $this->botNick = $this->getConfig('nickserv.botnick', 'NickServ');\r
61 \r
62         // Get the identify message\r
63         $this->identifyMessage = $this->getConfig(\r
64             'nickserv.identify_message',\r
65             '/This nickname is registered./'\r
66         );\r
67     }\r
68 \r
69     /**\r
70      * Checks for a notice from NickServ and responds accordingly if it is an\r
71      * authentication request or a notice that a ghost connection has been\r
72      * killed.\r
73      *\r
74      * @return void\r
75      */\r
76     public function onNotice()\r
77     {\r
78         $event = $this->event;\r
79         if (strtolower($event->getNick()) == strtolower($this->botNick)) {\r
80             $message = $event->getArgument(1);\r
81             $nick = $this->connection->getNick();\r
82             if (preg_match($this->identifyMessage, $message)) {\r
83                 $password = $this->config['nickserv.password'];\r
84                 if (!empty($password)) {\r
85                     $this->doPrivmsg($this->botNick, 'IDENTIFY ' . $password);\r
86                 }\r
87                 unset($password);\r
88             } elseif (preg_match('/^.*' . $nick . '.* has been killed/', $message)) {\r
89                 $this->doNick($nick);\r
90             }\r
91         }\r
92     }\r
93 \r
94     /**\r
95      * Checks to see if the original nick has quit; if so, take the name back.\r
96      *\r
97      * @return void\r
98      */\r
99     public function onQuit()\r
100     {\r
101         $eventNick = $this->event->getNick();\r
102         $nick = $this->connection->getNick();\r
103         if ($eventNick == $nick) {\r
104             $this->doNick($nick);\r
105         }\r
106     }\r
107 \r
108     /**\r
109      * Changes the in-memory configuration setting for the bot nick if it is\r
110      * successfully changed.\r
111      *\r
112      * @return void\r
113      */\r
114     public function onNick()\r
115     {\r
116         $event = $this->event;\r
117         $connection = $this->connection;\r
118         if ($event->getNick() == $connection->getNick()) {\r
119             $connection->setNick($event->getArgument(0));\r
120         }\r
121     }\r
122 \r
123     /**\r
124      * Provides a command to terminate ghost connections.\r
125      *\r
126      * @return void\r
127      */\r
128     public function onCommandGhostbust()\r
129     {\r
130         $event = $this->event;\r
131         $user = $event->getNick();\r
132         $conn = $this->connection;\r
133         $nick = $conn->getNick();\r
134 \r
135         if ($nick != $this->config['connections'][$conn->getHost()]['nick']) {\r
136             $password = $this->config['nickserv.password'];\r
137             if (!empty($password)) {\r
138                 $this->doPrivmsg(\r
139                     $this->event->getSource(),\r
140                     $user . ': Attempting to ghost ' . $nick .'.'\r
141                 );\r
142                 $this->doPrivmsg(\r
143                     $this->botNick,\r
144                     'GHOST ' . $nick . ' ' . $password,\r
145                     true\r
146                 );\r
147             }\r
148         }\r
149     }\r
150 \r
151     /**\r
152      * Automatically send the GHOST command if the bot's nick is in use.\r
153      *\r
154      * @return void\r
155      */\r
156     public function onResponse()\r
157     {\r
158         if ($this->event->getCode() == Phergie_Event_Response::ERR_NICKNAMEINUSE) {\r
159             $password = $this->config['nickserv.password'];\r
160             if (!empty($password)) {\r
161                 $this->doPrivmsg(\r
162                     $this->botNick,\r
163                     'GHOST ' . $this->connection->getNick() . ' ' . $password\r
164                 );\r
165             }\r
166         }\r
167     }\r
168 \r
169     /**\r
170      * Handle the server sending a KILL request.\r
171      *\r
172      * @return void\r
173      */\r
174     public function onKill()\r
175     {\r
176         $this->doQuit($this->event->getArgument(1));\r
177     }\r
178 }\r