+
+######################################################
+# Processes the LOGIN command.
+#
+class Login
+ def Login.good_login?(str)
+ tokens = str.split
+ if (((tokens.length == 3) || ((tokens.length == 4) && tokens[3] == "x1")) &&
+ (tokens[0] == "LOGIN") &&
+ (good_identifier?(tokens[1])))
+ return true
+ else
+ return false
+ end
+ end
+
+ def Login.good_game_name?(str)
+ if ((str =~ /^(.+)-\d+-\d+$/) && (good_identifier?($1)))
+ return true
+ else
+ return false
+ end
+ end
+
+ def Login.good_identifier?(str)
+ if str =~ /\A[\w\d_@\-\.]{1,#{Max_Identifier_Length}}\z/
+ return true
+ else
+ return false
+ end
+ end
+
+ def Login.factory(str, player)
+ (login, player.name, password, ext) = str.chomp.split
+ if (ext)
+ return Loginx1.new(player, password)
+ else
+ return LoginCSA.new(player, password)
+ end
+ end
+
+ attr_reader :player
+
+ # the first command that will be executed just after LOGIN.
+ # If it is nil, the default process will be started.
+ attr_reader :csa_1st_str
+
+ def initialize(player, password)
+ @player = player
+ @csa_1st_str = nil
+ parse_password(password)
+ end
+
+ def process
+ @player.write_safe(sprintf("LOGIN:%s OK\n", @player.name))
+ log_message(sprintf("user %s run in %s mode", @player.name, @player.protocol))
+ end
+
+ def incorrect_duplicated_player(str)
+ @player.write_safe("LOGIN:incorrect\n")
+ @player.write_safe(sprintf("username %s is already connected\n", @player.name)) if (str.split.length >= 4)
+ sleep 3 # wait for sending the above messages.
+ @player.name = "%s [duplicated]" % [@player.name]
+ @player.finish
+ end
+end
+
+######################################################
+# Processes LOGIN for the CSA standard mode.
+#
+class LoginCSA < Login
+ PROTOCOL = "CSA"
+
+ def initialize(player, password)
+ @gamename = nil
+ super
+ @player.protocol = PROTOCOL
+ end
+
+ def parse_password(password)
+ if Login.good_game_name?(password)
+ @gamename = password
+ @player.set_password(nil)
+ elsif password.split(",").size > 1
+ @gamename, *trip = password.split(",")
+ @player.set_password(trip.join(","))
+ else
+ @player.set_password(password)
+ @gamename = Default_Game_Name
+ end
+ @gamename = self.class.good_game_name?(@gamename) ? @gamename : Default_Game_Name
+ end
+
+ def process
+ super
+ @csa_1st_str = "%%GAME #{@gamename} *"
+ end
+end
+
+######################################################
+# Processes LOGIN for the extented mode.
+#
+class Loginx1 < Login
+ PROTOCOL = "x1"
+
+ def initialize(player, password)
+ super
+ @player.protocol = PROTOCOL
+ end
+
+ def parse_password(password)
+ @player.set_password(password)
+ end
+
+ def process
+ super
+ @player.write_safe(sprintf("##[LOGIN] +OK %s\n", PROTOCOL))
+ end
+end
+
+
+class BasicPlayer
+ # Idetifier of the player in the rating system
+ attr_accessor :id
+
+ # Name of the player
+ attr_accessor :name
+
+ # Password of the player, which does not include a trip
+ attr_accessor :password
+
+ # Score in the rating sysem
+ attr_accessor :rate
+
+ # Group in the rating system
+ attr_accessor :rating_group
+
+ # Last timestamp when the rate was modified
+ attr_accessor :modified_at
+
+ def initialize