Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "socket"
- class IRCSession
- def initialize(server, port, nick, user, channels)
- @server = server
- @port = port
- @nick = nick
- @user = user
- @channels = channels
- end
- ## Send to the server, output to console
- def send(msg)
- begin
- puts "send> #{msg}"
- @sock.send "#{msg}\n", 0
- rescue
- puts "## Message failed to send ##"
- end
- end
- ## Connect to server, start the loop
- def connect()
- @sock = TCPSocket.open(@server, @port)
- send "USER #{@user} #{@user} #{@user} #{@user}"
- send "NICK #{@nick}"
- @channels.each { |chan| send "JOIN #{chan}" }
- start_loop
- end
- ## Processes input from server -- Just have the one case there now
- ## to prevent ping timeouts, will develop this part later
- def server_input(s)
- puts s
- case s.strip
- when /^ping /i
- puts "recv> PING {$'}"
- send "PONG #{$'}"
- end
- end
- ## Loop that keeps connection open, processes input
- def start_loop()
- loop do
- input = select([@sock, $stdin], nil, nil)
- next if !input
- for s in input[0]
- if (s == $stdin) then
- return if $stdin.eof?
- s = $stdin.gets
- command(s)
- else#if (s == @sock) then
- return if @sock.eof?
- s = @sock.gets
- #server_input(s)
- p s
- end
- end
- end
- end
- ## Process console input -- Yeah, I know it's shitty, but it's just a
- ## quick fix to get it working
- def command(s)
- s.chomp!
- case s.strip
- when /^msg /i
- privmsg $'
- when /^join /i
- if !(@channels.include?($'))
- send "JOIN #{$'}"
- @channels.push($')
- end
- when /^part /i
- if @channels.include?($')
- send "PART #{$'}"
- @channels.delete($')
- end
- end
- end
- ## Send a message to all active channels
- def privmsg (msg)
- @channels.each { |chan| send "PRIVMSG #{chan} #{msg}" }
- end
- end
- bot = IRCSession.new('irc.tddirc.net', 6667, 'scamper', 'scamper', ['#bots'])
- bot.connect
Add Comment
Please, Sign In to add comment