3 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
4 ## Copyright (C) 2007-2012 Daigo Moriwaki (daigo at debian dot org)
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 require 'shogi_server/handicapped_boards'
22 module ShogiServer # for a namespace
24 ######################################################
25 # Processes the LOGIN command.
28 def Login.good_login?(str)
30 if (((tokens.length == 3) ||
31 ((tokens.length == 4) && tokens[3] == "x1")) &&
32 (tokens[0] == "LOGIN") &&
33 (good_identifier?(tokens[1])))
40 def Login.good_game_name?(str)
41 if ((str =~ /^(.+)-\d+-\d+$/) && (good_identifier?($1)))
48 # Check if a game name str is a handicapped game.
49 # @return a subclass of Board coresponding to the handicapped game; false,
52 def Login.handicapped_game_name?(str)
53 return false unless good_game_name?(str)
63 when %r!^hcrooklance_!
81 def Login.good_identifier?(str)
82 if str =~ /\A[\w\d_@\-\.]{1,#{Max_Identifier_Length}}\z/
89 def Login.factory(str, player)
90 (login, player.name, password, ext) = str.chomp.split
92 return Loginx1.new(player, password)
94 return LoginCSA.new(player, password)
100 # the first command that will be executed just after LOGIN.
101 # If it is nil, the default process will be started.
102 attr_reader :csa_1st_str
104 def initialize(player, password)
107 parse_password(password)
111 @player.write_safe(sprintf("LOGIN:%s OK\n", @player.name))
112 log_message(sprintf("user %s run in %s mode", @player.name, @player.protocol))
115 def incorrect_duplicated_player(str)
116 @player.write_safe("LOGIN:incorrect\n")
117 @player.write_safe(sprintf("username %s is already connected\n", @player.name)) if (str.split.length >= 4)
118 sleep 3 # wait for sending the above messages.
119 @player.name = "%s [duplicated]" % [@player.name]
124 ######################################################
125 # Processes LOGIN for the CSA standard mode.
127 class LoginCSA < Login
130 attr_reader :gamename
132 # A turn preference string: "+", "-" or default "*"
133 attr_reader :turn_preference
135 def initialize(player, password)
137 @turn_preference = "*"
139 @player.protocol = PROTOCOL
142 # Parse a gamename str and see if it includes an optional turn
144 # ex. foo-1500-0-B for black
145 # ex. foo-1500-0-W for white
147 # Return an array of a valid gamename without an turn preference and a
148 # turn character "+" or "-"; false otherwise
150 def parse_gamename_turn(str)
151 if str =~ /^(.+)-\d+-\d+-(\w)$/
154 return [str[0, str.length-2], "+"]
156 return [str[0, str.length-2], "-"]
162 def parse_password(password)
163 if Login.good_game_name?(password) || parse_gamename_turn(password)
165 @player.set_password(nil)
166 elsif password.split(",").size > 1
167 @gamename, *trip = password.split(",")
168 @player.set_password(trip.join(","))
170 @player.set_password(password)
171 @gamename = Default_Game_Name
173 array = parse_gamename_turn(@gamename)
175 @gamename = array.first
176 @turn_preference = array.last
178 @gamename = Login.good_game_name?(@gamename) ? @gamename : Default_Game_Name
183 @csa_1st_str = "%%GAME #{@gamename} #{@turn_preference}"
187 ######################################################
188 # Processes LOGIN for the extented mode.
190 class Loginx1 < Login
193 def initialize(player, password)
195 @player.protocol = PROTOCOL
198 def parse_password(password)
199 @player.set_password(password)
204 @player.write_safe(sprintf("##[LOGIN] +OK %s\n", PROTOCOL))