X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fnslookup.rb;h=0f901017c29b8c021288953811375f1c3e21543c;hb=36a9b6341daab4e8e63c2ad04418ecba4d877e27;hp=92da1ba779e2f4250d219cc5155a68d503278b6e;hpb=21949774b91eaec6ecde4eaa8ad121e2c0a36b87;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/nslookup.rb b/data/rbot/plugins/nslookup.rb index 92da1ba7..0f901017 100644 --- a/data/rbot/plugins/nslookup.rb +++ b/data/rbot/plugins/nslookup.rb @@ -1,56 +1,39 @@ 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 + require 'resolv' + def gethostname(address) + Resolv.getname(address) + end + def getaddresses(name) + Resolv.getaddresses(name) end def help(plugin, topic="") - "nslookup|dns => show local resolution results for hostname or ip address" + "dns => 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 + + def name_to_ip(m, params) + begin + a = getaddresses(params[:host]) + if a.length > 0 + m.reply m.params + ": " + a.join(", ") else - m.reply "incorrect usage: " + help(m.plugin) + m.reply "#{params[:host]}: not found" end + rescue StandardError => err + m.reply "#{params[:host]}: not found" + end + end + + def ip_to_name(m, params) + 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 plugin = DnsPlugin.new -plugin.register("nslookup") -plugin.register("dns") +plugin.map 'dns :ip', :action => 'ip_to_name', :thread => true, + :requirements => {:ip => /^\d+\.\d+\.\d+\.\d+$/} +plugin.map 'dns :host', :action => 'name_to_ip', :thread => true