1 # aproxy.rb --- async proxy between an IRC server and a client
2 # Copyright (C) 1998-2005 Daiki Ueno
4 # Author: Daiki Ueno <ueno@unixuser.org>
8 # This file is part of Riece.
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2, or (at your option)
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with GNU Emacs; see the file COPYING. If not, write to the
22 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 # Boston, MA 02111-1307, USA.
29 def initialize(host, port, size, back)
33 @back = File.new(back, 'a') if back
37 $stdout.nonblock = true
38 trap('STOP', 'IGNORE')
39 trap('TSTP', 'IGNORE')
40 socket = TCPSocket.new(@host, @port)
41 @out.write("NOTICE CONNECTED #{$$}\r\n")
45 rfds, wfds, = select([socket, $stdin], wfds_in)
48 s = buf.slice!(0 ... @size)
49 @back.write(s) if @back
53 len = $stdout.syswrite(buf)
60 if rfds.include?(socket)
61 line = socket.gets("\r\n")
63 if line =~ /\A(?::\S+\s+)?PING\s+(.*)\r\n/i
64 socket.write("PONG #{$1}\r\n")
70 if rfds.include?($stdin)
71 line = $stdin.gets("\r\n")
83 opt_size, opt_back = nil, nil
84 opts = OptionParser.new do |opts|
86 Usage: #{$0} [OPTIONS] host port
88 opts.on('-s', '--size SIZE', 'Size of buffer.') do |size|
91 opts.on('-b', '--back BACK', 'Send outdated messages to BACK.') do |back|
94 opts.on_tail('--help', '-h', 'Show this message.') do
95 $stdout.print(opts.to_s)
101 rescue OptionParser::ParseError
102 $stderr.print(opts.to_s)
106 AProxy.new(ARGV.shift, ARGV.shift, opt_size, opt_back).start