Module: Ponder::AsyncIRC

Included in:
Thaum
Defined in:
lib/ponder/async_irc.rb

Instance Method Summary collapse

Instance Method Details

#channel_info(channel) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ponder/async_irc.rb', line 35

def channel_info(channel)
  queue = Queue.new
  @observer_queues[queue] = [/:\S+ (324|329|403) \S+ #{Regexp.escape(channel)}/i]
  raw "MODE #{channel}"
  information = {}
  running = true
  
  begin
    Timeout::timeout(30) do
      while running
        response = queue.pop
        raw_numeric = response.scan(/^:\S+ (\d{3})/)[0][0]
        
         case raw_numeric
         when '324'
           information[:modes] = response.scan(/^:\S+ 324 \S+ \S+ \+(\w*)/)[0][0].split('')
           limit = response.scan(/^:\S+ 324 \S+ \S+ \+\w* (\w*)/)[0]
           information[:channel_limit] = limit[0].to_i if limit
         when '329'
           information[:created_at] = Time.at(response.scan(/^:\S+ 329 \S+ \S+ (\d+)/)[0][0].to_i)
           running = false
         when '403'
           information = false
           running = false
         end
       end
    end
  rescue Timeout::Error
    information = false
  end
  
  @observer_queues.delete queue
  return information
end

#get_topic(channel) ⇒ Object



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
# File 'lib/ponder/async_irc.rb', line 6

def get_topic(channel)
  queue = Queue.new
  @observer_queues[queue] = [/:\S+ (331|332|403|442) \S+ #{Regexp.escape(channel)} :/i]
  raw "TOPIC #{channel}"
  
  topic = begin
    Timeout::timeout(30) do
       response = queue.pop
       raw_numeric = response.scan(/^:\S+ (\d{3})/)[0][0]
       
       case raw_numeric
       when '331'
         {:raw_numeric => 331, :message => 'No topic is set'}
       when '332'
         {:raw_numeric => 332, :message => response.scan(/ :(.*)/)[0][0]}
       when '403'
         {:raw_numeric => 403, :message => 'No such channel'}
       when '442'
         {:raw_numeric => 442, :message => "You're not on that channel"}
       end
    end
  rescue Timeout::Error
    false
  end
  
  @observer_queues.delete queue
  return topic
end

#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