Assembla home | Assembla project page
 

root/trunk/Phergie/Plugin/ServerInfo.php

Revision 223, 9.2 kB (checked in by Slynderdale, 6 months ago)

Various bug fixes and enhancements:

Notable Changes:

  • Bot.php: The auto loader now strips out the "Phergie_" prefix from class names while getting the path to the class file to include for those who don't have the bot in a directory called Phergie. Both the Phergie directory and the parent directory were added to the include path. Also added a check to see if they have a timezone is set in PHP.ini, if not, set the timezone to the default timezone to prevent strict errors while using date.
  • Streams.php: Added support for invite requests and also fixed a small bug where an empty command gets ent to call_user_func due to a packet getting cut off.
  • Base.php: Added onInvite and changed fromAdmin so the first argunent needs to be set to true for hostmask admin checking.
  • Lart.php: Improved lart a bit and fixed bugs where you couldn't remove certain terms due to spacing/character casing issues
  • Seen.php: Added a quote command to get a random quote by a specified user
  • Users.php: Renamed Users to ServerInfo to prepare it for future changes to make it store more information about users, channels and the server. Also fixed a bug where halfops and voiced users were considered an OP.
Line 
1 <?php
2
3 class Phergie_Plugin_ServerInfo extends Phergie_Plugin_Abstract_Base
4 {
5     /**
6      * Determines if the plugin is a passive plugin or not
7      *
8      * @var bool
9      */
10     public $passive = true;
11
12     const OP = 8;
13     const HALFOP = 4;
14     const VOICE = 2;
15     const REGULAR = 1;
16
17     /**
18      * An array containing all the user information for a given channel
19      *
20      * @var array
21      */
22     protected static $list = array();
23
24     /**
25      * Static instance of the Users class
26      *
27      * @var Phergie_Plugin_Users
28      */
29     protected static $instance;
30
31     /**
32      * Initializes instance variables.
33      *
34      * @return void
35      */
36     public function init()
37     {
38         self::$instance = $this;
39     }
40
41     /**
42      * Tracks mode changes.
43      *
44      * @return void
45      */
46     public function onMode()
47     {
48         $args = $this->event->getArguments();
49         if (count($args) != 3) {
50             return;
51         }
52         list($chan, $modes, $nicks) = array_pad($args, 3, null);
53         if (preg_match('/(?:\+|-)[hov+-]+/i', $modes)) {
54             $chan = trim(strtolower($chan));
55             $modes = str_split(trim(strtolower($modes)), 1);
56             $nicks = explode(' ', trim(strtolower($nicks)));
57             while ($char = array_shift($modes)) {
58                 switch ($char) {
59                     case '+':
60                         $mode = '+';
61                     break;
62
63                     case '-':
64                         $mode = '-';
65                     break;
66
67                     case 'o':
68                         $nick = array_shift($nicks);
69                         if ($mode == '+') {
70                             self::$list[$chan][$nick] |= self::OP;
71                         } elseif ($mode == '-') {
72                             self::$list[$chan][$nick] ^= self::OP;
73                         }
74                     break;
75
76                     case 'h':
77                         $nick = array_shift($nicks);
78                         if ($mode == '+') {
79                             self::$list[$chan][$nick] |= self::HALFOP;
80                         } elseif ($mode == '-') {
81                             self::$list[$chan][$nick] ^= self::HALFOP;
82                         }
83                     break;
84
85                     case 'v':
86                         $nick = array_shift($nicks);
87                         if ($mode == '+') {
88                             self::$list[$chan][$nick] |= self::VOICE;
89                         } elseif ($mode == '-') {
90                             self::$list[$chan][$nick] ^= self::VOICE;
91                         }
92                     break;
93                 }
94             }
95         }
96     }
97
98     /**
99      * Debugging function
100      *
101      * @return void
102      */
103     public function onPrivmsg()
104     {
105         if ($this->getIni('debug') && $this->fromAdmin(true)) {
106             list($target, $msg) = array_pad($this->event->getArguments(), 2, null);
107             if (preg_match('#^ishere (\S+)$#', $msg, $m)) {
108                 $this->doPrivmsg($target, self::isIn($m[1], $target) ? 'true' : 'false');
109             } elseif (preg_match('#^isop (\S+)$#', $msg, $m)) {
110                 $this->doPrivmsg($target, self::isOp($m[1], $target) ? 'true' : 'false');
111             } elseif (preg_match('#^isvoice (\S+)$#', $msg, $m)) {
112                 $this->doPrivmsg($target, self::isVoice($m[1], $target) ? 'true' : 'false');
113             }
114         }
115     }
116
117     /**
118      * Tracks users joining channels.
119      *
120      * @return void
121      */
122     public function onJoin()
123     {
124         $arg = trim(strtolower($this->event->getArgument(0)));
125         $nick = trim(strtolower($this->event->getNick()));
126
127         self::$list[$arg][$nick] = self::REGULAR;
128     }
129
130     /**
131      * Tracks users parting channels.
132      *
133      * @return void
134      */
135     public function onPart()
136     {
137         $arg = trim(strtolower($this->event->getArgument(0)));
138         $nick = trim(strtolower($this->event->getNick()));
139
140         if (isset(self::$list[$arg][$nick])) {
141             unset(self::$list[$arg][$nick]);
142         }
143     }
144
145     /**
146      * Tracks users quitting from the server.
147      *
148      * @return void
149      */
150     public function onQuit()
151     {
152         $nick = trim(strtolower($this->event->getNick()));
153
154         foreach(self::$list as $channame => $chan) {
155             if (isset($chan[$nick])) {
156                 unset(self::$list[$channame][$nick]);
157             }
158         }
159     }
160
161     /**
162      * Tracks users changing nicks.
163      *
164      * @return void
165      */
166     public function onNick()
167     {
168         $nick = trim(strtolower($this->event->getNick()));
169         $newNick = trim(strtolower($this->event->getArgument(0)));
170
171         foreach(self::$list as $channame => $chan) {
172             if (isset($chan[$nick])) {
173                 self::$list[$channame][$newNick] = $chan[$nick];
174                 unset(self::$list[$channame][$nick]);
175             }
176         }
177     }
178
179     /**
180      * Populates the internal using listing for a channel when the bot joins
181      * it.
182      *
183      * @return void
184      */
185     public function onResponse()
186     {
187         if ($this->event->getCode() == Phergie_Event_Response::RPL_NAMREPLY) {
188             $desc = preg_split('/[@*=]\s*/', $this->event->getDescription(), 2);
189             list($chan, $users) = array_pad(explode(' :', trim($desc[1])), 2, null);
190             $users = explode(' ', trim($users));
191             foreach($users as $user) {
192                 if (empty($user)) continue;
193                 $flag = self::REGULAR;
194                 if (substr($user, 0, 1) === '@') {
195                     $user = substr($user, 1);
196                     $flag |= self::OP;
197                 }
198                 if (substr($user, 0, 1) === '%') {
199                     $user = substr($user, 1);
200                     $flag |= self::HALFOP;
201                 }
202                 if (substr($user, 0, 1) === '+') {
203                     $user = substr($user, 1);
204                     $flag |= self::VOICE;
205                 }
206                 self::$list[trim(strtolower($chan))][trim(strtolower($user))] = $flag;
207             }
208         }
209     }
210
211     /**
212      * Checks whether or not a given user has op (@) status.
213      *
214      * @param string $nick User nick to check
215      * @param string $chan Channel to check in
216      * @return bool
217      */
218     public static function isOp($nick, $chan)
219     {
220         $nick = trim(strtolower($nick));
221         $chan = trim(strtolower($chan));
222
223         return isset(self::$list[$chan][$nick]) && (self::$list[$chan][$nick] & self::OP) != 0;
224     }
225
226     /**
227      * Checks whether or not a given user has voice (+) status.
228      *
229      * @param string $nick User nick to check
230      * @param string $chan Channel to check in
231      * @return bool
232      */
233     public static function isVoice($nick, $chan)
234     {
235         $nick = trim(strtolower($nick));
236         $chan = trim(strtolower($chan));
237
238         return isset(self::$list[$chan][$nick]) && (self::$list[$chan][$nick] & self::VOICE) != 0;
239     }
240
241     /**
242      * Checks whether or not a given user has halfop (%) status.
243      *
244      * @param string $nick User nick to check
245      * @param string $chan Channel to check in
246      * @return bool
247      */
248     public static function isHalfop($nick, $chan)
249     {
250         $nick = trim(strtolower($nick));
251         $chan = trim(strtolower($chan));
252
253         return isset(self::$list[$chan][$nick]) && (self::$list[$chan][$nick] & self::HALFOP) != 0;
254     }
255
256     /**
257      * Checks whether or not a particular user is in a particular channel.
258      *
259      * @param string $nick User nick to check
260      * @param string $chan Channel to check in
261      * @return bool
262      */
263     public static function isIn($nick, $chan)
264     {
265         $nick = trim(strtolower($nick));
266         $chan = trim(strtolower($chan));
267
268         return isset(self::$list[$chan][$nick]);
269     }
270
271     /**
272      * Returns the entire user list for a channel or false if the bot is not
273      * present in the channel.
274      *
275      * @param string $chan Channel name
276      * @return array|bool
277      */
278     public static function getUsers($chan)
279     {
280         $chan = trim(strtolower($chan));
281         if (isset(self::$list[$chan])) {
282             return array_keys(self::$list[$chan]);
283         }
284         return false;
285     }
286
287     /**
288      * Returns the nick of a random user present in a given channel or false
289      * if the bot is not present in the channel.
290      *
291      * @param string $chan Channel name
292      * @return string|bool
293      */
294     public static function getRandomUser($chan)
295     {
296         $chan = trim(strtolower($chan));
297         if (isset(self::$list[$chan])) {
298             while (array_search(($nick = array_rand(self::$list[$chan], 1)), array('chanserv', 'q', 'l', 's')) !== false) {}
299             return $nick;
300         }
301         return false;
302     }
303
304     /**
305      * Returns a list of channels in which a given user is present.
306      *
307      * @param string $user Nick of the user (optional, defaults to the bot's
308      *                     nick)
309      * @return array List of channels
310      */
311     public static function getChannels($nick = null)
312     {
313         if (empty($nick)) {
314             $nick = trim(strtolower(self::$instance->getIni('nick')));
315         }
316         $out = array();
317         foreach(self::$list as $channame => $chan) {
318             if (isset($chan[$nick])) {
319                 $out[] = $channame;
320             }
321         }
322         return $out;
323     }
324 }
325
Note: See TracBrowser for help on using the browser.