OSDN Git Service

- shogi_server/command.rb:
[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-2008 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+$/) && (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     if str =~ /\A[\w\d_@\-\.]{1,#{Max_Identifier_Length}}\z/
83       return true
84     else
85       return false
86     end
87   end
88
89   def Login.factory(str, player)
90     (login, player.name, password, ext) = str.chomp.split
91     if ext
92       return Loginx1.new(player, password)
93     else
94       return LoginCSA.new(player, password)
95     end
96   end
97
98   attr_reader :player
99   
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
103
104   def initialize(player, password)
105     @player = player
106     @csa_1st_str = nil
107     parse_password(password)
108   end
109
110   def process
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))
113   end
114
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]
120     @player.finish
121   end
122 end
123
124 ######################################################
125 # Processes LOGIN for the CSA standard mode.
126 #
127 class LoginCSA < Login
128   PROTOCOL = "CSA"
129
130   def initialize(player, password)
131     @gamename = nil
132     super
133     @player.protocol = PROTOCOL
134   end
135
136   def parse_password(password)
137     if Login.good_game_name?(password)
138       @gamename = password
139       @player.set_password(nil)
140     elsif password.split(",").size > 1
141       @gamename, *trip = password.split(",")
142       @player.set_password(trip.join(","))
143     else
144       @player.set_password(password)
145       @gamename = Default_Game_Name
146     end
147     @gamename = self.class.good_game_name?(@gamename) ? @gamename : Default_Game_Name
148   end
149
150   def process
151     super
152     @csa_1st_str = "%%GAME #{@gamename} *"
153   end
154 end
155
156 ######################################################
157 # Processes LOGIN for the extented mode.
158 #
159 class Loginx1 < Login
160   PROTOCOL = "x1"
161
162   def initialize(player, password)
163     super
164     @player.protocol = PROTOCOL
165   end
166   
167   def parse_password(password)
168     @player.set_password(password)
169   end
170
171   def process
172     super
173     @player.write_safe(sprintf("##[LOGIN] +OK %s\n", PROTOCOL))
174   end
175 end
176
177 end # ShogiServer