Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/ruby
- require "socket"
- # Don't allow use of "tainted" data by potentially dangerous operations
- $SAFE=1
- # The irc class, which talks to the server and holds the main event loop
- class IRC
- def initialize(server, port, nick, channel)
- @server = server
- @port = port
- @nick = nick
- @channel = channel
- 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 rubybot didabot master :win loose"
- send "NICK #{@nick}"
- 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
- 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 DidaBot - version 0.1a\001"
- when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s(.+)\s:EVAL (.+)$/i
- puts "[ EVAL #{$5} from #{$1}!#{$2}@#{$3} ]"
- send "PRIVMSG #{(($4==@nick)?$1:$4)} :#{evaluate($5)}"
- # when /^:([^!]+)![^@]+@[^ ]+ PRIVMSG #{@channel} :(.+)$/
- # when /^:([^!]+)![^@]+@[^ ]+ PRIVMSG #{@channel} :(!about)$/
- # when /^:dida![^@]+@[^ ]+ PRIVMSG #{@channel} :(.+)$/
- when /^:dida![^@]+@[^ ]+ PRIVMSG #{@channel} :(.+)$/
- @rawr = $1.split(/ * /)
- case @rawr[0]
- when "!kick"
- puts "Admin command: #{@rawr[0]}"
- send "KICK #{@channel} #{@rawr[1]} :#{@rawr.drop(1).join(" ")}"
- when "!ban"
- puts "Admin command: #{@rawr[0]}"
- send "MODE #{@channel} +b #{@rawr[1]}"
- send "KICK #{@channel} #{@rawr[1]} :#{@rawr.drop(1).join(" ")}"
- when "!unban"
- puts "Admin command: #{@rawr[0]}"
- send "MODE #{@channel} -b #{@rawr[1]}!*@*"
- when "!op"
- puts "Admin command: #{@rawr[0]}"
- send "MODE #{@channel} +o #{@rawr[1]}"
- when "!voice"
- puts "Admin command: #{@rawr[0]}"
- send "MODE #{@channel} +v #{@rawr[1]}"
- when "!halfop"
- puts "Admin command: #{@rawr[0]}"
- send "MODE #{@channel} +h #{@rawr[1]}"
- when "!deop"
- puts "Admin command: #{@rawr[0]}"
- send "MODE #{@channel} -o #{@rawr[1]}"
- when "!devoice"
- puts "Admin command: #{@rawr[0]}"
- send "MODE #{@channel} -v #{@rawr[1]}"
- when "!dehalfop"
- puts "Admin command: #{@rawr[0]}"
- send "MODE #{@channel} -h #{@rawr[1]}"
- when "!say"
- puts "User command: #{@rawr[0]}"
- send "PRIVMSG #{@channel} :#{@rawr.drop(1).join(" ")}"
- else
- puts @rawr[0]
- puts s
- end
- when /^:([^!]+)![^@]+@[^ ]+ PRIVMSG #{@channel} :(!.+)$/
- @dat = $1.split(/ * /)
- case @dat[0]
- when "!say"
- puts "User command: #{@dat[0]}"
- send "PRIVMSG #{@channel} :#{@dat.drop(1).join(" ")}"
- else
- puts @rawr[0]
- puts s
- end
- 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
- # 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('irc.freenode.net', 6667, 'didabot', '#webdevrefinery')
- irc.connect()
- begin
- irc.main_loop()
- irc.send_message()
- rescue Interrupt
- rescue Exception => detail
- puts detail.message()
- print detail.backtrace.join("\n")
- retry
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement