ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/login.ext
Revision: 1.11
Committed: Sun Jan 7 02:39:14 2007 UTC (17 years, 4 months ago) by root
Branch: MAIN
Changes since 1.10: +66 -18 lines
Log Message:
""

File Contents

# User Rev Content
1 root 1.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 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     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 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     unless (delete $pl->{clean_save}) {
61     #d#TODO
62     }
63     }
64    
65 root 1.1 # 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 root 1.8 sub addme {
77 root 1.1 my ($ns) = @_;
78    
79     $ns->destroy if $ns->pl;
80    
81 root 1.10 $ns->async (sub {
82 root 1.1 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 root 1.3
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 root 1.1 }
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 root 1.3 # 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 root 1.1 # try to read the user file and check the password
150 root 1.11 if (my $fh = aio_open cf::player::path $user, O_RDONLY, 0) {
151 root 1.1 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 root 1.9 nuke_str $pass;
159 root 1.1 # password matches, wonderful
160 root 1.11 my $pl = cf::player::find $user or next;
161 root 1.1 $pl->connect ($ns);
162 root 1.11 check_clean_save $pl;
163 root 1.1 last;
164 root 1.3 } elsif (can_cleanup $buf, $mtime) {
165 root 1.1 Coro::Timer::sleep 1;
166    
167     $ns->send_drawinfo (
168 root 1.3 "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 root 1.1 . "because it has not been in-use for some time.",
171     cf::NDI_RED
172     );
173    
174 root 1.9 #TODO: nuke_str
175 root 1.1 (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 root 1.11 aio_stat cf::player::path $user and next;
180 root 1.1 $mtime == (stat _)[9] or next;
181    
182     nuke_playerdir $user;
183    
184     # fall through to creation
185     } else {
186 root 1.9 nuke_str $pass;
187    
188 root 1.1 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 root 1.3 # just to make sure nothing is left over
202 root 1.1 nuke_playerdir $user;
203    
204 root 1.3 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again.";
205    
206     if ($pass2 ne $pass) {
207 root 1.9 nuke_str $pass;
208     nuke_str $pass2;
209 root 1.3 $ns->send_drawinfo (
210     "The passwords do not match, please try again.",
211     cf::NDI_RED
212     );
213     next;
214     }
215    
216 root 1.9 nuke_str $pass2;
217    
218 root 1.11 my $pl = cf::player::new $user;
219 root 1.1 $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
220 root 1.9 nuke_str $pass;
221 root 1.1 $pl->connect ($ns);
222 root 1.3
223 root 1.1 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 root 1.11 delete $pl->{deny_save};#d# too early
254 root 1.1
255     last;
256     }
257     });
258     }
259    
260 root 1.11 # "Quitting will delete your character PERMANENTLY. If you are sure you want to do this, then use the quit_character command instead of quit."
261     # "Quitting will delete your character.\nAre you sure you want to quit (y/n):"
262     # quit, quit_character, save
263    
264 root 1.1 cf::object->attach (
265     type => cf::SAVEBED,
266     on_apply => sub {
267     my ($bed, $ob) = @_;
268    
269     return cf::override 0 unless $ob->type == cf::PLAYER;
270    
271 root 1.11 my $pl = $ob->pl;
272    
273 root 1.1 # update respawn position
274 root 1.11 $pl->savebed ($bed->map->path, $bed->x, $bed->y);
275 root 1.1
276 root 1.11 $pl->killer ("left");
277 root 1.5 $ob->check_score;
278 root 1.1
279     $ob->reply (undef, "In the future, you will wake up here when you die.");
280    
281 root 1.11 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub {
282 root 1.6 if ($_[0] !~ /^[yY]/) {
283 root 1.11 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
284     $pl->deactivate;
285     $pl->ns->destroy;
286 root 1.7 } else {
287 root 1.11 cf::async {
288     $pl->{clean_save} = 1;
289     $pl->save;
290     };
291 root 1.6 }
292 root 1.1 });
293     },
294     );
295    
296 root 1.8 cf::player->attach (
297     on_login => sub {
298     my ($pl) = @_;
299     my $name = $pl->ob->name;
300    
301     $_->ob->message ("$name has entered the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
302     },
303     on_logout => sub {
304     my ($pl, $cleanly) = @_;
305     my $name = $pl->ob->name;
306    
307     if ($cleanly) {
308     $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
309     } else {
310     $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
311     }
312     },
313     );
314    
315     cf::client->attach (
316     on_addme => \&addme,
317     );
318 root 1.1
319 root 1.11 #############################################################################
320    
321     our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs
322     our $SWAP_TIMEOUT = 30; # time after which an unused player is evicted form memory
323     our $SAVE_TIMEOUT = 20; # save players every n seconds
324     our $SAVE_INTERVAL = 0.1; # save at max. one player every $SAVE_INTERVAL
325    
326     our $SCHEDULER = cf::async_ext {
327     while () {
328     Coro::Timer::sleep $SCHEDULE_INTERVAL;
329    
330     # this weird form of iteration over values is used because
331     # the hash changes underneath us frequently, and for
332     # keeps a direct reference to the value without (in 5.8 perls)
333     # keeping a reference, so this is prone to crashes or worse.
334     my @players = keys %cf::PLAYER;
335     for (@players) {
336     my $pl = $cf::PLAYER{$_}
337     or next;
338     $pl->valid or next;
339    
340     eval {
341     if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
342     $pl->save;
343     Coro::Timer::sleep $SAVE_INTERVAL;
344     }
345     };
346     warn $@ if $@;
347     Coro::cede;
348     };
349     }
350     };
351    
352     $SCHEDULER->prio (1);
353