Skip to content

Instantly share code, notes, and snippets.

@atamis
Created April 7, 2009 02:53
Show Gist options
  • Save atamis/91075 to your computer and use it in GitHub Desktop.
Save atamis/91075 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby
# Perhaps the most dangerous irc bot.
# To use it, edit the file to have the irc server and channel of your choice
# launch it with ruby. Don't worry about it. You should launch 2, just in case.
$version = 0.1
require "socket"
#require 'lib/irc'
#require 'lib/general.rb'
# Config variables:
server = "irc.foonetic.net"
port = 6667
nick = make_name
channel = "#bomb"
nicks = ["Deady", "Deady_", "Deady__", "Bomb", "Bomb_maker"]
class IRC
def initialize(server, port, nick, channel)
@server = server
@port = port
@nick = nick
@channel = channel
$botsnacks = 0
end
def send(s)
# Send a message to the irc server and print it to the screen
puts "--> #{s}"
@irc.send "#{s}\n", 0
end
def connect()
# Connect to the IRC server
@irc = TCPSocket.open(@server, @port)
send "USER #{@nick} #{@nick} #{@nick} :Owned by..."
send "NICK #{@nick}"
send "MODE #{@nick} +B"
# send "JOIN #{@channel}"
end
def evaluate(s)
# Make sure we have a valid expression (for security reasons), and
# evaluate it if we do, otherwise return an error message
if s =~ /^[-+*\/\d\s\eE.()]*$/ then
begin
s.untaint
return eval(s).to_s
rescue Exception => detail
puts detail.message()
end
end
return "Error"
end
def handle_server_input(s)
# This isn't at all efficient, but it shows what we can do with Ruby
# (Dave Thomas calls this construct "a multiway if on steroids")
case s.strip
when /^PING :(.+)$/i
puts "[ Server ping ]"
send "PONG :#{$1}"
when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s.+\s:[\001]PING (.+)[\001]$/i
puts "[ CTCP PING from #{$1}!#{$2}@#{$3} ]"
send "NOTICE #{$1} :\001PING #{$4}\001"
when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s.+\s:[\001]VERSION[\001]$/i
puts "[ CTCP VERSION from #{$1}!#{$2}@#{$3} ]"
send "NOTICE #{$1} :\001VERSION Ruby-irc v0.042\001"
when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s(.+)\s:EVAL (.+)$/i
puts "[ EVAL #{$5} from #{$1}!#{$2}@#{$3} ]"
send "PRIVMSG #{(($4==@nick)?$1:$4)} :#{evaluate($5)}"
when /End of \/MOTD/
send("JOIN #{@channel}")
when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s(.+)\s:(.+)$/i
public_proccess($1, $2, $3, $4, $5)
else
puts s
end
end
def main_loop()
# Just keep on truckin' until we disconnect
while true
ready = select([@irc, $stdin], nil, nil, nil)
next if !ready
for s in ready[0]
if s == $stdin then
return if $stdin.eof
s = $stdin.gets
send s
elsif s == @irc then
return if @irc.eof
s = @irc.gets
handle_server_input(s)
end
end
end
end
end
def public_proccess(nick, user, host, channel, msg)
msg = msg.split
case msg[0]
when /botsnack/
puts "--> BOTSNACK!"
send "PRIVMSG #{channel} ::D"
send "PRIVMSG #{channel} :botsnack"
when /quit/
puts "lol"
send "PRIVMSG #{channel} exiting!"
exit or die "Not a chance."
end
end
def number_to_letter(n=1)
n.to_i.to_s(27).tr("0-9a-q", "A-Z")
end
def make_name
name = []
rand(10)+1.times do
name = name.push(rand(26)+1)
end
result = ""
name.each do |n|
result = result + number_to_letter(n)
end
return result.downcase
end
# Don't allow use of "tainted" data by potentially dangerous operations
$SAFE=1
# The main program
# If we get an exception, then print it out and keep going (we do NOT want
# to disconnect unexpectedly!)
irc = IRC.new(server, port, nick, channel)
irc.connect()
begin
irc.main_loop()
rescue Interrupt
rescue Exception => detail
puts detail.message()
print detail.backtrace.join("\n")
retry
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment