All pastes #461597 Raw Edit

Unnamed

public ruby v1 · immutable
#461597 ·published 2007-04-27 19:44 UTC
rendered paste body
#!/usr/bin/env rubyrequire 'socket'class Network    attr_accessor :host    attr_accessor :port    attr_accessor :socket    attr_accessor :user    attr_accessor :call_args    def initialize(host, port, user)        @host = host        @port = port        @user = user        @call_args = []    end    def command_join        puts @call_args[0]    end    def connect        @socket = TCPSocket.new(@host, @port)        produce "NICK #{@user.nickname}"        produce "USER #{@user.username} #{@user.hostname} #{@user.servername} :#{@user.realname}"    end    def consume        # Asynchronous IO loop, handling socket and stdin input        while true            results = select([@socket, STDIN], nil, nil)            next if !results            for result in results[0]                if result == $stdin then                    return if $stdin.eof                        input = $stdin.gets                        handle_command(input)                elsif result == @socket then                    return if @socket.eof                        input = @socket.gets                        handle_server(input)                end            end        end    end    def die        @socket.close    end    def handle_command(input)        puts "Console input -> #{input}"        if input[0, 1] == '/' and input[0, 2] != '//' then            call_array = input.split            call_string = call_array[0][1..call_array[0].length]            if call_array.length > 1 then                @call_args = call_array[1..call_array.length]            end            begin                eval "command_" + call_string                @call_args = []            rescue NameError, ArgumentError                $stderr.print "System error -> #{$!}\n"            end        end    end    def handle_server(input)        puts "Server input -> #{input}"        case input.strip            when /^PING :(.+)$/i                produce "PONG :#{$1}"        end    end    def produce(output)        puts "Output to server -> #{output}"        @socket.send "#{output}\n", 0    endendclass User    attr_accessor :nickname    attr_accessor :username    attr_accessor :hostname    attr_accessor :servername    attr_accessor :realname    def initialize(nickname, username, hostname, servername, realname)        @nickname = nickname        @username = username        @hostname = hostname        @servername = servername        @realname = realname    endendif __FILE__ == $0    iUser = User.new('monkeynut^^', 'monkeynut^^', 'orchid', 'orchid', 'go bananas')    iNetwork = Network.new('irc.p2p-network.org', 6667, iUser)    iNetwork.connect    iNetwork.consume    iNetwork.dieend