This repository has been archived on 2020-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
ildus/lib/ildus/server/handler.rb

105 lines
3.1 KiB
Ruby

# = ildus/server/handler - Ildus server command handler
#
# Copyright (C) 2005 Paul van Tilburg <paul@luon.net>
#
# Ildus is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
module Ildus
class Server < GServer
class Handler
class ProtocolException < StandardError; end
class CmdUnknownError < ProtocolException; end
class ArgsSyntaxError < ProtocolException; end
class NotImplementedError < ProtocolException; end
class AlreadyAuthError < ProtocolException; end
class TooManyUnknownError < ProtocolException; end
MaxCmdErrs = 3
ProtocolCodeMapping = {
200 => "Command OK.",
# 210, 211, 212, 213
214 => "Direct comments to %s.",
215 => "Listing done.",
220 => "%s %s %s %s, welcome.",
221 => "%s closed connection, goodbye!",
230 => "User %s authenticated, proceed.",
240 => "Updated host %s to IP %s.",
# 330
331 => "User name OK, need password.",
# 420
421 => "%s forcibly closed connection.",
422 => "connection timed out, %s forcibly closed connection.",
# 423, 424
425 => "Can't find host %s.",
426 => "Can't find %s-record of host %s.",
500 => "Syntax error, command `%s' unrecognized.",
501 => "Syntax error in arguments.",
502 => "Command not implemented.",
503 => "Too many unrecognized commands!",
504 => "You are already authenticated!",
505 => "Server error: %s!",
506 => "Server error, update failed: %s",
530 => "Not authenticated!"
}
def intialize(server, io)
@server = server
@io = io
end
def handle_client
io.puts prot_msg(220, "localhost", Program, Version, Time.now.to_s)
cmd_errs = 0
io.each_line do |line|
begin
cmd, *args = line.split /\s+/
handle_command(io, cmd, args)
rescue CmdUnknownError # 500
raise TooManyUnknownError if cmd_errs > MaxCmdErrs
io.puts prot_msg(500, cmd)
cmd_errs = cmd_errs + 1
rescue ArgsSyntaxError # 501
io.puts prot_msg(501)
rescue NotImplementedError # 502
io.puts prot_msg(502)
rescue AlreadyAuthError # 504
io.puts prot_msg(504)
end
end
rescue TooManyUnknownError # 503
io.puts prot_msg(503)
rescue RuntimeError => msg # 505
io.puts prot_msg(505, msg)
raise
end
#########
private
#########
def handle_command(io, cmd, args)
method_name = cmd.downcase + "_cmd"
meth = method(method_name)
meth.call(*args)
rescue NameError
raise CmdUnknownError
end
def prot_msg(code, *args)
code.to_s << " " << ProtocolCodeMapping[code] % args
end
end # class Handler
end # class Server
end # module Ildus