ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/login.ext
Revision: 1.30
Committed: Sun Feb 11 17:47:22 2007 UTC (17 years, 3 months ago) by root
Branch: MAIN
Changes since 1.29: +1 -1 lines
Log Message:
- passive knowledge of a feature does not mean you can't actively
  use it wrongly for years: do use \Z instead of $ for security-relevant stuff
  (\n was allowed in a login name).

File Contents

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