Method: Ponder::AsyncIRC#whois

Defined in:
lib/ponder/async_irc.rb

#whois(nick) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ponder/async_irc.rb', line 70

def whois(nick)
  queue = Queue.new
  @observer_queues[queue] = [/^:\S+ (307|311|312|318|319|401) \S+ #{Regexp.escape(nick)}/i]
  raw "WHOIS #{nick}"
  whois = {}
  running = true
  
  while running
    begin
      Timeout::timeout(30) do
         response = queue.pop
         raw_numeric = response.scan(/^:\S+ (\d{3})/)[0][0]
         
         case raw_numeric
         when '307'
           whois[:registered] = true
         when '311'
           response = response.scan(/^:\S+ 311 \S+ (\S+) (\S+) (\S+) \* :(.*)$/)[0]
           whois[:nick]      = response[0]
           whois[:username]  = response[1]
           whois[:host]      = response[2]
           whois[:real_name] = response[3]
         when '312'
           response = response.scan(/^:\S+ 312 \S+ \S+ (\S+) :(.*)/)[0]
           whois[:server] = {:address => response[0], :name => response[1]}
         when '318'
           running = false
         when '319'
           channels_with_mode = response.scan(/^:\S+ 319 \S+ \S+ :(.*)/)[0][0].split(' ')
           whois[:channels] = {}
           channels_with_mode.each do |c|
             whois[:channels][c.scan(/(.)?(#\S+)/)[0][1]] = c.scan(/(.)?(#\S+)/)[0][0]
           end
         when '401'
           whois  = false
           running = false
         end
      end
    rescue Timeout::Error
      nil
    end
  end
  
  @observer_queues.delete queue
  return whois
end