1
0
Fork 0
mirror of https://github.com/nishiki/botish.git synced 2024-11-30 11:03:03 +00:00
botish/lib/botish.rb

53 lines
1 KiB
Ruby
Raw Normal View History

2017-04-17 09:07:45 +00:00
#!/usr/bin/ruby
require 'socket'
class Botish
HOST = 'irc.freenode.net'
PORT = 6667
USERNAME = 'botish'
CHANNEL = '#testnishiki'
def initialize
@connection = TCPSocket.open(HOST, PORT)
send("NICK #{USERNAME}")
send("USER #{USERNAME} localhost * :#{USERNAME}")
Kernel.loop do
msg = @connection.gets
log("<- #{msg}")
break if msg.include?('End of /MOTD command.')
end
send("JOIN #{CHANNEL}")
send("PRIVMSG #{CHANNEL} :Je suis là :')")
end
def listen
Kernel.loop do
msg = @connection.gets
log("<- #{msg}")
parse(msg)
end
end
private
def parse(msg)
case msg
when /^PING (?<host>.+)/
send("PONG #{Regexp.last_match('host')}")
when /^:(?<user>[[:alpha:]]+)([^ ]+)? PRIVMSG #{CHANNEL} :#{USERNAME}: ping/
send("PRIVMSG #{CHANNEL} :#{Regexp.last_match('user')}: pong")
end
end
def send(msg)
log("-> #{msg}")
@connection.puts(msg)
end
def log(msg)
puts "#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%L')} #{msg}"
end
end