]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/nslookup.rb
Fri Jul 29 13:07:56 BST 2005 Tom Gilbert <tom@linuxbrit.co.uk>
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / nslookup.rb
1 class DnsPlugin < Plugin
2   require 'resolv'
3   def gethostname(address)
4     Resolv.getname(address)
5   end
6   def getaddresses(name)
7     Resolv.getaddresses(name)
8   end
9
10   def help(plugin, topic="")
11     "dns <hostname|ip> => show local resolution results for hostname or ip address"
12   end
13   
14   def name_to_ip(m, params)
15     Thread.new do
16       begin
17         a = getaddresses(params[:host])
18         if a.length > 0
19           m.reply m.params + ": " + a.join(", ")
20         else
21           m.reply "#{params[:host]}: not found"
22         end
23       rescue StandardError => err
24         m.reply "#{params[:host]}: not found"
25       end
26     end
27   end
28   
29   def ip_to_name(m, params)
30     Thread.new do
31        begin
32          a = gethostname(params[:ip])
33          m.reply m.params + ": " + a if a
34        rescue StandardError => err
35          m.reply "#{params[:ip]}: not found (does not reverse resolve)"
36        end
37      end
38   end
39 end
40 plugin = DnsPlugin.new
41 plugin.map 'dns :ip', :action => 'ip_to_name', 
42                       :requirements => {:ip => /^\d+\.\d+\.\d+\.\d+$/}
43 plugin.map 'dns :host', :action => 'name_to_ip'