Skip to content

Instantly share code, notes, and snippets.

Created July 19, 2012 20:56
Show Gist options
  • Save anonymous/3146763 to your computer and use it in GitHub Desktop.
Save anonymous/3146763 to your computer and use it in GitHub Desktop.
CoyoBOT redux 2
#!/usr/bin/ruby
# written by Alex Maurin AKA Coyo
require "socket"
SERVER = "irc.freenode.net" # server hostname to connect to
PORT = 6667 # port number to connect to
NICK = "coyobot" # nickname
USER = "unf" # username
GECOS = "Coyo's Bot" # "real name"
PASS = "silverfox22" # server password
MODES = "+iswx" # usermodes to set on connection
CHANNELS = "#coyobot" # channel to monitor
CMDCHAR = "!" # string prefix to address the bot
class IRCSocket
#define methods
def send(line)
@socket.puts "#{line}"
puts "<-- #{line}"
# send line to socket as well as to terminal
end
def ping(server)
send "PING #{server} :coyobot"
# ping server
end
def whois(nick, context)
send "WHOIS #{nick} #{context}"
# whois nick within context
end
def nick(nickname)
send "NICK #{nickname}"
# change or set nickname
end
def mode(modes, context, target)
send "MODE #{context} #{modes} #{target}"
# change modes within context
end
def login(nickname, username, gecos, password)
nick nickname
send "USER #{username} 8 * :#{gecos}"
# send username and gecos to server
send "PASS #{password}"
# send pass, usually unnecessary, but eh.
end
def join(channel)
send "JOIN #{channel}"
# join a given channel
end
def part(channel)
send "PART #{channel}"
# part a given channel
end
def msg(message, context)
send "PRIVMSG #{context} :#{message}"
# send a privmsg
end
def pingreply(message)
send "PONG :#{message}"
# reply to pings
end
# initialization method
def initialize(server, port, nick, user, gecos, pass)
puts "Connecting to #{server} on port #{port}..."
@socket = TCPSocket.new server, port
@lifestate = true
# connect to irc server and set lifestate
login nick, user, gecos, pass
end
# logging returns output from IRC server
def log
line = @socket.gets
return "--> #{line}"
end
# close socket when closed
def close
@lifestate = false
@socket.close
# set lifestate of false, and close socket
end
# check lifestate when called
def isAlive
return @lifestate
end
# kick a nickname in a given context
def kick(nickname, context)
send "KICK #{context} #{nickname} :THIS! IS! SPAARTAAAAA!!!"
end
end
irc = IRCSocket.new SERVER, PORT, NICK, USER, GECOS, PASS
irc.mode MODES, NICK
irc.join CHANNELS
while irc.isAlive == true
line = irc.log
puts line
/PING :(?'request'[A-Za-z0-9\.\/]+)/.match(line) do |match|
irc.pingreply match["request"]
end
/:(?'nickname'[A-Za-z0-9\.\/]+)!~?(?'username'[A-Za-z0-9\.\/]+)@(?'hostname'[A-Za-z0-9\.\/\-]+) PRIVMSG (?'channel'#[A-Za-z0-9\.\/\-]+) :!(?'command'[A-Za-z0-9\.\/\-]+) ?(?'parameters'[A-Za-z0-9\.\/\-]+)*/.match(line) do |match|
case
when match["command"] == "ping"
irc.msg "#{match["nickname"]}: pong #{match["parameters"]}!", match["channel"]
when match["command"] == "nom"
irc.msg "#{match["nickname"]}: om nom nom~", match["channel"]
when match["command"] == "kick"
irc.kick match["nickname"], match["channel"]
when match["command"] == "op"
irc.mode "+o", match["channel"], match["parameters"]
when match["command"] == "voice"
irc.mode "+v", match["channel"], match["parameters"]
end
end
end
irc.part CHANNELS
irc.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment