-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathirc_bot.rb
More file actions
52 lines (41 loc) · 1001 Bytes
/
irc_bot.rb
File metadata and controls
52 lines (41 loc) · 1001 Bytes
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
require 'socket'
class IrcBot
attr_accessor :name
attr_reader :socket, :server, :port, :channel
def initialize(server, port, channel)
@server = server
@port = port
@channel = channel
end
def run
connect
until socket.eof? do
message = socket.gets
puts message
if message.start_with? "PING"
handle_ping_message(message)
elsif message.include? "PRIVMSG #{channel}"
handle_channel_message(message)
end
end
end
def connect
@socket = TCPSocket.open(server, port)
irc_send "NICK #{name}"
irc_send "USER #{name} 0 * :#{name}"
irc_send "JOIN #{channel}"
end
def irc_send(message)
puts "Sending: #{message}"
socket.puts message
end
def handle_ping_message(message)
hostname = message.split(" ").last
irc_send "PONG #{hostname}"
end
def handle_channel_message(message)
end
end
bot = IrcBot.new("irc.freenode.net", 6667, "#rubymonstas")
bot.name = "nerdinand-bot"
bot.run