summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/nslookup.rb
blob: 160fee85f7aa133ce1d0673505e14ce8dccde2bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class DnsPlugin < Plugin
  require 'resolv'
  def gethostname(address)
    Resolv.getname(address)
  end
  def getaddresses(name)
    Resolv.getaddresses(name)
  end

  def help(plugin, topic="")
    "dns <hostname|ip> => show local resolution results for hostname or ip address"
  end
  
  def name_to_ip(m, params)
    Thread.new do
      begin
        a = getaddresses(params[:host])
        if a.length > 0
          m.reply m.params + ": " + a.join(", ")
        else
          m.reply "#{params[:host]}: not found"
        end
      rescue StandardError => err
        m.reply "#{params[:host]}: not found"
      end
    end
  end
  
  def ip_to_name(m, params)
    Thread.new do
       begin
         a = gethostname(params[:ip])
         m.reply m.params + ": " + a if a
       rescue StandardError => err
         m.reply "#{params[:ip]}: not found (does not reverse resolve)"
       end
     end
  end
end
plugin = DnsPlugin.new
plugin.map 'dns :ip', :action => 'ip_to_name', 
                      :requirements => {:ip => /^\d+\.\d+\.\d+\.\d+$/}
plugin.map 'dns :host', :action => 'name_to_ip'