1 # server.rb --- A simple IPC server executing Ruby programs.
2 # Copyright (C) 1998-2005 Daiki Ueno
4 # Author: Daiki Ueno <ueno@unixuser.org>
6 # Keywords: IRC, riece, Ruby
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(outfile, errfile, logfile)
32 $stdout = outfile ? File.new(outfile, 'a') : StringIO.new
33 $stderr = errfile ? File.new(errfile, 'a') : StringIO.new
34 @log = File.new(logfile, 'a') if logfile
43 @log.puts(line) if @log
50 d = "dispatch_#{c.downcase}"
51 if respond_to?(d, true)
56 send_line("ERR 103 Unknown command\r\n")
61 def dispatch_cancel(c, r)
62 send_line("ERR 100 Not implemented\r\n")
65 def dispatch_bye(c, r)
66 send_line("ERR 100 Not implemented\r\n")
69 def dispatch_auth(c, r)
70 send_line("ERR 100 Not implemented\r\n")
73 def dispatch_reset(c, r)
74 send_line("ERR 100 Not implemented\r\n")
77 def dispatch_end(c, r)
81 def dispatch_help(c, r)
82 send_line("ERR 100 Not implemented\r\n")
85 def dispatch_quit(c, r)
86 send_line("ERR 100 Not implemented\r\n")
89 def dispatch_eval(c, r)
90 r = deq_data if r.empty?
93 while @thr.include?(name = @cnt.to_s)
96 @thr[name] = Thread.current
98 send_line("S name #{name}\r\n")
100 Thread.current[:rubyserv_name] = name
102 Thread.current[:rubyserv_error] = false
103 Thread.current[:rubyserv_response] = eval(r, exec_env.empty_binding)
104 rescue Exception => e
105 Thread.current[:rubyserv_error] = true
106 Thread.current[:rubyserv_response] = e.to_s.sub(/\A.*?\n/, '')
108 send_line("# exit #{name}\r\n")
111 def dispatch_poll(c, r)
114 send_line("ERR 105 Parameter error: no such name \"#{r}\"\r\n")
116 send_line("S running #{r}\r\n")
119 if thr[:rubyserv_error]
120 send_line("S exited #{r}\r\n")
122 send_line("S finished #{r}\r\n")
124 if d = thr[:rubyserv_response]
131 def dispatch_exit(c, r)
134 send_line("ERR 105 Parameter error: no such name \"#{r}\"\r\n")
137 thr.kill if thr.alive?
143 s.gsub(/[%\r\n]/) {|m| '%%%02X' % m[0]}
147 s.gsub(/%([0-9A-Z][0-9A-Z])/) {[$1].pack('H*')}
153 len = [d.length, 998].min # 998 = 1000 - "D "
154 send_line("D #{d[0 ... len]}\r\n")
171 @log.puts(line) if @log
176 def env.empty_binding
179 out, log = @out, @log
180 env.instance_eval {@out, @log = out, log}
181 def env.send_line(line)
183 @log.puts(line) if @log
186 send_line("# output #{Thread.current[:rubyserv_name]} #{s}\r\n")
195 opt_outfile, opt_errfile, opt_logfile = nil, nil, nil
196 opts = OptionParser.new do |opts|
197 opts.banner = <<"End"
198 Usage: #{$0} [OPTIONS]
200 opts.on('-o', '--out OUTFILE', 'Send stdout to OUTFILE.') do |outfile|
201 opt_outfile = outfile
203 opts.on('-e', '--err ERRFILE', 'Send stderr to ERRFILE.') do |errfile|
204 opt_errfile = errfile
206 opts.on('-l', '--log LOGFILE', 'Send stdlog to LOGFILE.') do |logfile|
207 opt_logfile = logfile
209 opts.on_tail('--help', '-h', 'Show this message.') do
210 $stdout.print(opts.to_s)
216 rescue OptionParser::ParseError
217 $stderr.print(opts.to_s)
221 server = Server.new(opt_outfile, opt_errfile, opt_logfile)