Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## main.rb
- #!/usr/bin/ruby
- require 'irc_bot.rb'
- puts "Loading IRC bot class..."
- i = IRC.new "irc://Vanniz:p255w0rd@irc.freenode.net:6667/kongbots"
- puts "Loaded, connecting..."
- i.connect
- i.loop
- puts "Terminated"
- ## irc_bot.rb
- #!/usr/bin/ruby
- require 'uri'
- require 'socket'
- class IRC
- @nick = ""
- @password = nil
- @server = ""
- @port = 6667
- @channel = ""
- @irc = nil
- @pong_pid = 0
- def initialize(uri)
- u = URI.parse(uri)
- if u.scheme != "irc" || u.user == "" || u.path == "" || u.host == ""
- raise "Incorrect IRC URI"
- end
- # Required parts
- @nick = u.user
- @server = u.host
- @channel = "##{u.path.split('/')[1]}"
- # Optional, but are still set
- @password = u.password || nil
- @port = u.port || 6667
- end
- def connect
- puts "Connecting to #{@server}..."
- @irc = TCPSocket.new @server, @port
- raw "NICK #{@nick}"
- raw "USER foo bar baz :borked"
- @pong_pid = fork do
- pong_count = 1
- while true
- raw "PONG :#{pong_count}"
- pong_count += 1
- sleep 15
- end
- end
- end
- def raw(msg)
- begin
- @irc.puts msg
- puts "-- #{msg}"
- rescue Exception => e
- # Do error stuff
- puts "Error writing to TCP socket at #{@server}"
- puts e.messagee
- puts e.backtrace.join("\n")
- end
- end
- def loop
- while true
- break if @irc.eof
- line = @irc.gets
- break if line == nil || line == ""
- if line =~ /^PING\s:(.*)/
- puts "Server PING #{$1}"
- raw "PONG #{$1}"
- elsif line =~ /^:(.*?)\s([0-9]*?)\s(.*?)\s:(.*)/
- case $2
- when "375" : puts "+++ MOTD"
- when "376" : puts "--- MOTD"; introduce
- when "372" : puts "(MOTD) #{$4}"
- when "001" : puts $4
- when "002" : puts $4
- when "003" : puts $4
- when "005" : # Nothing
- when "251" : puts $4
- when "252" : puts $3.split(' ')[1] + " " + $4
- when "253" : puts $3.split(' ')[1] + " " + $4
- when "254" : puts $3.split(' ')[1] + " " + $4
- when "255" : puts $4
- when "265" : puts $4
- when "266" : puts $4
- when "353" : puts "Current users: #{$4}"
- when "366" : # Nothing
- else puts line
- end
- elsif line =~ /.*? 004 (.*?) (.*?) (.*?) (.*?)$/
- puts "Connected to #{$2} version #{$3}"
- # elsif line =~ /:(.*?)\s([A-Z]+?)\s[#{@nick}|(#.*?)]\s:(.*?)/
- # puts $1, $2, $3, $4
- else
- puts line
- end
- end
- end
- def introduce
- raw "JOIN #{@channel}"
- say "The #{@nick} is watching"
- end
- def say(msg, target=@channel)
- raw "PRIVMSG #{target} :#{msg}"
- end
- def killall
- @irc.close
- Process.kill("HUP", @pong_pid)
- end
- end
- if __FILE__ == $0
- i = IRC.new "irc://Vanniz:p255w0rd@irc.frenode.net:6667/kongbots"
- trap(:INT) do
- i.say "My owner has called the quits, bye bye"
- i.killall
- end
- begin
- i.connect
- i.loop
- rescue Exception => e
- # Neccessary because a fork{}-ed process will continue to run
- # so the error is printed AND the process is completely killed
- puts e.message
- puts e.backtrace.join("\n")
- i.killall
- end
- puts "Terminated!"
- end
Add Comment
Please, Sign In to add comment