1 |
#! perl |
2 |
|
3 |
# login handling |
4 |
|
5 |
use Fcntl; |
6 |
use Coro::AIO; |
7 |
|
8 |
my $PLAYERDIR = sprintf "%s/%s", cf::localdir, cf::playerdir; |
9 |
|
10 |
# testbed for coroutines in crossfire : |
11 |
|
12 |
sub query { |
13 |
my ($ns, $flags, $text) = @_; |
14 |
|
15 |
my $current = $Coro::current; |
16 |
$ns->query ($flags, $text, sub { $current->ready; $current = $_[0]; }); |
17 |
Coro::schedule while ref $current; |
18 |
|
19 |
$current |
20 |
} |
21 |
|
22 |
sub can_cleanup { |
23 |
my ($playerfile, $mtime) = @_; |
24 |
|
25 |
my $age = time - $mtime; |
26 |
my $level = $playerfile =~ /^level (\d+)$/m ? $1 : return; |
27 |
|
28 |
($level <= 3 && $age > 7 * 86400) # 7 days for level 0..3 |
29 |
|| ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 |
30 |
|| ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 |
31 |
|| $age > 700 * 86400 # 2 years for everybody else |
32 |
} |
33 |
|
34 |
sub check_playing { |
35 |
my ($ns, $user) = @_; |
36 |
|
37 |
return unless cf::player::find $user; |
38 |
|
39 |
$ns->send_drawinfo ( |
40 |
"That player is already logged in on this server. " |
41 |
. "If you want to create a new player, choose another name. " |
42 |
. "If you are already a registered player, make sure nobody " |
43 |
. "else is using your account at this time. If you lost your conenction " |
44 |
. "then the server will likely timeout within a minute. If you still " |
45 |
. "cannot log-in after a minute, you are still logged in. Make sure " |
46 |
. "you do not have another client running. If you use windows, reboot, " |
47 |
. "this will fix anything.", |
48 |
cf::NDI_RED |
49 |
); |
50 |
|
51 |
1 |
52 |
} |
53 |
|
54 |
# delete a player directory, be non-blocking AND synchronous... |
55 |
# (thats hard, so we crap out and fork). |
56 |
sub nuke_playerdir { |
57 |
my ($user) = @_; |
58 |
|
59 |
aio_stat "$PLAYERDIR/$user"; |
60 |
system "cd \Q$PLAYERDIR\E " |
61 |
. "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " |
62 |
. "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; |
63 |
} |
64 |
|
65 |
sub on_addme { |
66 |
my ($ns) = @_; |
67 |
|
68 |
$ns->destroy if $ns->pl; |
69 |
|
70 |
$ns->coro (sub { |
71 |
my ($user, $pass); |
72 |
|
73 |
$ns->send_packet ("addme_success"); |
74 |
|
75 |
for (;;) { |
76 |
$ns->send_drawinfo ( |
77 |
"Please enter your username now. If you are a new user, " |
78 |
. "make one up that describes your character best. " |
79 |
. "Only letters and digits are allowed, though.", |
80 |
cf::NDI_BLUE |
81 |
); |
82 |
|
83 |
# read username |
84 |
while () { |
85 |
$user = query $ns, 0, "What is your name?\n:"; |
86 |
|
87 |
if ($cf::LOGIN_LOCK{$user}) { |
88 |
$ns->send_drawinfo ( |
89 |
"That username is currently used in another login session. " |
90 |
. "Chose another, or wait till the other session has ended.", |
91 |
cf::NDI_RED |
92 |
); |
93 |
} elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}$/) { |
94 |
last; |
95 |
} else { |
96 |
$ns->send_drawinfo ( |
97 |
"Your username contains illegal characters " |
98 |
. "(only a-z, A-Z and 0-9 are allowed), " |
99 |
. "or is not between 3 and 18 characters in length.", |
100 |
cf::NDI_RED |
101 |
); |
102 |
} |
103 |
} |
104 |
|
105 |
check_playing $ns, $user and next; |
106 |
|
107 |
$ns->send_drawinfo ( |
108 |
"Welcome $user, please enter your password now. " |
109 |
. "New users should now choose a password. " |
110 |
. "Anything your client lets you enter is fine.", |
111 |
cf::NDI_BLUE |
112 |
); |
113 |
|
114 |
# read password |
115 |
while () { |
116 |
$pass = query $ns, cf::CS_QUERY_HIDEINPUT, "What is your password?\n:"; |
117 |
last if $pass =~ /.../; |
118 |
$ns->send_drawinfo ( |
119 |
"Try to use at least three characters as your password please, " |
120 |
. "that cannot be too much to ask for :)", |
121 |
cf::NDI_RED |
122 |
); |
123 |
} |
124 |
|
125 |
my $dir = "$PLAYERDIR/$user"; |
126 |
my $plfile = "$dir/$user.pl"; |
127 |
|
128 |
# lock this username for the remainder of this login session |
129 |
if ($cf::LOGIN_LOCK{$user}) { |
130 |
$ns->send_drawinfo ( |
131 |
"That username is currently used in another login session. " |
132 |
. "Chose another, or wait till the other session has ended.", |
133 |
cf::NDI_RED |
134 |
); |
135 |
next; |
136 |
} |
137 |
local $cf::LOGIN_LOCK{$user} = 1; |
138 |
|
139 |
check_playing $ns, $user and next; |
140 |
|
141 |
# try to read the user file and check the password |
142 |
if (my $fh = aio_open $plfile, O_RDONLY, 0) { |
143 |
my $mtime = (stat $fh)[9]; |
144 |
|
145 |
0 < aio_read $fh, 0, 16384, my $buf, 0 or next; |
146 |
$buf =~ /^password (\S+)$/m or next; |
147 |
my $hash = $1; |
148 |
|
149 |
if ($hash eq crypt $pass, $hash) { |
150 |
# password matches, wonderful |
151 |
my $pl = cf::player::load $plfile or next; |
152 |
$pl->enable_save (1); |
153 |
$pl->connect ($ns); |
154 |
last; |
155 |
} elsif (can_cleanup $buf, $mtime) { |
156 |
Coro::Timer::sleep 1; |
157 |
|
158 |
$ns->send_drawinfo ( |
159 |
"Player exists, but password does not match. If this is your account, " |
160 |
. "please try again. If not, you can now decide to take over this account " |
161 |
. "because it has not been in-use for some time.", |
162 |
cf::NDI_RED |
163 |
); |
164 |
|
165 |
(query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ |
166 |
or next; |
167 |
|
168 |
# check if the file hasn't changed |
169 |
aio_stat $plfile and next; |
170 |
$mtime == (stat _)[9] or next; |
171 |
|
172 |
nuke_playerdir $user; |
173 |
|
174 |
# fall through to creation |
175 |
} else { |
176 |
Coro::Timer::sleep 1; |
177 |
|
178 |
$ns->send_drawinfo ( |
179 |
"Wrong username or password. Please try again " |
180 |
. "(check for Numlock and other semi-obvious error sources).", |
181 |
cf::NDI_RED |
182 |
); |
183 |
next; |
184 |
} |
185 |
} |
186 |
|
187 |
# the rest of this function is character creation |
188 |
|
189 |
# just to make sure nothing is left over |
190 |
nuke_playerdir $user; |
191 |
|
192 |
my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; |
193 |
|
194 |
if ($pass2 ne $pass) { |
195 |
$ns->send_drawinfo ( |
196 |
"The passwords do not match, please try again.", |
197 |
cf::NDI_RED |
198 |
); |
199 |
next; |
200 |
} |
201 |
|
202 |
my $pl = cf::player::create; |
203 |
$pl->ob->name ($user); |
204 |
$pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); |
205 |
$pl->connect ($ns); |
206 |
|
207 |
my $ob = $pl->ob; |
208 |
|
209 |
while () { |
210 |
$ob->update_stats; |
211 |
$pl->save_stats; |
212 |
|
213 |
my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
214 |
"[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?"; |
215 |
|
216 |
if ($res =~ /^[Nn]/) { |
217 |
last; |
218 |
} elsif ($res > 0 && $res <= 7) { |
219 |
my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?"; |
220 |
|
221 |
if ($swap > 0 && $swap <= 7) { |
222 |
$ob->swap_stats ($res - 1, $swap - 1); |
223 |
} |
224 |
} else { |
225 |
$ob->roll_stats; |
226 |
} |
227 |
} |
228 |
|
229 |
$ob->set_animation (2); |
230 |
$ob->add_statbonus; |
231 |
|
232 |
$ns->send_drawinfo ($ob->msg, cf::NDI_BLUE); |
233 |
$ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR, |
234 |
"Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"); |
235 |
|
236 |
$ns->state (cf::ST_CHANGE_CLASS); |
237 |
$pl->enable_save (1);#d# too early |
238 |
|
239 |
last; |
240 |
} |
241 |
}); |
242 |
} |
243 |
|
244 |
cf::object->attach ( |
245 |
type => cf::SAVEBED, |
246 |
on_apply => sub { |
247 |
my ($bed, $ob) = @_; |
248 |
|
249 |
return cf::override 0 unless $ob->type == cf::PLAYER; |
250 |
|
251 |
# update respawn position |
252 |
$ob->contr->savebed ($bed->map->path, $bed->x, $bed->y); |
253 |
|
254 |
$ob->contr->killer ("left"); |
255 |
$ob->check_score; |
256 |
|
257 |
$ob->reply (undef, "In the future, you will wake up here when you die."); |
258 |
$ob->contr->save (1); |
259 |
|
260 |
$ob->contr->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { |
261 |
if ($_[0] !~ /^[yY]/) { |
262 |
$ob->contr->ns->destroy; |
263 |
} else { |
264 |
$ob->contr->enable_save (1); |
265 |
} |
266 |
}); |
267 |
}, |
268 |
); |
269 |
|
270 |
cf::client->attach (package => __PACKAGE__); |
271 |
|