ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/login.ext
Revision: 1.12
Committed: Sun Jan 7 21:54:59 2007 UTC (17 years, 4 months ago) by root
Branch: MAIN
Changes since 1.11: +28 -3 lines
Log Message:
implement quit

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 last;
164 } elsif (can_cleanup $buf, $mtime) {
165 Coro::Timer::sleep 1;
166
167 $ns->send_drawinfo (
168 "Player exists, but password does not match. If this is your account, "
169 . "please try again. If not, you can now decide to take over this account "
170 . "because it has not been in-use for some time.",
171 cf::NDI_RED
172 );
173
174 #TODO: nuke_str
175 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/
176 or next;
177
178 # check if the file hasn't changed
179 aio_stat cf::player::path $user and next;
180 $mtime == (stat _)[9] or next;
181
182 nuke_playerdir $user;
183
184 # fall through to creation
185 } else {
186 nuke_str $pass;
187
188 Coro::Timer::sleep 1;
189
190 $ns->send_drawinfo (
191 "Wrong username or password. Please try again "
192 . "(check for Numlock and other semi-obvious error sources).",
193 cf::NDI_RED
194 );
195 next;
196 }
197 }
198
199 # the rest of this function is character creation
200
201 # just to make sure nothing is left over
202 nuke_playerdir $user;
203
204 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again.";
205
206 if ($pass2 ne $pass) {
207 nuke_str $pass;
208 nuke_str $pass2;
209 $ns->send_drawinfo (
210 "The passwords do not match, please try again.",
211 cf::NDI_RED
212 );
213 next;
214 }
215
216 nuke_str $pass2;
217
218 my $pl = cf::player::new $user;
219 $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
220 nuke_str $pass;
221 $pl->connect ($ns);
222
223 my $ob = $pl->ob;
224
225 while () {
226 $ob->update_stats;
227 $pl->save_stats;
228
229 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
230 "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?";
231
232 if ($res =~ /^[Nn]/) {
233 last;
234 } elsif ($res > 0 && $res <= 7) {
235 my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?";
236
237 if ($swap > 0 && $swap <= 7) {
238 $ob->swap_stats ($res - 1, $swap - 1);
239 }
240 } else {
241 $ob->roll_stats;
242 }
243 }
244
245 $ob->set_animation (2);
246 $ob->add_statbonus;
247
248 $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE);
249 $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR,
250 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n");
251
252 $ns->state (cf::ST_CHANGE_CLASS);
253 delete $pl->{deny_save};#d# too early
254
255 last;
256 }
257 });
258 }
259
260 cf::register_command quit => sub {
261 my ($ob, $arg) = @_;
262
263 $ob->reply (undef,
264 "Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. "
265 . "If you are sure you want to do this, then use the quit_character command instead of quit.",
266 cf::NDI_UNIQUE | cf::NDI_RED);
267 };
268
269 cf::register_command quit_character => sub {
270 my ($ob, $arg) = @_;
271
272 my $pl = $ob->contr;
273
274 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub {
275 if ($_[0] !~ /^[yY]/) {
276 $ob->reply (undef,
277 "Ok, not not quitting then.",
278 cf::NDI_UNIQUE | cf::NDI_RED);
279 } else {
280 $ob->reply (undef,
281 "Ok, quitting, hope to see you again.",
282 cf::NDI_UNIQUE | cf::NDI_RED);
283 $pl->ns->flush;
284 $pl->quit_character;
285 }
286 });
287 };
288
289 cf::object->attach (
290 type => cf::SAVEBED,
291 on_apply => sub {
292 my ($bed, $ob) = @_;
293
294 return cf::override 0 unless $ob->type == cf::PLAYER;
295
296 my $pl = $ob->pl;
297
298 # update respawn position
299 $pl->savebed ($bed->map->path, $bed->x, $bed->y);
300
301 $pl->killer ("left");
302 $ob->check_score;
303
304 $ob->reply (undef, "In the future, you will wake up here when you die.");
305
306 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub {
307 if ($_[0] !~ /^[yY]/) {
308 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
309 $pl->deactivate;
310 $pl->ns->destroy;
311 } else {
312 cf::async {
313 $pl->{clean_save} = 1;
314 $pl->save;
315 };
316 }
317 });
318 },
319 );
320
321 cf::player->attach (
322 on_login => sub {
323 my ($pl) = @_;
324 my $name = $pl->ob->name;
325
326 $_->ob->message ("$name has entered the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
327 },
328 on_logout => sub {
329 my ($pl, $cleanly) = @_;
330 my $name = $pl->ob->name;
331
332 if ($cleanly) {
333 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
334 } else {
335 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
336 }
337 },
338 );
339
340 cf::client->attach (
341 on_addme => \&addme,
342 );
343
344 #############################################################################
345
346 our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs
347 our $SWAP_TIMEOUT = 30; # time after which an unused player is evicted form memory
348 our $SAVE_TIMEOUT = 20; # save players every n seconds
349 our $SAVE_INTERVAL = 0.1; # save at max. one player every $SAVE_INTERVAL
350
351 our $SCHEDULER = cf::async_ext {
352 while () {
353 Coro::Timer::sleep $SCHEDULE_INTERVAL;
354
355 # this weird form of iteration over values is used because
356 # the hash changes underneath us frequently, and for
357 # keeps a direct reference to the value without (in 5.8 perls)
358 # keeping a reference, so this is prone to crashes or worse.
359 my @players = keys %cf::PLAYER;
360 for (@players) {
361 my $pl = $cf::PLAYER{$_}
362 or next;
363 $pl->valid or next;
364
365 eval {
366 if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
367 $pl->save;
368 Coro::Timer::sleep $SAVE_INTERVAL;
369 }
370 };
371 warn $@ if $@;
372 Coro::cede;
373 };
374 }
375 };
376
377 $SCHEDULER->prio (1);
378