lastfm plugin: added now playing info to the help
[rbot] / 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     begin
16       a = getaddresses(params[:host])
17       if a.length > 0
18         m.reply m.params + ": " + a.join(", ")
19       else
20         m.reply "#{params[:host]}: not found"
21       end
22     rescue StandardError => err
23       m.reply "#{params[:host]}: not found"
24     end
25   end
26   
27   def ip_to_name(m, params)
28     begin
29       a = gethostname(params[:ip])
30       m.reply m.params + ": " + a if a
31     rescue StandardError => err
32       m.reply "#{params[:ip]}: not found (does not reverse resolve)"
33     end
34   end
35 end
36 plugin = DnsPlugin.new
37 plugin.map 'dns :ip', :action => 'ip_to_name', :thread => true,
38                       :requirements => {:ip => /^\d+\.\d+\.\d+\.\d+$/}
39 plugin.map 'dns :host', :action => 'name_to_ip', :thread => true