Debian Codegraph @

server/login.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * Player login/logout/save functions.
17 */
18
19 #include "global.h"
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24
25 #include "define.h"
26 #include "loader.h"
27 #include "output_file.h"
28 #include "spells.h"
29 #include "sproto.h"
30 #include "server.h"
31
32 static void copy_file(const char *filename, FILE *fpout);
33
34 /**
35 * Save all players.
36 *
37 * @param flag
38 * if non zero, it means that we want to try and save everyone, but
39 * keep the game running. Thus, we don't want to free any information.
40 */
41 void emergency_save(int flag) {
42 #ifndef NO_EMERGENCY_SAVE
43 player *pl;
44
45 trying_emergency_save = 1;
46 LOG(llevError, "Emergency save: ");
47 for (pl = first_player; pl != NULL; pl = pl->next) {
48 if (!pl->ob) {
49 LOG(llevError, "No name, ignoring this.\n");
50 continue;
51 }
52 LOG(llevError, "%s ", pl->ob->name);
53 draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOADSAVE,
54 "Emergency save...");
55
56 /* If we are not exiting the game (ie, this is sort of a backup save), then
57 * don't change the location back to the village. Note that there are other
58 * options to have backup saves be done at the starting village
59 */
60 if (!flag) {
61 strcpy(pl->maplevel, first_map_path);
62 if (pl->ob->map != NULL)
63 pl->ob->map = NULL;
64 pl->ob->x = -1;
65 pl->ob->y = -1;
66 }
67 if (!save_player(pl->ob, flag)) {
68 LOG(llevError, "(failed) ");
69 draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOADSAVE,
70 "Emergency save failed, checking score...");
71 }
72 hiscore_check(pl->ob, 1);
73 }
74 LOG(llevError, "\n");
75 #else
76 LOG(llevInfo, "Emergency saves disabled, no save attempted\n");
77 #endif
78 }
79
80 /**
81 * Totally deletes a character. The contents of its directory are effectively totally removed.
82 * Used when a player 'quits' the game, or dies on a server with permadeath and no resurrect.
83 *
84 * @param name
85 * player to delete.
86 */
87 void delete_character(const char *name) {
88 char buf[MAX_BUF];
89
90 snprintf(buf, sizeof(buf), "%s/%s/%s", settings.localdir, settings.playerdir, name);
91 /* this effectively does an rm -rf on the directory */
92 remove_directory(buf);
93 }
94
95 /**
96 * This verify that a character of name exits, and that it matches
97 * password.
98 *
99 * @param name
100 * player name.
101 * @param password
102 * player's password, not encrypted.
103 * @retval 0
104 * there is match.
105 * @retval 1
106 * no such player.
107 * @retval 2
108 * incorrect password.
109 */
110 int verify_player(const char *name, char *password) {
111 char buf[MAX_BUF];
112 FILE *fp;
113
114 if (strpbrk(name, "/.\\") != NULL) {
115 LOG(llevError, "Username contains illegal characters: %s\n", name);
116 return 1;
117 }
118
119 snprintf(buf, sizeof(buf), "%s/%s/%s/%s.pl", settings.localdir, settings.playerdir, name, name);
120 if (strlen(buf) >= sizeof(buf)-1) {
121 LOG(llevError, "Username too long: %s\n", name);
122 return 1;
123 }
124
125 fp = fopen(buf, "r");
126 if (fp == NULL)
127 return 1;
128
129 /* Read in the file until we find the password line. Our logic could
130 * be a bit better on cleaning up the password from the file, but since
131 * it is written by the program, I think it is fair to assume that the
132 * syntax should be pretty standard.
133 */
134 while (fgets(buf, MAX_BUF-1, fp) != NULL) {
135 if (!strncmp(buf, "password ", 9)) {
136 buf[strlen(buf)-1] = 0; /* remove newline */
137 if (check_password(password, buf+9)) {
138 fclose(fp);
139 return 0;
140 }
141
142 fclose(fp);
143 return 2;
144 }
145 }
146 LOG(llevDebug, "Could not find a password line in player %s\n", name);
147 fclose(fp);
148 return 1;
149 }
150
151 /**
152 * Ensure player's name is valid.
153 *
154 * @param me
155 * player to report to.
156 * @param name
157 * name to check.
158 * @retval 0
159 * invalid name.
160 * @retval 1
161 * valid name.
162 */
163 int check_name(player *me, const char *name) {
164 if (*name == '\0') {
165 draw_ext_info(NDI_UNIQUE, 0, me->ob, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOGIN,
166 "Your username cannot be blank.");
167 return 0;
168 }
169
170 if (!playername_ok(name)) {
171 draw_ext_info(NDI_UNIQUE, 0, me->ob, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOGIN,
172 "That name contains illegal characters. Use letters, hyphens and underscores only. Hyphens and underscores are not allowed as the first character.");
173 return 0;
174 }
175 if (strlen(name) >= MAX_NAME) {
176 draw_ext_info_format(NDI_UNIQUE, 0, me->ob, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOGIN,
177 "That name is too long. (Max length: %d characters)", MAX_NAME);
178 return 0;
179 }
180
181 return 1;
182 }
183
184 /**
185 * Recursively object_free_drop_inventory() op and its inventory.
186 *
187 * @param op
188 * object to totally object_free_drop_inventory().
189 * @todo trash that function
190 */
191 void destroy_object(object *op) {
192 while (op->inv != NULL)
193 destroy_object(op->inv);
194
195 if (!QUERY_FLAG(op, FLAG_REMOVED))
196 object_remove(op);
197 object_free_drop_inventory(op);
198 }
199
200 /**
201 * Saves a player to disk.
202 *
203 * @param op
204 * player to save.
205 * @param flag
206 * if is set, it's only backup, ie dont remove objects from inventory.
207 * If BACKUP_SAVE_AT_HOME is set, and the flag is set, then the player
208 * will be saved at the emergency save location.
209 * @return
210 * non zero if successful.
211 */
212 int save_player(object *op, int flag) {
213 FILE *fp;
214 OutputFile of;
215 char filename[MAX_BUF], *tmpfilename;
216 object *container = NULL;
217 player *pl = op->contr;
218 int i, wiz = QUERY_FLAG(op, FLAG_WIZ);
219 long checksum;
220 #ifdef BACKUP_SAVE_AT_HOME
221 int16_t backup_x, backup_y;
222 #endif
223
224 if (!op->stats.exp)
225 return 0; /* no experience, no save */
226
227 flag &= 1;
228
229 if (!pl->name_changed || (!flag && !op->stats.exp)) {
230 if (!flag) {
231 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOADSAVE,
232 "Your game is not valid, game not saved.");
233 }
234 return 0;
235 }
236
237 /* Sanity check - some stuff changes this when player is exiting */
238 if (op->type != PLAYER)
239 return 0;
240
241 /* Prevent accidental saves if connection is reset after player has
242 * mostly exited.
243 */
244 if (pl->state != ST_PLAYING && pl->state != ST_GET_PARTY_PASSWORD)
245 return 0;
246
247 if (flag == 0)
248 pets_terminate_all(op);
249
250 /* Update information on this character. Only do it if it is eligible for
251 * for saving.
252 */
253 pl->socket.account_chars = account_char_add(pl->socket.account_chars, pl);
254 if (pl->socket.account_name) {
255 account_char_save(pl->socket.account_name, pl->socket.account_chars);
256 /* Add this character to the account. This really only comes up
257 * for new characters, at which time we want to wait until save -
258 * otherwise there is a good chance that character will be
259 * terminated.
260 */
261 if (!account_get_account_for_char(pl->ob->name))
262 account_link(pl->socket.account_name, pl->ob->name);
263 }
264
265
266 snprintf(filename, sizeof(filename), "%s/%s/%s/%s.pl", settings.localdir, settings.playerdir, op->name, op->name);
267 make_path_to_file(filename);
268 fp = tempnam_secure(settings.tmpdir, NULL, &tmpfilename);
269 if (!fp) {
270 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOADSAVE,
271 "Can't get secure temporary file for save.");
272 LOG(llevDebug, "Can't get secure temporary file for save.\n");
273 return 0;
274 }
275
276 /* Eneq(@csd.uu.se): If we have an open container hide it. */
277 if (op->container) {
278 container = op->container;
279 op->container = NULL;
280 }
281
282 fprintf(fp, "password %s\n", pl->password);
283 if (settings.set_title == TRUE)
284 if (player_has_own_title(pl))
285 fprintf(fp, "title %s\n", player_get_own_title(pl));
286
287 fprintf(fp, "gen_hp %d\n", pl->gen_hp);
288 fprintf(fp, "gen_sp %d\n", pl->gen_sp);
289 fprintf(fp, "gen_grace %d\n", pl->gen_grace);
290 fprintf(fp, "listening %d\n", pl->listening);
291 fprintf(fp, "shoottype %d\n", pl->shoottype);
292 fprintf(fp, "bowtype %d\n", pl->bowtype);
293 fprintf(fp, "petmode %d\n", pl->petmode);
294 fprintf(fp, "peaceful %d\n", pl->peaceful);
295 fprintf(fp, "no_shout %d\n", pl->no_shout);
296 fprintf(fp, "digestion %d\n", pl->digestion);
297 fprintf(fp, "pickup %u\n", pl->mode);
298 /*
299 * outputs_sync and outputs_count are now unused in favor of the facility
300 * being supported on the client instead of in the server, but for now,
301 * set sane values in case an older server is run on a new player file.
302 * Once the server is officially 2.x, this should likely be removed.
303 */
304 fprintf(fp, "outputs_sync %d\n", 16);
305 fprintf(fp, "outputs_count %d\n", 1);
306 /* Match the enumerations but in string form */
307 fprintf(fp, "usekeys %s\n", pl->usekeys == key_inventory ? "key_inventory" : (pl->usekeys == keyrings ? "keyrings" : "containers"));
308 /* Match the enumerations but in string form */
309 fprintf(fp, "unapply %s\n", pl->unapply == unapply_nochoice ? "unapply_nochoice" : (pl->unapply == unapply_never ? "unapply_never" : "unapply_always"));
310 if (pl->unarmed_skill) fprintf(fp, "unarmed_skill %s\n", pl->unarmed_skill);
311
312 #ifdef BACKUP_SAVE_AT_HOME
313 if (op->map != NULL && flag == 0)
314 #else
315 if (op->map != NULL)
316 #endif
317 fprintf(fp, "map %s\n", op->map->path);
318 else
319 fprintf(fp, "map %s\n", settings.emergency_mapname);
320
321 fprintf(fp, "savebed_map %s\n", pl->savebed_map);
322 fprintf(fp, "bed_x %d\nbed_y %d\n", pl->bed_x, pl->bed_y);
323 fprintf(fp, "Str %d\n", pl->orig_stats.Str);
324 fprintf(fp, "Dex %d\n", pl->orig_stats.Dex);
325 fprintf(fp, "Con %d\n", pl->orig_stats.Con);
326 fprintf(fp, "Int %d\n", pl->orig_stats.Int);
327 fprintf(fp, "Pow %d\n", pl->orig_stats.Pow);
328 fprintf(fp, "Wis %d\n", pl->orig_stats.Wis);
329 fprintf(fp, "Cha %d\n", pl->orig_stats.Cha);
330
331 fprintf(fp, "lev_array %d\n", MIN(op->level, 10));
332 for (i = 1; i <= MIN(op->level, 10) && i <= 10; i++) {
333 fprintf(fp, "%d\n", pl->levhp[i]);
334 fprintf(fp, "%d\n", pl->levsp[i]);
335 fprintf(fp, "%d\n", pl->levgrace[i]);
336 }
337 fprintf(fp, "party_rejoin_mode %d\n", pl->rejoin_party);
338 if (pl->party != NULL) {
339 fprintf(fp, "party_rejoin_name %s\n", pl->party->partyname);
340 fprintf(fp, "party_rejoin_password %s\n", party_get_password(pl->party));
341 }
342 fprintf(fp, "language %s\n", i18n_get_language_code(pl->language));
343 fprintf(fp, "ticks_played %u\n", pl->ticks_played);
344 fprintf(fp, "endplst\n");
345
346 SET_FLAG(op, FLAG_NO_FIX_PLAYER);
347 CLEAR_FLAG(op, FLAG_WIZ);
348 #ifdef BACKUP_SAVE_AT_HOME
349 if (flag) {
350 backup_x = op->x;
351 backup_y = op->y;
352 op->x = -1;
353 op->y = -1;
354 }
355 /* Save objects, but not unpaid objects. Don't remove objects from
356 * inventory.
357 */
358 i = save_object(fp, op, SAVE_FLAG_NO_REMOVE);
359 if (flag) {
360 op->x = backup_x;
361 op->y = backup_y;
362 }
363 #else
364 i = save_object(fp, op, SAVE_FLAG_SAVE_UNPAID|SAVE_FLAG_NO_REMOVE); /* don't check and don't remove */
365 #endif
366
367 if (wiz)
368 SET_FLAG(op, FLAG_WIZ);
369
370 if (fclose(fp) != 0 || i != SAVE_ERROR_OK) { /* make sure the write succeeded */
371 draw_ext_info(NDI_UNIQUE|NDI_RED, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOADSAVE,
372 "Can't save character!");
373 draw_ext_info_format(NDI_ALL_DMS|NDI_RED, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOADSAVE, "Save failure for player %s!", op->name);
374 unlink(tmpfilename);
375 free(tmpfilename);
376 return 0;
377 }
378
379 CLEAR_FLAG(op, FLAG_NO_FIX_PLAYER);
380
381 if (!flag) {
382 // Clear last_skill_ob before removing inventory. This prevents us
383 // from accessing removed skill objects during cleanup.
384 for (int i = 0; i < MAX_SKILLS; i++) {
385 op->contr->last_skill_ob[i] = NULL;
386 }
387
388 while (op->inv != NULL)
389 destroy_object(op->inv);
390
391 /* destroying objects will most likely destroy the pointer
392 * in op->contr->ranges[], so clear the range to a safe value.
393 */
394 op->contr->shoottype = range_none;
395 }
396
397 checksum = 0;
398 fp = of_open(&of, filename);
399 if (fp == NULL) {
400 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOADSAVE,
401 "Can't open file for save.");
402 unlink(tmpfilename);
403 free(tmpfilename);
404 return 0;
405 }
406 fprintf(fp, "checksum %lx\n", checksum);
407 copy_file(tmpfilename, fp);
408 unlink(tmpfilename);
409 free(tmpfilename);
410 if (!of_close(&of)) {
411 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOADSAVE,
412 "Can't close file for save.");
413 return 0;
414 }
415
416 /* Eneq(@csd.uu.se): Reveal the container if we have one. */
417 if (flag && container != NULL)
418 op->container = container;
419
420 if (!flag)
421 esrv_send_inventory(op, op);
422
423 if (chmod(filename, SAVE_MODE) != 0) {
424 LOG(llevError, "Could not set permissions on '%s'\n", filename);
425 }
426
427 /* if this is the first player save, quest or knowledge states can be unsaved */
428 if (!op->contr->has_directory) {
429 op->contr->has_directory = 1;
430 knowledge_first_player_save(op->contr);
431 quest_first_player_save(op->contr);
432 }
433
434 return 1;
435 }
436
437 /**
438 * Copy a file.
439 *
440 * @param filename
441 * source file.
442 * @param fpout
443 * where to copy to.
444 */
445 static void copy_file(const char *filename, FILE *fpout) {
446 FILE *fp;
447 char buf[MAX_BUF];
448
449 fp = fopen(filename, "r");
450 if (fp == NULL) {
451 LOG(llevError, "copy_file failed to open \"%s\", player file(s) may be corrupt.\n", filename);
452 return;
453 }
454 while (fgets(buf, MAX_BUF, fp) != NULL)
455 fputs(buf, fpout);
456 fclose(fp);
457 }
458
459 /**
460 * Simple function to print errors when password is
461 * not correct, and reinitialise the name.
462 *
463 * @param op
464 * player.
465 */
466 static void wrong_password(object *op) {
467 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOGIN,
468 "\nA character with this name already exists. "
469 "Please choose another name, or make sure you entered your "
470 "password correctly.\n");
471
472 FREE_AND_COPY(op->name, "noname");
473 FREE_AND_COPY(op->name_pl, "noname");
474
475 op->contr->socket.password_fails++;
476 if (op->contr->socket.password_fails >= MAX_PASSWORD_FAILURES) {
477 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOGIN,
478 "You gave an incorrect password too many times, "
479 "you will now be dropped from the server.");
480
481 LOG(llevInfo, "A player connecting from %s has been dropped for password failure\n",
482 op->contr->socket.host);
483
484 op->contr->socket.status = Ns_Dead; /* the socket loop should handle the rest for us */
485 } else
486 get_name(op);
487 }
488
489 /**
490 * Actually login a player, load from disk and such.
491 *
492 * @param op
493 * player.
494 * @param check_pass
495 * If true, we should do password checking. Otherwise, we just log this
496 * character in. This later setting is used for the account code,
497 * where that already does authentication.
498 *
499 * @todo describe connect/login/logout/disconnect process.
500 */
501 void check_login(object *op, int check_pass) {
502 FILE *fp;
503 char filename[MAX_BUF];
504 char buf[MAX_BUF], bufall[MAX_BUF];
505 int i, value;
506 uint32_t uvalue;
507 player *pl = op->contr, *pltmp;
508 int correct = 0;
509 time_t elapsed_save_time = 0;
510 struct stat statbuf;
511 char *party_name = NULL, party_password[9];
512
513 strcpy(pl->maplevel, first_map_path);
514 party_password[0] = 0;
515
516 /* Check if this matches a connected player, and if yes disconnect old / connect new. */
517 for (pltmp = first_player; pltmp != NULL; pltmp = pltmp->next) {
518 if (pltmp != pl && pltmp->ob->name != NULL && !strcmp(pltmp->ob->name, op->name)) {
519 if (!check_pass || check_password(pl->write_buf+1, pltmp->password)) {
520 /* We could try and be more clever and re-assign the existing
521 * object to the new player, etc. However, I'm concerned that
522 * there may be a lot of other state that still needs to be sent
523 * in that case (we can't make any assumptions on what the
524 * client knows, as maybe the client crashed), so treating it
525 * as just a normal login is the safest and easiest thing to do.
526 */
527
528 pltmp->socket.status = Ns_Dead;
529
530 save_player(pltmp->ob, 0);
531 leave(pltmp, 1);
532 final_free_player(pltmp);
533 break;
534 }
535 if (check_pass) {
536 wrong_password(op);
537 return;
538 }
539 }
540 }
541
542 snprintf(filename, sizeof(filename), "%s/%s/%s/%s.pl", settings.localdir, settings.playerdir, op->name, op->name);
543
544 /* If no file, must be a new player, so lets get confirmation of
545 * the password. Return control to the higher level dispatch,
546 * since the rest of this just deals with loading of the file.
547 */
548 fp = fopen(filename, "r");
549 if (fp == NULL) {
550 confirm_password(op);
551 return;
552 }
553 if (fstat(fileno(fp), &statbuf)) {
554 LOG(llevError, "Unable to stat %s?\n", filename);
555 elapsed_save_time = 0;
556 } else {
557 elapsed_save_time = time(NULL)-statbuf.st_mtime;
558 if (elapsed_save_time < 0) {
559 LOG(llevError, "Player file %s was saved in the future? (%ld time)\n", filename, (long)elapsed_save_time);
560 elapsed_save_time = 0;
561 }
562 }
563
564 if (fgets(bufall, MAX_BUF, fp) != NULL) {
565 if (!strncmp(bufall, "checksum ", 9)) {
566 if ( fgets(bufall, MAX_BUF, fp) == NULL ) {
567 bufall[0]=0; /* should never happen */
568 }
569 }
570 if (sscanf(bufall, "password %s\n", buf)) {
571 /* New password scheme: */
572 correct = check_password(pl->write_buf+1, buf);
573 if (!check_pass) {
574 /* We want to preserve the password. Normally,
575 * pl->password is filled in when user enters
576 * data in the password prompt, but with new login,
577 * there is no password prompt.
578 */
579 strncpy(pl->password, buf, 15);
580 pl->password[15] = 0;
581 }
582 }
583 /* Old password mode removed - I have no idea what it
584 * was, and the current password mechanism has been used
585 * for at least several years.
586 */
587 }
588 if (!correct && check_pass) {
589 wrong_password(op);
590 fclose(fp);
591 return;
592 }
593
594 #ifdef SAVE_INTERVAL
595 pl->last_save_time = time(NULL);
596 #endif /* SAVE_INTERVAL */
597 pl->party = NULL;
598 if (settings.search_items == TRUE)
599 pl->search_str[0] = '\0';
600 pl->name_changed = 1;
601 pl->orig_stats.Str = 0;
602 pl->orig_stats.Dex = 0;
603 pl->orig_stats.Con = 0;
604 pl->orig_stats.Int = 0;
605 pl->orig_stats.Pow = 0;
606 pl->orig_stats.Wis = 0;
607 pl->orig_stats.Cha = 0;
608 strcpy(pl->savebed_map, first_map_path);
609 pl->bed_x = 0,
610 pl->bed_y = 0;
611 pl->spellparam[0] = '\0';
612
613 /* Loop through the file, loading the rest of the values */
614 while (fgets(bufall, MAX_BUF, fp) != NULL) {
615 char *val_string, *p;
616
617 sscanf(bufall, "%s %d\n", buf, &value);
618
619 val_string = bufall + strlen(buf) +1;
620 p = strchr(val_string, '\n');
621 if (p != NULL)
622 *p = '\0';
623
624 /* uvalue is an unsigned value. Since at least a
625 * couple different things want an usigned value, cleaner
626 * to just do it once here vs everyplace it may be needed.
627 */
628
629 uvalue = strtoul(val_string, (char **)NULL, 10);
630
631 if (!strcmp(buf, "endplst"))
632 break;
633 if (!strcmp(buf, "title") && settings.set_title == TRUE)
634 player_set_own_title(pl, val_string);
635 else if (!strcmp(buf, "unarmed_skill"))
636 pl->unarmed_skill = add_string(val_string);
637 else if (!strcmp(buf, "explore"))
638 ; /* ignore: explore mode has been removed */
639 else if (!strcmp(buf, "gen_hp"))
640 pl->gen_hp = value;
641 else if (!strcmp(buf, "shoottype"))
642 pl->shoottype = (rangetype)value;
643 else if (!strcmp(buf, "bowtype"))
644 pl->bowtype = (bowtype_t)value;
645 else if (!strcmp(buf, "petmode"))
646 pl->petmode = (petmode_t)value;
647 else if (!strcmp(buf, "gen_sp"))
648 pl->gen_sp = value;
649 else if (!strcmp(buf, "gen_grace"))
650 pl->gen_grace = value;
651 else if (!strcmp(buf, "listening"))
652 pl->listening = value;
653 else if (!strcmp(buf, "peaceful"))
654 pl->peaceful = value;
655 else if (!strcmp(buf, "no_shout"))
656 pl->no_shout = value;
657 else if (!strcmp(buf, "digestion"))
658 pl->digestion = value;
659 else if (!strcmp(buf, "pickup")) {
660 pl->mode = uvalue;
661 }
662 else if (!strcmp(buf, "map"))
663 strlcpy(pl->maplevel, val_string, sizeof(pl->maplevel));
664 else if (!strcmp(buf, "savebed_map"))
665 strlcpy(pl->savebed_map, val_string, sizeof(pl->savebed_map));
666 else if (!strcmp(buf, "bed_x"))
667 pl->bed_x = value;
668 else if (!strcmp(buf, "bed_y"))
669 pl->bed_y = value;
670 else if (!strcmp(buf, "Str"))
671 pl->orig_stats.Str = value;
672 else if (!strcmp(buf, "Dex"))
673 pl->orig_stats.Dex = value;
674 else if (!strcmp(buf, "Con"))
675 pl->orig_stats.Con = value;
676 else if (!strcmp(buf, "Int"))
677 pl->orig_stats.Int = value;
678 else if (!strcmp(buf, "Pow"))
679 pl->orig_stats.Pow = value;
680 else if (!strcmp(buf, "Wis"))
681 pl->orig_stats.Wis = value;
682 else if (!strcmp(buf, "Cha"))
683 pl->orig_stats.Cha = value;
684 else if (!strcmp(buf, "usekeys")) {
685 if (!strcmp(val_string, "key_inventory"))
686 pl->usekeys = key_inventory;
687 else if (!strcmp(val_string, "keyrings"))
688 pl->usekeys = keyrings;
689 else if (!strcmp(val_string, "containers"))
690 pl->usekeys = containers;
691 else
692 LOG(llevDebug, "load_player: got unknown usekeys type: %s\n", val_string);
693 } else if (!strcmp(buf, "unapply")) {
694 if (!strcmp(val_string, "unapply_nochoice"))
695 pl->unapply = unapply_nochoice;
696 else if (!strcmp(val_string, "unapply_never"))
697 pl->unapply = unapply_never;
698 else if (!strcmp(val_string, "unapply_always"))
699 pl->unapply = unapply_always;
700 else
701 LOG(llevDebug, "load_player: got unknown unapply type: %s\n", val_string);
702 } else if (!strcmp(buf, "lev_array")) {
703 for (i = 1; i <= value; i++) {
704 int j;
705 int count=0;
706
707 count = fscanf(fp, "%d\n", &j);
708 if ( !count ) j=0; // sanity; should never happen
709 if (j < 3)
710 j = 3;
711 else if (j > 9)
712 j = 9;
713 pl->levhp[i] = j;
714 count = fscanf(fp, "%d\n", &j);
715 if ( !count ) j=0; // sanity; should never happen
716 if (j < 2)
717 j = 2;
718 else if (j > 6)
719 j = 6;
720 pl->levsp[i] = j;
721 count = fscanf(fp, "%d\n", &j);
722 if ( !count ) j=0; // sanity; should never happen
723 if (j < 1)
724 j = 1;
725 else if (j > 3)
726 j = 3;
727 pl->levgrace[i] = j;
728 }
729 } else if (!strcmp(buf, "party_rejoin_mode"))
730 pl->rejoin_party = (enum party_rejoin_mode)value;
731 else if (!strcmp(buf, "party_rejoin_name"))
732 party_name = strdup_local(val_string);
733 else if (!strcmp(buf, "party_rejoin_password")) {
734 strncpy(party_password, val_string, sizeof(party_password));
735 party_password[sizeof(party_password) - 1] = 0;
736 } else if (!strcmp(buf, "language")) {
737 pl->language = i18n_get_language_by_code(val_string);
738 }
739 else if (!strcmp(buf, "ticks_played")) {
740 pl->ticks_played = uvalue;
741 }
742 } /* End of loop loading the character file */
743
744 /* on first login via account, this player does not exist anyplace -
745 * so don't remove them.
746 */
747 if (!QUERY_FLAG(op, FLAG_REMOVED))
748 object_remove(op);
749 op->speed = 0;
750 object_update_speed(op);
751 /*FIXME dangerous call, object_reset() should be used to init freshly allocated obj struct!*/
752 object_reset(op);
753 op->contr = pl;
754 pl->ob = op;
755 /* this loads the standard objects values. */
756 load_object(fp, op, LO_NEWFILE, 0);
757 fclose(fp);
758
759 CLEAR_FLAG(op, FLAG_NO_FIX_PLAYER);
760
761 LOG(llevInfo, "login: %s from %s\n", op->name, op->contr->socket.host);
762 strncpy(pl->title, op->arch->clone.name, sizeof(pl->title)-1);
763 pl->title[sizeof(pl->title)-1] = '\0';
764
765 /* If the map where the person was last saved does not exist,
766 * restart them on their home-savebed. This is good for when
767 * maps change between versions
768 * First, we check for partial path, then check to see if the full
769 * path (for unique player maps)
770 */
771 if (has_been_loaded(pl->maplevel) == NULL
772 && check_path(pl->maplevel, 1) == -1
773 && check_path(pl->maplevel, 0) == -1) {
774 strcpy(pl->maplevel, pl->savebed_map);
775 op->x = pl->bed_x,
776 op->y = pl->bed_y;
777 /* if the map was a shop, the player can have unpaid items, remove them. */
778 remove_unpaid_objects(op, NULL, 1);
779 }
780
781 /* If player saved beyond some time ago, and the feature is
782 * enabled, put the player back on his savebed map.
783 */
784 if ((settings.reset_loc_time > 0) && (elapsed_save_time > settings.reset_loc_time)) {
785 strcpy(pl->maplevel, pl->savebed_map);
786 op->x = pl->bed_x, op->y = pl->bed_y;
787 /* if the map was a shop, the player can have unpaid items, remove them. */
788 remove_unpaid_objects(op, NULL, 1);
789 }
790
791 /* make sure he's a player--needed because of class change. */
792 op->type = PLAYER;
793 enter_player_maplevel(op);
794
795 pl->name_changed = 1;
796 player_set_state(pl, ST_PLAYING);
797 #ifdef AUTOSAVE
798 pl->last_save_tick = pticks;
799 #endif
800 op->carrying = object_sum_weight(op);
801
802 link_player_skills(op);
803
804 if (!legal_range(op, op->contr->shoottype))
805 op->contr->shoottype = range_none;
806
807 /* if it's a dragon player, set the correct title here */
808 if (is_dragon_pl(op) && op->inv != NULL) {
809 object *abil, *skin;
810
811 abil = object_find_by_type_and_arch_name(op, FORCE, "dragon_ability_force");
812 skin = object_find_by_type_and_arch_name(op, FORCE, "dragon_skin_force");
813 set_dragon_name(op, abil, skin);
814 }
815
816 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOGIN,
817 "Welcome Back!");
818 draw_ext_info_format(NDI_UNIQUE|NDI_ALL|NDI_DK_ORANGE, 5, NULL,
819 MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_PLAYER,
820 "%s has entered the game.",
821 pl->ob->name);
822 login_check_shutdown(op);
823
824 /* Lauwenmark : Here we handle the LOGIN global event */
825 execute_global_event(EVENT_LOGIN, pl, pl->socket.host);
826 op->contr->socket.update_look = 1;
827 /* If the player should be dead, call kill_player for them
828 * Only check for hp - if player lacks food, let the normal
829 * logic for that to take place. If player is permanently
830 * dead, and not using permadeath mode, the kill_player will
831 * set the play_again flag, so return.
832 */
833 if (op->stats.hp < 0) {
834 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOGIN,
835 "Your character was dead last time you played.");
836 kill_player(op, NULL);
837 if (pl->state != ST_PLAYING)
838 {
839 // Prevent memory leak from strdup-ed party_name.
840 if (party_name)
841 free(party_name);
842 return;
843 }
844 }
845
846 /* Do this after checking for death - no reason sucking up bandwidth if
847 * the data isn't needed.
848 */
849 esrv_new_player(op->contr, op->weight+op->carrying);
850 /* Need to do these after esvr_new_player, as once the client
851 * sees that, it wipes any info it has about the player.
852 */
853 esrv_add_spells(op->contr, NULL);
854
855 /* Need to call fix_object now - program modified so that it is not
856 * called during the load process (FLAG_NO_FIX_PLAYER set when
857 * saved)
858 * Moved ahead of the esrv functions, so proper weights will be
859 * sent to the client. Needs to be after esvr_add_spells, otherwise
860 * we'll try to update spells from fix_object.
861 */
862 fix_object(op);
863
864 pl->has_directory = 1;
865
866 esrv_send_inventory(op, op);
867 esrv_send_pickup(pl);
868 quest_send_initial_states(pl);
869 knowledge_send_known(pl);
870
871 CLEAR_FLAG(op, FLAG_FRIENDLY);
872
873 /* can_use_shield is a new flag. However, the can_use.. seems to largely come
874 * from the class, and not race. I don't see any way to get the class information
875 * to then update this. I don't think this will actually break anything - anyone
876 * that can use armour should be able to use a shield. What this may 'break'
877 * are features new characters get, eg, if someone starts up with a Q, they
878 * should be able to use a shield. However, old Q's won't get that advantage.
879 */
880 if (QUERY_FLAG(op, FLAG_USE_ARMOUR))
881 SET_FLAG(op, FLAG_USE_SHIELD);
882
883 /* Rejoin party if needed. */
884 if (pl->rejoin_party != party_rejoin_no && party_name != NULL) {
885 partylist *party;
886
887 party = party_find(party_name);
888 if (!party && pl->rejoin_party == party_rejoin_always) {
889 party = party_form(op, party_name);
890 if (party)
891 party_set_password(party, party_password);
892 }
893 if (party && !pl->party && party_confirm_password(party, party_password)) {
894 party_join(op, party);
895 }
896
897 if (pl->party)
898 snprintf(buf, MAX_BUF, "Rejoined party %s.", party->partyname);
899 else
900 snprintf(buf, MAX_BUF, "Couldn't rejoin party %s: %s.", party_name, party ? "invalid password." : "no such party.");
901 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
902 buf);
903 }
904 free(party_name);
905 }