Sha256: 3857a70e881f4c3236d3f3b3b46164088fc174c8e34c80a8a639ae4d954d14c6

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

module Lita
  module Handlers
    class Whois < Handler
      route(
        /^whois\s(\w+).(\w+)$/,
        :whois_domain,
        help: {
          'whois example.com' => 'Get the WHOIS info for a domain'
        }
      )

      route(
        /^whois\s.(\w+)$/,
        :whois_tld,
        help: {
          'whois .io' => 'Get the WHOIS info for a TLD'
        }
      )

      route(
        /^whois\s(.+)$/,
        :whois_ip,
        help: {
          'whois 8.8.8.8' => 'Get the WHOIS info for an IPv4 or IPv6 address'
        }
      )

      def whois_domain(response)
        response.reply(lookup("#{response.matches[0][0]}." \
                              "#{response.matches[0][1]}"))
      end

      def whois_tld(response)
        response.reply(lookup(".#{response.matches[0][0]}"))
      end

      def whois_ip(response)
        if IPAddress.valid? response.matches[0][0]
          response.reply(reverse_lookup(response.matches[0][0]))
        end
      end

      private

      def lookup(domain)
        client = ::Whois::Client.new
        record = nil
        begin
          record = client.lookup(domain)
        rescue ::Whois::ServerNotFound
          return "Cannot find a WHOIS server for #{domain}"
        end

        record.to_s
      end

      def reverse_lookup(ip)
        client = ::Whois::Client.new
        record = nil
        begin
          record = client.lookup(ip)
        rescue ::Whois::AllocationUnknown, ::Whois::ServerNotFound,
               ::Whois::NoInterfaceError
          return "Cannot find a WHOIS server for #{ip}"
        end

        record.to_s
      end
    end

    Lita.register_handler(Whois)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lita-whois-0.1.0 lib/lita/handlers/whois.rb