-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgitbot.rb
69 lines (59 loc) · 1.55 KB
/
gitbot.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
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env ruby
# vi: set sw=2 ts=2 et :
#
# Simple IRC bot that echoes everything written to $fifo to
# $irc_channel on $server
# Based on code from Kevin Glowacz:
# http://kevin.glowacz.info/2009/03/simple-irc-bot-in-ruby.html
require 'socket'
#####################################################################
# Configuration #
#####################################################################
$server = "chat.freenode.net"
$irc_port = 6667
$irc_channel = "gitbot"
$irc_nick = "gitbot#{rand(100000)}"
$fifo = "/tmp/gitbotfifo"
#####################################################################
class SimpleIrcBot
def initialize()
@socket = TCPSocket.open($server, $irc_port)
say "NICK #{$irc_nick}"
say "USER gitbot 0 * https://github.com/ehamberg/simple-gitbot"
say "JOIN ##{$irc_channel}"
end
def say(msg)
puts msg
@socket.puts msg
end
def run
Thread.new($fifo) do |fifoname|
while true
File.open(fifoname, "r+") do |fi|
while line = fi.gets
say "PRIVMSG ##{$irc_channel} :#{line}"
end
end
end
end
until @socket.eof? do
msg = @socket.gets
puts msg
if msg.match(/^PING :(.*)$/)
say "PONG #{$~[1]}"
next
end
end
end
def quit
say 'QUIT'
end
end
if File.exist?($fifo)
abort "#{$fifo} exists but is not a fifo" if File.ftype($fifo) != "fifo"
else
system("mkfifo #{$fifo}")
end
bot = SimpleIrcBot.new
trap("INT"){ bot.quit }
bot.run