]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/NickServ.php
c3af4ed42d1e07d6b5c3ee3f786d2e26aa7d9abf
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / NickServ.php
1 <?php\r
2 \r
3 /**\r
4  * Intercepts and responds to messages from the NickServ agent requesting that\r
5  * the bot authenticate its identify.\r
6  *\r
7  * The password configuration setting should contain the password registered\r
8  * with NickServ for the nick used by the bot.\r
9  */\r
10 class Phergie_Plugin_NickServ extends Phergie_Plugin_Abstract {\r
11     /**\r
12      * The name of the nickserv bot\r
13      *\r
14      * @var string\r
15      */\r
16     protected $botNick;\r
17 \r
18     /**\r
19     * Identify message\r
20     */\r
21     protected $identifyMessage;\r
22 \r
23     /**\r
24      * Initializes instance variables.\r
25      *\r
26      * @return void\r
27      */\r
28     public function onLoad() {\r
29         $this->getPluginHandler()->getPlugin('Command');\r
30 \r
31         // Get the name of the NickServ bot, defaults to NickServ\r
32         $this->botNick = $this->config['nickserv.botnick'];\r
33         if (!$this->botNick) $this->botNick = 'NickServ';\r
34 \r
35         // Get the identify message\r
36         $this->identifyMessage = $this->config['nickserv.identify_message'];\r
37         if (!$this->identifyMessage) $this->identifyMessage = 'This nickname is registered.';\r
38     }\r
39 \r
40     /**\r
41      * Checks for a notice from NickServ and responds accordingly if it is an\r
42      * authentication request or a notice that a ghost connection has been\r
43      * killed.\r
44      *\r
45      * @return void\r
46      */\r
47     public function onNotice() {\r
48         $event = $this->event;\r
49         if (strtolower($event->getNick()) == strtolower($this->botNick)) {\r
50             $message = $event->getArgument(1);\r
51             $nick = $this->connection->getNick();\r
52             if (strpos($message, $this->identifyMessage) !== false) {\r
53                 $password = $this->config['nickserv.password'];\r
54                 if (!empty($password)) {\r
55                     $this->doPrivmsg($this->botNick, 'IDENTIFY ' . $password);\r
56                 }\r
57                 unset($password);\r
58             } elseif (preg_match('/^.*' . $nick . '.* has been killed/', $message)) {\r
59                 $this->doNick($nick);\r
60             }\r
61         }\r
62     }\r
63 \r
64     /**\r
65      * Checks to see if the original Nick has quit, if so, take the name back\r
66      *\r
67      * @return void\r
68      */\r
69     public function onQuit() {\r
70         $eventnick = $this->event->getNick();\r
71         $nick = $this->connection->getNick();\r
72         if ($eventnick == $nick) {\r
73             $this->doNick($nick);\r
74         }\r
75     }\r
76 \r
77     /**\r
78      * Changes the in-memory configuration setting for the bot nick if it is\r
79      * successfully changed.\r
80      *\r
81      * @return void\r
82      */\r
83     public function onNick() {\r
84         $event = $this->event;\r
85         $connection = $this->connection;\r
86         if ($event->getNick() == $connection->getNick()) {\r
87             $connection->setNick($event->getArgument(0));\r
88         }\r
89     }\r
90 \r
91     /**\r
92      * Provides a command to terminate ghost connections.\r
93      *\r
94      * @return void\r
95      */\r
96     public function onDoGhostbust() {\r
97         $event = $this->event;\r
98         $user = $event->getNick();\r
99         $conn = $this->connection;\r
100         $nick = $conn->getNick();\r
101 \r
102         if ($nick != $this->config['connections'][$conn->getHost()]['nick']) {\r
103             $password = $this->config['nickserv.password'];\r
104             if (!empty($password)) {\r
105                 $this->doPrivmsg($this->event->getSource(), $user . ': Attempting to ghost ' . $nick .'.');\r
106                 $this->doPrivmsg(\r
107                     $this->botNick,\r
108                     'GHOST ' . $nick . ' ' . $password,\r
109                     true\r
110                 );\r
111             }\r
112             unset($password);\r
113         }\r
114     }\r
115 \r
116     /**\r
117      * Automatically send the GHOST command if the Nickname is in use\r
118      *\r
119      * @return void\r
120      */\r
121     public function onResponse() {\r
122         if ($this->event->getCode() == Phergie_Event_Response::ERR_NICKNAMEINUSE) {\r
123             $password = $this->config['nickserv.password'];\r
124             if (!empty($password)) {\r
125                 $this->doPrivmsg(\r
126                     $this->botNick,\r
127                     'GHOST ' . $this->connection->getNick() . ' ' . $password,\r
128                     true\r
129                 );\r
130             }\r
131             unset($password);\r
132         }\r
133     }\r
134 \r
135     /**\r
136      * The server sent a KILL request, so quit the server\r
137      *\r
138      * @return void\r
139      */\r
140     public function onKill() {\r
141         $this->doQuit($this->event->getArgument(1));\r
142     }\r
143 }\r