Assembla home | Assembla project page
 

root/trunk/Phergie/Plugin/Dns.php

Revision 257, 2.3 kB (checked in by Seldaek, 4 months ago)

* Fixed dns answering to messages that don't match either 'dns domain.tld' or 'dns ip'

Line 
1 <?php
2
3 /**
4  * Parses incoming messages for requests to perform a DNS or reverse DNS
5  * lookup on a given host name or IP address, performs the lookup, and
6  * responds with a message containing the lookup result.
7  */
8 class Phergie_Plugin_Dns extends Phergie_Plugin_Abstract_Command
9 {
10     /**
11      * Processes a DNS or reverse DNS lookup request.
12      *
13      * @param string $arg Host or IP address to look up
14      * @return void
15      */
16     protected function processRequest($arg)
17     {
18         $source = $this->event->getSource();
19         $target = $this->event->getNick();
20
21         if (preg_match('/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/', $arg)) {
22             $resolved = gethostbyaddr(long2ip(ip2long($arg)));
23         } elseif (preg_match('/^(?:[a-z0-9]+\.)+[a-z]{2,6}$/', $arg)) {
24             $resolved = gethostbyname($arg);
25         } else {
26             return;
27         }
28
29         if (!isset($resolved)) {
30             $this->doPrivmsg($source, $target . ': ' . $arg . ' cannot be resolved.');
31         } else {
32             $this->doPrivmsg($source, $target . ': ' . $arg . ' resolved to ' . $resolved);
33         }
34     }
35
36     /**
37      * Forwards DNS lookup requests onto a central handler.
38      *
39      * @param string $host Host to look up
40      * @return void
41      */
42     public function onDoDns($host)
43     {
44         $this->processRequest($host);
45     }
46
47     /**
48      * Forwards reverse DNS lookup requests onto a central handler.
49      *
50      * @param string $ip IP address to look up
51      * @return void
52      */
53     public function onDoRevdns($ip)
54     {
55         $this->processRequest($ip);
56     }
57
58     /**
59      * Intercepts, processes, and responds with the result of prototype lookup
60      * requests.
61      *
62      * @param string $function Name of the function to look up
63      * @return void
64      */
65     public function onDoHostIp($host)
66     {
67         $target = $this->event->getNick();
68
69         $tmp = file_get_contents('http://api.hostip.info/get_html.php?position=true&ip=' . urlencode(trim($host)));
70         if (!empty($tmp) && stripos($tmp, 'Private Address') === false) {
71             $contents = $host . ' -> ' . preg_replace(array('/\s+/', '/[\r\n]+/'), array(' ', ' - '), trim($tmp));
72             $this->doPrivmsg($this->event->getSource(), $target . ': ' . $contents);
73         }
74         unset($host, $tmp, $contents);
75     }
76
77 }
78
Note: See TracBrowser for help on using the browser.