Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'socket'
- class IRCBot
- attr_accessor :sock
- def initialize(server, port, nick, channel)
- @sock = TCPSocket.new(server, port)
- @nick = nick
- @first_channel = channel
- @channels = Hash.new
- @priveleged_buffer = Hash.new(Array.new)
- end
- def start
- send "USER #{@nick} #{@nick} #{@nick} :#{@nick}"
- send "NICK #{@nick}"
- wait
- end
- def wait #TODO Handle this more efficiently and simply
- while !@sock.closed?
- line = @sock.readline.chomp.strip
- log line, :in
- if line =~ /^PING :(.+)$/i
- pong($1)
- elsif line =~ /^.*?PRIVMSG (\S+) :!say (.+)/
- say($2, $1)
- elsif line =~ /^.*?PRIVMSG \S+ :!join (.+)/
- join($1)
- elsif line =~ /^.*?PRIVMSG \S+ :!part (\S+)\s*(\S)*/
- part($1, $2)
- elsif line =~ /^.*?001 #{@nick}.*$/
- join(@first_channel) #TODO Split this off into a separate function and handle things such as identifying and the like
- #elsif line =~ /^.*?352 \S+ (\S+) \S+ \S+ \S+ (\S+) \S+ :\S \S+/ #:server_name 352 botsnick channel name host server nick modes :0 name
- # who_response($1, $2)
- end
- end
- end
- def send(message)
- log message, :out
- @sock.print "#{message}\r\n"
- end
- def log(message, direction = :info)
- if direction == :in
- puts "--> #{message}"
- elsif direction == :out
- puts "<-- #{message}"
- else
- puts "#{message}"
- end
- end
- def pong(target)
- send "PONG :#{target}"
- end
- def say(text, channel)
- send "PRIVMSG #{channel} :#{text}"
- end
- def join(channel)
- send "JOIN #{channel}"
- @channels[channel] = Channel.new(channel, self) unless @channels[channel]
- end
- def part(channel, reason = "Leaving")
- send "PART #{channel} :#{reason}"
- @channels.delete(channel)
- end
- def who(channel)
- send "WHO #{channel}"
- end
- end
- class Channel
- attr_accessor :name
- def initialize(name, bot)
- @name = name
- @bot = bot
- @users = Hash.new
- end
- end
- marcobot = IRCBot.new("irc.hackthissite.org", 6667, "MarcoB", "#bots")
- marcobot.start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement