-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathamqp2irc.rb
60 lines (46 loc) · 964 Bytes
/
amqp2irc.rb
1
2
3
4
5
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env ruby
require 'rubygems'
require 'eventmachine'
require 'amqp'
require 'pp'
class IRCBot < EM::Connection
include EventMachine::Protocols::LineText2
def initialize(queue)
@queue = queue
@channel = "bot"
EM.add_timer(1){ pop }
end
def pop
@queue.pop { |msg|
say msg if msg
EM.add_timer(1){ pop }
}
end
def say(msg)
msg.split("\n").each do |msg|
next if msg =~ /^\//
command( "PRIVMSG ##{@channel } :#{ msg }")
end
end
def post_init
puts "connected"
command "USER", ["bot"]*4
command "NICK", "[X]"
command("JOIN", "##{ @channel }")
end
def receive_line(line)
puts "received #{line}"
case line
when /^PING (.*)/
command('PONG', $1)
else
end
end
def command(*cmd)
send_data "#{ cmd.flatten.join(' ') }\r\n"
end
end
EM.run do
queue = MQ.queue('bot')
EM.connect('localhost', 6667, IRCBot, queue)
end