Assembla home | Assembla project page
 

root/trunk/Phergie/Driver/Abstract.php

Revision 226, 9.6 kB (checked in by Slynderdale, 3 months ago)

Some bug fixes and tweaks. Fixed up the error handling in URL a bit and also its URL checking. Added a check to the restart command in quit to check the syntax of Phergie before restarting.

Line 
1 <?php
2
3 /**
4  * Handles reception, transmission, and processing of data sent to and
5  * from IRC server.
6  */
7 abstract class Phergie_Driver_Abstract
8 {
9     /**
10      * Return codes for the run function, that tells the Bot script whether
11      * it should re-run or not
12      */
13     const RETURN_RECONNECT = "reconnect";
14     const RETURN_KEEPALIVE = "keepalive";
15     const RETURN_END = "end";
16     /**
17
18      /**
19      * Associative array mapping configuration setting names to their
20      * respective values
21      *
22      * @var array
23      */
24     protected $config = array();
25
26     /**
27      * List of plugin instances
28      *
29      * @var array
30      */
31     protected $plugins;
32
33     /**
34      * Returns the value associated with a specified configuration setting.
35      *
36      * @param string $name Name of the setting
37      * @return string Value of the setting, or NULL if the setting is not set
38      */
39     public final function getIni($name)
40     {
41         $name = strtolower($name);
42         if (!isset($this->config[$name])) {
43             return null;
44         }
45         return $this->config[$name];
46     }
47
48     /**
49      * Sets the value of a specified configuration setting, overwriting any
50      * existing value for that setting.
51      *
52      * @param string $name Name of the setting
53      * @param string $value New value for the setting
54      * @return void
55      */
56     public final function setIni($name, $value)
57     {
58         $this->config[strtolower($name)] = $value;
59     }
60
61     /**
62      * Parses a IRC hostmask and sets nick, user and host bits.
63      *
64      * @param string $hostmask Hostmask to parse
65      * @param string $nick Container for the nick
66      * @param string $user Container for the username
67      * @param string $host Container for the hostname
68      * @return void
69      */
70     public function parseHostmask($hostmask, &$nick, &$user, &$host)
71     {
72         if (preg_match('/^([^!@]+)!([^@]+)@(.*)$/', $hostmask, $match) > 0) {
73             list(, $nick, $user, $host) = array_pad($match, 4, null);
74         } else {
75             $host = $hostmask;
76         }
77     }
78
79     /**
80      * Converts a delimited string of hostmasks into a regular expression
81      * that will match any hostmask in the original string.
82      *
83      * @param string $list Delimited string of hostmasks
84      * @return string Regular expression
85      */
86     public function hostmasksToRegex($list)
87     {
88         $patterns = array();
89
90         foreach(preg_split('#[\s\r\n,]+#', $list) as $hostmask) {
91             // Find out which chars are present in the config mask and exclude them from the regex match
92             $excluded = '';
93             if (strpos($hostmask, '!') !== false) {
94                 $excluded .= '!';
95             }
96             if (strpos($hostmask, '@') !== false) {
97                 $excluded .= '@';
98             }
99
100             // Escape regex meta characters
101             $hostmask = str_replace(
102             array('\\',   '^',   '$',   '.',   '[',   ']',   '|',   '(',   ')',   '?',   '+',   '{',   '}'),
103             array('\\\\', '\\^', '\\$', '\\.', '\\[', '\\]', '\\|', '\\(', '\\)', '\\?', '\\+', '\\{', '\\}'),
104             $hostmask
105             );
106
107             // Replace * so that they match correctly in a regex
108             $patterns[] = str_replace('*', ($excluded === '' ? '.*' : '[^' . $excluded . ']*'), $hostmask);
109         }
110
111         return ('#^' . implode('|', $patterns) . '$#i');
112     }
113
114     /**
115      * Sends a debugging message to stdout if the debug configuration setting
116      * is enabled.
117      *
118      * @param string $message Debugging message
119      * @param bool $displayDebug Toggle whether to display the message in the
120      *                           console or not
121      * @return void
122      */
123     public function debug($message, $displayDebug = true)
124     {
125         if ($this->getIni('debug')) {
126             $message = '[' . date('H:i:s') . '] ' . $message . PHP_EOL;
127             if ($displayDebug) {
128                 echo $message;
129             }
130             if ($log = $this->getIni('log') and !empty($log)) {
131                 file_put_contents($log, $message, FILE_APPEND);
132             }
133         }
134     }
135
136     /**
137      * Adds a set of callbacks for events received from the server.
138      *
139      * @param Phergie_Plugin_Abstract_Base $plugin
140      */
141     public function addPlugin(Phergie_Plugin_Abstract_Base $plugin)
142     {
143         $this->plugins[strtolower($plugin->getName())] = $plugin;
144     }
145
146     /**
147      * Returns all the plugins.
148      *
149      * @return Phergie_Plugin_Abstract_Base
150      */
151     public function getPlugins()
152     {
153         return $this->plugins;
154     }
155
156     /**
157      * Returns a list of all the plugins either currently loaded or within the
158      * plugin directory.
159      *
160      * @param $plugin $dirList If true, scan the Plugin directory for the list
161      * @return array
162      */
163     public function getPluginList($dirList = false, $preserveExt = false)
164     {
165         if (!$dirList) {
166             return array_keys($this->plugins);
167         } else {
168             $iterator = new DirectoryIterator(PHERGIE_PLUGIN_DIR);
169             $plugins = array();
170
171             foreach($iterator as $filename) {
172                 if ($iterator->isFile() && pathinfo($filename, PATHINFO_EXTENSION) == 'php') {
173                     $plugins[] = ($preserveExt ? (string)$filename : basename($filename, '.php'));
174                 }
175             }
176             unset($iterator, $filename, $dirList);
177
178             return $plugins;
179         }
180     }
181
182     /**
183      * Returns a plugin instance.
184      *
185      * @param $plugin The plugin class name (without the Phergie_Plugin_ prefix)
186      * @return Phergie_Plugin_Abstract_Base
187      */
188     public function getPlugin($plugin)
189     {
190         $plugin = strtolower($plugin);
191         if (isset($this->plugins[$plugin])) {
192             return $this->plugins[$plugin];
193         }
194         return false;
195     }
196
197     /**
198      * Executes a continuous loop in which the client listens for events from
199      * the server and processes them until the connection is terminated.
200      *
201      * @return void
202      */
203     public abstract function run();
204
205     /**
206      * Terminates the connection with the server.
207      *
208      * @param string $reason Reason for connection termination (optional)
209      * @param bool $reconnect if true, the bot will reconnect to the server
210      * @return void
211      */
212     public abstract function doQuit($reason = null, $reconnect = false);
213
214     /**
215      * Joins a channel.
216      *
217      * @param string $channel Name of the channel to join
218      * @param string $keys Channel key if needed (optional)
219      * @return void
220      */
221     public abstract function doJoin($channel, $key = null);
222
223     /**
224      * Leaves a channel.
225      *
226      * @param string $channel Name of the channel to leave
227      * @return void
228      */
229     public abstract function doPart($channel);
230
231     /**
232      * Invites a user to an invite-only channel.
233      *
234      * @param string $nick Nick of the user to invite
235      * @param string $channel Name of the channel
236      * @return void
237      */
238     public abstract function doInvite($nick, $channel);
239
240     /**
241      * Obtains a list of nicks of usrs in currently joined channels.
242      *
243      * @param string $channels Comma-delimited list of one or more channels
244      * @return void
245      */
246     public abstract function doNames($channels);
247
248     /**
249      * Obtains a list of channel names and topics.
250      *
251      * @param string $channels Comma-delimited list of one or more channels
252      *                         to which the response should be restricted
253      *                         (optional)
254      * @return void
255      */
256     public abstract function doList($channels = null);
257
258     /**
259      * Retrieves or changes a channel topic.
260      *
261      * @param string $channel Name of the channel
262      * @param string $topic New topic to assign (optional)
263      * @return void
264      */
265     public abstract function doTopic($channel, $topic = null);
266
267     /**
268      * Retrieves or changes a channel or user mode.
269      *
270      * @param string $target Channel name or user nick
271      * @param string $mode New mode to assign (optional)
272      * @return void
273      */
274     public abstract function doMode($target, $mode = null);
275
276     /**
277      * Changes the client nick.
278      *
279      * @param string $nick New nick to assign
280      * @return void
281      */
282     public abstract function doNick($nick);
283
284     /**
285      * Retrieves information about a nick.
286      *
287      * @param string $nick
288      * @return void
289      */
290     public abstract function doWhois($nick);
291
292     /**
293      * Sends a message to a nick or channel.
294      *
295      * @param string $target Channel name or user nick
296      * @param string $text Text of the message to send
297      * @return void
298      */
299     public abstract function doPrivmsg($target, $text);
300
301     /**
302      * Sends a notice to a nick or channel.
303      *
304      * @param string $target Channel name or user nick
305      * @param string $text Text of the notice to send
306      * @return void
307      */
308     public abstract function doNotice($target, $text);
309
310     /**
311      * Kicks a user from a channel.
312      *
313      * @param string $nick Nick of the user
314      * @param string $channel Channel name
315      * @param string $reason Reason for the kick (optional)
316      * @return void
317      */
318     public abstract function doKick($nick, $channel, $reason = null);
319
320     /**
321      * Responds to a server test of client responsiveness.
322      *
323      * @param string $daemon Daemon from which the original request originates
324      * @return void
325      */
326     public abstract function doPong($daemon);
327
328     /**
329      * Sends a CTCP ACTION (/me) command to a nick or channel.
330      *
331      * @param string $target Channel name or user nick
332      * @param string $text Text of the action to perform
333      * @return void
334      */
335     public abstract function doAction($target, $text);
336 }
337
Note: See TracBrowser for help on using the browser.