Class: Ponder::AsyncIRC::Whois

Inherits:
Object
  • Object
show all
Includes:
EventMachine::Deferrable
Defined in:
lib/ponder/async_irc.rb

Constant Summary collapse

TIMEOUT =

number of seconds the deferrable will wait for a response before failing

15

Instance Method Summary collapse

Constructor Details

#initialize(nick, timeout_after, thaum) ⇒ Whois

Returns a new instance of Whois.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ponder/async_irc.rb', line 51

def initialize(nick, timeout_after, thaum)
  @nick = nick
  @thaum = thaum
  @whois_data = {}

  self.timeout(timeout_after)
  self.errback { @thaum.deferrables.delete self }

  @thaum.deferrables.add self
  @thaum.raw "WHOIS #{@nick}"
end

Instance Method Details

#succeed(*args) ⇒ Object



91
92
93
94
# File 'lib/ponder/async_irc.rb', line 91

def succeed(*args)
  @thaum.deferrables.delete self
  set_deferred_status :succeeded, *args
end

#try(message) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ponder/async_irc.rb', line 63

def try(message)
  if message =~ /^:\S+ (307|311|312|318|319|330|401) \S+ #{Regexp.escape(@nick)}/i
    case $1
    when '307', '330'
      @whois_data[:registered] = true
    when '311'
      message = message.scan(/^:\S+ 311 \S+ (\S+) :?(\S+) (\S+) \* :(.*)$/)[0]
      @whois_data[:nick]      = message[0]
      @whois_data[:username]  = message[1]
      @whois_data[:host]      = message[2]
      @whois_data[:real_name] = message[3]
    when '312'
      message = message.scan(/^:\S+ 312 \S+ \S+ (\S+) :(.*)/)[0]
      @whois_data[:server] = {:address => message[0], :name => message[1]}
    when '318'
      succeed @whois_data
    when '319'
      channels_with_mode = message.scan(/^:\S+ 319 \S+ \S+ :(.*)/)[0][0].split(' ')
      @whois_data[:channels] = {}
      channels_with_mode.each do |c|
        @whois_data[:channels][c.scan(/(.)?(#\S+)/)[0][1]] = c.scan(/(.)?(#\S+)/)[0][0]
      end
    when '401'
      succeed false
    end
  end
end