summaryrefslogtreecommitdiff
path: root/lib/rbot/plugins/nslookup.rb
blob: 92da1ba779e2f4250d219cc5155a68d503278b6e (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
44
45
46
47
48
49
50
51
52
53
54
55
56
class DnsPlugin < Plugin
  begin
    require 'resolv-replace'
    def gethostname(address)
      Resolv.getname(address)
    end
    def getaddresses(name)
      Resolv.getaddresses(name)
    end
  rescue LoadError
    def gethostname(address)
      Socket.gethostbyname(address).first
    end
    def getaddresses(name)
      a = Socket.gethostbyname(name)
      list = Socket.getaddrinfo(a[0], 'http')
      addresses = Array.new
      list.each {|line|
       addresses << line[3]
      }
      addresses
    end
  end

  def help(plugin, topic="")
    "nslookup|dns <hostname|ip> => show local resolution results for hostname or ip address"
  end
  def privmsg(m)
    unless(m.params)
      m.reply "incorrect usage: " + help(m.plugin)
      return
    end
    Thread.new do
      if(m.params =~ /^\d+\.\d+\.\d+\.\d+$/)
       begin
         a = gethostname(m.params)
         m.reply m.params + ": " + a if a
       rescue StandardError => err
         m.reply "#{m.params}: not found"
       end
      elsif(m.params =~ /^\S+$/)
       begin
         a = getaddresses(m.params)
         m.reply m.params + ": " + a.join(", ")
       rescue StandardError => err
         m.reply "#{m.params}: not found"
       end
      else
       m.reply "incorrect usage: " + help(m.plugin)
      end
    end
  end
end
plugin = DnsPlugin.new
plugin.register("nslookup")
plugin.register("dns")