ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/login.ext
Revision: 1.10
Committed: Fri Jan 5 20:04:02 2007 UTC (17 years, 4 months ago) by root
Branch: MAIN
Changes since 1.9: +1 -1 lines
Log Message:
fix the bug: on_destroy is obviously not being called on pooled coroutines, aslo use more sensible names than 'coro'

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     return unless cf::player::find $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     # delete a player directory, be non-blocking AND synchronous...
58     # (thats hard, so we crap out and fork).
59     sub nuke_playerdir {
60     my ($user) = @_;
61    
62     aio_stat "$PLAYERDIR/$user";
63     system "cd \Q$PLAYERDIR\E "
64     . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null "
65     . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)";
66     }
67    
68 root 1.8 sub addme {
69 root 1.1 my ($ns) = @_;
70    
71     $ns->destroy if $ns->pl;
72    
73 root 1.10 $ns->async (sub {
74 root 1.1 my ($user, $pass);
75    
76     $ns->send_packet ("addme_success");
77    
78     for (;;) {
79     $ns->send_drawinfo (
80     "Please enter your username now. If you are a new user, "
81     . "make one up that describes your character best. "
82     . "Only letters and digits are allowed, though.",
83     cf::NDI_BLUE
84     );
85    
86     # read username
87     while () {
88     $user = query $ns, 0, "What is your name?\n:";
89 root 1.3
90     if ($cf::LOGIN_LOCK{$user}) {
91     $ns->send_drawinfo (
92     "That username is currently used in another login session. "
93     . "Chose another, or wait till the other session has ended.",
94     cf::NDI_RED
95     );
96     } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}$/) {
97     last;
98     } else {
99     $ns->send_drawinfo (
100     "Your username contains illegal characters "
101     . "(only a-z, A-Z and 0-9 are allowed), "
102     . "or is not between 3 and 18 characters in length.",
103     cf::NDI_RED
104     );
105     }
106 root 1.1 }
107    
108     check_playing $ns, $user and next;
109    
110     $ns->send_drawinfo (
111     "Welcome $user, please enter your password now. "
112     . "New users should now choose a password. "
113     . "Anything your client lets you enter is fine.",
114     cf::NDI_BLUE
115     );
116    
117     # read password
118     while () {
119     $pass = query $ns, cf::CS_QUERY_HIDEINPUT, "What is your password?\n:";
120     last if $pass =~ /.../;
121     $ns->send_drawinfo (
122     "Try to use at least three characters as your password please, "
123     . "that cannot be too much to ask for :)",
124     cf::NDI_RED
125     );
126     }
127    
128     my $dir = "$PLAYERDIR/$user";
129     my $plfile = "$dir/$user.pl";
130    
131 root 1.3 # lock this username for the remainder of this login session
132     if ($cf::LOGIN_LOCK{$user}) {
133     $ns->send_drawinfo (
134     "That username is currently used in another login session. "
135     . "Chose another, or wait till the other session has ended.",
136     cf::NDI_RED
137     );
138     next;
139     }
140     local $cf::LOGIN_LOCK{$user} = 1;
141    
142     check_playing $ns, $user and next;
143    
144 root 1.1 # try to read the user file and check the password
145     if (my $fh = aio_open $plfile, O_RDONLY, 0) {
146     my $mtime = (stat $fh)[9];
147    
148     0 < aio_read $fh, 0, 16384, my $buf, 0 or next;
149     $buf =~ /^password (\S+)$/m or next;
150     my $hash = $1;
151    
152     if ($hash eq crypt $pass, $hash) {
153 root 1.9 nuke_str $pass;
154 root 1.1 # password matches, wonderful
155     my $pl = cf::player::load $plfile or next;
156     $pl->enable_save (1);
157     $pl->connect ($ns);
158     last;
159 root 1.3 } elsif (can_cleanup $buf, $mtime) {
160 root 1.1 Coro::Timer::sleep 1;
161    
162     $ns->send_drawinfo (
163 root 1.3 "Player exists, but password does not match. If this is your account, "
164     . "please try again. If not, you can now decide to take over this account "
165 root 1.1 . "because it has not been in-use for some time.",
166     cf::NDI_RED
167     );
168    
169 root 1.9 #TODO: nuke_str
170 root 1.1 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/
171     or next;
172    
173     # check if the file hasn't changed
174 root 1.3 aio_stat $plfile and next;
175 root 1.1 $mtime == (stat _)[9] or next;
176    
177     nuke_playerdir $user;
178    
179     # fall through to creation
180     } else {
181 root 1.9 nuke_str $pass;
182    
183 root 1.1 Coro::Timer::sleep 1;
184    
185     $ns->send_drawinfo (
186     "Wrong username or password. Please try again "
187     . "(check for Numlock and other semi-obvious error sources).",
188     cf::NDI_RED
189     );
190     next;
191     }
192     }
193    
194     # the rest of this function is character creation
195    
196 root 1.3 # just to make sure nothing is left over
197 root 1.1 nuke_playerdir $user;
198    
199 root 1.3 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again.";
200    
201     if ($pass2 ne $pass) {
202 root 1.9 nuke_str $pass;
203     nuke_str $pass2;
204 root 1.3 $ns->send_drawinfo (
205     "The passwords do not match, please try again.",
206     cf::NDI_RED
207     );
208     next;
209     }
210    
211 root 1.9 nuke_str $pass2;
212    
213 root 1.1 my $pl = cf::player::create;
214     $pl->ob->name ($user);
215     $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
216 root 1.9 nuke_str $pass;
217 root 1.1 $pl->connect ($ns);
218 root 1.3
219 root 1.1 my $ob = $pl->ob;
220    
221     while () {
222     $ob->update_stats;
223     $pl->save_stats;
224    
225     my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
226     "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?";
227    
228     if ($res =~ /^[Nn]/) {
229     last;
230     } elsif ($res > 0 && $res <= 7) {
231     my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?";
232    
233     if ($swap > 0 && $swap <= 7) {
234     $ob->swap_stats ($res - 1, $swap - 1);
235     }
236     } else {
237     $ob->roll_stats;
238     }
239     }
240    
241     $ob->set_animation (2);
242     $ob->add_statbonus;
243    
244     $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE);
245     $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR,
246     "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n");
247    
248     $ns->state (cf::ST_CHANGE_CLASS);
249 root 1.2 $pl->enable_save (1);#d# too early
250 root 1.1
251     last;
252     }
253     });
254     }
255    
256     cf::object->attach (
257     type => cf::SAVEBED,
258     on_apply => sub {
259     my ($bed, $ob) = @_;
260    
261     return cf::override 0 unless $ob->type == cf::PLAYER;
262    
263     # update respawn position
264     $ob->contr->savebed ($bed->map->path, $bed->x, $bed->y);
265    
266 root 1.4 $ob->contr->killer ("left");
267 root 1.5 $ob->check_score;
268 root 1.1
269     $ob->reply (undef, "In the future, you will wake up here when you die.");
270 root 1.6 $ob->contr->save (1);
271 root 1.1
272 root 1.2 $ob->contr->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub {
273 root 1.6 if ($_[0] !~ /^[yY]/) {
274 root 1.8 $ob->contr->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
275 root 1.6 $ob->contr->ns->destroy;
276 root 1.7 } else {
277     $ob->contr->enable_save (1);
278 root 1.6 }
279 root 1.1 });
280     },
281     );
282    
283 root 1.8 cf::player->attach (
284     on_login => sub {
285     my ($pl) = @_;
286     my $name = $pl->ob->name;
287    
288     $_->ob->message ("$name has entered the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
289     },
290     on_logout => sub {
291     my ($pl, $cleanly) = @_;
292     my $name = $pl->ob->name;
293    
294     if ($cleanly) {
295     $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
296     } else {
297     $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
298     }
299     },
300     );
301    
302     cf::client->attach (
303     on_addme => \&addme,
304     );
305 root 1.1