OSDN Git Service

Merge remote-tracking branch 'origin/master' into wdoor-stable
[shogi-server/shogi-server.git] / shogi_server / login.rb
1 ## $Id$
2
3 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
4 ## Copyright (C) 2007-2012 Daigo Moriwaki (daigo at debian dot org)
5 ##
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.
10 ##
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.
15 ##
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
19
20 require 'shogi_server/handicapped_boards'
21
22 module ShogiServer # for a namespace
23
24 ######################################################
25 # Processes the LOGIN command.
26 #
27 class Login
28   def Login.good_login?(str)
29     tokens = str.split
30     if (((tokens.length == 3) || 
31         ((tokens.length == 4) && tokens[3] == "x1")) &&
32         (tokens[0] == "LOGIN") &&
33         (good_identifier?(tokens[1])))
34       return true
35     else
36       return false
37     end
38   end
39
40   def Login.good_game_name?(str)
41     if ((str =~ /^(.+)-\d+-\d+F?$/) && (good_identifier?($1)))
42       return true
43     else
44       return false
45     end
46   end
47
48   # Check if a game name str is a handicapped game.
49   # @return a subclass of Board coresponding to the handicapped game; false,
50   # otherwise.
51   #
52   def Login.handicapped_game_name?(str)
53     return false unless good_game_name?(str)
54     ret = nil
55     
56     case str
57     when %r!^hclance_!
58       ret = HCKYBoard
59     when %r!^hcbishop_!
60       ret = HCKABoard
61     when %r!^hcrook_!
62       ret = HCHIBoard
63     when %r!^hcrooklance_!
64       ret = HCHIKYBoard
65     when %r!^hc2p_!
66       ret = HC2PBoard
67     when %r!^hc4p_!
68       ret = HC4PBoard
69     when %r!^hc6p_!
70       ret = HC6PBoard
71     when %r!^hc8p_!
72       ret = HC8PBoard
73     when %r!^hc10p_!
74       ret = HC10PBoard
75     else
76       ret = false
77     end
78     return ret
79   end
80
81   def Login.good_identifier?(str)
82     max = $options["max-identifier"]
83     if str =~ /\A[\w@\-\.]{1,#{max}}\z/
84       return true
85     else
86       return false
87     end
88   end
89
90   def Login.factory(str, player)
91     (_, player.name, password, ext) = str.chomp.split
92     if ext
93       return Loginx1.new(player, password)
94     else
95       return LoginCSA.new(player, password)
96     end
97   end
98
99   attr_reader :player
100   
101   # the first command that will be executed just after LOGIN.
102   # If it is nil, the default process will be started.
103   attr_reader :csa_1st_str
104
105   def initialize(player, password)
106     @player = player
107     @csa_1st_str = nil
108     parse_password(password)
109   end
110
111   def process
112     @player.write_safe(sprintf("LOGIN:%s OK\n", @player.name))
113     log_message("user %s run in %s mode" % [(@player.rated? ? @player.player_id : @player.name),
114                                             @player.protocol])
115   end
116
117   def incorrect_duplicated_player(str)
118     @player.write_safe("LOGIN:incorrect\n")
119     @player.write_safe(sprintf("username %s is already connected\n", @player.name)) if (str.split.length >= 4)
120     sleep 3 # wait for sending the above messages.
121     @player.name = "%s [duplicated]" % [@player.name]
122     @player.finish
123   end
124 end
125
126 ######################################################
127 # Processes LOGIN for the CSA standard mode.
128 #
129 class LoginCSA < Login
130   PROTOCOL = "CSA"
131
132   attr_reader :gamename
133
134   # A turn preference string: "+", "-" or default "*"
135   attr_reader :turn_preference
136
137   def initialize(player, password)
138     @gamename = nil
139     @turn_preference = "*"
140     super
141     @player.protocol = PROTOCOL
142   end
143
144   # Parse a gamename str and see if it includes an optional turn 
145   # preference. 
146   # ex. foo-1500-0-B for black
147   # ex. foo-1500-0-W for white
148   #
149   # Return an array of a valid gamename without an turn preference and a
150   # turn character "+" or "-"; false otherwise
151   #
152   def parse_gamename_turn(str)
153     if str =~ /^(.+)-\d+-\d+F?-(\w)$/
154       case $2
155       when "b","B"
156         return [str[0, str.length-2], "+"]
157       when "w","W"
158         return [str[0, str.length-2], "-"]
159       end
160     end
161     return false
162   end
163
164   def parse_password(password)
165     if Login.good_game_name?(password) || parse_gamename_turn(password)
166       @gamename = password
167       @player.set_password(nil)
168     elsif password.split(",").size > 1
169       @gamename, *trip = password.split(",")
170       @player.set_password(trip.join(","))
171     else
172       @player.set_password(password)
173       @gamename = Default_Game_Name
174     end
175     array = parse_gamename_turn(@gamename)
176     if array
177       @gamename = array.first
178       @turn_preference = array.last
179     end
180     @gamename = Login.good_game_name?(@gamename) ? @gamename : Default_Game_Name
181   end
182
183   def process
184     super
185     @csa_1st_str = "%%GAME #{@gamename} #{@turn_preference}"
186   end
187 end
188
189 ######################################################
190 # Processes LOGIN for the extented mode.
191 #
192 class Loginx1 < Login
193   PROTOCOL = "x1"
194
195   def initialize(player, password)
196     super
197     @player.protocol = PROTOCOL
198   end
199   
200   def parse_password(password)
201     @player.set_password(password)
202   end
203
204   def process
205     super
206     @player.write_safe(sprintf("##[LOGIN] +OK %s\n", PROTOCOL))
207   end
208 end
209
210 end # ShogiServer