ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/login.ext
Revision: 1.17
Committed: Mon Jan 8 14:11:05 2007 UTC (17 years, 4 months ago) by root
Branch: MAIN
Changes since 1.16: +23 -10 lines
Log Message:
quick and dirty emergency fix

File Contents

# Content
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 # paranoia function to overwrite a string-in-place
11 sub nuke_str {
12 substr $_[0], 0, (length $_[0]), "x" x length $_[0]
13 }
14
15 sub query {
16 my ($ns, $flags, $text) = @_;
17
18 my $current = $Coro::current;
19 $ns->query ($flags, $text, sub { $current->ready; $current = $_[0]; });
20 Coro::schedule while ref $current;
21
22 $current
23 }
24
25 sub can_cleanup {
26 my ($playerfile, $mtime) = @_;
27
28 my $age = time - $mtime;
29 my $level = $playerfile =~ /^level (\d+)$/m ? $1 : return;
30
31 ($level <= 3 && $age > 7 * 86400) # 7 days for level 0..3
32 || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9
33 || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20
34 || $age > 700 * 86400 # 2 years for everybody else
35 }
36
37 sub check_playing {
38 my ($ns, $user) = @_;
39
40 return unless cf::player::find_active $user;
41
42 $ns->send_drawinfo (
43 "That player is already logged in on this server. "
44 . "If you want to create a new player, choose another name. "
45 . "If you are already a registered player, make sure nobody "
46 . "else is using your account at this time. If you lost your conenction "
47 . "then the server will likely timeout within a minute. If you still "
48 . "cannot log-in after a minute, you are still logged in. Make sure "
49 . "you do not have another client running. If you use windows, reboot, "
50 . "this will fix anything.",
51 cf::NDI_RED
52 );
53
54 1
55 }
56
57 sub check_clean_save {
58 my ($pl) = @_;
59
60 unless (delete $pl->{clean_save}) {
61 #d#TODO
62 }
63 }
64
65 # delete a player directory, be non-blocking AND synchronous...
66 # (thats hard, so we crap out and fork).
67 sub nuke_playerdir {
68 my ($user) = @_;
69
70 aio_stat "$PLAYERDIR/$user";
71 system "cd \Q$PLAYERDIR\E "
72 . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null "
73 . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)";
74 }
75
76 sub addme {
77 my ($ns) = @_;
78
79 $ns->destroy if $ns->pl;
80
81 $ns->async (sub {
82 my ($user, $pass);
83
84 $ns->send_packet ("addme_success");
85
86 for (;;) {
87 $ns->send_drawinfo (
88 "Please enter your username now. If you are a new user, "
89 . "make one up that describes your character best. "
90 . "Only letters and digits are allowed, though.",
91 cf::NDI_BLUE
92 );
93
94 # read username
95 while () {
96 $user = query $ns, 0, "What is your name?\n:";
97
98 if ($cf::LOGIN_LOCK{$user}) {
99 $ns->send_drawinfo (
100 "That username is currently used in another login session. "
101 . "Chose another, or wait till the other session has ended.",
102 cf::NDI_RED
103 );
104 } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}$/) {
105 last;
106 } else {
107 $ns->send_drawinfo (
108 "Your username contains illegal characters "
109 . "(only a-z, A-Z and 0-9 are allowed), "
110 . "or is not between 3 and 18 characters in length.",
111 cf::NDI_RED
112 );
113 }
114 }
115
116 check_playing $ns, $user and next;
117
118 $ns->send_drawinfo (
119 "Welcome $user, please enter your password now. "
120 . "New users should now choose a password. "
121 . "Anything your client lets you enter is fine.",
122 cf::NDI_BLUE
123 );
124
125 # read password
126 while () {
127 $pass = query $ns, cf::CS_QUERY_HIDEINPUT, "What is your password?\n:";
128 last if $pass =~ /.../;
129 $ns->send_drawinfo (
130 "Try to use at least three characters as your password please, "
131 . "that cannot be too much to ask for :)",
132 cf::NDI_RED
133 );
134 }
135
136 # lock this username for the remainder of this login session
137 if ($cf::LOGIN_LOCK{$user}) {
138 $ns->send_drawinfo (
139 "That username is currently used in another login session. "
140 . "Chose another, or wait till the other session has ended.",
141 cf::NDI_RED
142 );
143 next;
144 }
145 local $cf::LOGIN_LOCK{$user} = 1;
146
147 check_playing $ns, $user and next;
148
149 # try to read the user file and check the password
150 if (my $fh = aio_open cf::player::path $user, O_RDONLY, 0) {
151 my $mtime = (stat $fh)[9];
152
153 0 < aio_read $fh, 0, 16384, my $buf, 0 or next;
154 $buf =~ /^password (\S+)$/m or next;
155 my $hash = $1;
156
157 if ($hash eq crypt $pass, $hash) {
158 nuke_str $pass;
159 # password matches, wonderful
160 my $pl = cf::player::find $user or next;
161 $pl->connect ($ns);
162 check_clean_save $pl;
163 $pl->{clean_save} = 1;
164 last;
165 } elsif (can_cleanup $buf, $mtime) {
166 Coro::Timer::sleep 1;
167
168 $ns->send_drawinfo (
169 "Player exists, but password does not match. If this is your account, "
170 . "please try again. If not, you can now decide to take over this account "
171 . "because it has not been in-use for some time.",
172 cf::NDI_RED
173 );
174
175 #TODO: nuke_str
176 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/
177 or next;
178
179 # check if the file hasn't changed
180 aio_stat cf::player::path $user and next;
181 $mtime == (stat _)[9] or next;
182
183 nuke_playerdir $user;
184
185 # fall through to creation
186 } else {
187 nuke_str $pass;
188
189 Coro::Timer::sleep 1;
190
191 $ns->send_drawinfo (
192 "Wrong username or password. Please try again "
193 . "(check for Numlock and other semi-obvious error sources).",
194 cf::NDI_RED
195 );
196 next;
197 }
198 }
199
200 # the rest of this function is character creation
201
202 # just to make sure nothing is left over
203 nuke_playerdir $user;
204
205 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again.";
206
207 if ($pass2 ne $pass) {
208 nuke_str $pass;
209 nuke_str $pass2;
210 $ns->send_drawinfo (
211 "The passwords do not match, please try again.",
212 cf::NDI_RED
213 );
214 next;
215 }
216
217 nuke_str $pass2;
218
219 my $pl = cf::player::new $user;
220 $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
221 nuke_str $pass;
222 $pl->connect ($ns);
223
224 my $ob = $pl->ob;
225
226 while () {
227 $ob->update_stats;
228 $pl->save_stats;
229
230 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
231 "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?";
232
233 if ($res =~ /^[Nn]/) {
234 last;
235 } elsif ($res > 0 && $res <= 7) {
236 my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?";
237
238 if ($swap > 0 && $swap <= 7) {
239 $ob->swap_stats ($res - 1, $swap - 1);
240 }
241 } else {
242 $ob->roll_stats;
243 }
244 }
245
246 $ob->set_animation (2);
247 $ob->add_statbonus;
248
249 $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE);
250 $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR,
251 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n");
252
253 $ns->state (cf::ST_CHANGE_CLASS);
254 delete $pl->{deny_save};#d# too early
255
256 last;
257 }
258 });
259 }
260
261 cf::register_command quit => sub {
262 my ($ob, $arg) = @_;
263
264 $ob->reply (undef,
265 "Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. "
266 . "If you are sure you want to do this, then use the quit_character command instead of quit.",
267 cf::NDI_UNIQUE | cf::NDI_RED);
268 };
269
270 cf::register_command quit_character => sub {
271 my ($ob, $arg) = @_;
272
273 my $pl = $ob->contr;
274
275 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub {
276 if ($_[0] !~ /^[yY]/) {
277 $ob->reply (undef,
278 "Ok, not not quitting then.",
279 cf::NDI_UNIQUE | cf::NDI_RED);
280 } else {
281 $ob->reply (undef,
282 "Ok, quitting, hope to see you again.",
283 cf::NDI_UNIQUE | cf::NDI_RED);
284 $pl->ns->flush;
285 $pl->quit_character;
286 }
287 });
288 };
289
290 cf::object->attach (
291 type => cf::SAVEBED,
292 on_apply => sub {
293 my ($bed, $ob) = @_;
294
295 return cf::override 0 unless $ob->type == cf::PLAYER;
296
297 my $pl = $ob->contr;
298
299 # update respawn position
300 $pl->savebed ($bed->map->path, $bed->x, $bed->y);
301
302 $pl->killer ("left");
303 $ob->check_score;
304
305 $ob->reply (undef, "In the future, you will wake up here when you die.");
306
307 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub {
308 if ($_[0] !~ /^[yY]/) {
309 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
310 $pl->deactivate;
311 $pl->ns->destroy;
312 } else {
313 cf::async { $pl->save };
314 }
315 });
316 },
317 );
318
319 cf::player->attach (
320 on_login => sub {
321 my ($pl) = @_;
322 my $name = $pl->ob->name;
323
324 $_->ob->message ("$name has entered the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
325 },
326 on_logout => sub {
327 my ($pl, $cleanly) = @_;
328 my $name = $pl->ob->name;
329
330 if ($cleanly) {
331 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
332 } else {
333 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
334 delete $pl->{clean_save};
335 }
336 },
337 );
338
339 cf::client->attach (
340 on_addme => \&addme,
341 );
342
343 #############################################################################
344
345 our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs
346 our $SAVE_TIMEOUT = 200; # save players every n seconds
347 our $SAVE_INTERVAL = 0.1; # save at max. one player every $SAVE_INTERVAL
348
349 our $SCHEDULER = cf::async_ext {
350 while () {
351 Coro::Timer::sleep $SCHEDULE_INTERVAL;
352
353 # this weird form of iteration over values is used because
354 # the hash changes underneath us frequently, and for
355 # keeps a direct reference to the value without (in 5.8 perls)
356 # keeping a reference, so this is prone to crashes or worse.
357 my @players = keys %cf::PLAYER;
358 for (@players) {
359 my $pl = $cf::PLAYER{$_}
360 or next;
361 $pl->valid or next;
362
363 eval {
364 if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
365 $pl->save;
366 Coro::Timer::sleep $SAVE_INTERVAL;
367 }
368
369 unless ($pl->active) {
370 # check refcounts, this is tricky and needs to be adjusted to fit server internals
371 my $ob = $pl->ob;
372 Scalar::Util::weaken $pl;
373 Scalar::Util::weaken $ob;
374 my $a_ = $pl->refcnt;
375 my $b_ = $ob->refcnt;
376 my $pl_ref = $pl->refcnt_cnt;
377 my $ob_ref = $ob->refcnt_cnt;
378
379 if ($pl_ref == 2 && $ob_ref == 1) {
380 warn "player-scheduler destroy ", $ob->name;#d#
381 delete $cf::PLAYER{$ob->name};
382 # pl_ref == one from object + one from cf::PLAYER
383 # ob_ref == one from simply being an object
384 $ob->destroy;
385 $pl->destroy;
386 } else {
387 warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d#
388 }
389 }
390 };
391 warn $@ if $@;
392 Coro::cede;
393 };
394 }
395 };
396
397 $SCHEDULER->prio (1);
398