Freeciv-3.1
Loading...
Searching...
No Matches
stdinhand.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include "fc_prehdrs.h"
19
20#include <stdarg.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#ifdef FREECIV_HAVE_LIBREADLINE
26#include <readline/readline.h>
27#endif
28
29/* utility */
30#include "astring.h"
31#include "bitvector.h"
32#include "deprecations.h"
33#include "fc_cmdline.h"
34#include "fciconv.h"
35#include "fcintl.h"
36#include "log.h"
37#include "mem.h"
38#include "rand.h"
39#include "registry.h"
40#include "support.h" /* fc__attribute, bool type, etc. */
41#include "timing.h"
42#include "section_file.h"
43
44/* common */
45#include "capability.h"
46#include "events.h"
47#include "fc_types.h" /* LINE_BREAK */
48#include "featured_text.h"
49#include "game.h"
50#include "map.h"
51#include "mapimg.h"
52#include "packets.h"
53#include "player.h"
54#include "research.h"
55#include "rgbcolor.h"
56#include "srvdefs.h"
57#include "unitlist.h"
58#include "version.h"
59
60/* server */
61#include "aiiface.h"
62#include "citytools.h"
63#include "connecthand.h"
64#include "diplhand.h"
65#include "gamehand.h"
66#include "mapgen.h"
67#include "maphand.h"
68#include "meta.h"
69#include "notify.h"
70#include "plrhand.h"
71#include "report.h"
72#include "ruleset.h"
73#include "sanitycheck.h"
74#include "score.h"
75#include "sernet.h"
76#include "settings.h"
77#include "srv_log.h"
78#include "srv_main.h"
79#include "techtools.h"
80#include "voting.h"
81
82/* server/savegame */
83#include "savemain.h"
84
85/* server/scripting */
86#include "script_server.h"
87#include "script_fcdb.h"
88
89/* ai */
90#include "difficulty.h"
91#include "handicaps.h"
92
93#include "stdinhand.h"
94
95#define OPTION_NAME_SPACE 25
96
97static enum cmdlevel default_access_level = ALLOW_BASIC;
98static enum cmdlevel first_access_level = ALLOW_BASIC;
99
100static time_t *time_duplicate(const time_t *t);
101
102/* 'struct kick_hash' and related functions. */
103#define SPECHASH_TAG kick
104#define SPECHASH_ASTR_KEY_TYPE
105#define SPECHASH_IDATA_TYPE time_t *
106#define SPECHASH_UDATA_TYPE time_t
107#define SPECHASH_IDATA_COPY time_duplicate
108#define SPECHASH_IDATA_FREE (kick_hash_data_free_fn_t) free
109#define SPECHASH_UDATA_TO_IDATA(t) (&(t))
110#define SPECHASH_IDATA_TO_UDATA(p) (NULL != p ? *p : 0)
111#include "spechash.h"
112
113const char *script_extension = ".serv";
114
115static struct kick_hash *kick_table_by_addr = NULL;
116static struct kick_hash *kick_table_by_user = NULL;
117
118
119static bool cut_client_connection(struct connection *caller, char *name,
120 bool check);
121static bool show_help(struct connection *caller, char *arg);
122static bool show_list(struct connection *caller, char *arg);
123static void show_colors(struct connection *caller);
124static bool set_ai_level_named(struct connection *caller, const char *name,
125 const char *level_name, bool check);
126static bool set_ai_level(struct connection *caller, const char *name,
127 enum ai_level level, bool check);
128static bool away_command(struct connection *caller, bool check);
129static bool set_rulesetdir(struct connection *caller, char *str, bool check,
130 int read_recursion);
131static bool show_command(struct connection *caller, char *str, bool check);
132static bool show_settings(struct connection *caller,
133 enum command_id called_as,
134 char *str, bool check);
135static void show_settings_one(struct connection *caller, enum command_id cmd,
136 struct setting *pset);
137static void show_ruleset_info(struct connection *caller, enum command_id cmd,
138 bool check, int read_recursion);
139static void show_mapimg(struct connection *caller, enum command_id cmd);
140static bool set_command(struct connection *caller, char *str, bool check);
141
142static bool create_command(struct connection *caller, const char *str,
143 bool check);
144static bool end_command(struct connection *caller, char *str, bool check);
145static bool surrender_command(struct connection *caller, char *str, bool check);
146static bool handle_stdin_input_real(struct connection *caller, char *str,
147 bool check, int read_recursion);
148static bool read_init_script_real(struct connection *caller,
149 char *script_filename, bool from_cmdline,
150 bool check, int read_recursion);
151static bool reset_command(struct connection *caller, char *arg, bool check,
152 int read_recursion);
153static bool default_command(struct connection *caller, char *arg, bool check);
154static bool lua_command(struct connection *caller, char *arg, bool check,
155 int read_recursion);
156static bool kick_command(struct connection *caller, char *name, bool check);
157static bool delegate_command(struct connection *caller, char *arg,
158 bool check);
159static const char *delegate_player_str(struct player *pplayer, bool observer);
160static bool aicmd_command(struct connection *caller, char *arg, bool check);
161static bool fcdb_command(struct connection *caller, char *arg, bool check);
162static const char *fcdb_accessor(int i);
163static char setting_status(struct connection *caller,
164 const struct setting *pset);
165static bool player_name_check(const char *name, char *buf, size_t buflen);
166static bool playercolor_command(struct connection *caller,
167 char *str, bool check);
168static bool playernation_command(struct connection *caller,
169 char *str, bool check);
170static bool mapimg_command(struct connection *caller, char *arg, bool check);
171static const char *mapimg_accessor(int i);
172
173static void show_delegations(struct connection *caller);
174
175static const char horiz_line[] =
176"------------------------------------------------------------------------------";
177
178/**********************************************************************/
182static bool is_restricted(struct connection *caller)
183{
184 return (caller && caller->access_level != ALLOW_HACK);
185}
186
187/**********************************************************************/
191static bool player_name_check(const char *name, char *buf, size_t buflen)
192{
193 size_t len = strlen(name);
194
195 if (len == 0) {
196 fc_snprintf(buf, buflen, _("Can't use an empty name."));
197 return FALSE;
198 } else if (len > MAX_LEN_NAME-1) {
199 fc_snprintf(buf, buflen, _("That name exceeds the maximum of %d chars."),
200 MAX_LEN_NAME-1);
201 return FALSE;
202 } else if (fc_strcasecmp(name, ANON_PLAYER_NAME) == 0
203 || fc_strcasecmp(name, "Observer") == 0) {
204 fc_snprintf(buf, buflen, _("That name is not allowed."));
205 /* "Observer" used to be illegal and we keep it that way for now. */
206 /* FIXME: This disallows anonymous player name as it appears in English,
207 * but not one in any other language that the user may see. */
208 return FALSE;
209 }
210
211 return TRUE;
212}
213
214/**********************************************************************/
221static enum command_id command_named(const char *token, bool accept_ambiguity)
222{
223 enum m_pre_result result;
224 int ind;
225
227 fc_strncasecmp, NULL, token, &ind);
228
229 if (result < M_PRE_AMBIGUOUS) {
230 return ind;
231 } else if (result == M_PRE_AMBIGUOUS) {
232 return accept_ambiguity ? ind : CMD_AMBIGUOUS;
233 } else {
234 return CMD_UNRECOGNIZED;
235 }
236}
237
238/**********************************************************************/
242{
244 kick_table_by_addr = kick_hash_new();
245
247 kick_table_by_user = kick_hash_new();
248}
249
250/**********************************************************************/
255{
256 /* Nothing at the moment. */
257}
258
259/**********************************************************************/
263{
265 if (NULL != kick_table_by_addr) {
266 kick_hash_destroy(kick_table_by_addr);
267 kick_table_by_addr = NULL;
268 }
269
271 if (NULL != kick_table_by_user) {
272 kick_hash_destroy(kick_table_by_user);
273 kick_table_by_user = NULL;
274 }
275}
276
277/**********************************************************************/
281static bool may_use(struct connection *caller, enum command_id cmd)
282{
283 if (!caller) {
284 return TRUE; /* on the console, everything is allowed */
285 }
286 return (caller->access_level >= command_level(command_by_number(cmd)));
287}
288
289/**********************************************************************/
293static bool may_use_nothing(struct connection *caller)
294{
295 if (!caller) {
296 return FALSE; /* on the console, everything is allowed */
297 }
298 return (ALLOW_NONE == conn_get_access(caller));
299}
300
301/**********************************************************************/
305static char setting_status(struct connection *caller,
306 const struct setting *pset)
307{
308 /* first check for a ruleset lock as this is included in
309 * setting_is_changeable() */
310 if (setting_locked(pset)) {
311 /* setting is locked by the ruleset */
312 return '!';
313 }
314
315 if (setting_is_changeable(pset, caller, NULL, 0)) {
316 /* setting can be changed */
317 return '+';
318 }
319
320 /* setting is fixed */
321 return ' ';
322}
323
324/**********************************************************************/
331static void cmd_reply_line(enum command_id cmd, struct connection *caller,
332 enum rfc_status rfc_status, const char *prefix,
333 const char *line)
334{
335 const char *cmdname = cmd < CMD_NUM
337 : cmd == CMD_AMBIGUOUS
338 /* TRANS: ambiguous command */
339 ? _("(ambiguous)")
340 : cmd == CMD_UNRECOGNIZED
341 /* TRANS: unrecognized command */
342 ? _("(unknown)")
343 : "(?!?)"; /* this case is a bug! */
344
345 if (caller) {
346 notify_conn(caller->self, NULL, E_SETTING, ftc_command,
347 "/%s: %s%s", cmdname, prefix, line);
348 /* cc: to the console - testing has proved it's too verbose - rp
349 con_write(rfc_status, "%s/%s: %s%s", caller->name, cmdname, prefix, line);
350 */
351 } else {
352 con_write(rfc_status, "%s%s", prefix, line);
353 }
354
355 if (rfc_status == C_OK) {
356 struct packet_chat_msg packet;
357
358 package_event(&packet, NULL, E_SETTING, ftc_server, "%s", line);
360 /* Do not tell caller, since they were told above! */
361 if (caller != pconn) {
362 send_packet_chat_msg(pconn, &packet);
363 }
366
367 if (NULL != caller) {
368 /* Echo to the console. */
369 log_normal("%s", line);
370 }
371 }
372}
373
374/**********************************************************************/
378static void vcmd_reply_prefix(enum command_id cmd, struct connection *caller,
379 enum rfc_status rfc_status, const char *prefix,
380 const char *format, va_list ap)
381{
382 char buf[4096];
383 char *c0, *c1;
384
385 fc_vsnprintf(buf, sizeof(buf), format, ap);
386
387 c0 = buf;
388 while ((c1 = strstr(c0, "\n"))) {
389 *c1 = '\0';
390 cmd_reply_line(cmd, caller, rfc_status, (c0 == buf ? "" : prefix), c0);
391 c0 = c1 + 1;
392 }
393
394 cmd_reply_line(cmd, caller, rfc_status, (c0 == buf ? "" : prefix), c0);
395}
396
397/**********************************************************************/
401static void cmd_reply_prefix(enum command_id cmd, struct connection *caller,
402 enum rfc_status rfc_status, const char *prefix,
403 const char *format, ...)
404 fc__attribute((__format__ (__printf__, 5, 6)));
405static void cmd_reply_prefix(enum command_id cmd, struct connection *caller,
406 enum rfc_status rfc_status, const char *prefix,
407 const char *format, ...)
408{
409 va_list ap;
410 va_start(ap, format);
411 vcmd_reply_prefix(cmd, caller, rfc_status, prefix, format, ap);
412 va_end(ap);
413}
414
415/**********************************************************************/
418void cmd_reply(enum command_id cmd, struct connection *caller,
419 enum rfc_status rfc_status, const char *format, ...)
420{
421 va_list ap;
422 va_start(ap, format);
423 vcmd_reply_prefix(cmd, caller, rfc_status, "", format, ap);
424 va_end(ap);
425}
426
427/**********************************************************************/
432 struct connection *caller,
433 const char *name,
434 enum m_pre_result match_result)
435{
436 switch (match_result) {
437 case M_PRE_EMPTY:
438 cmd_reply(cmd, caller, C_SYNTAX,
439 _("Name is empty, so cannot be a player."));
440 break;
441 case M_PRE_LONG:
442 cmd_reply(cmd, caller, C_SYNTAX,
443 _("Name is too long, so cannot be a player."));
444 break;
445 case M_PRE_AMBIGUOUS:
446 cmd_reply(cmd, caller, C_FAIL,
447 _("Player name prefix '%s' is ambiguous."), name);
448 break;
449 case M_PRE_FAIL:
450 cmd_reply(cmd, caller, C_FAIL,
451 _("No player by the name of '%s'."), name);
452 break;
453 default:
454 cmd_reply(cmd, caller, C_FAIL,
455 _("Unexpected match_result %d (%s) for '%s'."),
456 match_result, _(m_pre_description(match_result)), name);
457 log_error("Unexpected match_result %d (%s) for '%s'.",
458 match_result, m_pre_description(match_result), name);
459 }
460}
461
462/**********************************************************************/
467 struct connection *caller,
468 const char *name,
469 enum m_pre_result match_result)
470{
471 switch (match_result) {
472 case M_PRE_EMPTY:
473 cmd_reply(cmd, caller, C_SYNTAX,
474 _("Name is empty, so cannot be a connection."));
475 break;
476 case M_PRE_LONG:
477 cmd_reply(cmd, caller, C_SYNTAX,
478 _("Name is too long, so cannot be a connection."));
479 break;
480 case M_PRE_AMBIGUOUS:
481 cmd_reply(cmd, caller, C_FAIL,
482 _("Connection name prefix '%s' is ambiguous."), name);
483 break;
484 case M_PRE_FAIL:
485 cmd_reply(cmd, caller, C_FAIL,
486 _("No connection by the name of '%s'."), name);
487 break;
488 default:
489 cmd_reply(cmd, caller, C_FAIL,
490 _("Unexpected match_result %d (%s) for '%s'."),
491 match_result, _(m_pre_description(match_result)), name);
492 log_error("Unexpected match_result %d (%s) for '%s'.",
493 match_result, m_pre_description(match_result), name);
494 }
495}
496
497/**********************************************************************/
500static void open_metaserver_connection(struct connection *caller,
501 bool persistent)
502{
505 cmd_reply(CMD_METACONN, caller, C_OK,
506 _("Open metaserver connection to [%s]."),
508 }
509}
510
511/**********************************************************************/
514static void close_metaserver_connection(struct connection *caller)
515{
518 cmd_reply(CMD_METACONN, caller, C_OK,
519 _("Close metaserver connection to [%s]."),
521 }
522}
523
524/**********************************************************************/
527static bool metaconnection_command(struct connection *caller, char *arg,
528 bool check)
529{
530 bool persistent = FALSE;
531
532 if ((*arg == '\0')
533 || (!strcmp(arg, "?"))) {
534 if (is_metaserver_open()) {
536 _("Metaserver connection is open."));
537 } else {
539 _("Metaserver connection is closed."));
540 }
541 return TRUE;
542 }
543
544 if (!fc_strcasecmp(arg, "p")
545 || !fc_strcasecmp(arg, "persistent")) {
547 }
548
549 if (persistent
550 || !fc_strcasecmp(arg, "u")
551 || !fc_strcasecmp(arg, "up")) {
552 if (!is_metaserver_open()) {
553 if (!check) {
555 }
556 } else {
558 _("Metaserver connection is already open."));
559 return FALSE;
560 }
561 } else if (!fc_strcasecmp(arg, "d")
562 || !fc_strcasecmp(arg, "down")) {
563 if (is_metaserver_open()) {
564 if (!check) {
566 }
567 } else {
569 _("Metaserver connection is already closed."));
570 return FALSE;
571 }
572 } else {
574 _("Argument must be 'u', 'up', 'd', 'down', 'p', 'persistent', or '?'."));
575 return FALSE;
576 }
577 return TRUE;
578}
579
580/**********************************************************************/
583static bool metapatches_command(struct connection *caller,
584 char *arg, bool check)
585{
586 if (check) {
587 return TRUE;
588 }
589
591
592 if (is_metaserver_open()) {
595 _("Metaserver patches string set to '%s'."), arg);
596 } else {
598 _("Metaserver patches string set to '%s', "
599 "not reporting to metaserver."), arg);
600 }
601
602 return TRUE;
603}
604
605/**********************************************************************/
608static bool metamessage_command(struct connection *caller,
609 char *arg, bool check)
610{
611 struct setting *pset;
612
613 log_deprecation(_("/metamessage command is deprecated. "
614 "Set metamessage setting instead."));
615
616 if (check) {
617 return TRUE;
618 }
619
621 if (is_metaserver_open()) {
624 _("Metaserver message string set to '%s'."), arg);
625 } else {
627 _("Metaserver message string set to '%s', "
628 "not reporting to metaserver."), arg);
629 }
630
631 /* Metamessage is also a setting. */
632 pset = setting_by_name("metamessage");
633 setting_changed(pset);
634 send_server_setting(NULL, pset);
635
636 return TRUE;
637}
638
639/**********************************************************************/
642static bool metaserver_command(struct connection *caller, char *arg,
643 bool check)
644{
645 if (check) {
646 return TRUE;
647 }
649
651
653 _("Metaserver is now [%s]."), meta_addr_port());
654 return TRUE;
655}
656
657/**********************************************************************/
660static bool show_serverid(struct connection *caller, char *arg)
661{
662 cmd_reply(CMD_SRVID, caller, C_COMMENT, _("Server id: %s"), srvarg.serverid);
663
664 return TRUE;
665}
666
667/**********************************************************************/
671static bool save_command(struct connection *caller, char *arg, bool check)
672{
673 if (is_restricted(caller)) {
674 cmd_reply(CMD_SAVE, caller, C_FAIL,
675 _("You cannot save games manually on this server."));
676 return FALSE;
677 }
678 if (!check) {
679 save_game(arg, "User request", FALSE);
680 }
681 return TRUE;
682}
683
684/**********************************************************************/
688static bool scensave_command(struct connection *caller, char *arg, bool check)
689{
690 if (is_restricted(caller)) {
691 cmd_reply(CMD_SAVE, caller, C_FAIL,
692 _("You cannot save games manually on this server."));
693 return FALSE;
694 }
695 if (!check) {
696 save_game(arg, "Scenario", TRUE);
697 }
698 return TRUE;
699}
700
701/**********************************************************************/
704void toggle_ai_player_direct(struct connection *caller, struct player *pplayer)
705{
706 fc_assert_ret(pplayer != NULL);
707
708 if (is_human(pplayer)) {
709 cmd_reply(CMD_AITOGGLE, caller, C_OK,
710 _("%s is now under AI control."),
711 player_name(pplayer));
712 player_set_to_ai_mode(pplayer,
713 !ai_level_is_valid(pplayer->ai_common.skill_level)
715 : pplayer->ai_common.skill_level);
716 fc_assert(is_ai(pplayer));
717 } else {
718 cmd_reply(CMD_AITOGGLE, caller, C_OK,
719 _("%s is now under human control."),
720 player_name(pplayer));
722 fc_assert(is_human(pplayer));
723 }
724}
725
726/**********************************************************************/
729static bool toggle_ai_command(struct connection *caller, char *arg, bool check)
730{
731 enum m_pre_result match_result;
732 struct player *pplayer;
733
734 pplayer = player_by_name_prefix(arg, &match_result);
735
736 if (!pplayer) {
737 cmd_reply_no_such_player(CMD_AITOGGLE, caller, arg, match_result);
738 return FALSE;
739 } else if (!check) {
740 toggle_ai_player_direct(caller, pplayer);
742 }
743 return TRUE;
744}
745
746/**********************************************************************/
752static bool create_command(struct connection *caller, const char *str,
753 bool check)
754{
755 enum rfc_status status;
756 char buf[MAX_LEN_CONSOLE_LINE];
757
758 /* 2 legal arguments, and extra space for stuffing illegal part */
759 char *arg[3];
760 int ntokens;
761 const char *ai_type_name;
762
763 sz_strlcpy(buf, str);
764 ntokens = get_tokens(buf, arg, 3, TOKEN_DELIMITERS);
765
766 if (ntokens == 1) {
767 ai_type_name = default_ai_type_name();
768 } else if (ntokens == 2) {
769 ai_type_name = arg[1];
770 } else {
772 _("Wrong number of arguments to create command."));
773 free_tokens(arg, ntokens);
774 return FALSE;
775 }
776
777 if (game_was_started()) {
778 status = create_command_newcomer(arg[0], ai_type_name, check,
779 NULL, NULL, buf, sizeof(buf));
780 } else {
781 status = create_command_pregame(arg[0], ai_type_name, check,
782 NULL, buf, sizeof(buf));
783 }
784
785 free_tokens(arg, ntokens);
786
787 if (status != C_OK) {
788 /* No player created. */
789 cmd_reply(CMD_CREATE, caller, status, "%s", buf);
790 return FALSE;
791 }
792
793 if (strlen(buf) > 0) {
794 /* Send a notification. */
795 cmd_reply(CMD_CREATE, caller, C_OK, "%s", buf);
796 }
797
798 return TRUE;
799}
800
801/**********************************************************************/
811 const char *ai,
812 bool check,
813 struct nation_type *pnation,
814 struct player **newplayer,
815 char *buf, size_t buflen)
816{
817 struct player *pplayer = NULL;
818 struct research *presearch;
819 bool new_slot = FALSE;
820
821 /* Check player name. */
822 if (!player_name_check(name, buf, buflen)) {
823 return C_SYNTAX;
824 }
825
826 /* Check first if we can replace a player with
827 * [1a] - the same username. */
828 pplayer = player_by_user(name);
829 if (pplayer && pplayer->is_alive) {
830 fc_snprintf(buf, buflen,
831 _("A living user already exists by that name."));
832 return C_BOUNCE;
833 }
834
835 /* [1b] - the same player name. */
836 pplayer = player_by_name(name);
837 if (pplayer && pplayer->is_alive) {
838 fc_snprintf(buf, buflen,
839 _("A living player already exists by that name."));
840 return C_BOUNCE;
841 }
842
843 if (pnation) {
844 if (!nation_is_in_current_set(pnation)) {
845 fc_snprintf(buf, buflen,
846 _("Can't create player, requested nation %s not in "
847 "current nation set."),
849 return C_FAIL;
850 }
851 players_iterate(aplayer) {
852 if (0 > nations_match(pnation, nation_of_player(aplayer), FALSE)) {
853 fc_snprintf(buf, buflen,
854 _("Can't create players, nation %s conflicts with %s."),
856 nation_plural_for_player(pplayer));
857 return C_FAIL;
858 }
860 } else {
861 /* Try to find a nation. */
862 pnation = pick_a_nation(NULL, FALSE, TRUE, NOT_A_BARBARIAN);
863 if (pnation == NO_NATION_SELECTED) {
864 fc_snprintf(buf, buflen,
865 _("Can't create players, no nations available."));
866 return C_FAIL;
867 }
868 }
869
870 if (pplayer == NULL) {
871 if (player_count() == player_slot_count()) {
872 bool dead_found = FALSE;
873
874 players_iterate(aplayer) {
875 if (!aplayer->is_alive) {
876 dead_found = TRUE;
877 break;
878 }
880
881 if (!dead_found) {
882 fc_snprintf(buf, buflen,
883 _("Can't create players, no slots available."));
884 return C_FAIL;
885 }
886 } else if (normal_player_count() == game.server.max_players) {
887 fc_snprintf(buf, buflen,
888 _("Maxplayers setting prevents creation of more players."));
889 return C_FAIL;
890 }
891 }
892
893 if (check) {
894 /* All code below will change the game state. */
895
896 /* Return an empty string. */
897 buf[0] = '\0';
898
899 return C_OK;
900 }
901
902 if (pplayer) {
903 /* [1] Replace a player. 'pplayer' was set above. */
904 fc_snprintf(buf, buflen,
905 _("%s is replacing dead player %s as an AI-controlled "
906 "player."), name, player_name(pplayer));
907 /* remove player and thus free a player slot */
908 server_remove_player(pplayer);
909 pplayer = NULL;
910 } else if (player_count() == player_slot_count()) {
911 /* [2] All player slots are used; try to remove a dead player. */
912 bool dead_found = FALSE;
913
914 players_iterate(aplayer) {
915 if (!aplayer->is_alive) {
916 if (!dead_found) {
917 /* Fill the buffer with the name of the first found dead player */
918 fc_snprintf(buf, buflen,
919 _("%s is replacing dead player %s as an AI-controlled "
920 "player."), name, player_name(aplayer));
921 dead_found = TRUE;
922 }
923
924 /* Remove player and thus free a player slot */
925 server_remove_player(aplayer);
926 }
927
928 fc_assert(dead_found);
930 } else {
931 /* [3] An empty player slot must be used for the new player. */
932 new_slot = TRUE;
933 }
934
935 /* Create the new player. */
936 pplayer = server_create_player(-1, ai, NULL, FALSE);
937 if (!pplayer) {
938 fc_snprintf(buf, buflen, _("Failed to create new player %s."), name);
939 return C_FAIL;
940 }
941
942 if (new_slot) {
943 /* 'buf' must be set if a new player slot is used. */
944 fc_snprintf(buf, buflen, _("New player %s created."), name);
945 }
946
947 /* We have a player; now initialise all needed data. */
948 (void) aifill(game.info.aifill);
949
950 /* Initialise player. */
951 server_player_init(pplayer, TRUE, TRUE);
952
953 player_nation_defaults(pplayer, pnation, FALSE);
954 pplayer->government = pplayer->target_government =
956 /* Find a color for the new player. */
958
959 /* TRANS: keep one space at the beginning of the string. */
960 cat_snprintf(buf, buflen, _(" Nation of the new player: %s."),
961 nation_rule_name(pnation));
962
963 presearch = research_get(pplayer);
964 init_tech(presearch, TRUE);
965 give_initial_techs(presearch, 0);
966
969 pplayer->unassigned_user = TRUE;
970
971 pplayer->was_created = TRUE; /* must use /remove explicitly to remove */
972 set_as_ai(pplayer);
974
975 CALL_PLR_AI_FUNC(gained_control, pplayer, pplayer);
976
977 send_player_info_c(pplayer, NULL);
978 /* Send updated diplstate information to all players. */
979 send_player_diplstate_c(NULL, NULL);
980 /* Send research info after player info, else the client will complain
981 * about invalid team. */
982 send_research_info(presearch, NULL);
984
985 if (newplayer != NULL) {
986 *newplayer = pplayer;
987 }
988 return C_OK;
989}
990
991/**********************************************************************/
995 const char *ai,
996 bool check,
997 struct player **newplayer,
998 char *buf, size_t buflen)
999{
1000 char leader_name[MAX_LEN_NAME]; /* Must be in whole function scope */
1001 struct player *pplayer = NULL;
1002 bool rand_name = FALSE;
1003
1004 if (name[0] == '\0') {
1005 int filled = 1;
1006
1007 do {
1008 fc_snprintf(leader_name, sizeof(leader_name), "%s*%d", ai, filled++);
1009 } while (player_by_name(leader_name));
1010
1011 name = leader_name;
1012 rand_name = TRUE;
1013 }
1014
1015 if (!player_name_check(name, buf, buflen)) {
1016 return C_SYNTAX;
1017 }
1018
1019 if (NULL != player_by_name(name)) {
1020 fc_snprintf(buf, buflen,
1021 _("A player already exists by that name."));
1022 return C_BOUNCE;
1023 }
1024 if (NULL != player_by_user(name)) {
1025 fc_snprintf(buf, buflen,
1026 _("A user already exists by that name."));
1027 return C_BOUNCE;
1028 }
1029
1030 /* Search for first uncontrolled player */
1031 pplayer = find_uncontrolled_player();
1032
1033 if (NULL == pplayer) {
1034 /* Check that we are not going over max players setting */
1036 fc_snprintf(buf, buflen,
1037 _("Can't add more players, server is full."));
1038 return C_FAIL;
1039 }
1040 /* Check that we have nations available */
1041 if (normal_player_count() >= server.playable_nations) {
1042 if (nation_set_count() > 1) {
1043 fc_snprintf(buf, buflen,
1044 _("Can't add more players, not enough playable nations "
1045 "in current nation set (see 'nationset' setting)."));
1046 } else {
1047 fc_snprintf(buf, buflen,
1048 _("Can't add more players, not enough playable nations."));
1049 }
1050 return C_FAIL;
1051 }
1052 }
1053
1054 if (pplayer) {
1055 struct ai_type *ait = ai_type_by_name(ai);
1056
1057 if (ait == NULL) {
1058 fc_snprintf(buf, buflen,
1059 _("There is no AI type %s."), ai);
1060 return C_FAIL;
1061 }
1062 }
1063
1064 if (check) {
1065 /* All code below will change the game state. */
1066
1067 /* Return an empty string. */
1068 buf[0] = '\0';
1069
1070 return C_OK;
1071 }
1072
1073 if (pplayer) {
1074 fc_snprintf(buf, buflen,
1075 /* TRANS: <name> replacing <name> ... */
1076 _("%s replacing %s as an AI-controlled player."),
1077 name, player_name(pplayer));
1078
1079 team_remove_player(pplayer);
1080 pplayer->ai = ai_type_by_name(ai);
1081 } else {
1082 /* add new player */
1083 pplayer = server_create_player(-1, ai, NULL, FALSE);
1084 /* pregame so no need to assign_player_colors() */
1085 if (!pplayer) {
1086 fc_snprintf(buf, buflen,
1087 _("Failed to create new player %s."), name);
1088 return C_GENFAIL;
1089 }
1090
1091 fc_snprintf(buf, buflen,
1092 _("%s has been added as an AI-controlled player (%s)."),
1093 name, ai_name(pplayer->ai));
1094 }
1095 server_player_init(pplayer, FALSE, TRUE);
1096
1097 server_player_set_name(pplayer, name);
1098 sz_strlcpy(pplayer->username, _(ANON_USER_NAME));
1099 pplayer->unassigned_user = TRUE;
1100
1101 pplayer->was_created = TRUE; /* must use /remove explicitly to remove */
1102 pplayer->random_name = rand_name;
1103 set_as_ai(pplayer);
1105 CALL_PLR_AI_FUNC(gained_control, pplayer, pplayer);
1107
1108 (void) aifill(game.info.aifill);
1111
1112 if (newplayer != NULL) {
1113 *newplayer = pplayer;
1114 }
1115 return C_OK;
1116}
1117
1118/**********************************************************************/
1121static bool remove_player_command(struct connection *caller, char *arg,
1122 bool check)
1123{
1124 enum m_pre_result match_result;
1125 struct player *pplayer;
1126 char name[MAX_LEN_NAME];
1127
1128 pplayer = player_by_name_prefix(arg, &match_result);
1129
1130 if (NULL == pplayer) {
1131 cmd_reply_no_such_player(CMD_REMOVE, caller, arg, match_result);
1132 return FALSE;
1133 }
1134
1135 if (game_was_started() && caller && caller->access_level < ALLOW_ADMIN) {
1136 cmd_reply(CMD_REMOVE, caller, C_FAIL,
1137 _("Command level '%s' or greater needed to remove a player "
1138 "once the game has started."), cmdlevel_name(ALLOW_ADMIN));
1139 return FALSE;
1140 }
1141 if (check) {
1142 return TRUE;
1143 }
1144
1145 sz_strlcpy(name, player_name(pplayer));
1146 server_remove_player(pplayer);
1147 if (!caller || caller->used) { /* may have removed self */
1148 cmd_reply(CMD_REMOVE, caller, C_OK,
1149 _("Removed player %s from the game."), name);
1150 }
1151 (void) aifill(game.info.aifill);
1152 return TRUE;
1153}
1154
1155/**********************************************************************/
1158static bool read_command(struct connection *caller, char *arg, bool check,
1159 int read_recursion)
1160{
1161 return read_init_script_real(caller, arg, FALSE, check, read_recursion);
1162}
1163
1164/**********************************************************************/
1167bool read_init_script(struct connection *caller, char *script_filename,
1168 bool from_cmdline, bool check)
1169{
1170 return read_init_script_real(caller, script_filename, from_cmdline,
1171 check, 0);
1172}
1173
1174/**********************************************************************/
1185static bool read_init_script_real(struct connection *caller,
1186 char *script_filename, bool from_cmdline,
1187 bool check, int read_recursion)
1188{
1189 FILE *script_file;
1190 char serv_filename[strlen(script_extension) + strlen(script_filename) + 2];
1191 char tilde_filename[4096];
1192 const char *real_filename;
1193 size_t fnlen;
1194
1195 /* check recursion depth */
1196 if (read_recursion > GAME_MAX_READ_RECURSION) {
1197 log_error("Error: recursive calls to read!");
1198 return FALSE;
1199 }
1200
1201 /* abuse real_filename to find if we already have a .serv extension */
1202 fnlen = strlen(script_filename);
1203 real_filename = script_filename + fnlen
1204 - MIN(strlen(script_extension), fnlen);
1205 if (strcmp(real_filename, script_extension) != 0) {
1206 fc_snprintf(serv_filename, sizeof(serv_filename), "%s%s",
1207 script_filename, script_extension);
1208 } else {
1209 sz_strlcpy(serv_filename, script_filename);
1210 }
1211
1212 if (is_restricted(caller) && !from_cmdline) {
1213 if (!is_safe_filename(serv_filename)) {
1215 _("Name \"%s\" disallowed for security reasons."),
1216 serv_filename);
1217 return FALSE;
1218 }
1219 sz_strlcpy(tilde_filename, serv_filename);
1220 } else {
1221 interpret_tilde(tilde_filename, sizeof(tilde_filename), serv_filename);
1222 }
1223
1224 real_filename = fileinfoname(get_data_dirs(), tilde_filename);
1225 if (!real_filename) {
1226 if (is_restricted(caller) && !from_cmdline) {
1228 _("No command script found by the name \"%s\"."),
1229 serv_filename);
1230 return FALSE;
1231 }
1232 /* File is outside data directories */
1233 real_filename = tilde_filename;
1234 }
1235
1236 log_testmatic_alt(LOG_NORMAL, _("Loading script file '%s'."), real_filename);
1237
1238 if (is_reg_file_for_access(real_filename, FALSE)
1239 && (script_file = fc_fopen(real_filename, "r"))) {
1240 char buffer[MAX_LEN_CONSOLE_LINE];
1241
1242 /* The size is set as to not overflow buffer in handle_stdin_input */
1243 while (fgets(buffer, MAX_LEN_CONSOLE_LINE - 1, script_file)) {
1244 /* Execute script contents with same permissions as caller */
1245 handle_stdin_input_real(caller, buffer, check, read_recursion + 1);
1246 }
1247 fclose(script_file);
1248
1249 show_ruleset_info(caller, CMD_READ_SCRIPT, check, read_recursion);
1250
1251 return TRUE;
1252 } else {
1254 _("Cannot read command line scriptfile '%s'."), real_filename);
1255 if (NULL != caller) {
1256 log_error(_("Could not read script file '%s'."), real_filename);
1257 }
1258 return FALSE;
1259 }
1260}
1261
1262/**********************************************************************/
1272
1273/**********************************************************************/
1278static bool write_init_script(char *script_filename)
1279{
1280 char real_filename[1024], buf[256];
1281 FILE *script_file;
1282
1283 interpret_tilde(real_filename, sizeof(real_filename), script_filename);
1284
1285 if (is_reg_file_for_access(real_filename, TRUE)
1286 && (script_file = fc_fopen(real_filename, "w"))) {
1287 fprintf(script_file,
1288 "#FREECIV SERVER COMMAND FILE, version %s\n", VERSION_STRING);
1289 fputs("# These are server options saved from a running freeciv-server.\n",
1290 script_file);
1291
1292 /* First rulesetdir. Setting rulesetdir resets the settings to their
1293 * default value, so they would be lost if placed before this. */
1294 fprintf(script_file, "rulesetdir %s\n", game.server.rulesetdir);
1295
1296 /* Some state info from commands (we can't save everything) */
1297
1298 fprintf(script_file, "cmdlevel %s new\n",
1299 cmdlevel_name(default_access_level));
1300
1301 fprintf(script_file, "cmdlevel %s first\n",
1302 cmdlevel_name(first_access_level));
1303
1304 fprintf(script_file, "%s\n",
1306
1307 if (*srvarg.metaserver_addr != '\0'
1308 && ((0 != strcmp(srvarg.metaserver_addr, DEFAULT_META_SERVER_ADDR)))) {
1309 fprintf(script_file, "metaserver %s\n", meta_addr_port());
1310 }
1311
1313 fprintf(script_file, "metapatches %s\n", get_meta_patches_string());
1314 }
1316 fprintf(script_file, "metamessage %s\n", get_meta_message_string());
1317 }
1318
1319 /* Then, the 'set' option settings */
1320
1321 settings_iterate(SSET_ALL, pset) {
1322 fprintf(script_file, "set %s \"%s\"\n", setting_name(pset),
1323 setting_value_name(pset, FALSE, buf, sizeof(buf)));
1325
1326 fclose(script_file);
1327
1328 return TRUE;
1329 } else {
1330 log_error(_("Could not write script file '%s'."), real_filename);
1331
1332 return FALSE;
1333 }
1334}
1335
1336/**********************************************************************/
1339static bool write_command(struct connection *caller, char *arg, bool check)
1340{
1341 if (is_restricted(caller)) {
1343 _("You cannot use the write command on this server"
1344 " for security reasons."));
1345 return FALSE;
1346 } else if (!check) {
1347 char serv_filename[strlen(script_extension) + strlen(arg) + 2];
1348 const char *real_filename;
1349 size_t arglen = strlen(arg);
1350
1351 /* abuse real_filename to find if we already have a .serv extension */
1352 real_filename = arg + arglen - MIN(strlen(script_extension), arglen);
1353 if (strcmp(real_filename, script_extension) != 0) {
1354 fc_snprintf(serv_filename, sizeof(serv_filename), "%s%s",
1355 arg, script_extension);
1356 } else {
1357 sz_strlcpy(serv_filename, arg);
1358 }
1359
1360 if (!write_init_script(serv_filename)) {
1362 /* TRANS: Failed to write server script, e.g., 'example.serv' */
1363 _("Failed to write %s."), serv_filename);
1364 return FALSE;
1365 }
1366
1368 /* TRANS: Wrote server script, e.g., 'example.serv' */
1369 _("Wrote %s."), serv_filename);
1370 }
1371
1372 return TRUE;
1373}
1374
1375/**********************************************************************/
1378static bool set_cmdlevel(struct connection *caller,
1379 struct connection *ptarget,
1380 enum cmdlevel level)
1381{
1382 /* Only ever call me for specific connection. */
1383 fc_assert_ret_val(ptarget != NULL, FALSE);
1384
1385 if (caller && ptarget->access_level > caller->access_level) {
1386 /*
1387 * This command is intended to be used at ctrl access level
1388 * and thus this if clause is needed.
1389 * (Imagine a ctrl level access player that wants to change
1390 * access level of a hack level access player)
1391 * At the moment it can be used only by hack access level
1392 * and thus this clause is never used.
1393 */
1394 cmd_reply(CMD_CMDLEVEL, caller, C_FAIL,
1395 _("Cannot decrease command access level '%s' "
1396 "for connection '%s'; you only have '%s'."),
1397 cmdlevel_name(ptarget->access_level),
1398 ptarget->username,
1399 cmdlevel_name(caller->access_level));
1400 return FALSE;
1401 } else {
1402 conn_set_access(ptarget, level, TRUE);
1403 cmd_reply(CMD_CMDLEVEL, caller, C_OK,
1404 _("Command access level set to '%s' for connection %s."),
1405 cmdlevel_name(level), ptarget->username);
1406 return TRUE;
1407 }
1408}
1409
1410/**********************************************************************/
1413static bool a_connection_exists(void)
1414{
1415 return conn_list_size(game.est_connections) > 0;
1416}
1417
1418/**********************************************************************/
1422{
1424 if (pconn->access_level >= first_access_level) {
1425 return TRUE;
1426 }
1427 }
1429 return FALSE;
1430}
1431
1432/**********************************************************************/
1436{
1438 && !a_connection_exists()) {
1439 return first_access_level;
1440 } else {
1441 return default_access_level;
1442 }
1443}
1444
1445/**********************************************************************/
1450{
1453 notify_conn(NULL, NULL, E_SETTING, ftc_any,
1454 _("Anyone can now become game organizer "
1455 "'%s' by issuing the 'first' command."),
1456 cmdlevel_name(first_access_level));
1457 }
1458}
1459
1460/**********************************************************************/
1463static bool cmdlevel_command(struct connection *caller, char *str, bool check)
1464{
1465 char *arg[2];
1466 int ntokens;
1467 bool ret = FALSE;
1468 enum m_pre_result match_result;
1469 enum cmdlevel level;
1470 struct connection *ptarget;
1471
1472 ntokens = get_tokens(str, arg, 2, TOKEN_DELIMITERS);
1473
1474 if (ntokens == 0) {
1475 /* No argument supplied; list the levels */
1478 _("Command access levels in effect:"));
1481 const char *lvl_name = cmdlevel_name(conn_get_access(pconn));
1482
1483 if (lvl_name != NULL) {
1484 cmd_reply(CMD_CMDLEVEL, caller, C_COMMENT, "cmdlevel %s %s",
1485 lvl_name, pconn->username);
1486 } else {
1487 fc_assert(lvl_name != NULL); /* Always fails when reached. */
1488 }
1491 _("Command access level for new connections: %s"),
1492 cmdlevel_name(default_access_level));
1494 _("Command access level for first player to take it: %s"),
1495 cmdlevel_name(first_access_level));
1497 return TRUE;
1498 }
1499
1500 /* A level name was supplied; set the level. */
1501 level = cmdlevel_by_name(arg[0], fc_strcasecmp);
1502 if (!cmdlevel_is_valid(level)) {
1503 const char *cmdlevel_names[CMDLEVEL_COUNT];
1504 struct astring astr = ASTRING_INIT;
1505 int i = 0;
1506
1507 for (level = cmdlevel_begin(); level != cmdlevel_end();
1508 level = cmdlevel_next(level)) {
1509 cmdlevel_names[i++] = cmdlevel_name(level);
1510 }
1512 /* TRANS: comma and 'or' separated list of access levels */
1513 _("Command access level must be one of %s."),
1514 astr_build_or_list(&astr, cmdlevel_names, i));
1515 astr_free(&astr);
1516 goto CLEAN_UP;
1517 } else if (caller && level > conn_get_access(caller)) {
1518 cmd_reply(CMD_CMDLEVEL, caller, C_FAIL,
1519 _("Cannot increase command access level to '%s';"
1520 " you only have '%s' yourself."),
1521 arg[0], cmdlevel_name(conn_get_access(caller)));
1522 goto CLEAN_UP;
1523 }
1524
1525 if (check) {
1526 return TRUE; /* looks good */
1527 }
1528
1529 if (ntokens == 1) {
1530 /* No playername supplied: set for all connections */
1532 if (pconn != caller) {
1533 (void) set_cmdlevel(caller, pconn, level);
1534 }
1536
1537 /* Set the caller access level at last, because it could make the
1538 * previous operations impossible if set before. */
1539 if (caller) {
1540 (void) set_cmdlevel(caller, caller, level);
1541 }
1542
1543 /* Set default access for new connections. */
1545 cmd_reply(CMD_CMDLEVEL, caller, C_OK,
1546 _("Command access level set to '%s' for new players."),
1547 cmdlevel_name(level));
1548 /* Set default access for first connection. */
1550 cmd_reply(CMD_CMDLEVEL, caller, C_OK,
1551 _("Command access level set to '%s' "
1552 "for first player to grab it."),
1553 cmdlevel_name(level));
1554
1555 ret = TRUE;
1556
1557 } else if (fc_strcasecmp(arg[1], "new") == 0) {
1559 cmd_reply(CMD_CMDLEVEL, caller, C_OK,
1560 _("Command access level set to '%s' for new players."),
1561 cmdlevel_name(level));
1562 if (level > first_access_level) {
1564 cmd_reply(CMD_CMDLEVEL, caller, C_OK,
1565 _("Command access level set to '%s' "
1566 "for first player to grab it."),
1567 cmdlevel_name(level));
1568 }
1569
1570 ret = TRUE;
1571
1572 } else if (fc_strcasecmp(arg[1], "first") == 0) {
1574 cmd_reply(CMD_CMDLEVEL, caller, C_OK,
1575 _("Command access level set to '%s' "
1576 "for first player to grab it."),
1577 cmdlevel_name(level));
1580 cmd_reply(CMD_CMDLEVEL, caller, C_OK,
1581 _("Command access level set to '%s' for new players."),
1582 cmdlevel_name(level));
1583 }
1584
1585 ret = TRUE;
1586
1587 } else if ((ptarget = conn_by_user_prefix(arg[1], &match_result))) {
1588 if (set_cmdlevel(caller, ptarget, level)) {
1589 ret = TRUE;
1590 }
1591 } else {
1592 cmd_reply_no_such_conn(CMD_CMDLEVEL, caller, arg[1], match_result);
1593 }
1594
1595CLEAN_UP:
1596 free_tokens(arg, ntokens);
1597 return ret;
1598}
1599
1600/**********************************************************************/
1606static bool firstlevel_command(struct connection *caller, bool check)
1607{
1608 if (!caller) {
1610 _("The 'first' command makes no sense from the server command line."));
1611 return FALSE;
1612 } else if (caller->access_level >= first_access_level) {
1614 _("You already have command access level '%s' or better."),
1615 cmdlevel_name(first_access_level));
1616 return FALSE;
1617 } else if (is_first_access_level_taken()) {
1619 _("Someone else is already game organizer."));
1620 return FALSE;
1621 } else if (!check) {
1623 cmd_reply(CMD_FIRSTLEVEL, caller, C_OK,
1624 _("Connection %s has opted to become the game organizer."),
1625 caller->username);
1626 }
1627 return TRUE;
1628}
1629
1630/**********************************************************************/
1634{
1635 if (default_access_level > ALLOW_BASIC) {
1636 notify_conn(NULL, NULL, E_SETTING, ftc_server,
1637 _("Default cmdlevel lowered to 'basic' on game start."));
1638 default_access_level = ALLOW_BASIC;
1639 }
1640}
1641
1642/**********************************************************************/
1646static const char *optname_accessor(int i)
1647{
1649}
1650
1651#ifdef FREECIV_HAVE_LIBREADLINE
1652/**********************************************************************/
1655static const char *olvlname_accessor(int i)
1656{
1657 if (i == 0) {
1658 return "rulesetdir";
1659 } else if (i < OLEVELS_NUM+1) {
1660 return sset_level_name(i-1);
1661 } else {
1662 return optname_accessor(i-OLEVELS_NUM-1);
1663 }
1664}
1665#endif /* FREECIV_HAVE_LIBREADLINE */
1666
1667/**********************************************************************/
1670static bool timeout_command(struct connection *caller, char *str, bool check)
1671{
1672 char buf[MAX_LEN_CONSOLE_LINE];
1673 char *arg[4];
1674 int i = 0, ntokens;
1675 int *timeouts[4];
1676
1677 timeouts[0] = &game.server.timeoutint;
1678 timeouts[1] = &game.server.timeoutintinc;
1679 timeouts[2] = &game.server.timeoutinc;
1680 timeouts[3] = &game.server.timeoutincmult;
1681
1682 sz_strlcpy(buf, str);
1683 ntokens = get_tokens(buf, arg, 4, TOKEN_DELIMITERS);
1684
1685 for (i = 0; i < ntokens; i++) {
1686 if (!str_to_int(arg[i], timeouts[i])) {
1687 cmd_reply(CMD_TIMEOUT, caller, C_FAIL, _("Invalid argument %d."),
1688 i + 1);
1689 }
1690 free(arg[i]);
1691 }
1692
1693 if (ntokens == 0) {
1694 cmd_reply(CMD_TIMEOUT, caller, C_SYNTAX, _("Usage:\n%s"),
1696 return FALSE;
1697 } else if (check) {
1698 return TRUE;
1699 }
1700
1701 cmd_reply(CMD_TIMEOUT, caller, C_OK, _("Dynamic timeout set to "
1702 "%d %d %d %d"),
1705
1706 /* if we set anything here, reset the counter */
1708 return TRUE;
1709}
1710
1711/**********************************************************************/
1714static enum sset_level lookup_option_level(const char *name)
1715{
1716 enum sset_level i;
1717
1718 for (i = SSET_ALL; i < OLEVELS_NUM; i++) {
1719 if (0 == fc_strcasecmp(name, sset_level_name(i))) {
1720 return i;
1721 }
1722 }
1723
1724 return SSET_NONE;
1725}
1726
1727/* Special return values of lookup options */
1728#define LOOKUP_OPTION_NO_RESULT (-1)
1729#define LOOKUP_OPTION_AMBIGUOUS (-2)
1730#define LOOKUP_OPTION_LEVEL_NAME (-3)
1731#define LOOKUP_OPTION_RULESETDIR (-4)
1732
1733/**********************************************************************/
1740static int lookup_option(const char *name)
1741{
1742 enum m_pre_result result;
1743 int ind;
1744
1745 /* Check for option levels, first off */
1746 if (lookup_option_level(name) != SSET_NONE) {
1748 }
1749
1751 0, fc_strncasecmp, NULL, name, &ind);
1752 if (M_PRE_AMBIGUOUS > result) {
1753 return ind;
1754 } else if (M_PRE_AMBIGUOUS == result) {
1756 } else if ('\0' != name[0]
1757 && 0 == fc_strncasecmp("rulesetdir", name, strlen(name))) {
1759 } else {
1761 }
1762}
1763
1764/**********************************************************************/
1769static void show_help_option(struct connection *caller,
1770 enum command_id help_cmd, int id)
1771{
1772 char val_buf[256], def_buf[256];
1773 struct setting *pset = setting_by_number(id);
1774 const char *sethelp;
1775
1776 if (setting_short_help(pset)) {
1777 cmd_reply(help_cmd, caller, C_COMMENT,
1778 /* TRANS: <untranslated name> - translated short help */
1779 _("Option: %s - %s"), setting_name(pset),
1780 _(setting_short_help(pset)));
1781 } else {
1782 cmd_reply(help_cmd, caller, C_COMMENT,
1783 /* TRANS: <untranslated name> */
1784 _("Option: %s"), setting_name(pset));
1785 }
1786
1787 sethelp = setting_extra_help(pset, FALSE);
1788 if (strlen(sethelp) > 0) {
1789 char *help = fc_strdup(sethelp);
1790
1792 cmd_reply(help_cmd, caller, C_COMMENT, _("Description:"));
1793 cmd_reply_prefix(help_cmd, caller, C_COMMENT, " ", " %s", help);
1794 FC_FREE(help);
1795 }
1796 cmd_reply(help_cmd, caller, C_COMMENT,
1797 _("Status: %s"), (setting_is_changeable(pset, NULL, NULL, 0)
1798 ? _("changeable") : _("fixed")));
1799
1800 if (setting_is_visible(pset, caller)) {
1801 setting_value_name(pset, TRUE, val_buf, sizeof(val_buf));
1802 setting_default_name(pset, TRUE, def_buf, sizeof(def_buf));
1803
1804 switch (setting_type(pset)) {
1805 case SST_INT:
1806 cmd_reply(help_cmd, caller, C_COMMENT, "%s %s, %s %d, %s %s, %s %d",
1807 _("Value:"), val_buf,
1808 _("Minimum:"), setting_int_min(pset),
1809 _("Default:"), def_buf,
1810 _("Maximum:"), setting_int_max(pset));
1811 break;
1812 case SST_ENUM:
1813 {
1814 int i;
1815 const char *value;
1816
1817 cmd_reply(help_cmd, caller, C_COMMENT, _("Possible values:"));
1818 for (i = 0; (value = setting_enum_val(pset, i, FALSE)); i++) {
1819 cmd_reply(help_cmd, caller, C_COMMENT, "- %s: \"%s\"",
1820 value, setting_enum_val(pset, i, TRUE));
1821 }
1822 }
1823
1825 case SST_BOOL:
1826 case SST_STRING:
1827 cmd_reply(help_cmd, caller, C_COMMENT, "%s %s, %s %s",
1828 _("Value:"), val_buf, _("Default:"), def_buf);
1829 break;
1830 case SST_BITWISE:
1831 {
1832 int i;
1833 const char *value;
1834
1835 cmd_reply(help_cmd, caller, C_COMMENT,
1836 _("Possible values (option can take any number of these):"));
1837 for (i = 0; (value = setting_bitwise_bit(pset, i, FALSE)); i++) {
1838 cmd_reply(help_cmd, caller, C_COMMENT, "- %s: \"%s\"",
1839 value, setting_bitwise_bit(pset, i, TRUE));
1840 }
1841 cmd_reply(help_cmd, caller, C_COMMENT, "%s %s",
1842 _("Value:"), val_buf);
1843 cmd_reply(help_cmd, caller, C_COMMENT, "%s %s",
1844 _("Default:"), def_buf);
1845 }
1846 break;
1847 case SST_COUNT:
1848 fc_assert(setting_type(pset) != SST_COUNT);
1849 break;
1850 }
1851 }
1852}
1853
1854/**********************************************************************/
1859static void show_help_option_list(struct connection *caller,
1860 enum command_id help_cmd)
1861{
1862 cmd_reply(help_cmd, caller, C_COMMENT, horiz_line);
1863 cmd_reply(help_cmd, caller, C_COMMENT,
1864 _("Explanations are available for the following server options:"));
1865 cmd_reply(help_cmd, caller, C_COMMENT, horiz_line);
1866 if (!caller && con_get_style()) {
1867 settings_iterate(SSET_ALL, pset) {
1868 cmd_reply(help_cmd, caller, C_COMMENT, "%s", setting_name(pset));
1870 } else {
1871 char buf[MAX_LEN_CONSOLE_LINE];
1872 int j = 0;
1873 buf[0] = '\0';
1874
1875 settings_iterate(SSET_ALL, pset) {
1876 if (setting_is_visible(pset, caller)) {
1877 cat_snprintf(buf, sizeof(buf), "%-19s", setting_name(pset));
1878 if ((++j % 4) == 0) {
1879 cmd_reply(help_cmd, caller, C_COMMENT, "%s", buf);
1880 buf[0] = '\0';
1881 }
1882 }
1884
1885 if (buf[0] != '\0') {
1886 cmd_reply(help_cmd, caller, C_COMMENT, "%s", buf);
1887 }
1888 }
1889 cmd_reply(help_cmd, caller, C_COMMENT, horiz_line);
1890}
1891
1892/**********************************************************************/
1895static bool explain_option(struct connection *caller, char *str, bool check)
1896{
1897 int cmd;
1898
1900
1901 if (*str != '\0') {
1902 cmd = lookup_option(str);
1903 if (cmd >= 0 && cmd < settings_number()) {
1904 show_help_option(caller, CMD_EXPLAIN, cmd);
1905 } else if (cmd == LOOKUP_OPTION_NO_RESULT
1906 || cmd == LOOKUP_OPTION_LEVEL_NAME
1907 || cmd == LOOKUP_OPTION_RULESETDIR) {
1908 cmd_reply(CMD_EXPLAIN, caller, C_FAIL,
1909 _("No explanation for that yet."));
1910 return FALSE;
1911 } else if (cmd == LOOKUP_OPTION_AMBIGUOUS) {
1912 cmd_reply(CMD_EXPLAIN, caller, C_FAIL, _("Ambiguous option name."));
1913 return FALSE;
1914 } else {
1915 log_error("Unexpected case %d in %s line %d", cmd, __FILE__,
1916 __FC_LINE__);
1917 return FALSE;
1918 }
1919 } else {
1921 }
1922 return TRUE;
1923}
1924
1925/**********************************************************************/
1928static bool wall(char *str, bool check)
1929{
1930 if (!check) {
1931 notify_conn(NULL, NULL, E_MESSAGE_WALL, ftc_server_prompt,
1932 _("Server Operator: %s"), str);
1933 }
1934 return TRUE;
1935}
1936
1937/**********************************************************************/
1940static bool connectmsg_command(struct connection *caller, char *str,
1941 bool check)
1942{
1943 unsigned int bufsize = sizeof(game.server.connectmsg);
1944
1945 if (is_restricted(caller)) {
1946 return FALSE;
1947 }
1948 if (!check) {
1949 int i;
1950 int c = 0;
1951
1952 for (i = 0; c < bufsize -1 && str[i] != '\0'; i++) {
1953 if (str[i] == '\\') {
1954 i++;
1955
1956 if (str[i] == 'n') {
1957 game.server.connectmsg[c++] = '\n';
1958 } else {
1959 game.server.connectmsg[c++] = str[i];
1960 }
1961 } else {
1962 game.server.connectmsg[c++] = str[i];
1963 }
1964 }
1965
1966 game.server.connectmsg[c++] = '\0';
1967
1968 if (c == bufsize) {
1969 /* Truncated */
1971 _("Connectmsg truncated to %u bytes."), bufsize);
1972 }
1973 }
1974 return TRUE;
1975}
1976
1977/**********************************************************************/
1981static enum command_id cmd_of_level(enum ai_level level)
1982{
1983 switch (level) {
1984 case AI_LEVEL_AWAY : return CMD_AWAY;
1985 case AI_LEVEL_RESTRICTED : return CMD_RESTRICTED;
1986 case AI_LEVEL_NOVICE : return CMD_NOVICE;
1987 case AI_LEVEL_EASY : return CMD_EASY;
1988 case AI_LEVEL_NORMAL : return CMD_NORMAL;
1989 case AI_LEVEL_HARD : return CMD_HARD;
1990 case AI_LEVEL_CHEATING : return CMD_CHEATING;
1991#ifdef FREECIV_DEBUG
1992 case AI_LEVEL_EXPERIMENTAL : return CMD_EXPERIMENTAL;
1993#endif /* FREECIV_DEBUG */
1994 case AI_LEVEL_COUNT : return CMD_NORMAL;
1995 }
1996 log_error("Unknown AI level variant: %d.", level);
1997 return CMD_NORMAL;
1998}
1999
2000/**********************************************************************/
2003void set_ai_level_direct(struct player *pplayer, enum ai_level level)
2004{
2005 set_ai_level_directer(pplayer, level);
2006 send_player_info_c(pplayer, NULL);
2008 _("Player '%s' now has AI skill level '%s'."),
2009 player_name(pplayer),
2010 ai_level_translated_name(level));
2011
2012}
2013
2014/**********************************************************************/
2017static bool set_ai_level_named(struct connection *caller, const char *name,
2018 const char *level_name, bool check)
2019{
2020 enum ai_level level = ai_level_by_name(level_name, fc_strcasecmp);
2021
2022 return set_ai_level(caller, name, level, check);
2023}
2024
2025/**********************************************************************/
2028static bool set_ai_level(struct connection *caller, const char *name,
2029 enum ai_level level, bool check)
2030{
2031 enum m_pre_result match_result;
2032 struct player *pplayer;
2033
2034 fc_assert_ret_val(level > 0 && level < 11, FALSE);
2035
2036 pplayer = player_by_name_prefix(name, &match_result);
2037
2038 if (pplayer) {
2039 if (is_ai(pplayer)) {
2040 if (check) {
2041 return TRUE;
2042 }
2043 set_ai_level_directer(pplayer, level);
2044 send_player_info_c(pplayer, NULL);
2045 cmd_reply(cmd_of_level(level), caller, C_OK,
2046 _("Player '%s' now has AI skill level '%s'."),
2047 player_name(pplayer),
2048 ai_level_translated_name(level));
2049 } else {
2051 _("%s is not controlled by the AI."),
2052 player_name(pplayer));
2053 return FALSE;
2054 }
2055 } else if (match_result == M_PRE_EMPTY) {
2056 if (check) {
2057 return TRUE;
2058 }
2059 players_iterate(cplayer) {
2060 if (is_ai(cplayer)) {
2061 set_ai_level_directer(cplayer, level);
2062 send_player_info_c(cplayer, NULL);
2063 cmd_reply(cmd_of_level(level), caller, C_OK,
2064 _("Player '%s' now has AI skill level '%s'."),
2065 player_name(cplayer),
2066 ai_level_translated_name(level));
2067 }
2070 send_game_info(NULL);
2071 cmd_reply(cmd_of_level(level), caller, C_OK,
2072 _("Default AI skill level set to '%s'."),
2073 ai_level_translated_name(level));
2074 } else {
2075 cmd_reply_no_such_player(cmd_of_level(level), caller, name, match_result);
2076 return FALSE;
2077 }
2078 return TRUE;
2079}
2080
2081/**********************************************************************/
2084static bool away_command(struct connection *caller, bool check)
2085{
2086 struct player *pplayer;
2087
2088 if (caller == NULL) {
2089 cmd_reply(CMD_AWAY, caller, C_FAIL, _("This command is client only."));
2090 return FALSE;
2091 }
2092
2093 if (!conn_controls_player(caller)) {
2094 /* This happens for detached or observer connections. */
2095 cmd_reply(CMD_AWAY, caller, C_FAIL,
2096 _("Only players may use the away command."));
2097 return FALSE;
2098 }
2099
2100 if (check) {
2101 return TRUE;
2102 }
2103
2104 pplayer = conn_get_player(caller);
2105 if (is_human(pplayer)) {
2106 cmd_reply(CMD_AWAY, caller, C_OK,
2107 _("%s set to away mode."), player_name(pplayer));
2108 player_set_to_ai_mode(pplayer, AI_LEVEL_AWAY);
2109 fc_assert(!is_human(pplayer));
2110 } else {
2111 cmd_reply(CMD_AWAY, caller, C_OK,
2112 _("%s returned to game."), player_name(pplayer));
2114 fc_assert(is_human(pplayer));
2115 }
2116
2118
2119 return TRUE;
2120}
2121
2122/**********************************************************************/
2125static void show_ruleset_info(struct connection *caller, enum command_id cmd,
2126 bool check, int read_recursion)
2127{
2128 char *show_arg = "changed";
2129
2130 /* show changed settings only at the top level of recursion */
2131 if (read_recursion != 0) {
2132 return;
2133 }
2134
2135 show_settings(caller, cmd, show_arg, check);
2136
2137 if (game.ruleset_summary != NULL) {
2139
2141 cmd_reply(cmd, caller, C_COMMENT, "%s", translated);
2142 cmd_reply(cmd, caller, C_COMMENT, horiz_line);
2143 free(translated);
2144 }
2145}
2146
2147/**********************************************************************/
2150static bool show_command(struct connection *caller, char *str, bool check)
2151{
2152 return show_settings(caller, CMD_SHOW, str, check);
2153}
2154
2155/**********************************************************************/
2160static bool show_settings(struct connection *caller,
2161 enum command_id called_as,
2162 char *str, bool check)
2163{
2164 int cmd;
2165 enum sset_level level = SSET_ALL;
2166 size_t clen = 0;
2167
2169 if (str[0] != '\0') {
2170 /* In "/show forests", figure out that it's the forests option we're
2171 * looking at. */
2172 cmd = lookup_option(str);
2173 if (cmd >= 0) {
2174 /* Ignore levels when a particular option is specified. */
2175 level = SSET_NONE;
2176
2177 if (!setting_is_visible(setting_by_number(cmd), caller)) {
2178 cmd_reply(called_as, caller, C_FAIL,
2179 _("Sorry, you do not have access to view option '%s'."),
2180 str);
2181 return FALSE;
2182 }
2183 }
2184
2185 /* Valid negative values for 'cmd' are defined as LOOKUP_OPTION_*. */
2186 switch (cmd) {
2188 cmd_reply(called_as, caller, C_FAIL, _("Unknown option '%s'."), str);
2189 return FALSE;
2191 /* Allow ambiguous: show all matching. */
2192 clen = strlen(str);
2193 break;
2195 /* Option level. */
2197 break;
2199 /* Ruleset. */
2200 cmd_reply(called_as, caller, C_COMMENT,
2201 _("Current ruleset directory is \"%s\""),
2203 return TRUE;
2204 }
2205 } else {
2206 /* to indicate that no command was specified */
2208 /* Use vital level by default. */
2209 level = SSET_VITAL;
2210 }
2211
2213 || cmd == LOOKUP_OPTION_LEVEL_NAME
2214 || cmd == LOOKUP_OPTION_NO_RESULT, FALSE);
2215
2216#define cmd_reply_show(string) \
2217 cmd_reply(called_as, caller, C_COMMENT, "%s", string)
2218
2219 {
2220 const char *heading = NULL;
2221 switch (level) {
2222 case SSET_NONE:
2223 break;
2224 case SSET_CHANGED:
2225 heading = _("All options with non-default values");
2226 break;
2227 case SSET_ALL:
2228 heading = _("All options");
2229 break;
2230 case SSET_VITAL:
2231 heading = _("Vital options");
2232 break;
2233 case SSET_SITUATIONAL:
2234 heading = _("Situational options");
2235 break;
2236 case SSET_RARE:
2237 heading = _("Rarely used options");
2238 break;
2239 case SSET_LOCKED:
2240 heading = _("Options locked by the ruleset");
2241 break;
2242 case OLEVELS_NUM:
2243 /* nothing */
2244 break;
2245 }
2246 if (heading) {
2248 cmd_reply_show(heading);
2249 }
2250 }
2252 cmd_reply_show(_("In the column '##' the status of the option is shown:"));
2253 cmd_reply_show(_(" - a '!' means the option is locked by the ruleset."));
2254 cmd_reply_show(_(" - a '+' means you may change the option."));
2255 cmd_reply_show(_(" - a '~' means that option follows default value."));
2256 cmd_reply_show(_(" - a '=' means the value is same as default."));
2258 cmd_reply(called_as, caller, C_COMMENT, _("%-*s ## value (min, max)"),
2259 OPTION_NAME_SPACE, _("Option"));
2261
2262 /* Update changed and locked levels. */
2264
2265 switch (level) {
2266 case SSET_NONE:
2267 /* Show _one_ setting. */
2268 fc_assert_ret_val(0 <= cmd, FALSE);
2269 {
2270 struct setting *pset = setting_by_number(cmd);
2271
2272 show_settings_one(caller, called_as, pset);
2273 }
2274 break;
2275 case SSET_CHANGED:
2276 case SSET_ALL:
2277 case SSET_VITAL:
2278 case SSET_SITUATIONAL:
2279 case SSET_RARE:
2280 case SSET_LOCKED:
2281 settings_iterate(level, pset) {
2282 if (!setting_is_visible(pset, caller)) {
2283 continue;
2284 }
2285
2286 if (LOOKUP_OPTION_AMBIGUOUS == cmd
2287 && 0 != fc_strncasecmp(setting_name(pset), str, clen)) {
2288 continue;
2289 }
2290
2291 show_settings_one(caller, called_as, pset);
2293 break;
2294 case OLEVELS_NUM:
2295 /* nothing */
2296 break;
2297 }
2298
2300 /* Only emit this additional help for bona fide 'show' command */
2301 if (called_as == CMD_SHOW) {
2302 cmd_reply_show(_("A help text for each option is available via 'help "
2303 "<option>'."));
2305 if (level == SSET_VITAL) {
2306 cmd_reply_show(_("Try 'show situational' or 'show rare' to show "
2307 "more options.\n"
2308 "Try 'show changed' to show settings with "
2309 "non-default values.\n"
2310 "Try 'show locked' to show settings locked "
2311 "by the ruleset."));
2313 }
2314 }
2315 return TRUE;
2316#undef cmd_reply_show
2317}
2318
2319/**********************************************************************/
2332static void show_settings_one(struct connection *caller, enum command_id cmd,
2333 struct setting *pset)
2334{
2335 char buf[MAX_LEN_CONSOLE_LINE] = "", value[MAX_LEN_CONSOLE_LINE] = "";
2336 bool is_changed;
2337 static char prefix[OPTION_NAME_SPACE + 4 + 1] = "";
2338 char defaultness;
2339
2340 fc_assert_ret(pset != NULL);
2341
2342 is_changed = setting_non_default(pset);
2343 setting_value_name(pset, TRUE, value, sizeof(value));
2344
2345 /* Wrap long option values, such as bitwise options */
2346 fc_break_lines(value, LINE_BREAK - (sizeof(prefix)-1));
2347
2348 if (prefix[0] == '\0') {
2349 memset(prefix, ' ', sizeof(prefix)-1);
2350 }
2351
2352 if (is_changed) {
2353 /* Emphasizes the changed option. */
2354 /* Apply tags to each line fragment. */
2355 size_t startpos = 0;
2356 char *nl;
2357
2358 do {
2359 nl = strchr(value + startpos, '\n');
2360 featured_text_apply_tag(value, buf, sizeof(buf), TTT_COLOR,
2361 startpos, nl ? nl - value : FT_OFFSET_UNSET,
2362 ftc_changed);
2363 sz_strlcpy(value, buf);
2364 if (nl) {
2365 char *p = strchr(nl, '\n');
2366
2367 fc_assert_action(p != NULL, break);
2368 startpos = p + 1 - value;
2369 }
2370 } while (nl);
2371 }
2372
2373 if (SST_INT == setting_type(pset)) {
2374 /* Add the range. */
2375 cat_snprintf(value, sizeof(value), " (%d, %d)",
2376 setting_int_min(pset), setting_int_max(pset));
2377 }
2378
2379 if (setting_get_setdef(pset) == SETDEF_INTERNAL) {
2380 defaultness = '~';
2381 } else if (is_changed) {
2382 defaultness = ' ';
2383 } else {
2384 defaultness = '=';
2385 }
2386
2387 cmd_reply_prefix(cmd, caller, C_COMMENT, prefix, "%-*s %c%c %s",
2389 setting_status(caller, pset), defaultness,
2390 value);
2391}
2392
2393/**********************************************************************/
2396static bool team_command(struct connection *caller, char *str, bool check)
2397{
2398 struct player *pplayer;
2399 enum m_pre_result match_result;
2400 char buf[MAX_LEN_CONSOLE_LINE];
2401 char *arg[2];
2402 int ntokens = 0, i;
2403 bool res = FALSE;
2404 struct team_slot *tslot;
2405
2406 if (game_was_started()) {
2407 cmd_reply(CMD_TEAM, caller, C_SYNTAX,
2408 _("Cannot change teams once game has begun."));
2409 return FALSE;
2410 }
2411
2412 if (str != NULL || strlen(str) > 0) {
2413 sz_strlcpy(buf, str);
2414 ntokens = get_tokens(buf, arg, 2, TOKEN_DELIMITERS);
2415 }
2416 if (ntokens != 2) {
2417 cmd_reply(CMD_TEAM, caller, C_SYNTAX,
2418 _("Undefined argument. Usage:\n%s"),
2420 goto cleanup;
2421 }
2422
2423 pplayer = player_by_name_prefix(arg[0], &match_result);
2424 if (pplayer == NULL) {
2425 cmd_reply_no_such_player(CMD_TEAM, caller, arg[0], match_result);
2426 goto cleanup;
2427 }
2428
2429 tslot = team_slot_by_rule_name(arg[1]);
2430 if (NULL == tslot) {
2431 int teamno;
2432
2433 if (str_to_int(arg[1], &teamno)) {
2434 tslot = team_slot_by_number(teamno);
2435 }
2436 }
2437
2438 if (NULL == tslot) {
2439 cmd_reply(CMD_TEAM, caller, C_SYNTAX,
2440 _("No such team %s. Please give a "
2441 "valid team name or number."), arg[1]);
2442 goto cleanup;
2443 }
2444
2445 if (is_barbarian(pplayer)) {
2446 /* This can happen if we change team settings on a loaded game. */
2447 cmd_reply(CMD_TEAM, caller, C_SYNTAX, _("Cannot team a barbarian."));
2448 goto cleanup;
2449 }
2450
2451 if (!check) {
2452 /* Should never fail when slot given is not NULL */
2453 team_add_player(pplayer, team_new(tslot));
2454 send_player_info_c(pplayer, NULL);
2455 cmd_reply(CMD_TEAM, caller, C_OK, _("Player %s set to team %s."),
2456 player_name(pplayer),
2458 }
2459
2460 res = TRUE;
2461
2462 cleanup:
2463 for (i = 0; i < ntokens; i++) {
2464 free(arg[i]);
2465 }
2466
2467 return res;
2468}
2469
2470/**********************************************************************/
2473static void show_votes(struct connection *caller)
2474{
2475 int count = 0;
2476 const char *title;
2477
2478 if (vote_list != NULL) {
2480 if (NULL != caller && !conn_can_see_vote(caller, pvote)) {
2481 continue;
2482 }
2483 /* TRANS: "Vote" or "Teamvote" is voting-as-a-process. Used as
2484 * part of a sentence. */
2485 title = vote_is_team_only(pvote) ? _("Teamvote") : _("Vote");
2486 cmd_reply(CMD_VOTE, caller, C_COMMENT,
2487 /* TRANS: "[Vote|Teamvote] 3 \"proposed change\" (needs ..." */
2488 _("%s %d \"%s\" (needs %0.0f%%%s): %d for, "
2489 "%d against, and %d abstained out of %d players."),
2490 title, pvote->vote_no, pvote->cmdline,
2491 MIN(100, pvote->need_pc * 100 + 1),
2492 /* TRANS: preserve leading space */
2493 pvote->flags & VCF_NODISSENT ? _(" no dissent") : "",
2494 pvote->yes, pvote->no, pvote->abstain, count_voters(pvote));
2495 count++;
2497 }
2498
2499 if (count == 0) {
2500 cmd_reply(CMD_VOTE, caller, C_COMMENT,
2501 _("There are no votes going on."));
2502 }
2503}
2504
2505/**********************************************************************/
2508static const char *const vote_args[] = {
2509 "yes",
2510 "no",
2511 "abstain",
2512 NULL
2513};
2514static const char *vote_arg_accessor(int i)
2515{
2516 return vote_args[i];
2517}
2518
2519/**********************************************************************/
2522static bool vote_command(struct connection *caller, char *str,
2523 bool check)
2524{
2525 char buf[MAX_LEN_CONSOLE_LINE];
2526 char *arg[2];
2527 int ntokens = 0, i = 0, which = -1;
2528 enum m_pre_result match_result;
2529 struct vote *pvote = NULL;
2530 bool res = FALSE;
2531
2532 if (check) {
2533 /* This should never happen, since /vote must always be
2534 * set to ALLOW_BASIC or less. But just in case... */
2535 return FALSE;
2536 }
2537
2538 sz_strlcpy(buf, str);
2539 ntokens = get_tokens(buf, arg, 2, TOKEN_DELIMITERS);
2540
2541 if (ntokens == 0) {
2542 show_votes(caller);
2543 goto CLEANUP;
2544 } else if (!conn_can_vote(caller, NULL)) {
2545 cmd_reply(CMD_VOTE, caller, C_FAIL,
2546 _("You are not allowed to use this command."));
2547 goto CLEANUP;
2548 }
2549
2550 match_result = match_prefix(vote_arg_accessor, VOTE_NUM, 0,
2551 fc_strncasecmp, NULL, arg[0], &i);
2552
2553 if (match_result == M_PRE_AMBIGUOUS) {
2554 cmd_reply(CMD_VOTE, caller, C_SYNTAX,
2555 _("The argument \"%s\" is ambiguous."), arg[0]);
2556 goto CLEANUP;
2557 } else if (match_result > M_PRE_AMBIGUOUS) {
2558 /* Failed */
2559 cmd_reply(CMD_VOTE, caller, C_SYNTAX,
2560 _("Undefined argument. Usage:\n%s"),
2562 goto CLEANUP;
2563 }
2564
2565 if (ntokens == 1) {
2566 /* Applies to last vote */
2568 which = vote_number_sequence;
2569 } else {
2570 int num_votes = vote_list_size(vote_list);
2571 if (num_votes == 0) {
2572 cmd_reply(CMD_VOTE, caller, C_FAIL, _("There are no votes running."));
2573 } else {
2574 /* TRANS: "vote" as a process */
2575 cmd_reply(CMD_VOTE, caller, C_FAIL, _("No legal last vote (%d %s)."),
2576 num_votes, PL_("other vote running", "other votes running",
2577 num_votes));
2578 }
2579 goto CLEANUP;
2580 }
2581 } else {
2582 if (!str_to_int(arg[1], &which)) {
2583 cmd_reply(CMD_VOTE, caller, C_SYNTAX, _("Value must be an integer."));
2584 goto CLEANUP;
2585 }
2586 }
2587
2588 if (!(pvote = get_vote_by_no(which))) {
2589 /* TRANS: "vote" as a process */
2590 cmd_reply(CMD_VOTE, caller, C_FAIL, _("No such vote (%d)."), which);
2591 goto CLEANUP;
2592 }
2593
2594 if (!conn_can_vote(caller, pvote)) {
2595 cmd_reply(CMD_VOTE, caller, C_FAIL,
2596 _("You are not allowed to vote on that."));
2597 goto CLEANUP;
2598 }
2599
2600 if (i == VOTE_YES) {
2601 cmd_reply(CMD_VOTE, caller, C_COMMENT, _("You voted for \"%s\""),
2602 pvote->cmdline);
2603 connection_vote(caller, pvote, VOTE_YES);
2604 } else if (i == VOTE_NO) {
2605 cmd_reply(CMD_VOTE, caller, C_COMMENT, _("You voted against \"%s\""),
2606 pvote->cmdline);
2607 connection_vote(caller, pvote, VOTE_NO);
2608 } else if (i == VOTE_ABSTAIN) {
2609 cmd_reply(CMD_VOTE, caller, C_COMMENT,
2610 _("You abstained from voting on \"%s\""), pvote->cmdline);
2611 connection_vote(caller, pvote, VOTE_ABSTAIN);
2612 } else {
2613 /* Must never happen. */
2614 fc_assert_action(FALSE, goto CLEANUP);
2615 }
2616
2617 res = TRUE;
2618
2619CLEANUP:
2620 free_tokens(arg, ntokens);
2621 return res;
2622}
2623
2624/**********************************************************************/
2627static bool cancelvote_command(struct connection *caller,
2628 char *arg, bool check)
2629{
2630 struct vote *pvote = NULL;
2631 int vote_no;
2632
2633 if (check) {
2634 /* This should never happen anyway, since /cancelvote
2635 * is set to ALLOW_BASIC in both pregame and while the
2636 * game is running. */
2637 return FALSE;
2638 }
2639
2641
2642 if (arg[0] == '\0') {
2643 if (caller == NULL) {
2644 /* Server prompt */
2646 /* TRANS: "vote" as a process */
2647 _("Missing argument <vote number> or "
2648 "the string \"all\"."));
2649 return FALSE;
2650 }
2651 /* The caller is canceling their own vote. */
2652 if (!(pvote = get_vote_by_caller(caller))) {
2654 _("You don't have any vote going on."));
2655 return FALSE;
2656 }
2657 } else if (fc_strcasecmp(arg, "all") == 0) {
2658 /* Cancel all votes (needs some privileges). */
2659 if (vote_list_size(vote_list) == 0) {
2661 _("There isn't any vote going on."));
2662 return FALSE;
2663 } else if (!caller || conn_get_access(caller) >= ALLOW_ADMIN) {
2665 notify_conn(NULL, NULL, E_VOTE_ABORTED, ftc_server,
2666 /* TRANS: "votes" as a process */
2667 _("All votes have been removed."));
2668 return TRUE;
2669 } else {
2671 _("You are not allowed to use this command."));
2672 return FALSE;
2673 }
2674 } else if (str_to_int(arg, &vote_no)) {
2675 /* Cancel one particular vote (needs some privileges if the vote
2676 * is not owned). */
2677 if (!(pvote = get_vote_by_no(vote_no))) {
2679 /* TRANS: "vote" as a process */
2680 _("No such vote (%d)."), vote_no);
2681 return FALSE;
2682 } else if (caller && conn_get_access(caller) < ALLOW_ADMIN
2683 && caller->id != pvote->caller_id) {
2685 /* TRANS: "vote" as a process */
2686 _("You are not allowed to cancel this vote (%d)."),
2687 vote_no);
2688 return FALSE;
2689 }
2690 } else {
2692 /* TRANS: "vote" as a process */
2693 _("Usage: /cancelvote [<vote number>|all]"));
2694 return FALSE;
2695 }
2696
2697 fc_assert_ret_val(NULL != pvote, FALSE);
2698
2699 if (caller) {
2701 NULL, E_VOTE_ABORTED, ftc_server,
2702 /* TRANS: "vote" as a process */
2703 _("%s has canceled the vote \"%s\" (number %d)."),
2704 caller->username, pvote->cmdline, pvote->vote_no);
2705 } else {
2706 /* Server prompt */
2708 NULL, E_VOTE_ABORTED, ftc_server,
2709 /* TRANS: "vote" as a process */
2710 _("The vote \"%s\" (number %d) has been canceled."),
2711 pvote->cmdline, pvote->vote_no);
2712 }
2713 /* Make it after, prevent crashs about a free pointer (pvote). */
2714 remove_vote(pvote);
2715
2716 return TRUE;
2717}
2718
2719/**********************************************************************/
2722static bool debug_command(struct connection *caller, char *str,
2723 bool check)
2724{
2725 char buf[MAX_LEN_CONSOLE_LINE];
2726 char *arg[3];
2727 int ntokens = 0, i;
2728
2729 if (game.info.is_new_game) {
2730 cmd_reply(CMD_DEBUG, caller, C_SYNTAX,
2731 _("Can only use this command once game has begun."));
2732 return FALSE;
2733 }
2734 if (check) {
2735 return TRUE; /* whatever! */
2736 }
2737
2738 if (str != NULL && strlen(str) > 0) {
2739 sz_strlcpy(buf, str);
2740 ntokens = get_tokens(buf, arg, 3, TOKEN_DELIMITERS);
2741 } else {
2742 ntokens = 0;
2743 }
2744
2745 if (ntokens > 0 && strcmp(arg[0], "diplomacy") == 0) {
2746 struct player *pplayer;
2747 enum m_pre_result match_result;
2748
2749 if (ntokens != 2) {
2750 cmd_reply(CMD_DEBUG, caller, C_SYNTAX,
2751 _("Undefined argument. Usage:\n%s"),
2753 goto cleanup;
2754 }
2755 pplayer = player_by_name_prefix(arg[1], &match_result);
2756 if (pplayer == NULL) {
2757 cmd_reply_no_such_player(CMD_DEBUG, caller, arg[1], match_result);
2758 goto cleanup;
2759 }
2762 cmd_reply(CMD_DEBUG, caller, C_OK, _("%s diplomacy no longer debugged"),
2763 player_name(pplayer));
2764 } else {
2766 cmd_reply(CMD_DEBUG, caller, C_OK, _("%s diplomacy debugged"),
2767 player_name(pplayer));
2768 /* TODO: print some info about the player here */
2769 }
2770 } else if (ntokens > 0 && strcmp(arg[0], "tech") == 0) {
2771 struct player *pplayer;
2772 enum m_pre_result match_result;
2773
2774 if (ntokens != 2) {
2775 cmd_reply(CMD_DEBUG, caller, C_SYNTAX,
2776 _("Undefined argument. Usage:\n%s"),
2778 goto cleanup;
2779 }
2780 pplayer = player_by_name_prefix(arg[1], &match_result);
2781 if (pplayer == NULL) {
2782 cmd_reply_no_such_player(CMD_DEBUG, caller, arg[1], match_result);
2783 goto cleanup;
2784 }
2785 if (BV_ISSET(pplayer->server.debug, PLAYER_DEBUG_TECH)) {
2787 cmd_reply(CMD_DEBUG, caller, C_OK, _("%s tech no longer debugged"),
2788 player_name(pplayer));
2789 } else {
2791 cmd_reply(CMD_DEBUG, caller, C_OK, _("%s tech debugged"),
2792 player_name(pplayer));
2793 /* TODO: print some info about the player here */
2794 }
2795 } else if (ntokens > 0 && strcmp(arg[0], "info") == 0) {
2796 int cities = 0, players = 0, units = 0, citizen_count = 0;
2797
2798 players_iterate(plr) {
2799 players++;
2800 city_list_iterate(plr->cities, pcity) {
2801 cities++;
2802 citizen_count += city_size_get(pcity);
2804 units += unit_list_size(plr->units);
2806 log_normal(_("players=%d cities=%d citizens=%d units=%d"),
2807 players, cities, citizen_count, units);
2808 notify_conn(game.est_connections, NULL, E_AI_DEBUG, ftc_log,
2809 _("players=%d cities=%d citizens=%d units=%d"),
2810 players, cities, citizen_count, units);
2811 } else if (ntokens > 0 && strcmp(arg[0], "city") == 0) {
2812 int x, y;
2813 struct tile *ptile;
2814 struct city *pcity;
2815
2816 if (ntokens != 3) {
2817 cmd_reply(CMD_DEBUG, caller, C_SYNTAX,
2818 _("Undefined argument. Usage:\n%s"),
2820 goto cleanup;
2821 }
2822 if (!str_to_int(arg[1], &x) || !str_to_int(arg[2], &y)) {
2823 cmd_reply(CMD_DEBUG, caller, C_SYNTAX, _("Value 2 & 3 must be integer."));
2824 goto cleanup;
2825 }
2826 if (!(ptile = map_pos_to_tile(&(wld.map), x, y))) {
2827 cmd_reply(CMD_DEBUG, caller, C_SYNTAX, _("Bad map coordinates."));
2828 goto cleanup;
2829 }
2830 pcity = tile_city(ptile);
2831 if (!pcity) {
2832 cmd_reply(CMD_DEBUG, caller, C_SYNTAX, _("No city at this coordinate."));
2833 goto cleanup;
2834 }
2835 if (pcity->server.debug) {
2836 pcity->server.debug = FALSE;
2837 cmd_reply(CMD_DEBUG, caller, C_OK, _("%s no longer debugged"),
2838 city_name_get(pcity));
2839 } else {
2840 pcity->server.debug = TRUE;
2841 CITY_LOG(LOG_NORMAL, pcity, "debugged");
2842 }
2843 } else if (ntokens > 0 && strcmp(arg[0], "units") == 0) {
2844 int x, y;
2845 struct tile *ptile;
2846
2847 if (ntokens != 3) {
2848 cmd_reply(CMD_DEBUG, caller, C_SYNTAX,
2849 _("Undefined argument. Usage:\n%s"),
2851 goto cleanup;
2852 }
2853 if (!str_to_int(arg[1], &x) || !str_to_int(arg[2], &y)) {
2854 cmd_reply(CMD_DEBUG, caller, C_SYNTAX, _("Value 2 & 3 must be integer."));
2855 goto cleanup;
2856 }
2857 if (!(ptile = map_pos_to_tile(&(wld.map), x, y))) {
2858 cmd_reply(CMD_DEBUG, caller, C_SYNTAX, _("Bad map coordinates."));
2859 goto cleanup;
2860 }
2861 unit_list_iterate(ptile->units, punit) {
2862 if (punit->server.debug) {
2864 cmd_reply(CMD_DEBUG, caller, C_OK, _("%s %s no longer debugged."),
2867 } else {
2869 UNIT_LOG(LOG_NORMAL, punit, "%s %s debugged.",
2872 }
2874 } else if (ntokens > 0 && strcmp(arg[0], "timing") == 0) {
2876 } else if (ntokens > 0 && strcmp(arg[0], "ferries") == 0) {
2879 cmd_reply(CMD_DEBUG, caller, C_OK, _("Ferry system is no longer "
2880 "in debug mode."));
2881 } else {
2883 cmd_reply(CMD_DEBUG, caller, C_OK, _("Ferry system in debug mode."));
2884 }
2885 } else if (ntokens > 0 && strcmp(arg[0], "unit") == 0) {
2886 int id;
2887 struct unit *punit;
2888
2889 if (ntokens != 2) {
2890 cmd_reply(CMD_DEBUG, caller, C_SYNTAX,
2891 _("Undefined argument. Usage:\n%s"),
2893 goto cleanup;
2894 }
2895 if (!str_to_int(arg[1], &id)) {
2896 cmd_reply(CMD_DEBUG, caller, C_SYNTAX, _("Value 2 must be integer."));
2897 goto cleanup;
2898 }
2899 if (!(punit = game_unit_by_number(id))) {
2900 cmd_reply(CMD_DEBUG, caller, C_SYNTAX, _("Unit %d does not exist."), id);
2901 goto cleanup;
2902 }
2903 if (punit->server.debug) {
2905 cmd_reply(CMD_DEBUG, caller, C_OK, _("%s %s no longer debugged."),
2908 } else {
2910 UNIT_LOG(LOG_NORMAL, punit, "%s %s debugged.",
2913 }
2914 } else {
2915 cmd_reply(CMD_DEBUG, caller, C_SYNTAX,
2916 _("Undefined argument. Usage:\n%s"),
2918 }
2919 cleanup:
2920 for (i = 0; i < ntokens; i++) {
2921 free(arg[i]);
2922 }
2923 return TRUE;
2924}
2925
2926/**********************************************************************/
2931 struct connection *caller,
2932 char *arg)
2933{
2934 int opt = lookup_option(arg);
2935
2936 if (opt < 0) {
2937 switch (opt) {
2940 cmd_reply(cmd, caller, C_SYNTAX, _("Option '%s' not recognized."), arg);
2941 break;
2943 cmd_reply(cmd, caller, C_SYNTAX, _("Ambiguous option name."));
2944 break;
2946 cmd_reply(cmd, caller, C_SYNTAX,
2947 /* TRANS: 'rulesetdir' is the command. Do not translate. */
2948 _("Use the '%srulesetdir' command to change the ruleset "
2949 "directory."), caller ? "/" : "");
2950 break;
2951 default:
2953 break;
2954 }
2955 return NULL;
2956 }
2957
2958 return setting_by_number(opt);
2959}
2960
2961/**********************************************************************/
2964static bool set_command(struct connection *caller, char *str, bool check)
2965{
2966 char *args[2];
2967 int val, nargs;
2968 struct setting *pset;
2969 bool do_update;
2970 char reject_msg[256] = "";
2971 bool ret = FALSE;
2972
2973 /* '=' is also a valid delimiter for this function. */
2974 nargs = get_tokens(str, args, ARRAY_SIZE(args), TOKEN_DELIMITERS "=");
2975
2976 if (nargs < 2) {
2977 cmd_reply(CMD_SET, caller, C_SYNTAX,
2978 _("Undefined argument. Usage:\n%s"),
2980 goto cleanup;
2981 }
2982
2983 pset = validate_setting_arg(CMD_SET, caller, args[0]);
2984
2985 if (!pset) {
2986 /* Reason already reported. */
2987 goto cleanup;
2988 }
2989
2990 if (!setting_is_changeable(pset, caller, reject_msg, sizeof(reject_msg))
2991 && !check) {
2992 cmd_reply(CMD_SET, caller, C_FAIL, "%s", reject_msg);
2993 goto cleanup;
2994 }
2995
2996 do_update = FALSE;
2997
2998 switch (setting_type(pset)) {
2999 case SST_BOOL:
3000 if (check) {
3001 if (!setting_is_changeable(pset, caller, reject_msg,
3002 sizeof(reject_msg))
3003 || (!setting_bool_validate(pset, args[1], caller,
3004 reject_msg, sizeof(reject_msg)))) {
3005 cmd_reply(CMD_SET, caller, C_FAIL, "%s", reject_msg);
3006 goto cleanup;
3007 }
3008 } else if (setting_bool_set(pset, args[1], caller,
3009 reject_msg, sizeof(reject_msg))) {
3010 do_update = TRUE;
3011 } else {
3012 cmd_reply(CMD_SET, caller, C_FAIL, "%s", reject_msg);
3013 goto cleanup;
3014 }
3015 break;
3016
3017 case SST_INT:
3018 if (!str_to_int(args[1], &val)) {
3019 cmd_reply(CMD_SET, caller, C_SYNTAX,
3020 _("The parameter %s should only contain +- and 0-9."),
3021 setting_name(pset));
3022 goto cleanup;
3023 }
3024 if (check) {
3025 if (!setting_is_changeable(pset, caller, reject_msg,
3026 sizeof(reject_msg))
3027 || !setting_int_validate(pset, val, caller, reject_msg,
3028 sizeof(reject_msg))) {
3029 cmd_reply(CMD_SET, caller, C_FAIL, "%s", reject_msg);
3030 goto cleanup;
3031 }
3032 } else {
3033 if (setting_int_set(pset, val, caller, reject_msg,
3034 sizeof(reject_msg))) {
3035 do_update = TRUE;
3036 } else {
3037 cmd_reply(CMD_SET, caller, C_FAIL, "%s", reject_msg);
3038 goto cleanup;
3039 }
3040 }
3041 break;
3042
3043 case SST_STRING:
3044 if (check) {
3045 if (!setting_is_changeable(pset, caller, reject_msg,
3046 sizeof(reject_msg))
3047 || !setting_str_validate(pset, args[1], caller, reject_msg,
3048 sizeof(reject_msg))) {
3049 cmd_reply(CMD_SET, caller, C_FAIL, "%s", reject_msg);
3050 goto cleanup;
3051 }
3052 } else {
3053 if (setting_str_set(pset, args[1], caller, reject_msg,
3054 sizeof(reject_msg))) {
3055 do_update = TRUE;
3056 } else {
3057 cmd_reply(CMD_SET, caller, C_FAIL, "%s", reject_msg);
3058 goto cleanup;
3059 }
3060 }
3061 break;
3062
3063 case SST_ENUM:
3064 if (check) {
3065 if (!setting_is_changeable(pset, caller, reject_msg,
3066 sizeof(reject_msg))
3067 || (!setting_enum_validate(pset, args[1], caller,
3068 reject_msg, sizeof(reject_msg)))) {
3069 cmd_reply(CMD_SET, caller, C_FAIL, "%s", reject_msg);
3070 goto cleanup;
3071 }
3072 } else if (setting_enum_set(pset, args[1], caller,
3073 reject_msg, sizeof(reject_msg))) {
3074 do_update = TRUE;
3075 } else {
3076 cmd_reply(CMD_SET, caller, C_FAIL, "%s", reject_msg);
3077 goto cleanup;
3078 }
3079 break;
3080
3081 case SST_BITWISE:
3082 if (check) {
3083 if (!setting_is_changeable(pset, caller, reject_msg,
3084 sizeof(reject_msg))
3085 || (!setting_bitwise_validate(pset, args[1], caller,
3086 reject_msg, sizeof(reject_msg)))) {
3087 cmd_reply(CMD_SET, caller, C_FAIL, "%s", reject_msg);
3088 goto cleanup;
3089 }
3090 } else if (setting_bitwise_set(pset, args[1], caller,
3091 reject_msg, sizeof(reject_msg))) {
3092 do_update = TRUE;
3093 } else {
3094 cmd_reply(CMD_SET, caller, C_FAIL, "%s", reject_msg);
3095 goto cleanup;
3096 }
3097 break;
3098
3099 case SST_COUNT:
3100 fc_assert(setting_type(pset) != SST_COUNT);
3101 goto cleanup;
3102 break;
3103 }
3104
3105 ret = TRUE; /* Looks like a success. */
3106
3107 if (!check && do_update) {
3108 /* Send only to connections able to see that. */
3109 char buf[256];
3110 struct packet_chat_msg packet;
3111
3112 package_event(&packet, NULL, E_SETTING, ftc_server,
3113 _("Console: '%s' has been set to %s."), setting_name(pset),
3114 setting_value_name(pset, TRUE, buf, sizeof(buf)));
3116 if (setting_is_visible(pset, pconn)) {
3117 send_packet_chat_msg(pconn, &packet);
3118 }
3120 /* Notify the console. */
3121 con_write(C_OK, "%s", packet.message);
3122
3123 setting_changed(pset);
3124 setting_action(pset);
3125 send_server_setting(NULL, pset);
3126 /*
3127 * send any modified game parameters to the clients -- if sent
3128 * before S_S_RUNNING, triggers a popdown_races_dialog() call
3129 * in client/packhand.c#handle_game_info()
3130 */
3131 send_game_info(NULL);
3134 }
3135
3136 cleanup:
3137 free_tokens(args, nargs);
3138 return ret;
3139}
3140
3141/**********************************************************************/
3148static bool is_allowed_to_take(struct connection *requester,
3149 struct connection *taker,
3150 struct player *pplayer, bool will_obs,
3151 char *msg, size_t msg_len)
3152{
3153 const char *allow;
3154
3155 if (!pplayer && !will_obs) {
3156 /* Auto-taking a new player */
3157
3158 if (game_was_started()) {
3159 fc_strlcpy(msg, _("You cannot take a new player at this time."),
3160 msg_len);
3161 return FALSE;
3162 }
3163
3165 fc_snprintf(msg, msg_len,
3166 /* TRANS: Do not translate "maxplayers". */
3167 PL_("You cannot take a new player because "
3168 "the maximum of %d player has already "
3169 "been reached (maxplayers setting).",
3170 "You cannot take a new player because "
3171 "the maximum of %d players has already "
3172 "been reached (maxplayers setting).",
3175 return FALSE;
3176 }
3177
3178 if (player_count() >= player_slot_count()) {
3179 fc_strlcpy(msg, _("You cannot take a new player because there "
3180 "are no free player slots."),
3181 msg_len);
3182 return FALSE;
3183 }
3184
3185 return TRUE;
3186
3187 }
3188
3189#ifdef HAVE_FCDB
3190 if (srvarg.fcdb_enabled) {
3191 bool ok = FALSE;
3192
3193 if (script_fcdb_call("user_take", requester, taker, pplayer, will_obs,
3194 &ok) && ok) {
3195 return TRUE;
3196 }
3197 }
3198#endif
3199
3200 if (!pplayer && will_obs) {
3201 /* Global observer. */
3202 if (!(allow = strchr(game.server.allow_take,
3203 (game.info.is_new_game ? 'O' : 'o')))) {
3204 fc_strlcpy(msg, _("Sorry, one can't observe globally in this game."),
3205 msg_len);
3206 return FALSE;
3207 }
3208 } else if (is_barbarian(pplayer)) {
3209 if (!(allow = strchr(game.server.allow_take, 'b'))) {
3210 if (will_obs) {
3211 fc_strlcpy(msg,
3212 _("Sorry, one can't observe barbarians in this game."),
3213 msg_len);
3214 } else {
3215 fc_strlcpy(msg, _("Sorry, one can't take barbarians in this game."),
3216 msg_len);
3217 }
3218 return FALSE;
3219 }
3220 } else if (!pplayer->is_alive) {
3221 if (!(allow = strchr(game.server.allow_take, 'd'))) {
3222 if (will_obs) {
3223 fc_strlcpy(msg,
3224 _("Sorry, one can't observe dead players in this game."),
3225 msg_len);
3226 } else {
3227 fc_strlcpy(msg,
3228 _("Sorry, one can't take dead players in this game."),
3229 msg_len);
3230 }
3231 return FALSE;
3232 }
3233 } else if (is_ai(pplayer)) {
3234 if (!(allow = strchr(game.server.allow_take,
3235 (game.info.is_new_game ? 'A' : 'a')))) {
3236 if (will_obs) {
3237 fc_strlcpy(msg,
3238 _("Sorry, one can't observe AI players in this game."),
3239 msg_len);
3240 } else {
3241 fc_strlcpy(msg, _("Sorry, one can't take AI players in this game."),
3242 msg_len);
3243 }
3244 return FALSE;
3245 }
3246 } else {
3247 if (!(allow = strchr(game.server.allow_take,
3248 (game.info.is_new_game ? 'H' : 'h')))) {
3249 if (will_obs) {
3250 fc_strlcpy(msg,
3251 _("Sorry, one can't observe human players in this game."),
3252 msg_len);
3253 } else {
3254 fc_strlcpy(msg,
3255 _("Sorry, one can't take human players in this game."),
3256 msg_len);
3257 }
3258 return FALSE;
3259 }
3260 }
3261
3262 allow++;
3263
3264 if (will_obs && (*allow == '2' || *allow == '3')) {
3265 fc_strlcpy(msg, _("Sorry, one can't observe in this game."), msg_len);
3266 return FALSE;
3267 }
3268
3269 if (!will_obs && *allow == '4') {
3270 fc_strlcpy(msg, _("Sorry, one can't take players in this game."),
3271 MAX_LEN_MSG);
3272 return FALSE;
3273 }
3274
3275 if (!will_obs && pplayer->is_connected
3276 && (*allow == '1' || *allow == '3')) {
3277 fc_strlcpy(msg, _("Sorry, one can't take players already "
3278 "connected in this game."), msg_len);
3279 return FALSE;
3280 }
3281
3282 return TRUE;
3283}
3284
3285/**********************************************************************/
3290static bool observe_command(struct connection *caller, char *str, bool check)
3291{
3292 int i = 0, ntokens = 0;
3293 char buf[MAX_LEN_CONSOLE_LINE], *arg[2], msg[MAX_LEN_MSG];
3294 bool is_newgame = !game_was_started();
3295 enum m_pre_result result;
3296 struct connection *pconn = NULL;
3297 struct player *pplayer = NULL;
3298 bool res = FALSE;
3299
3300 /******** PART I: fill pconn and pplayer ********/
3301
3302 sz_strlcpy(buf, str);
3303 ntokens = get_tokens(buf, arg, 2, TOKEN_DELIMITERS);
3304
3305 /* check syntax, only certain syntax if allowed depending on the caller */
3306 if (!caller && ntokens < 1) {
3307 cmd_reply(CMD_OBSERVE, caller, C_SYNTAX, _("Usage:\n%s"),
3309 goto end;
3310 }
3311
3312 if (ntokens == 2 && (caller && caller->access_level != ALLOW_HACK)) {
3314 _("Only the player name form is allowed."));
3315 goto end;
3316 }
3317
3318 /* match connection if we're console, match a player if we're not */
3319 if (ntokens == 1) {
3320 if (!caller && !(pconn = conn_by_user_prefix(arg[0], &result))) {
3321 cmd_reply_no_such_conn(CMD_OBSERVE, caller, arg[0], result);
3322 goto end;
3323 } else if (caller
3324 && !(pplayer = player_by_name_prefix(arg[0], &result))) {
3325 cmd_reply_no_such_player(CMD_OBSERVE, caller, arg[0], result);
3326 goto end;
3327 }
3328 }
3329
3330 /* get connection name then player name */
3331 if (ntokens == 2) {
3332 if (!(pconn = conn_by_user_prefix(arg[0], &result))) {
3333 cmd_reply_no_such_conn(CMD_OBSERVE, caller, arg[0], result);
3334 goto end;
3335 }
3336 if (!(pplayer = player_by_name_prefix(arg[1], &result))) {
3337 cmd_reply_no_such_player(CMD_OBSERVE, caller, arg[1], result);
3338 goto end;
3339 }
3340 }
3341
3342 /* if we can't force other connections to observe, assign us to be pconn. */
3343 if (!pconn) {
3344 pconn = caller;
3345 }
3346
3347 /* if we have no pplayer, it means that we want to be a global observer */
3348
3349 /******** PART II: do the observing ********/
3350
3351 /* check allowtake for permission */
3352 if (!is_allowed_to_take(caller, pconn, pplayer, TRUE, msg, sizeof(msg))) {
3353 cmd_reply(CMD_OBSERVE, caller, C_FAIL, "%s", msg);
3354 goto end;
3355 }
3356
3357 /* observing your own player (during pregame) makes no sense. */
3358 if (NULL != pplayer
3359 && pplayer == pconn->playing
3360 && !pconn->observer
3361 && is_newgame
3362 && !pplayer->was_created) {
3363 cmd_reply(CMD_OBSERVE, caller, C_FAIL,
3364 _("%s already controls %s. Using 'observe' would remove %s"),
3365 pconn->username,
3366 player_name(pplayer),
3367 player_name(pplayer));
3368 goto end;
3369 }
3370
3371 /* attempting to observe a player you're already observing should fail. */
3372 if (pplayer == pconn->playing && pconn->observer) {
3373 if (pplayer) {
3374 cmd_reply(CMD_OBSERVE, caller, C_FAIL,
3375 _("%s is already observing %s."),
3376 pconn->username,
3377 player_name(pplayer));
3378 } else {
3379 cmd_reply(CMD_OBSERVE, caller, C_FAIL,
3380 _("%s is already observing."),
3381 pconn->username);
3382 }
3383 goto end;
3384 }
3385
3386 res = TRUE; /* all tests passed */
3387 if (check) {
3388 goto end;
3389 }
3390
3391 /* if the connection is already attached to a player,
3392 * unattach and cleanup old player (rename, remove, etc) */
3393 if (TRUE) {
3394 char name[MAX_LEN_NAME];
3395
3396 if (pplayer) {
3397 /* if pconn->playing is removed, we'll lose pplayer */
3398 sz_strlcpy(name, player_name(pplayer));
3399 }
3400
3401 connection_detach(pconn, TRUE);
3402
3403 if (pplayer) {
3404 /* find pplayer again, the pointer might have been changed */
3405 pplayer = player_by_name(name);
3406 }
3407 }
3408
3409 /* attach pconn to new player as an observer or as global observer */
3410 if ((res = connection_attach(pconn, pplayer, TRUE))) {
3411 if (pplayer) {
3412 cmd_reply(CMD_OBSERVE, caller, C_OK, _("%s now observes %s"),
3413 pconn->username,
3414 player_name(pplayer));
3415 } else {
3416 cmd_reply(CMD_OBSERVE, caller, C_OK, _("%s now observes"),
3417 pconn->username);
3418 }
3419 }
3420
3421 end:;
3422 /* free our args */
3423 for (i = 0; i < ntokens; i++) {
3424 free(arg[i]);
3425 }
3426 return res;
3427}
3428
3429/**********************************************************************/
3438static bool take_command(struct connection *caller, char *str, bool check)
3439{
3440 int i = 0, ntokens = 0;
3441 char buf[MAX_LEN_CONSOLE_LINE], *arg[2], msg[MAX_LEN_MSG];
3442 bool is_newgame = !game_was_started();
3443 enum m_pre_result match_result;
3444 struct connection *pconn = caller;
3445 struct player *pplayer = NULL;
3446 bool res = FALSE;
3447
3448 /******** PART I: fill pconn and pplayer ********/
3449
3450 sz_strlcpy(buf, str);
3451 ntokens = get_tokens(buf, arg, 2, TOKEN_DELIMITERS);
3452
3453 /* check syntax */
3454 if (!caller && ntokens != 2) {
3455 cmd_reply(CMD_TAKE, caller, C_SYNTAX, _("Usage:\n%s"),
3457 goto end;
3458 }
3459
3460 if (caller && caller->access_level != ALLOW_HACK && ntokens != 1) {
3461 cmd_reply(CMD_TAKE, caller, C_SYNTAX,
3462 _("Only the player name form is allowed."));
3463 goto end;
3464 }
3465
3466 if (ntokens == 0) {
3467 cmd_reply(CMD_TAKE, caller, C_SYNTAX, _("Usage:\n%s"),
3469 goto end;
3470 }
3471
3472 if (ntokens == 2) {
3473 if (!(pconn = conn_by_user_prefix(arg[i], &match_result))) {
3474 cmd_reply_no_such_conn(CMD_TAKE, caller, arg[i], match_result);
3475 goto end;
3476 }
3477 i++; /* found a conn, now reference the second argument */
3478 }
3479
3480 if (strcmp(arg[i], "-") == 0) {
3481 if (!is_newgame) {
3482 cmd_reply(CMD_TAKE, caller, C_FAIL,
3483 _("You cannot issue \"/take -\" when "
3484 "the game has already started."));
3485 goto end;
3486 }
3487
3488 /* Find first uncontrolled player. This will return NULL if there is
3489 * no free players at the moment. Later call to
3490 * connection_attach() will create new player for such NULL
3491 * cases. */
3492 pplayer = find_uncontrolled_player();
3493 if (pplayer) {
3494 /* Make it human! */
3495 set_as_human(pplayer);
3496 }
3497 } else if (!(pplayer = player_by_name_prefix(arg[i], &match_result))) {
3498 cmd_reply_no_such_player(CMD_TAKE, caller, arg[i], match_result);
3499 goto end;
3500 }
3501
3502 /******** PART II: do the attaching ********/
3503
3504 /* Take not possible if the player is involved in a delegation (either
3505 * it's being controlled, or it's been put aside by the delegate). */
3506 if (player_delegation_active(pplayer)) {
3507 cmd_reply(CMD_TAKE, caller, C_FAIL, _("A delegation is active for player "
3508 "'%s'. /take not possible."),
3509 player_name(pplayer));
3510 goto end;
3511 }
3512
3513 /* check allowtake for permission */
3514 if (!is_allowed_to_take(caller, pconn, pplayer, FALSE, msg, sizeof(msg))) {
3515 cmd_reply(CMD_TAKE, caller, C_FAIL, "%s", msg);
3516 goto end;
3517 }
3518
3519 /* taking your own player makes no sense. */
3520 if ((NULL != pplayer && !pconn->observer && pplayer == pconn->playing)
3521 || (NULL == pplayer && !pconn->observer && NULL != pconn->playing)) {
3522 cmd_reply(CMD_TAKE, caller, C_FAIL, _("%s already controls %s."),
3523 pconn->username,
3524 player_name(pconn->playing));
3525 goto end;
3526 }
3527
3528 /* Make sure there is free player slot if there is need to
3529 * create new player. This is necessary for previously
3530 * detached connections only. Others can reuse the slot
3531 * they first release. */
3532 if (!pplayer && !pconn->playing
3534 || normal_player_count() >= server.playable_nations)) {
3535 cmd_reply(CMD_TAKE, caller, C_FAIL,
3536 _("There is no free player slot for %s."),
3537 pconn->username);
3538 goto end;
3539 }
3541
3542 res = TRUE;
3543 if (check) {
3544 goto end;
3545 }
3546
3547 /* If the player is controlled by another user, forcibly detach
3548 * the user. */
3549 if (pplayer && pplayer->is_connected) {
3550 if (NULL == caller) {
3551 notify_conn(NULL, NULL, E_CONNECTION, ftc_server,
3552 _("Reassigned nation to %s by server console."),
3553 pconn->username);
3554 } else {
3555 notify_conn(NULL, NULL, E_CONNECTION, ftc_server,
3556 _("Reassigned nation to %s by %s."),
3557 pconn->username,
3558 caller->username);
3559 }
3560
3561 /* We are reassigning this nation, so we need to detach the current
3562 * user to set a new one. */
3563 conn_list_iterate(pplayer->connections, aconn) {
3564 if (!aconn->observer) {
3565 connection_detach(aconn, FALSE);
3566 }
3568 }
3569
3570 /* if the connection is already attached to another player,
3571 * unattach and cleanup old player (rename, remove, etc)
3572 * We may have been observing the player we now want to take */
3573 if (NULL != pconn->playing || pconn->observer) {
3574 char name[MAX_LEN_NAME];
3575
3576 if (pplayer) {
3577 /* if pconn->playing is removed, we'll lose pplayer */
3578 sz_strlcpy(name, player_name(pplayer));
3579 }
3580
3581 connection_detach(pconn, TRUE);
3582
3583 if (pplayer) {
3584 /* find pplayer again; the pointer might have been changed */
3585 pplayer = player_by_name(name);
3586 }
3587 }
3588
3589 /* Now attach to new player */
3590 if ((res = connection_attach(pconn, pplayer, FALSE))) {
3591 /* Successfully attached */
3592 pplayer = pconn->playing; /* In case pplayer was NULL. */
3593
3594 /* inform about the status before changes */
3595 cmd_reply(CMD_TAKE, caller, C_OK, _("%s now controls %s (%s, %s)."),
3596 pconn->username,
3597 player_name(pplayer),
3598 is_barbarian(pplayer)
3599 ? _("Barbarian")
3600 : is_ai(pplayer)
3601 ? _("AI")
3602 : _("Human"),
3603 pplayer->is_alive
3604 ? _("Alive")
3605 : _("Dead"));
3606 } else {
3607 cmd_reply(CMD_TAKE, caller, C_FAIL,
3608 _("%s failed to attach to any player."),
3609 pconn->username);
3610 }
3611
3612 end:;
3613 /* free our args */
3614 for (i = 0; i < ntokens; i++) {
3615 free(arg[i]);
3616 }
3617 return res;
3618}
3619
3620/**********************************************************************/
3627static bool detach_command(struct connection *caller, char *str, bool check)
3628{
3629 int i = 0, ntokens = 0;
3630 char buf[MAX_LEN_CONSOLE_LINE], *arg[1];
3631 enum m_pre_result match_result;
3632 struct connection *pconn = NULL;
3633 struct player *pplayer = NULL;
3634 bool res = FALSE;
3635
3636 sz_strlcpy(buf, str);
3637 ntokens = get_tokens(buf, arg, 1, TOKEN_DELIMITERS);
3638
3639 if (!caller && ntokens == 0) {
3640 cmd_reply(CMD_DETACH, caller, C_SYNTAX, _("Usage:\n%s"),
3642 goto end;
3643 }
3644
3645 /* match the connection if the argument was given */
3646 if (ntokens == 1
3647 && !(pconn = conn_by_user_prefix(arg[0], &match_result))) {
3648 cmd_reply_no_such_conn(CMD_DETACH, caller, arg[0], match_result);
3649 goto end;
3650 }
3651
3652 /* if no argument is given, the caller wants to detach themself */
3653 if (!pconn) {
3654 pconn = caller;
3655 }
3656
3657 /* if pconn and caller are not the same, only continue
3658 * if we're console, or we have ALLOW_HACK */
3659 if (pconn != caller && caller && caller->access_level != ALLOW_HACK) {
3660 cmd_reply(CMD_DETACH, caller, C_FAIL,
3661 _("You can not detach other users."));
3662 goto end;
3663 }
3664
3665 pplayer = pconn->playing;
3666
3667 /* must have someone to detach from... */
3668 if (!pplayer && !pconn->observer) {
3669 cmd_reply(CMD_DETACH, caller, C_FAIL,
3670 _("%s is not attached to any player."), pconn->username);
3671 goto end;
3672 }
3673
3674 res = TRUE;
3675 if (check) {
3676 goto end;
3677 }
3678
3679 if (pplayer) {
3680 cmd_reply(CMD_DETACH, caller, C_OK, _("%s detaching from %s"),
3681 pconn->username, player_name(pplayer));
3682 } else {
3683 cmd_reply(CMD_DETACH, caller, C_OK, _("%s no longer observing."),
3684 pconn->username);
3685 }
3686
3687 /* Actually do the detaching. */
3688 connection_detach(pconn, TRUE);
3689
3690 /* The user explicitly wanted to detach, so if a player is marked for
3691 * them, reset its username. */
3692 players_iterate(aplayer) {
3693 if (0 == strncmp(aplayer->username, pconn->username, MAX_LEN_NAME)) {
3694 sz_strlcpy(aplayer->username, _(ANON_USER_NAME));
3695 aplayer->unassigned_user = TRUE;
3696 send_player_info_c(aplayer, NULL);
3697 }
3699
3701
3702 end:
3703 fc_assert_ret_val(ntokens <= 1, FALSE);
3704
3705 /* free our args */
3706 for (i = 0; i < ntokens; i++) {
3707 free(arg[i]);
3708 }
3709 return res;
3710}
3711
3712/**********************************************************************/
3733bool load_command(struct connection *caller, const char *filename, bool check,
3734 bool cmdline_load)
3735{
3736 struct timer *loadtimer, *uloadtimer;
3737 struct section_file *file;
3738 char arg[MAX_LEN_PATH];
3739 struct conn_list *global_observers;
3740
3741 if (!filename || filename[0] == '\0') {
3742 cmd_reply(CMD_LOAD, caller, C_FAIL, _("Usage:\n%s"),
3744 return FALSE;
3745 }
3746 if (S_S_INITIAL != server_state()) {
3747 cmd_reply(CMD_LOAD, caller, C_FAIL,
3748 _("Cannot load a game while another is running."));
3749 return FALSE;
3750 }
3751 if (!is_safe_filename(filename) && is_restricted(caller)) {
3752 cmd_reply(CMD_LOAD, caller, C_FAIL,
3753 _("Name \"%s\" disallowed for security reasons."),
3754 filename);
3755 return FALSE;
3756 }
3757
3758 {
3759 /* It is a normal savegame or maybe a scenario */
3760 char testfile[MAX_LEN_PATH];
3761 const struct strvec *paths[] = {
3763 };
3764 const char *exts[] = {
3765 "sav", "gz", "bz2", "xz", "sav.gz", "sav.bz2", "sav.xz", "sav.zst", NULL
3766 };
3767 const char **ext, *found = NULL;
3768 const struct strvec **path;
3769
3770 if (cmdline_load) {
3771 /* Allow plain names being loaded with '--file' option, but not otherwise
3772 * (no loading of arbitrary files by unauthorized users)
3773 * Iterate through ALL paths to check for file with plain name before
3774 * looking any path with an extension, i.e., prefer plain name file
3775 * in later directory over file with extension in name in earlier
3776 * directory. */
3777 for (path = paths; !found && *path; path++) {
3778 found = fileinfoname(*path, filename);
3779 if (found != NULL) {
3780 sz_strlcpy(arg, found);
3781 }
3782 }
3783 }
3784
3785 for (path = paths; !found && *path; path++) {
3786 for (ext = exts; !found && *ext; ext++) {
3787 fc_snprintf(testfile, sizeof(testfile), "%s.%s", filename, *ext);
3788 found = fileinfoname(*path, testfile);
3789 if (found != NULL) {
3790 sz_strlcpy(arg, found);
3791 }
3792 }
3793 }
3794
3795 if (is_restricted(caller) && !found) {
3796 cmd_reply(CMD_LOAD, caller, C_FAIL, _("Cannot find savegame or "
3797 "scenario with the name \"%s\"."), filename);
3798 return FALSE;
3799 }
3800
3801 if (!found) {
3802 sz_strlcpy(arg, filename);
3803 }
3804 }
3805
3806 /* attempt to parse the file */
3807
3808 if (!(file = secfile_load(arg, FALSE))) {
3809 log_error("Error loading savefile '%s': %s", arg, secfile_error());
3810 cmd_reply(CMD_LOAD, caller, C_FAIL, _("Could not load savefile: %s"),
3811 arg);
3813 return FALSE;
3814 }
3815
3816 if (check) {
3817 return TRUE;
3818 }
3819
3820 /* Detach current players, before we blow them away. */
3821 global_observers = conn_list_new();
3823 if (pconn->playing != NULL) {
3824 connection_detach(pconn, TRUE);
3825 } else if (pconn->observer) {
3826 conn_list_append(global_observers, pconn);
3827 connection_detach(pconn, TRUE);
3828 }
3830
3832
3833 /* Now free all game data. */
3835
3836 /* Keep old ruleset value. Scenario file will either use the old value,
3837 * or to initialize new value itself. */
3839
3840 loadtimer = timer_new(TIMER_CPU, TIMER_ACTIVE);
3841 timer_start(loadtimer);
3842 uloadtimer = timer_new(TIMER_USER, TIMER_ACTIVE);
3843 timer_start(uloadtimer);
3844
3846
3847 savegame_load(file);
3849 secfile_destroy(file);
3850
3851 log_verbose("Load time: %g seconds (%g apparent)",
3852 timer_read_seconds(loadtimer), timer_read_seconds(uloadtimer));
3853 timer_destroy(loadtimer);
3854 timer_destroy(uloadtimer);
3855
3856 sanity_check();
3857
3858 log_verbose("load_command() does send_rulesets()");
3866
3867 /* Send information about the new players. */
3869 send_player_diplstate_c(NULL, NULL);
3870
3871 /* Everything seemed to load ok; spread the good news. */
3873
3874 /* Attach connections to players. Currently, this applies only
3875 * to connections that have the same username as a player. */
3877 players_iterate(pplayer) {
3878 if (strcmp(pconn->username, pplayer->username) == 0) {
3879 connection_attach(pconn, pplayer, FALSE);
3880 break;
3881 }
3884
3885 /* Reattach global observers. */
3886 conn_list_iterate(global_observers, pconn) {
3887 if (NULL == pconn->playing) {
3888 /* May have been assigned to a player before. */
3889 connection_attach(pconn, NULL, TRUE);
3890 }
3892 conn_list_destroy(global_observers);
3893
3894 (void) aifill(game.info.aifill);
3895
3896 achievements_iterate(pach) {
3897 players_iterate(pplayer) {
3898 struct packet_achievement_info pack;
3899
3900 pack.id = achievement_index(pach);
3901 pack.gained = achievement_player_has(pach, pplayer);
3902 pack.first = (pach->first == pplayer);
3903
3904 lsend_packet_achievement_info(pplayer->connections, &pack);
3907
3908 return TRUE;
3909}
3910
3911/**********************************************************************/
3920static bool set_rulesetdir(struct connection *caller, char *str, bool check,
3921 int read_recursion)
3922{
3923 char filename[512];
3924 const char *pfilename;
3925
3926 if (NULL == str || '\0' == str[0]) {
3928 _("You must provide a ruleset name. Use \"/show ruleset\" to "
3929 "see what is the current ruleset."));
3930 return FALSE;
3931 }
3932 if (game_was_started() || !map_is_empty()) {
3934 _("This setting can't be modified after the game has started."));
3936 && !game_was_started()) {
3938 /* TRANS: scenario name */
3939 _("The ruleset of \"%s\" can be changed by switching to a"
3940 " compatible ruleset before loading it."),
3942 }
3943 return FALSE;
3944 }
3945
3946 if (strcmp(str, game.server.rulesetdir) == 0) {
3948 _("Ruleset directory is already \"%s\""), str);
3949 return FALSE;
3950 }
3951
3952 if (is_restricted(caller)
3953 && (!is_safe_filename(str) || strchr(str, '.'))) {
3955 _("Name \"%s\" disallowed for security reasons."),
3956 str);
3957 return FALSE;
3958 }
3959
3960 fc_snprintf(filename, sizeof(filename), "%s", str);
3961 pfilename = fileinfoname(get_data_dirs(), filename);
3962 if (!pfilename) {
3964 _("Ruleset directory \"%s\" not found"), str);
3965 return FALSE;
3966 }
3967
3968 if (!check) {
3969 bool success = TRUE;
3970 char old[512];
3971
3973 log_verbose("set_rulesetdir() does load_rulesets() with \"%s\"", str);
3975
3976 /* load the ruleset (and game settings defined in the ruleset) */
3978 if (!load_rulesets(old, NULL, FALSE, NULL, TRUE, FALSE, TRUE)) {
3979 success = FALSE;
3980
3981 /* While loading of the requested ruleset failed, we might
3982 * have changed ruleset from third one to default. Handle
3983 * rest of the ruleset changing accordingly. */
3984 }
3985
3986 if (game.est_connections) {
3987 /* Now that the rulesets are loaded we immediately send updates to any
3988 * connected clients. */
3990 }
3991 /* show ruleset summary and list changed values */
3992 show_ruleset_info(caller, CMD_RULESETDIR, check, read_recursion);
3994
3995 if (success) {
3996 cmd_reply(CMD_RULESETDIR, caller, C_OK,
3997 _("Ruleset directory set to \"%s\""), str);
3998 } else {
4000 _("Failed loading rulesets from directory \"%s\", using \"%s\""),
4002 }
4003
4004 return success;
4005 }
4006
4007 return TRUE;
4008}
4009
4010/**********************************************************************/
4013static bool ignore_command(struct connection *caller, char *str, bool check)
4014{
4015 char buf[128];
4016 struct conn_pattern *ppattern;
4017
4018 if (NULL == caller) {
4019 cmd_reply(CMD_IGNORE, caller, C_FAIL,
4020 _("That would be rather silly, since you are not a player."));
4021 return FALSE;
4022 }
4023
4024 ppattern = conn_pattern_from_string(str, CPT_USER, buf, sizeof(buf));
4025 if (NULL == ppattern) {
4026 cmd_reply(CMD_IGNORE, caller, C_SYNTAX,
4027 _("%s. Try /help ignore"), buf);
4028 return FALSE;
4029 }
4030
4031 if (check) {
4032 conn_pattern_destroy(ppattern);
4033 return TRUE;
4034 }
4035
4036 conn_pattern_to_string(ppattern, buf, sizeof(buf));
4037 conn_pattern_list_append(caller->server.ignore_list, ppattern);
4039 _("Added pattern %s as entry %d to your ignore list."),
4040 buf, conn_pattern_list_size(caller->server.ignore_list));
4041
4042 return TRUE;
4043}
4044
4045/**********************************************************************/
4048static bool unignore_command(struct connection *caller,
4049 char *str, bool check)
4050{
4051 char buf[128], *c;
4052 int first, last, n;
4053
4054 if (!caller) {
4055 cmd_reply(CMD_IGNORE, caller, C_FAIL,
4056 _("That would be rather silly, since you are not a player."));
4057 return FALSE;
4058 }
4059
4060 sz_strlcpy(buf, str);
4062
4063 n = conn_pattern_list_size(caller->server.ignore_list);
4064 if (n == 0) {
4065 cmd_reply(CMD_UNIGNORE, caller, C_FAIL, _("Your ignore list is empty."));
4066 return FALSE;
4067 }
4068
4069 /* Parse the range. */
4070 if ('\0' == buf[0]) {
4072 _("Missing range. Try /help unignore."));
4073 return FALSE;
4074 } else if ((c = strchr(buf, '-'))) {
4075 *c++ = '\0';
4076 if ('\0' == buf[0]) {
4077 first = 1;
4078 } else if (!str_to_int(buf, &first)) {
4079 *--c = '-';
4081 _("\"%s\" is not a valid range. Try /help unignore."), buf);
4082 return FALSE;
4083 }
4084 if ('\0' == *c) {
4085 last = n;
4086 } else if (!str_to_int(c, &last)) {
4087 *--c = '-';
4089 _("\"%s\" is not a valid range. Try /help unignore."), buf);
4090 return FALSE;
4091 }
4092 } else {
4093 if (!str_to_int(buf, &first)) {
4095 _("\"%s\" is not a valid range. Try /help unignore."), buf);
4096 return FALSE;
4097 }
4098 last = first;
4099 }
4100
4101 if (!(1 <= first && first <= last && last <= n)) {
4102 if (first == last) {
4103 cmd_reply(CMD_UNIGNORE, caller, C_FAIL,
4104 _("Invalid entry number: %d."), first);
4105 } else {
4106 cmd_reply(CMD_UNIGNORE, caller, C_FAIL,
4107 _("Invalid range: %d to %d."), first, last);
4108 }
4109 return FALSE;
4110 }
4111
4112 if (check) {
4113 return TRUE;
4114 }
4115
4116 n = 1;
4117 conn_pattern_list_iterate(caller->server.ignore_list, ppattern) {
4118 if (first <= n) {
4119 conn_pattern_to_string(ppattern, buf, sizeof(buf));
4121 _("Removed pattern %s (entry %d) from your ignore list."),
4122 buf, n);
4123 conn_pattern_list_remove(caller->server.ignore_list, ppattern);
4124 }
4125 n++;
4126 if (n > last) {
4127 break;
4128 }
4130
4131 return TRUE;
4132}
4133
4134/**********************************************************************/
4137static bool playercolor_command(struct connection *caller,
4138 char *str, bool check)
4139{
4140 enum m_pre_result match_result;
4141 struct player *pplayer;
4142 struct rgbcolor *prgbcolor = NULL;
4143 int ntokens = 0;
4144 char *token[2];
4145 bool ret = TRUE;
4146
4147 ntokens = get_tokens(str, token, 2, TOKEN_DELIMITERS);
4148
4149 if (ntokens != 2) {
4151 _("Two arguments needed. See '/help playercolor'."));
4152 ret = FALSE;
4153 goto cleanup;
4154 }
4155
4156 pplayer = player_by_name_prefix(token[0], &match_result);
4157
4158 if (!pplayer) {
4159 cmd_reply_no_such_player(CMD_PLAYERCOLOR, caller, token[0], match_result);
4160 ret = FALSE;
4161 goto cleanup;
4162 }
4163
4164 {
4165 const char *reason;
4166 if (!player_color_changeable(pplayer, &reason)) {
4167 cmd_reply(CMD_PLAYERCOLOR, caller, C_FAIL, "%s", reason);
4168 ret = FALSE;
4169 goto cleanup;
4170 }
4171 }
4172
4173 if (0 == fc_strcasecmp(token[1], "reset")) {
4174 if (!game_was_started()) {
4175 prgbcolor = NULL;
4176 } else {
4178 _("Can only unset player color before game starts."));
4179 ret = FALSE;
4180 goto cleanup;
4181 }
4182 } else if (!rgbcolor_from_hex(&prgbcolor, token[1])) {
4184 _("Invalid player color definition. See '/help playercolor'."));
4185 ret = FALSE;
4186 goto cleanup;
4187 }
4188
4189 if (prgbcolor != NULL) {
4190 players_iterate(pother) {
4191 if (pother != pplayer && pother->rgb != NULL
4192 && rgbcolors_are_equal(pother->rgb, prgbcolor)) {
4194 /* TRANS: "... [c0ffee] for Caesar ... to Hammurabi." */
4195 _("Warning: new color [%s] for %s is identical to %s."),
4196 player_color_ftstr(pother), player_name(pplayer),
4197 player_name(pother));
4198 }
4200 }
4201
4202 if (check) {
4203 goto cleanup;
4204 }
4205
4206 server_player_set_color(pplayer, prgbcolor);
4208 _("Color of player %s set to [%s]."), player_name(pplayer),
4209 player_color_ftstr(pplayer));
4210
4211 cleanup:
4212
4213 rgbcolor_destroy(prgbcolor);
4214 free_tokens(token, ntokens);
4215
4216 return ret;
4217}
4218
4219/**********************************************************************/
4222static bool playernation_command(struct connection *caller,
4223 char *str, bool check)
4224{
4225 enum m_pre_result match_result;
4226 struct player *pplayer;
4227 struct nation_type *pnation;
4228 struct nation_style *pstyle;
4229 bool is_male = FALSE;
4230 int ntokens = 0;
4231 char *token[5];
4232
4233 ntokens = get_tokens(str, token, 5, TOKEN_DELIMITERS);
4234
4235 if (ntokens == 0) {
4237 _("At least one argument needed. See '/help playernation'."));
4238 free_tokens(token, ntokens);
4239 return FALSE;
4240 }
4241
4242 if (game_was_started()) {
4244 _("Can only set player nation before game starts."));
4245 free_tokens(token, ntokens);
4246 return FALSE;
4247 }
4248
4249 pplayer = player_by_name_prefix(token[0], &match_result);
4250 if (!pplayer) {
4251 cmd_reply_no_such_player(CMD_PLAYERNATION, caller, token[0], match_result);
4252 free_tokens(token, ntokens);
4253 return FALSE;
4254 }
4255
4256 if (ntokens == 1) {
4257 if (!check) {
4259
4261 _("Nation of player %s reset."), player_name(pplayer));
4263 }
4264 } else {
4265 pnation = nation_by_rule_name(token[1]);
4266 if (pnation == NO_NATION_SELECTED) {
4268 _("Unrecognized nation: %s."), token[1]);
4269 free_tokens(token, ntokens);
4270 return FALSE;
4271 }
4272
4273 if (!client_can_pick_nation(pnation)) {
4275 _("%s nation is not available for user selection."),
4276 token[1]);
4277 free_tokens(token, ntokens);
4278 return FALSE;
4279 }
4280
4281 if (pnation->player && pnation->player != pplayer) {
4283 _("%s nation is already in use."), token[1]);
4284 free_tokens(token, ntokens);
4285 return FALSE;
4286 }
4287
4288 if (ntokens < 3) {
4290 /* TRANS: Nation resetting form of /playernation does not require sex */
4291 _("Leader sex must be given when setting nation."));
4292 free_tokens(token, ntokens);
4293 return FALSE;
4294 }
4295
4296 if (!strcmp(token[2], "0")) {
4297 is_male = FALSE;
4298 } else if (!strcmp(token[2], "1")) {
4299 is_male = TRUE;
4300 } else {
4302 _("Unrecognized gender: %s, expecting 1 or 0."), token[2]);
4303 free_tokens(token, ntokens);
4304 return FALSE;
4305 }
4306
4307 if (ntokens > 4) {
4308 pstyle = style_by_rule_name(token[4]);
4309 if (!pstyle) {
4311 _("Unrecognized style: %s."), token[4]);
4312 free_tokens(token, ntokens);
4313 return FALSE;
4314 }
4315 } else {
4316 pstyle = style_of_nation(pnation);
4317 }
4318
4319 if (!check) {
4320 char error_buf[256];
4321
4322 player_set_nation(pplayer, pnation);
4323 pplayer->style = pstyle;
4324 pplayer->is_male = is_male;
4325
4326 if (ntokens > 3) {
4327 if (!server_player_set_name_full(caller, pplayer, pnation, token[3],
4328 error_buf, sizeof(error_buf))) {
4329 cmd_reply(CMD_PLAYERNATION, caller, C_WARNING, "%s", error_buf);
4330 }
4331 } else {
4332 server_player_set_name(pplayer, token[0]);
4333 }
4335 _("Nation of player %s set to [%s]."), player_name(pplayer),
4336 nation_rule_name(pnation));
4338 }
4339 }
4340
4341 free_tokens(token, ntokens);
4342
4343 return TRUE;
4344}
4345
4346/**************************************************************************
4347 Handle quit command
4348**************************************************************************/
4349static bool quit_game(struct connection *caller, bool check)
4350{
4351 if (!check) {
4352 cmd_reply(CMD_QUIT, caller, C_OK, _("Goodbye."));
4353 server_quit();
4354 }
4355 return TRUE;
4356}
4357
4358/**********************************************************************/
4362bool handle_stdin_input(struct connection *caller, char *str)
4363{
4364 return handle_stdin_input_real(caller, str, FALSE, 0);
4365}
4366
4367/**********************************************************************/
4370bool handle_stdin_input_free(struct connection *caller, char *str)
4371{
4372 bool ret = handle_stdin_input_real(caller, str, FALSE, 0);
4373
4374 /* Since handle_stdin_input_real() returned,
4375 * we can be sure this was not freed in atexit(). */
4376 free(str);
4377
4378 return ret;
4379}
4380
4381/**********************************************************************/
4389static bool handle_stdin_input_real(struct connection *caller, char *str,
4390 bool check, int read_recursion)
4391{
4392 char full_command[MAX_LEN_CONSOLE_LINE];
4394 char *cptr_s, *cptr_d;
4395 enum command_id cmd;
4396 enum cmdlevel level;
4397
4398 /* Remove leading and trailing spaces, and server command prefix. */
4399 cptr_s = str = skip_leading_spaces(str);
4400 if ('\0' == *cptr_s || '#' == *cptr_s) {
4401 /* This appear to be a comment or blank line. */
4402 return FALSE;
4403 }
4404
4405 if (SERVER_COMMAND_PREFIX == *cptr_s) {
4406 /* Commands may be prefixed with SERVER_COMMAND_PREFIX, even when
4407 * given on the server command line. */
4408 cptr_s++;
4409 remove_leading_spaces(cptr_s);
4410 if ('\0' == *cptr_s) {
4411 /* This appear to be a blank line. */
4412 return FALSE;
4413 }
4414 }
4415 remove_trailing_spaces(cptr_s);
4416
4417 /* notify to the server console */
4418 if (!check && caller) {
4419 con_write(C_COMMENT, "%s: '%s'", caller->username, str);
4420 }
4421
4422 /* if the caller may not use any commands at all, don't waste any time */
4423 if (may_use_nothing(caller)) {
4424 cmd_reply(CMD_HELP, caller, C_FAIL,
4425 _("Sorry, you are not allowed to use server commands."));
4426 return FALSE;
4427 }
4428
4429 /* copy the full command, in case we need it for voting purposes. */
4430 sz_strlcpy(full_command, cptr_s);
4431
4432 /*
4433 * cptr_s points now to the beginning of the real command. It has
4434 * skipped leading whitespace, the SERVER_COMMAND_PREFIX and any
4435 * other non-alphanumeric characters.
4436 */
4437 for (cptr_d = command; *cptr_s != '\0' && fc_isalnum(*cptr_s)
4438 && cptr_d < command + sizeof(command) - 1; cptr_s++, cptr_d++) {
4439 *cptr_d = *cptr_s;
4440 }
4441 *cptr_d = '\0';
4442
4443 /* cptr_s now contains the arguments. */
4444 sz_strlcpy(arg, skip_leading_spaces(cptr_s));
4445
4446 cmd = command_named(command, FALSE);
4447 if (cmd == CMD_AMBIGUOUS) {
4448 cmd = command_named(command, TRUE);
4449 cmd_reply(cmd, caller, C_SYNTAX,
4450 _("Warning: '%s' interpreted as '%s', but it is ambiguous."
4451 " Try '%shelp'."),
4452 command, command_name_by_number(cmd), caller?"/":"");
4453 } else if (cmd == CMD_UNRECOGNIZED) {
4454 cmd_reply(cmd, caller, C_SYNTAX, _("Unknown command '%s%s'. "
4455 " Try '%shelp'."),
4456 caller ? "/" : "", command, caller ? "/" : "");
4457 return FALSE;
4458 }
4459
4461
4462 if (conn_can_vote(caller, NULL) && level == ALLOW_CTRL
4463 && conn_get_access(caller) == ALLOW_BASIC && !check
4464 && !vote_would_pass_immediately(caller, cmd)) {
4465 struct vote *vote;
4466 bool caller_had_vote = (NULL != get_vote_by_caller(caller));
4467
4468 /* Check if the vote command would succeed. If we already have a vote
4469 * going, cancel it in favour of the new vote command. You can only
4470 * have one vote at a time. This is done by vote_new(). */
4471 if (handle_stdin_input_real(caller, full_command, TRUE,
4472 read_recursion + 1)
4473 && (vote = vote_new(caller, arg, cmd))) {
4474 char votedesc[MAX_LEN_CONSOLE_LINE];
4475 const struct player *teamplr;
4476 const char *what;
4477 struct ft_color color;
4478
4479 if (caller_had_vote) {
4480 cmd_reply(CMD_VOTE, caller, C_COMMENT,
4481 /* TRANS: "vote" as a process */
4482 _("Your new vote canceled your previous vote."));
4483 }
4484
4485 describe_vote(vote, votedesc, sizeof(votedesc));
4486
4487 if (vote_is_team_only(vote)) {
4488 /* TRANS: "vote" as a process */
4489 what = _("New teamvote");
4490 teamplr = conn_get_player(caller);
4492 } else {
4493 /* TRANS: "vote" as a process */
4494 what = _("New vote");
4495 teamplr = NULL;
4497 }
4498 notify_team(teamplr, NULL, E_VOTE_NEW, color,
4499 /* TRANS: "[New vote|New teamvote] (number 3)
4500 * by fred: proposed change" */
4501 _("%s (number %d) by %s: %s"), what,
4502 vote->vote_no, caller->username, votedesc);
4503
4504 /* Vote on your own suggestion. */
4505 connection_vote(caller, vote, VOTE_YES);
4506 return TRUE;
4507
4508 } else {
4509 cmd_reply(CMD_VOTE, caller, C_FAIL,
4510 /* TRANS: "vote" as a process */
4511 _("Your new vote (\"%s\") was not "
4512 "legal or was not recognized."), full_command);
4513 return FALSE;
4514 }
4515 }
4516
4517 if (caller
4518 && !((check || vote_would_pass_immediately(caller, cmd))
4519 && conn_get_access(caller) >= ALLOW_BASIC
4520 && level == ALLOW_CTRL)
4521 && conn_get_access(caller) < level) {
4522 cmd_reply(cmd, caller, C_FAIL,
4523 _("You are not allowed to use this command."));
4524 return FALSE;
4525 }
4526
4527 if (!check) {
4528 struct conn_list *echo_list = NULL;
4529 bool echo_list_allocated = FALSE;
4530
4531 switch (command_echo(command_by_number(cmd))) {
4532 case CMD_ECHO_NONE:
4533 break;
4534 case CMD_ECHO_ADMINS:
4536 if (ALLOW_ADMIN <= conn_get_access(pconn)) {
4537 if (NULL == echo_list) {
4538 echo_list = conn_list_new();
4539 echo_list_allocated = TRUE;
4540 }
4541 conn_list_append(echo_list, pconn);
4542 }
4544 break;
4545 case CMD_ECHO_ALL:
4546 echo_list = game.est_connections;
4547 break;
4548 }
4549
4550 if (NULL != echo_list) {
4551 if (caller) {
4552 notify_conn(echo_list, NULL, E_SETTING, ftc_any,
4553 "%s: '%s %s'", caller->username, command, arg);
4554 } else {
4555 notify_conn(echo_list, NULL, E_SETTING, ftc_server_prompt,
4556 "%s: '%s %s'", _("(server prompt)"), command, arg);
4557 }
4558 if (echo_list_allocated) {
4559 conn_list_destroy(echo_list);
4560 }
4561 }
4562 }
4563
4564 switch (cmd) {
4565 case CMD_REMOVE:
4566 return remove_player_command(caller, arg, check);
4567 case CMD_SAVE:
4568 return save_command(caller, arg, check);
4569 case CMD_SCENSAVE:
4570 return scensave_command(caller, arg, check);
4571 case CMD_LOAD:
4572 return load_command(caller, arg, check, FALSE);
4573 case CMD_METAPATCHES:
4574 return metapatches_command(caller, arg, check);
4575 case CMD_METAMESSAGE:
4576 return metamessage_command(caller, arg, check);
4577 case CMD_METACONN:
4578 return metaconnection_command(caller, arg, check);
4579 case CMD_METASERVER:
4580 return metaserver_command(caller, arg, check);
4581 case CMD_HELP:
4582 return show_help(caller, arg);
4583 case CMD_SRVID:
4584 return show_serverid(caller, arg);
4585 case CMD_LIST:
4586 return show_list(caller, arg);
4587 case CMD_AITOGGLE:
4588 return toggle_ai_command(caller, arg, check);
4589 case CMD_TAKE:
4590 return take_command(caller, arg, check);
4591 case CMD_OBSERVE:
4592 return observe_command(caller, arg, check);
4593 case CMD_DETACH:
4594 return detach_command(caller, arg, check);
4595 case CMD_CREATE:
4596 return create_command(caller, arg, check);
4597 case CMD_AWAY:
4598 return away_command(caller, check);
4599 case CMD_RESTRICTED:
4600 case CMD_NOVICE:
4601 case CMD_EASY:
4602 case CMD_NORMAL:
4603 case CMD_HARD:
4604 case CMD_CHEATING:
4605#ifdef FREECIV_DEBUG
4606 case CMD_EXPERIMENTAL:
4607#endif
4608 return set_ai_level_named(caller, arg, command_name_by_number(cmd), check);
4609 case CMD_QUIT:
4610 return quit_game(caller, check);
4611 case CMD_CUT:
4612 return cut_client_connection(caller, arg, check);
4613 case CMD_SHOW:
4614 return show_command(caller, arg, check);
4615 case CMD_EXPLAIN:
4616 return explain_option(caller, arg, check);
4617 case CMD_DEBUG:
4618 return debug_command(caller, arg, check);
4619 case CMD_SET:
4620 return set_command(caller, arg, check);
4621 case CMD_TEAM:
4622 return team_command(caller, arg, check);
4623 case CMD_RULESETDIR:
4624 return set_rulesetdir(caller, arg, check, read_recursion);
4625 case CMD_WALL:
4626 return wall(arg, check);
4627 case CMD_CONNECTMSG:
4628 return connectmsg_command(caller, arg, check);
4629 case CMD_VOTE:
4630 return vote_command(caller, arg, check);
4631 case CMD_CANCELVOTE:
4632 return cancelvote_command(caller, arg, check);
4633 case CMD_READ_SCRIPT:
4634 return read_command(caller, arg, check, read_recursion);
4635 case CMD_WRITE_SCRIPT:
4636 return write_command(caller, arg, check);
4637 case CMD_RESET:
4638 return reset_command(caller, arg, check, read_recursion);
4639 case CMD_DEFAULT:
4640 return default_command(caller, arg, check);
4641 case CMD_LUA:
4642 return lua_command(caller, arg, check, read_recursion);
4643 case CMD_KICK:
4644 return kick_command(caller, arg, check);
4645 case CMD_DELEGATE:
4646 return delegate_command(caller, arg, check);
4647 case CMD_AICMD:
4648 return aicmd_command(caller, arg, check);
4649 case CMD_FCDB:
4650 return fcdb_command(caller, arg, check);
4651 case CMD_MAPIMG:
4652 return mapimg_command(caller, arg, check);
4653 case CMD_RFCSTYLE: /* see console.h for an explanation */
4654 if (!check) {
4656 }
4657 return TRUE;
4658 case CMD_CMDLEVEL:
4659 return cmdlevel_command(caller, arg, check);
4660 case CMD_FIRSTLEVEL:
4661 return firstlevel_command(caller, check);
4662 case CMD_TIMEOUT:
4663 return timeout_command(caller, arg, check);
4664 case CMD_START_GAME:
4665 return start_command(caller, check, FALSE);
4666 case CMD_END_GAME:
4667 return end_command(caller, arg, check);
4668 case CMD_SURRENDER:
4669 return surrender_command(caller, arg, check);
4670 case CMD_IGNORE:
4671 return ignore_command(caller, arg, check);
4672 case CMD_UNIGNORE:
4673 return unignore_command(caller, arg, check);
4674 case CMD_PLAYERCOLOR:
4675 return playercolor_command(caller, arg, check);
4676 case CMD_PLAYERNATION:
4677 return playernation_command(caller, arg, check);
4678 case CMD_NUM:
4679 case CMD_UNRECOGNIZED:
4680 case CMD_AMBIGUOUS:
4681 break;
4682 }
4683 /* should NEVER happen! */
4684 log_error("Unknown command variant: %d.", cmd);
4685 return FALSE;
4686}
4687
4688/**********************************************************************/
4691static bool end_command(struct connection *caller, char *str, bool check)
4692{
4693 if (S_S_RUNNING == server_state()) {
4694 if (check) {
4695 return TRUE;
4696 }
4697 notify_conn(game.est_connections, NULL, E_GAME_END, ftc_server,
4698 _("Game is over."));
4699 set_server_state(S_S_OVER);
4701 cmd_reply(CMD_END_GAME, caller, C_OK,
4702 _("Ending the game. The server will restart once all clients "
4703 "have disconnected."));
4704 return TRUE;
4705 } else {
4706 cmd_reply(CMD_END_GAME, caller, C_FAIL,
4707 _("Cannot end the game: no game running."));
4708 return FALSE;
4709 }
4710}
4711
4712/**********************************************************************/
4716static bool surrender_command(struct connection *caller, char *str, bool check)
4717{
4718 struct player *pplayer;
4719
4720 if (caller == NULL || !conn_controls_player(caller)) {
4722 _("You are not allowed to use this command."));
4723 return FALSE;
4724 }
4725
4726 if (S_S_RUNNING != server_state()) {
4727 cmd_reply(CMD_SURRENDER, caller, C_FAIL, _("You cannot surrender now."));
4728 return FALSE;
4729 }
4730
4731 pplayer = conn_get_player(caller);
4732 if (player_status_check(pplayer, PSTATUS_SURRENDER)) {
4734 _("You have already conceded the game."));
4735 return FALSE;
4736 }
4737
4738 if (check) {
4739 return TRUE;
4740 }
4741
4742 notify_conn(game.est_connections, NULL, E_GAME_END, ftc_server,
4743 _("%s has conceded the game and can no longer win."),
4744 player_name(pplayer));
4745 player_status_add(pplayer, PSTATUS_SURRENDER);
4746 return TRUE;
4747}
4748
4749/* Define the possible arguments to the reset command */
4750#define SPECENUM_NAME reset_args
4751#define SPECENUM_VALUE0 RESET_GAME
4752#define SPECENUM_VALUE0NAME "game"
4753#define SPECENUM_VALUE1 RESET_RULESET
4754#define SPECENUM_VALUE1NAME "ruleset"
4755#define SPECENUM_VALUE2 RESET_SCRIPT
4756#define SPECENUM_VALUE2NAME "script"
4757#define SPECENUM_VALUE3 RESET_DEFAULT
4758#define SPECENUM_VALUE3NAME "default"
4759#include "specenum_gen.h"
4760
4761/**********************************************************************/
4764static const char *reset_accessor(int i)
4765{
4766 i = CLIP(0, i, reset_args_max());
4767 return reset_args_name((enum reset_args) i);
4768}
4769
4770/**********************************************************************/
4774static bool reset_command(struct connection *caller, char *arg, bool check,
4775 int read_recursion)
4776{
4777 enum m_pre_result result;
4778 int ind;
4779
4780 /* match the argument */
4781 result = match_prefix(reset_accessor, reset_args_max() + 1, 0,
4782 fc_strncasecmp, NULL, arg, &ind);
4783
4784 switch (result) {
4785 case M_PRE_EXACT:
4786 case M_PRE_ONLY:
4787 /* we have a match */
4788 break;
4789 case M_PRE_AMBIGUOUS:
4790 case M_PRE_EMPTY:
4791 /* use 'ruleset' [1] if the game was not started; else use 'game' [2] */
4792 if (S_S_INITIAL == server_state() && game.info.is_new_game) {
4793 cmd_reply(CMD_RESET, caller, C_WARNING,
4794 _("Guessing argument 'ruleset'."));
4795 ind = RESET_RULESET;
4796 } else {
4797 cmd_reply(CMD_RESET, caller, C_WARNING,
4798 _("Guessing argument 'game'."));
4799 ind = RESET_GAME;
4800 }
4801 break;
4802 case M_PRE_LONG:
4803 case M_PRE_FAIL:
4804 case M_PRE_LAST:
4805 cmd_reply(CMD_RESET, caller, C_FAIL,
4806 _("The valid arguments are: 'game', 'ruleset', 'script' "
4807 "or 'default'."));
4808 return FALSE;
4809 break;
4810 }
4811
4812 if (check) {
4813 return TRUE;
4814 }
4815
4816 switch (ind) {
4817 case RESET_GAME:
4818 if (!game.info.is_new_game) {
4819 if (settings_game_reset()) {
4820 cmd_reply(CMD_RESET, caller, C_OK,
4821 _("Reset all settings to the values at the game start."));
4822 } else {
4823 cmd_reply(CMD_RESET, caller, C_FAIL,
4824 _("No saved settings from the game start available."));
4825 return FALSE;
4826 }
4827 } else {
4828 cmd_reply(CMD_RESET, caller, C_FAIL, _("No game started..."));
4829 return FALSE;
4830 }
4831 break;
4832
4833 case RESET_RULESET:
4834 /* Restore game settings saved in game.ruleset. */
4836 cmd_reply(CMD_RESET, caller, C_OK,
4837 _("Reset all settings to ruleset values."));
4838 } else {
4839 cmd_reply(CMD_RESET, caller, C_FAIL,
4840 _("Failed to reset settings to ruleset values."));
4841 }
4842 break;
4843
4844 case RESET_SCRIPT:
4845 cmd_reply(CMD_RESET, caller, C_OK,
4846 _("Reset all settings and rereading the server start "
4847 "script."));
4849 /* Load initial script */
4850 if (NULL != srvarg.script_filename
4852 read_recursion + 1)) {
4853 if (NULL != caller) {
4854 cmd_reply(CMD_RESET, caller, C_FAIL,
4855 _("Could not read script file '%s'."),
4857 }
4858 return FALSE;
4859 }
4860 break;
4861
4862 case RESET_DEFAULT:
4863 cmd_reply(CMD_RESET, caller, C_OK,
4864 _("Reset all settings to default values."));
4866 break;
4867 }
4868
4870 cmd_reply(CMD_RESET, caller, C_OK, _("Settings re-initialized."));
4871
4872 /* show ruleset summary and list changed values */
4873 show_ruleset_info(caller, CMD_RESET, check, read_recursion);
4874
4875 return TRUE;
4876}
4877
4878/**********************************************************************/
4881static bool default_command(struct connection *caller, char *arg, bool check)
4882{
4883 struct setting *pset;
4884 char reject_msg[256] = "";
4885
4886 pset = validate_setting_arg(CMD_DEFAULT, caller, arg);
4887
4888 if (!pset) {
4889 /* Reason already reported. */
4890 return FALSE;
4891 }
4892
4893 if (!setting_is_changeable(pset, caller, reject_msg, sizeof(reject_msg))) {
4894 cmd_reply(CMD_DEFAULT, caller, C_FAIL, "%s", reject_msg);
4895
4896 return FALSE;
4897 }
4898
4899 if (!check) {
4901 cmd_reply(CMD_DEFAULT, caller, C_OK,
4902 _("Option '%s' reset to default value, and will track any "
4903 "default changes."), arg);
4904 }
4905
4906 return TRUE;
4907}
4908
4909/* Define the possible arguments to the delegation command */
4910#define SPECENUM_NAME lua_args
4911#define SPECENUM_VALUE0 LUA_CMD
4912#define SPECENUM_VALUE0NAME "cmd"
4913#define SPECENUM_VALUE1 LUA_FILE
4914#define SPECENUM_VALUE1NAME "file"
4915#define SPECENUM_VALUE2 LUA_UNSAFE_CMD
4916#define SPECENUM_VALUE2NAME "unsafe-cmd"
4917#define SPECENUM_VALUE3 LUA_UNSAFE_FILE
4918#define SPECENUM_VALUE3NAME "unsafe-file"
4919#include "specenum_gen.h"
4920
4921/**********************************************************************/
4924static const char *lua_accessor(int i)
4925{
4926 i = CLIP(0, i, lua_args_max());
4927 return lua_args_name((enum lua_args) i);
4928}
4929
4930/**********************************************************************/
4933static bool lua_command(struct connection *caller, char *arg, bool check,
4934 int read_recursion)
4935{
4936 struct stat statbuf;
4937 const char extension[] = ".lua", *real_filename = NULL;
4938 char luafile[4096], tilde_filename[4096];
4939 char *tokens[1], *luaarg = NULL;
4940 int ntokens, ind;
4941 enum m_pre_result result;
4942 bool ret = FALSE;
4943
4944 ntokens = get_tokens(arg, tokens, 1, TOKEN_DELIMITERS);
4945
4946 if (ntokens > 0) {
4947 /* match the argument */
4948 result = match_prefix(lua_accessor, lua_args_max() + 1, 0,
4949 fc_strncasecmp, NULL, tokens[0], &ind);
4950
4951 switch (result) {
4952 case M_PRE_EXACT:
4953 case M_PRE_ONLY:
4954 /* We have a match */
4955 luaarg = arg + strlen(lua_args_name(ind));
4956 luaarg = skip_leading_spaces(luaarg);
4957 break;
4958 case M_PRE_EMPTY:
4959 /* Nothing. */
4960 break;
4961 case M_PRE_AMBIGUOUS:
4962 case M_PRE_LONG:
4963 case M_PRE_FAIL:
4964 case M_PRE_LAST:
4965 /* Fall back to depreciated 'lua <script command>' syntax. */
4966 cmd_reply(CMD_LUA, caller, C_SYNTAX,
4967 _("Fall back to old syntax '%slua <script command>'."),
4968 caller ? "/" : "");
4969 ind = LUA_CMD;
4970 luaarg = arg;
4971 break;
4972 }
4973 }
4974
4975 if (luaarg == NULL) {
4976 cmd_reply(CMD_LUA, caller, C_FAIL,
4977 _("No lua command or lua script file. See '%shelp lua'."),
4978 caller ? "/" : "");
4979 ret = TRUE;
4980 goto cleanup;
4981 }
4982
4983 switch (ind) {
4984 case LUA_CMD:
4985 /* Nothing to check. */
4986 break;
4987 case LUA_UNSAFE_CMD:
4988 if (read_recursion > 0) {
4989 cmd_reply(CMD_LUA, caller, C_FAIL,
4990 _("Unsafe Lua code can only be run by explicit command."));
4991 ret = FALSE;
4992 goto cleanup;
4993 } else if (is_restricted(caller)) {
4994 cmd_reply(CMD_LUA, caller, C_FAIL,
4995 _("You aren't allowed to run unsafe Lua code."));
4996 ret = FALSE;
4997 goto cleanup;
4998 }
4999 break;
5000 case LUA_UNSAFE_FILE:
5001 if (read_recursion > 0) {
5002 cmd_reply(CMD_LUA, caller, C_FAIL,
5003 _("Unsafe Lua code can only be run by explicit command."));
5004 ret = FALSE;
5005 goto cleanup;
5006 } else if (is_restricted(caller)) {
5007 cmd_reply(CMD_LUA, caller, C_FAIL,
5008 _("You aren't allowed to run unsafe Lua code."));
5009 ret = FALSE;
5010 goto cleanup;
5011 }
5012
5014 case LUA_FILE:
5015 /* Abuse real_filename to find if we already have a .lua extension. */
5016 real_filename = luaarg + strlen(luaarg) - MIN(strlen(extension),
5017 strlen(luaarg));
5018 if (strcmp(real_filename, extension) != 0) {
5019 fc_snprintf(luafile, sizeof(luafile), "%s%s", luaarg, extension);
5020 } else {
5021 sz_strlcpy(luafile, luaarg);
5022 }
5023
5024 if (is_restricted(caller)) {
5025 if (!is_safe_filename(luafile)) {
5026 cmd_reply(CMD_LUA, caller, C_FAIL,
5027 _("Freeciv script '%s' disallowed for security reasons."),
5028 luafile);
5029 ret = FALSE;
5030 goto cleanup;;
5031 }
5032 sz_strlcpy(tilde_filename, luafile);
5033 } else {
5034 interpret_tilde(tilde_filename, sizeof(tilde_filename), luafile);
5035 }
5036
5037 real_filename = fileinfoname(get_data_dirs(), tilde_filename);
5038 if (!real_filename) {
5039 if (is_restricted(caller)) {
5040 cmd_reply(CMD_LUA, caller, C_FAIL,
5041 _("No Freeciv script found by the name '%s'."),
5042 tilde_filename);
5043 ret = FALSE;
5044 goto cleanup;
5045 }
5046 /* File is outside data directories */
5047 real_filename = tilde_filename;
5048 }
5049 break;
5050 }
5051
5052 if (check) {
5053 ret = TRUE;
5054 goto cleanup;
5055 }
5056
5057 switch (ind) {
5058 case LUA_CMD:
5059 ret = script_server_do_string(caller, luaarg);
5060 break;
5061 case LUA_UNSAFE_CMD:
5062 ret = script_server_unsafe_do_string(caller, luaarg);
5063 break;
5064 case LUA_FILE:
5065 cmd_reply(CMD_LUA, caller, C_COMMENT,
5066 _("Loading Freeciv script file '%s'."), real_filename);
5067
5068 if (is_reg_file_for_access(real_filename, FALSE)
5069 && !fc_stat(real_filename, &statbuf)) {
5070 ret = script_server_do_file(caller, real_filename);
5071 } else {
5072 cmd_reply(CMD_LUA, caller, C_FAIL,
5073 _("Cannot read Freeciv script '%s'."), real_filename);
5074 ret = FALSE;
5075 }
5076 break;
5077 case LUA_UNSAFE_FILE:
5078 cmd_reply(CMD_LUA, caller, C_COMMENT,
5079 _("Loading Freeciv script file '%s'."), real_filename);
5080
5081 if (is_reg_file_for_access(real_filename, FALSE)
5082 && !fc_stat(real_filename, &statbuf)) {
5083 ret = script_server_unsafe_do_file(caller, real_filename);
5084 } else {
5085 cmd_reply(CMD_LUA, caller, C_FAIL,
5086 _("Cannot read Freeciv script '%s'."), real_filename);
5087 ret = FALSE;
5088 }
5089 break;
5090 }
5091
5092 cleanup:
5093 free_tokens(tokens, ntokens);
5094 return ret;
5095}
5096
5097/* Define the possible arguments to the delegation command */
5098#define SPECENUM_NAME delegate_args
5099#define SPECENUM_VALUE0 DELEGATE_CANCEL
5100#define SPECENUM_VALUE0NAME "cancel"
5101#define SPECENUM_VALUE1 DELEGATE_RESTORE
5102#define SPECENUM_VALUE1NAME "restore"
5103#define SPECENUM_VALUE2 DELEGATE_SHOW
5104#define SPECENUM_VALUE2NAME "show"
5105#define SPECENUM_VALUE3 DELEGATE_TAKE
5106#define SPECENUM_VALUE3NAME "take"
5107#define SPECENUM_VALUE4 DELEGATE_TO
5108#define SPECENUM_VALUE4NAME "to"
5109#include "specenum_gen.h"
5110
5111/**********************************************************************/
5114static const char *delegate_accessor(int i)
5115{
5116 i = CLIP(0, i, delegate_args_max());
5117 return delegate_args_name((enum delegate_args) i);
5118}
5119
5120/**********************************************************************/
5123static bool delegate_command(struct connection *caller, char *arg,
5124 bool check)
5125{
5126 char *tokens[3];
5127 int ntokens, ind = delegate_args_invalid();
5128 enum m_pre_result result;
5129 bool player_specified = FALSE; /* affects messages only */
5130 bool ret = FALSE;
5131 const char *username = NULL;
5132 struct player *dplayer = NULL;
5133
5134 if (!game_was_started()) {
5135 cmd_reply(CMD_DELEGATE, caller, C_FAIL, _("Game not started - "
5136 "cannot delegate yet."));
5137 return FALSE;
5138 }
5139
5140 ntokens = get_tokens(arg, tokens, 3, TOKEN_DELIMITERS);
5141
5142 if (ntokens > 0) {
5143 /* match the argument */
5144 result = match_prefix(delegate_accessor, delegate_args_max() + 1, 0,
5145 fc_strncasecmp, NULL, tokens[0], &ind);
5146
5147 switch (result) {
5148 case M_PRE_EXACT:
5149 case M_PRE_ONLY:
5150 /* we have a match */
5151 break;
5152 case M_PRE_EMPTY:
5153 if (caller) {
5154 /* Use 'delegate show' as default. */
5155 ind = DELEGATE_SHOW;
5156 }
5157 break;
5158 case M_PRE_AMBIGUOUS:
5159 case M_PRE_LONG:
5160 case M_PRE_FAIL:
5161 case M_PRE_LAST:
5162 ind = delegate_args_invalid();
5163 break;
5164 }
5165 } else {
5166 if (caller) {
5167 /* Use 'delegate show' as default. */
5168 ind = DELEGATE_SHOW;
5169 }
5170 }
5171
5172 if (!delegate_args_is_valid(ind)) {
5173 char buf[256] = "";
5174 enum delegate_args valid_args;
5175
5176 for (valid_args = delegate_args_begin();
5177 valid_args != delegate_args_end();
5178 valid_args = delegate_args_next(valid_args)) {
5179 const char *name = delegate_args_name(valid_args);
5180
5181 if (name != NULL) {
5182 cat_snprintf(buf, sizeof(buf), "'%s'", name);
5183 if (valid_args != delegate_args_max()) {
5184 cat_snprintf(buf, sizeof(buf), ", ");
5185 }
5186 }
5187 }
5188
5190 /* TRANS: do not translate the command 'delegate'. */
5191 _("Valid arguments for 'delegate' are: %s."), buf);
5192 ret = FALSE;
5193 goto cleanup;
5194 }
5195
5196 /* Get the data (player, username for delegation) and validate it. */
5197 switch (ind) {
5198 case DELEGATE_CANCEL:
5199 /* delegate cancel [player] */
5200 if (ntokens > 1) {
5201 if (!caller || conn_get_access(caller) >= ALLOW_ADMIN) {
5202 player_specified = TRUE;
5203 dplayer = player_by_name_prefix(tokens[1], &result);
5204 if (!dplayer) {
5205 cmd_reply_no_such_player(CMD_DELEGATE, caller, tokens[1], result);
5206 ret = FALSE;
5207 goto cleanup;
5208 }
5209 } else {
5211 _("Command level '%s' or greater needed to modify "
5212 "others' delegations."), cmdlevel_name(ALLOW_ADMIN));
5213 ret = FALSE;
5214 goto cleanup;
5215 }
5216 } else {
5217 dplayer = conn_get_player(caller);
5218 if (!dplayer) {
5220 _("Please specify a player for whom delegation should "
5221 "be canceled."));
5222 ret = FALSE;
5223 goto cleanup;
5224 }
5225 }
5226 break;
5227 case DELEGATE_RESTORE:
5228 /* delegate restore */
5229 if (!caller) {
5230 cmd_reply(CMD_DELEGATE, caller, C_FAIL,
5231 _("You can't switch players from the console."));
5232 ret = FALSE;
5233 goto cleanup;
5234 }
5235 break;
5236 case DELEGATE_SHOW:
5237 /* delegate show [player] */
5238 if (ntokens > 1) {
5239 player_specified = TRUE;
5240 dplayer = player_by_name_prefix(tokens[1], &result);
5241 if (!dplayer) {
5242 cmd_reply_no_such_player(CMD_DELEGATE, caller, tokens[1], result);
5243 ret = FALSE;
5244 goto cleanup;
5245 }
5246 } else {
5247 dplayer = conn_get_player(caller);
5248 if (!dplayer) {
5250 _("Please specify a player for whom the delegation should "
5251 "be shown."));
5252 ret = FALSE;
5253 goto cleanup;
5254 }
5255 }
5256 break;
5257 case DELEGATE_TAKE:
5258 /* delegate take <player> */
5259 if (!caller) {
5260 cmd_reply(CMD_DELEGATE, caller, C_FAIL,
5261 _("You can't switch players from the console."));
5262 ret = FALSE;
5263 goto cleanup;
5264 }
5265 if (ntokens > 1) {
5266 player_specified = TRUE;
5267 dplayer = player_by_name_prefix(tokens[1], &result);
5268 if (!dplayer) {
5269 cmd_reply_no_such_player(CMD_DELEGATE, caller, tokens[1], result);
5270 ret = FALSE;
5271 goto cleanup;
5272 }
5273 } else {
5275 _("Please specify a player to take control of."));
5276 ret = FALSE;
5277 goto cleanup;
5278 }
5279 break;
5280 case DELEGATE_TO:
5281 break;
5282 }
5283 /* All checks done to this point will give pretty much the same result at
5284 * any time. Checks after this point are more likely to vary over time. */
5285 if (check) {
5286 ret = TRUE;
5287 goto cleanup;
5288 }
5289
5290 switch (ind) {
5291 case DELEGATE_TO:
5292 /* delegate to <username> [player] */
5293 if (ntokens > 1) {
5294 username = tokens[1];
5295 } else {
5297 _("Please specify a user to whom control is to be delegated."));
5298 ret = FALSE;
5299 break;
5300 }
5301 if (ntokens > 2) {
5302 player_specified = TRUE;
5303 dplayer = player_by_name_prefix(tokens[2], &result);
5304 if (!dplayer) {
5305 cmd_reply_no_such_player(CMD_DELEGATE, caller, tokens[2], result);
5306 ret = FALSE;
5307 break;
5308 }
5309#ifndef HAVE_FCDB
5310 if (caller && conn_get_access(caller) < ALLOW_ADMIN) {
5311#else
5312 if (caller && conn_get_access(caller) < ALLOW_ADMIN
5313 && !(srvarg.fcdb_enabled
5314 && script_fcdb_call("user_delegate_to", caller, dplayer,
5315 username, &ret) && ret)) {
5316#endif
5318 _("Command level '%s' or greater or special permission "
5319 "needed to modify others' delegations."),
5320 cmdlevel_name(ALLOW_ADMIN));
5321 ret = FALSE;
5322 break;
5323 }
5324 } else {
5325 dplayer = conn_controls_player(caller) ? conn_get_player(caller) : NULL;
5326 if (!dplayer) {
5327 cmd_reply(CMD_DELEGATE, caller, C_FAIL,
5328 _("You do not control a player."));
5329 ret = FALSE;
5330 break;
5331 }
5332 }
5333
5334 /* Delegate control of player to another user. */
5335 fc_assert_ret_val(dplayer, FALSE);
5337
5338 /* Forbid delegation of players already controlled by a delegate, and
5339 * those 'put aside' by a delegate.
5340 * For the former, if player is already under active delegate control,
5341 * we wouldn't handle the revocation that would be necessary if their
5342 * delegation changed; and the authority granted to delegates does not
5343 * include the ability to sub-delegate.
5344 * For the latter, allowing control of the 'put aside' player to be
5345 * delegated would break the invariant that whenever a user is connected,
5346 * they are attached to 'their' player. */
5347 if (player_delegation_active(dplayer)) {
5348 if (!player_delegation_get(dplayer)) {
5349 /* Attempting to change a 'put aside' player. Must be admin
5350 * or console. */
5351 fc_assert(player_specified);
5352 cmd_reply(CMD_DELEGATE, caller, C_FAIL,
5353 _("Can't delegate control of '%s' belonging to %s while "
5354 "they are controlling another player."),
5355 player_name(dplayer), dplayer->username);
5356 } else if (player_specified) {
5357 /* Admin or console attempting to change a controlled player. */
5358 cmd_reply(CMD_DELEGATE, caller, C_FAIL,
5359 _("Can't change delegation of '%s' while controlled by "
5360 "delegate %s."), player_name(dplayer), dplayer->username);
5361 } else {
5362 /* Caller must be the delegate. Give more specific message.
5363 * (We don't know if they thought they were delegating their
5364 * original or delegated player, but we don't allow either.) */
5365 cmd_reply(CMD_DELEGATE, caller, C_FAIL,
5366 _("You can't delegate control while you are controlling "
5367 "a delegated player yourself."));
5368 }
5369 ret = FALSE;
5370 break;
5371 }
5372
5373 /* Forbid delegation to player's original owner
5374 * (from above test we know that dplayer->username is the original now) */
5375 if (fc_strcasecmp(dplayer->username, username) == 0) {
5376 if (player_specified) {
5377 /* Probably admin or console. */
5378 cmd_reply(CMD_DELEGATE, caller, C_FAIL,
5379 /* TRANS: don't translate 'delegate cancel' */
5380 _("%s already owns '%s', so cannot also be delegate. "
5381 "Use '%sdelegate cancel' to cancel an existing "
5382 "delegation."),
5383 username, player_name(dplayer), caller?"/":"");
5384 } else {
5385 /* Player not specified on command line, so they must have been trying
5386 * to delegate control to themself. Give more specific message. */
5387 cmd_reply(CMD_DELEGATE, caller, C_FAIL,
5388 /* TRANS: don't translate '/delegate cancel' */
5389 _("You can't delegate control to yourself. "
5390 "Use '/delegate cancel' to cancel an existing "
5391 "delegation."));
5392 }
5393 ret = FALSE;
5394 break;
5395 }
5396
5397 /* FIXME: if control was already delegated to someone else, that
5398 * delegation is implicitly canceled. Perhaps we should tell someone. */
5399
5401 cmd_reply(CMD_DELEGATE, caller, C_OK,
5402 _("Control of player '%s' delegated to user %s."),
5403 player_name(dplayer), username);
5404 ret = TRUE;
5405 break;
5406
5407 case DELEGATE_SHOW:
5408 /* Show delegations. */
5409 fc_assert_ret_val(dplayer, FALSE);
5410
5411 if (player_delegation_get(dplayer) == NULL) {
5412 /* No delegation set. */
5414 _("No delegation defined for '%s'."),
5415 player_name(dplayer));
5416 } else {
5418 _("Control of player '%s' delegated to user %s."),
5419 player_name(dplayer), player_delegation_get(dplayer));
5420 }
5421 ret = TRUE;
5422 break;
5423
5424 case DELEGATE_CANCEL:
5425 if (player_delegation_get(dplayer) == NULL) {
5426 /* No delegation set. */
5427 cmd_reply(CMD_DELEGATE, caller, C_FAIL,
5428 _("No delegation defined for '%s'."),
5429 player_name(dplayer));
5430 ret = FALSE;
5431 break;
5432 }
5433
5434 if (player_delegation_active(dplayer)) {
5435 /* Delegation is currently in use. Forcibly break connection. */
5436 struct connection *pdelegate;
5437 /* (Can only happen if admin/console issues this command, as owner
5438 * will end use by their mere presence.) */
5439 fc_assert(player_specified);
5440 pdelegate = conn_by_user(player_delegation_get(dplayer));
5441 fc_assert_ret_val(pdelegate != NULL, FALSE);
5442 if (!connection_delegate_restore(pdelegate)) {
5443 /* Should never happen. Generic failure message. */
5444 log_error("Failed to restore %s's connection as %s during "
5445 "'delegate cancel'.", pdelegate->username,
5447 pdelegate->server.delegation.observer));
5448 cmd_reply(CMD_DELEGATE, caller, C_FAIL, _("Unexpected failure."));
5449 ret = FALSE;
5450 break;
5451 }
5452 notify_conn(pdelegate->self, NULL, E_CONNECTION, ftc_server,
5453 _("Your delegated control of player '%s' was canceled."),
5454 player_name(dplayer));
5455 }
5456
5457 player_delegation_set(dplayer, NULL);
5458 cmd_reply(CMD_DELEGATE, caller, C_OK, _("Delegation of '%s' canceled."),
5459 player_name(dplayer));
5460 ret = TRUE;
5461 break;
5462
5463 case DELEGATE_TAKE:
5464 /* Try to take another player. */
5465 fc_assert_ret_val(dplayer, FALSE);
5466 fc_assert_ret_val(caller, FALSE);
5467
5468 if (caller->server.delegation.status) {
5469 cmd_reply(CMD_DELEGATE, caller, C_FAIL,
5470 /* TRANS: don't translate '/delegate restore'. */
5471 _("You are already controlling a delegated player. "
5472 "Use '/delegate restore' to relinquish control of your "
5473 "current player first."));
5474 ret = FALSE;
5475 break;
5476 }
5477
5478 /* Don't allow 'put aside' players to be delegated; the invariant is
5479 * that while the owning user is connected to the server, they are
5480 * in sole control of 'their' player. */
5481 if (conn_controls_player(caller)
5482 && player_delegation_get(conn_get_player(caller)) != NULL) {
5483 cmd_reply(CMD_DELEGATE, caller, C_FAIL,
5484 /* TRANS: don't translate '/delegate cancel'. */
5485 _("Can't take player while you have delegated control "
5486 "yourself. Use '/delegate cancel' to cancel your own "
5487 "delegation first."));
5488 ret = FALSE;
5489 break;
5490 }
5491
5492 /* Taking your own player makes no sense. */
5493 if (conn_controls_player(caller)
5494 && dplayer == conn_get_player(caller)) {
5495 cmd_reply(CMD_DELEGATE, caller, C_FAIL, _("You already control '%s'."),
5496 player_name(conn_get_player(caller)));
5497 ret = FALSE;
5498 break;
5499 }
5500
5501 if (!player_delegation_get(dplayer)
5502 || fc_strcasecmp(player_delegation_get(dplayer), caller->username) != 0) {
5503 cmd_reply(CMD_DELEGATE, caller, C_FAIL,
5504 _("Control of player '%s' has not been delegated to you."),
5505 player_name(dplayer));
5506 ret = FALSE;
5507 break;
5508 }
5509
5510 /* If the player is controlled by another user, fail. */
5511 if (dplayer->is_connected) {
5512 cmd_reply(CMD_DELEGATE, caller, C_FAIL,
5513 _("Another user already controls player '%s'."),
5514 player_name(dplayer));
5515 ret = FALSE;
5516 break;
5517 }
5518
5519 if (!connection_delegate_take(caller, dplayer)) {
5520 /* Should never happen. Generic failure message. */
5521 log_error("%s failed to take control of '%s' during 'delegate take'.",
5522 caller->username, player_name(dplayer));
5523 cmd_reply(CMD_DELEGATE, caller, C_FAIL, _("Unexpected failure."));
5524 ret = FALSE;
5525 break;
5526 }
5527
5528 cmd_reply(CMD_DELEGATE, caller, C_OK,
5529 _("%s is now controlling player '%s'."), caller->username,
5530 player_name(conn_get_player(caller)));
5531 ret = TRUE;
5532 break;
5533
5534 case DELEGATE_RESTORE:
5535 /* Delegate user relinquishes control of delegated player, returning to
5536 * previous view (e.g. observer) if any. */
5537 fc_assert_ret_val(caller, FALSE);
5538
5539 if (!caller->server.delegation.status) {
5540 cmd_reply(CMD_DELEGATE, caller, C_FAIL,
5541 _("You are not currently controlling a delegated player."));
5542 ret = FALSE;
5543 break;
5544 }
5545
5546 if (!connection_delegate_restore(caller)) {
5547 /* Should never happen. Generic failure message. */
5548 log_error("Failed to restore %s's connection as %s during "
5549 "'delegate restore'.", caller->username,
5551 caller->server.delegation.observer));
5552 cmd_reply(CMD_DELEGATE, caller, C_FAIL, _("Unexpected failure."));
5553 ret = FALSE;
5554 break;
5555 }
5556
5557 cmd_reply(CMD_DELEGATE, caller, C_OK,
5558 /* TRANS: "<user> is now connected to <player>" where <player>
5559 * can also be "global observer" or "nothing" */
5560 _("%s is now connected as %s."), caller->username,
5561 delegate_player_str(conn_get_player(caller), caller->observer));
5562 ret = TRUE;
5563 break;
5564 }
5565
5566 cleanup:
5567 free_tokens(tokens, ntokens);
5568 return ret;
5569}
5570
5571/**********************************************************************/
5574static const char *delegate_player_str(struct player *pplayer, bool observer)
5575{
5576 static struct astring buf;
5577
5578 if (pplayer) {
5579 if (observer) {
5580 astr_set(&buf, _("%s (observer)"), player_name(pplayer));
5581 } else {
5582 astr_set(&buf, "%s", player_name(pplayer));
5583 }
5584 } else if (observer) {
5585 astr_set(&buf, "%s", _("global observer"));
5586 } else {
5587 /* TRANS: in place of player name or "global observer" */
5588 astr_set(&buf, "%s", _("nothing"));
5589 }
5590
5591 return astr_str(&buf);
5592}
5593
5594/* Define the possible arguments to the mapimg command */
5595/* map image layers */
5596#define SPECENUM_NAME mapimg_args
5597#define SPECENUM_VALUE0 MAPIMG_COLORTEST
5598#define SPECENUM_VALUE0NAME "colortest"
5599#define SPECENUM_VALUE1 MAPIMG_CREATE
5600#define SPECENUM_VALUE1NAME "create"
5601#define SPECENUM_VALUE2 MAPIMG_DEFINE
5602#define SPECENUM_VALUE2NAME "define"
5603#define SPECENUM_VALUE3 MAPIMG_DELETE
5604#define SPECENUM_VALUE3NAME "delete"
5605#define SPECENUM_VALUE4 MAPIMG_SHOW
5606#define SPECENUM_VALUE4NAME "show"
5607#define SPECENUM_COUNT MAPIMG_COUNT
5608#include "specenum_gen.h"
5609
5610/**********************************************************************/
5613static const char *mapimg_accessor(int i)
5614{
5615 i = CLIP(0, i, mapimg_args_max());
5616 return mapimg_args_name((enum mapimg_args) i);
5617}
5618
5619/**********************************************************************/
5622static bool mapimg_command(struct connection *caller, char *arg, bool check)
5623{
5624 enum m_pre_result result;
5625 int ind, ntokens, id;
5626 char *token[2];
5627 bool ret = TRUE;
5628
5629 ntokens = get_tokens(arg, token, 2, TOKEN_DELIMITERS);
5630
5631 if (ntokens > 0) {
5632 /* match the argument */
5633 result = match_prefix(mapimg_accessor, MAPIMG_COUNT, 0,
5634 fc_strncasecmp, NULL, token[0], &ind);
5635
5636 switch (result) {
5637 case M_PRE_EXACT:
5638 case M_PRE_ONLY:
5639 /* we have a match */
5640 break;
5641 case M_PRE_AMBIGUOUS:
5642 cmd_reply(CMD_MAPIMG, caller, C_FAIL,
5643 _("Ambiguous 'mapimg' command."));
5644 ret = FALSE;
5645 goto cleanup;
5646 break;
5647 case M_PRE_EMPTY:
5648 /* use 'show' as default */
5649 ind = MAPIMG_SHOW;
5650 break;
5651 case M_PRE_LONG:
5652 case M_PRE_FAIL:
5653 case M_PRE_LAST:
5654 {
5655 char buf[256] = "";
5656 enum mapimg_args valid_args;
5657
5658 for (valid_args = mapimg_args_begin();
5659 valid_args != mapimg_args_end();
5660 valid_args = mapimg_args_next(valid_args)) {
5661 cat_snprintf(buf, sizeof(buf), "'%s'",
5662 mapimg_args_name(valid_args));
5663 if (valid_args != mapimg_args_max()) {
5664 cat_snprintf(buf, sizeof(buf), ", ");
5665 }
5666 }
5667
5668 cmd_reply(CMD_MAPIMG, caller, C_FAIL,
5669 _("The valid arguments are: %s."), buf);
5670 ret = FALSE;
5671 goto cleanup;
5672 }
5673 break;
5674 }
5675 } else {
5676 /* use 'show' as default */
5677 ind = MAPIMG_SHOW;
5678 }
5679
5680 switch (ind) {
5681 case MAPIMG_DEFINE:
5682 if (ntokens == 1) {
5683 cmd_reply(CMD_MAPIMG, caller, C_FAIL,
5684 _("Missing argument for 'mapimg define'."));
5685 ret = FALSE;
5686 } else {
5687 /* 'mapimg define <mapstr>' */
5688 if (!mapimg_define(token[1], check)) {
5689 cmd_reply(CMD_MAPIMG, caller, C_FAIL,
5690 _("Can't use definition: %s."), mapimg_error());
5691 ret = FALSE;
5692 } else if (check) {
5693 /* Validated OK, bail out now */
5694 goto cleanup;
5695 } else if (game_was_started()
5696 && mapimg_isvalid(mapimg_count() - 1) == NULL) {
5697 /* game was started - error in map image definition check */
5698 cmd_reply(CMD_MAPIMG, caller, C_FAIL,
5699 _("Can't use definition: %s."), mapimg_error());
5700 ret = FALSE;
5701 } else {
5702 char str[MAX_LEN_MAPDEF];
5703
5704 id = mapimg_count() - 1;
5705
5706 mapimg_id2str(id, str, sizeof(str));
5707 cmd_reply(CMD_MAPIMG, caller, C_OK, _("Defined as map image "
5708 "definition %d: '%s'."),
5709 id, str);
5710 }
5711 }
5712 break;
5713
5714 case MAPIMG_DELETE:
5715 if (ntokens == 1) {
5716 cmd_reply(CMD_MAPIMG, caller, C_FAIL,
5717 _("Missing argument for 'mapimg delete'."));
5718 ret = FALSE;
5719 } else if (ntokens == 2 && strcmp(token[1], "all") == 0) {
5720 /* 'mapimg delete all' */
5721 if (check) {
5722 goto cleanup;
5723 }
5724
5725 while (mapimg_count() > 0) {
5726 mapimg_delete(0);
5727 }
5728 cmd_reply(CMD_MAPIMG, caller, C_OK, _("All map image definitions "
5729 "deleted."));
5730 } else if (ntokens == 2 && sscanf(token[1], "%d", &id) != 0) {
5731 /* 'mapimg delete <id>' */
5732 if (check) {
5733 goto cleanup;
5734 }
5735
5736 if (!mapimg_delete(id)) {
5737 cmd_reply(CMD_MAPIMG, caller, C_FAIL,
5738 _("Couldn't delete definition: %s."), mapimg_error());
5739 ret = FALSE;
5740 } else {
5741 cmd_reply(CMD_MAPIMG, caller, C_OK, _("Map image definition %d "
5742 "deleted."), id);
5743 }
5744 } else {
5745 cmd_reply(CMD_MAPIMG, caller, C_FAIL,
5746 _("Bad argument for 'mapimg delete': '%s'."), token[1]);
5747 ret = FALSE;
5748 }
5749 break;
5750
5751 case MAPIMG_SHOW:
5752 if (ntokens < 2 || (ntokens == 2 && strcmp(token[1], "all") == 0)) {
5753 /* 'mapimg show' or 'mapimg show all' */
5754 if (check) {
5755 goto cleanup;
5756 }
5757 show_mapimg(caller, CMD_MAPIMG);
5758 } else if (ntokens == 2 && sscanf(token[1], "%d", &id) != 0) {
5759 char str[2048];
5760 /* 'mapimg show <id>' */
5761 if (check) {
5762 goto cleanup;
5763 }
5764
5765 if (mapimg_show(id, str, sizeof(str), TRUE)) {
5766 cmd_reply(CMD_MAPIMG, caller, C_OK, "%s", str);
5767 } else {
5768 cmd_reply(CMD_MAPIMG, caller, C_FAIL,
5769 _("Couldn't show definition: %s."), mapimg_error());
5770 ret = FALSE;
5771 }
5772 } else {
5773 cmd_reply(CMD_MAPIMG, caller, C_FAIL,
5774 _("Bad argument for 'mapimg show': '%s'."), token[1]);
5775 ret = FALSE;
5776 }
5777 break;
5778
5779 case MAPIMG_COLORTEST:
5780 if (check) {
5781 goto cleanup;
5782 }
5783
5785 cmd_reply(CMD_MAPIMG, caller, C_OK, _("Map color test images saved."));
5786 break;
5787
5788 case MAPIMG_CREATE:
5789 if (ntokens < 2) {
5790 cmd_reply(CMD_MAPIMG, caller, C_FAIL,
5791 _("Missing argument for 'mapimg create'."));
5792 ret = FALSE;
5793 goto cleanup;
5794 }
5795
5796 if (strcmp(token[1], "all") == 0) {
5797 /* 'mapimg create all' */
5798 if (check) {
5799 goto cleanup;
5800 }
5801
5802 for (id = 0; id < mapimg_count(); id++) {
5803 struct mapdef *pmapdef = mapimg_isvalid(id);
5804
5805 if (pmapdef == NULL
5806 || !mapimg_create(pmapdef, TRUE, game.server.save_name,
5808 cmd_reply(CMD_MAPIMG, caller, C_FAIL,
5809 _("Error saving map image %d: %s."), id, mapimg_error());
5810 ret = FALSE;
5811 }
5812 }
5813 } else if (sscanf(token[1], "%d", &id) != 0) {
5814 struct mapdef *pmapdef;
5815
5816 /* 'mapimg create <id>' */
5817 if (check) {
5818 goto cleanup;
5819 }
5820
5821 pmapdef = mapimg_isvalid(id);
5822 if (pmapdef == NULL
5823 || !mapimg_create(pmapdef, TRUE, game.server.save_name,
5825 cmd_reply(CMD_MAPIMG, caller, C_FAIL,
5826 _("Error saving map image %d: %s."), id, mapimg_error());
5827 ret = FALSE;
5828 }
5829 } else {
5830 cmd_reply(CMD_MAPIMG, caller, C_FAIL,
5831 _("Bad argument for 'mapimg create': '%s'."), token[1]);
5832 ret = FALSE;
5833 }
5834 break;
5835 }
5836
5837 cleanup:
5838
5839 free_tokens(token, ntokens);
5840
5841 return ret;
5842}
5843
5844/**********************************************************************/
5847static bool aicmd_command(struct connection *caller, char *arg, bool check)
5848{
5849 enum m_pre_result match_result;
5850 struct player *pplayer;
5851 char *tokens[1], *cmd = NULL;
5852 int ntokens;
5853 bool ret = FALSE;
5854
5855 ntokens = get_tokens(arg, tokens, 1, TOKEN_DELIMITERS);
5856
5857 if (ntokens < 1) {
5858 cmd_reply(CMD_AICMD, caller, C_FAIL,
5859 _("No player given for aicmd."));
5860 goto cleanup;
5861 }
5862
5863 pplayer = player_by_name_prefix(tokens[0], &match_result);
5864
5865 if (NULL == pplayer) {
5866 cmd_reply_no_such_player(CMD_AICMD, caller, tokens[0], match_result);
5867 goto cleanup;
5868 }
5869
5870 /* We have a player - extract the command. */
5871 cmd = arg + strlen(tokens[0]);
5872 cmd = skip_leading_spaces(cmd);
5873
5874 if (strlen(cmd) == 0) {
5875 cmd_reply(CMD_AICMD, caller, C_FAIL,
5876 _("No command for the AI console defined."));
5877 goto cleanup;
5878 }
5879
5880 if (check) {
5881 ret = TRUE;
5882 goto cleanup;
5883 }
5884
5885 /* This check is needed to return a message if the function is not defined
5886 * for the AI of the player. */
5887 if (pplayer && pplayer->ai) {
5888 if (pplayer->ai->funcs.player_console) {
5889 cmd_reply(CMD_AICMD, caller, C_OK,
5890 _("AI console for player %s. Command: '%s'."),
5891 player_name(pplayer), cmd);
5892 CALL_PLR_AI_FUNC(player_console, pplayer, pplayer, cmd);
5893 ret = TRUE;
5894 } else {
5895 cmd_reply(CMD_AICMD, caller, C_FAIL,
5896 _("No AI console defined for the AI '%s' of player %s."),
5897 ai_name(pplayer->ai), player_name(pplayer));
5898 }
5899 } else {
5900 cmd_reply(CMD_AICMD, caller, C_FAIL, _("No AI defined for player %s."),
5901 player_name(pplayer));
5902 }
5903
5904 cleanup:
5905 free_tokens(tokens, ntokens);
5906 return ret;
5907}
5908
5909/* Define the possible arguments to the fcdb command */
5910#define SPECENUM_NAME fcdb_args
5911#define SPECENUM_VALUE0 FCDB_RELOAD
5912#define SPECENUM_VALUE0NAME "reload"
5913#define SPECENUM_VALUE1 FCDB_LUA
5914#define SPECENUM_VALUE1NAME "lua"
5915#define SPECENUM_COUNT FCDB_COUNT
5916#include "specenum_gen.h"
5917
5918/**********************************************************************/
5921static const char *fcdb_accessor(int i)
5922{
5923 i = CLIP(0, i, fcdb_args_max());
5924 return fcdb_args_name((enum fcdb_args) i);
5925}
5926
5927/**********************************************************************/
5930static bool fcdb_command(struct connection *caller, char *arg, bool check)
5931{
5932 enum m_pre_result result;
5933 int ind, ntokens;
5934 char *token[1];
5935 bool ret = TRUE;
5936 bool usage = FALSE;
5937
5938#ifndef HAVE_FCDB
5939 cmd_reply(CMD_FCDB, caller, C_FAIL,
5940 _("Freeciv database script deactivated at compile time."));
5941 return FALSE;
5942#endif
5943
5944 if (!srvarg.fcdb_enabled) {
5945 /* Not supposed to be used. It isn't initialized. */
5946 cmd_reply(CMD_FCDB, caller, C_FAIL,
5947 _("Freeciv database script not activated at server start. "
5948 "See the Freeciv server's --auth command line option."));
5949 return FALSE;
5950 }
5951
5952 ntokens = get_tokens(arg, token, 1, TOKEN_DELIMITERS);
5953
5954 if (ntokens > 0) {
5955 /* match the argument */
5956 result = match_prefix(fcdb_accessor, FCDB_COUNT, 0,
5957 fc_strncasecmp, NULL, token[0], &ind);
5958
5959 switch (result) {
5960 case M_PRE_EXACT:
5961 case M_PRE_ONLY:
5962 /* we have a match */
5963 break;
5964 case M_PRE_AMBIGUOUS:
5965 cmd_reply(CMD_FCDB, caller, C_FAIL,
5966 _("Ambiguous fcdb command."));
5967 ret = FALSE;
5968 goto cleanup;
5969 break;
5970 case M_PRE_EMPTY:
5971 case M_PRE_LONG:
5972 case M_PRE_FAIL:
5973 case M_PRE_LAST:
5974 usage = TRUE;
5975 break;
5976 }
5977 } else {
5978 usage = TRUE;
5979 }
5980
5981 if (usage) {
5982 char buf[256] = "";
5983 enum fcdb_args valid_args;
5984
5985 for (valid_args = fcdb_args_begin();
5986 valid_args != fcdb_args_end();
5987 valid_args = fcdb_args_next(valid_args)) {
5988 cat_snprintf(buf, sizeof(buf), "'%s'",
5989 fcdb_args_name(valid_args));
5990 if (valid_args != fcdb_args_max()) {
5991 cat_snprintf(buf, sizeof(buf), ", ");
5992 }
5993 }
5994
5995 cmd_reply(CMD_FCDB, caller, C_FAIL,
5996 _("The valid arguments are: %s."), buf);
5997 ret = FALSE;
5998 goto cleanup;
5999 }
6000
6001 if (check) {
6002 ret = TRUE;
6003 goto cleanup;
6004 }
6005
6006 switch (ind) {
6007 case FCDB_RELOAD:
6008 /* Reload database lua script. */
6010 script_fcdb_init(NULL);
6011 break;
6012
6013 case FCDB_LUA:
6014 /* Skip whitespaces. */
6015 arg = skip_leading_spaces(arg);
6016 /* Skip the base argument 'lua'. */
6017 arg += 3;
6018 /* Now execute the scriptlet. */
6019 ret = script_fcdb_do_string(caller, arg);
6020 break;
6021 }
6022
6023 cleanup:
6024
6025 free_tokens(token, ntokens);
6026
6027 return ret;
6028}
6029
6030/**********************************************************************/
6033static void start_cmd_reply(struct connection *caller, bool notify, char *msg)
6034{
6035 cmd_reply(CMD_START_GAME, caller, C_FAIL, "%s", msg);
6036 if (notify) {
6037 notify_conn(NULL, NULL, E_SETTING, ftc_server, "%s", msg);
6038 }
6039}
6040
6041/**********************************************************************/
6044bool start_command(struct connection *caller, bool check, bool notify)
6045{
6046 int human_players;
6047
6048 switch (server_state()) {
6049 case S_S_INITIAL:
6050 /* Sanity check scenario */
6051 if (game.info.is_new_game && !check) {
6052 if (0 < map_startpos_count()
6054 /* If we load a pre-generated map (i.e., a scenario) it is possible
6055 * to increase the number of players beyond the number supported by
6056 * the scenario. The solution is a hack: cut the extra players
6057 * when the game starts. */
6058 log_verbose("Reduced maxplayers from %d to %d to fit "
6059 "to the number of start positions.",
6062 }
6063
6065 int i;
6066 struct player *pplayer;
6067
6068 for (i = player_slot_count() - 1; i >= 0; i--) {
6069 pplayer = player_by_number(i);
6070 if (pplayer) {
6071 server_remove_player(pplayer);
6072 }
6074 break;
6075 }
6076 }
6077
6078 log_verbose("Had to cut down the number of players to the "
6079 "number of map start positions, there must be "
6080 "something wrong with the savegame or you "
6081 "adjusted the maxplayers value.");
6082 }
6083 }
6084
6085 human_players = 0;
6086 players_iterate(plr) {
6087 if (is_human(plr)) {
6088 human_players++;
6089 }
6091
6092 /* check min_players.
6093 * Allow continuing of savegames where some of the original
6094 * players have died */
6096 && human_players < game.server.min_players) {
6097 char buf[512] = "";
6098
6099 fc_snprintf(buf, sizeof(buf),
6100 _("Not enough human players ('minplayers' server setting has value %d); game will not start."),
6102 start_cmd_reply(caller, notify, buf);
6103 return FALSE;
6104 } else if (player_count() < 1) {
6105 /* At least one player required */
6106 start_cmd_reply(caller, notify,
6107 _("No players; game will not start."));
6108 return FALSE;
6109 } else if (normal_player_count() > server.playable_nations) {
6110 if (nation_set_count() > 1) {
6111 start_cmd_reply(caller, notify,
6112 _("Not enough nations in the current nation set "
6113 "for all players; game will not start. "
6114 "(See 'nationset' setting.)"));
6115 } else {
6116 start_cmd_reply(caller, notify,
6117 _("Not enough nations for all players; game will "
6118 "not start."));
6119 }
6120 return FALSE;
6121 } else if (strlen(game.server.start_units) == 0 && !game.server.start_city) {
6122 start_cmd_reply(caller, notify,
6123 _("Neither 'startcity' nor 'startunits' setting gives "
6124 "players anything to start game with; game will "
6125 "not start."));
6126 return FALSE;
6127 } else if (check) {
6128 return TRUE;
6129 } else if (!caller) {
6130 if (notify) {
6131 /* Called from handle_player_ready()
6132 * Last player just toggled ready-status. */
6133 notify_conn(NULL, NULL, E_SETTING, ftc_game_start,
6134 _("All players are ready; starting game."));
6135 }
6136 start_game();
6137 return TRUE;
6138 } else if (NULL == caller->playing || caller->observer) {
6139 /* A detached or observer player can't do /start. */
6140 return TRUE;
6141 } else {
6142 /* This might trigger recursive call to start_command() if this is
6143 * last player who gets ready. In that case caller is NULL. */
6145 return TRUE;
6146 }
6147 case S_S_OVER:
6148 start_cmd_reply(caller, notify,
6149 /* TRANS: given when /start is invoked during gameover. */
6150 _("Cannot start the game: the game is waiting for all clients "
6151 "to disconnect."));
6152 return FALSE;
6153 case S_S_RUNNING:
6154 start_cmd_reply(caller, notify,
6155 /* TRANS: given when /start is invoked while the game
6156 * is running. */
6157 _("Cannot start the game: it is already running."));
6158 return FALSE;
6159 }
6160 log_error("Unknown server state variant: %d.", server_state());
6161 return FALSE;
6162}
6163
6164/**********************************************************************/
6167static bool cut_client_connection(struct connection *caller, char *name,
6168 bool check)
6169{
6170 enum m_pre_result match_result;
6171 struct connection *ptarget;
6172
6173 ptarget = conn_by_user_prefix(name, &match_result);
6174
6175 if (!ptarget) {
6176 cmd_reply_no_such_conn(CMD_CUT, caller, name, match_result);
6177 return FALSE;
6178 } else if (check) {
6179 return TRUE;
6180 }
6181
6182 if (conn_controls_player(ptarget)) {
6183 /* If we cut the connection, unassign the login name.*/
6185 ptarget->playing->unassigned_user = TRUE;
6186 }
6187
6189 _("Cutting connection %s."), ptarget->username);
6190 connection_close_server(ptarget, _("connection cut"));
6191
6192 return TRUE;
6193}
6194
6195
6196/**********************************************************************/
6199static time_t *time_duplicate(const time_t *t)
6200{
6201 time_t *d = fc_malloc(sizeof(*d));
6202 *d = *t;
6203 return d;
6204}
6205
6206/**********************************************************************/
6210bool conn_is_kicked(struct connection *pconn, int *time_remaining)
6211{
6212 time_t time_of_addr_kick, time_of_user_kick;
6213 time_t now, time_of_kick = 0;
6214
6215 if (NULL != time_remaining) {
6216 *time_remaining = 0;
6217 }
6218
6221 fc_assert_ret_val(NULL != pconn, FALSE);
6222
6223 if (kick_hash_lookup(kick_table_by_addr, pconn->server.ipaddr,
6224 &time_of_addr_kick)) {
6225 time_of_kick = time_of_addr_kick;
6226 }
6227 if (kick_hash_lookup(kick_table_by_user, pconn->username,
6228 &time_of_user_kick)
6229 && time_of_user_kick > time_of_kick) {
6230 time_of_kick = time_of_user_kick;
6231 }
6232
6233 if (0 == time_of_kick) {
6234 return FALSE; /* Not found. */
6235 }
6236
6237 now = time(NULL);
6238 if (now - time_of_kick > game.server.kick_time) {
6239 /* Kick timeout expired. */
6240 if (0 != time_of_addr_kick) {
6241 kick_hash_remove(kick_table_by_addr, pconn->server.ipaddr);
6242 }
6243 if (0 != time_of_user_kick) {
6244 kick_hash_remove(kick_table_by_user, pconn->username);
6245 }
6246 return FALSE;
6247 }
6248
6249 if (NULL != time_remaining) {
6250 *time_remaining = game.server.kick_time - (now - time_of_kick);
6251 }
6252 return TRUE;
6253}
6254
6255/**********************************************************************/
6258static bool kick_command(struct connection *caller, char *name, bool check)
6259{
6260 char ipaddr[FC_MEMBER_SIZEOF(struct connection, server.ipaddr)];
6261 struct connection *pconn;
6262 enum m_pre_result match_result;
6263 time_t now;
6264
6266 pconn = conn_by_user_prefix(name, &match_result);
6267 if (NULL == pconn) {
6268 cmd_reply_no_such_conn(CMD_KICK, caller, name, match_result);
6269 return FALSE;
6270 }
6271
6272 if (NULL != caller && ALLOW_ADMIN > conn_get_access(caller)) {
6273 const int MIN_UNIQUE_CONNS = 3;
6274 const char *unique_ipaddr[MIN_UNIQUE_CONNS];
6275 int i, num_unique_connections = 0;
6276
6277 if (pconn == caller) {
6278 cmd_reply(CMD_KICK, caller, C_FAIL, _("You may not kick yourself."));
6279 return FALSE;
6280 }
6281
6283 for (i = 0; i < num_unique_connections; i++) {
6284 if (0 == strcmp(unique_ipaddr[i], aconn->server.ipaddr)) {
6285 /* Already listed. */
6286 break;
6287 }
6288 }
6289 if (i >= num_unique_connections) {
6290 num_unique_connections++;
6291 if (MIN_UNIQUE_CONNS <= num_unique_connections) {
6292 /* We have enough already. */
6293 break;
6294 }
6295 unique_ipaddr[num_unique_connections - 1] = aconn->server.ipaddr;
6296 }
6298
6299 if (MIN_UNIQUE_CONNS > num_unique_connections) {
6300 cmd_reply(CMD_KICK, caller, C_FAIL,
6301 _("There must be at least %d unique connections to the "
6302 "server for this command to be valid."), MIN_UNIQUE_CONNS);
6303 return FALSE;
6304 }
6305 }
6306
6307 if (check) {
6308 return TRUE;
6309 }
6310
6311 sz_strlcpy(ipaddr, pconn->server.ipaddr);
6312 now = time(NULL);
6313 kick_hash_replace(kick_table_by_addr, ipaddr, now);
6314
6316 if (0 != strcmp(ipaddr, aconn->server.ipaddr)) {
6317 continue;
6318 }
6319
6320 if (conn_controls_player(aconn)) {
6321 /* Unassign the username. */
6322 sz_strlcpy(aconn->playing->username, _(ANON_USER_NAME));
6323 aconn->playing->unassigned_user = TRUE;
6324 }
6325
6326 kick_hash_replace(kick_table_by_user, aconn->username, now);
6327
6328 connection_close_server(aconn, _("kicked"));
6330
6331 return TRUE;
6332}
6333
6334
6335/**********************************************************************/
6339static void show_help_intro(struct connection *caller,
6340 enum command_id help_cmd)
6341{
6342 /* This is formatted like extra_help entries for settings and commands: */
6343 char *help = fc_strdup(
6344 _("Welcome - this is the introductory help text for the Freeciv "
6345 "server.\n"
6346 "\n"
6347 "Two important server concepts are Commands and Options. Commands, "
6348 "such as 'help', are used to interact with the server. Some commands "
6349 "take one or more arguments, separated by spaces. In many cases "
6350 "commands and command arguments may be abbreviated. Options are "
6351 "settings which control the server as it is running.\n"
6352 "\n"
6353 "To find out how to get more information about commands and options, "
6354 "use 'help help'.\n"
6355 "\n"
6356 "For the impatient, the main commands to get going are:\n"
6357 " show - to see current options\n"
6358 " set - to set options\n"
6359 " start - to start the game once players have connected\n"
6360 " save - to save the current game\n"
6361 " quit - to exit"));
6362
6364 cmd_reply(help_cmd, caller, C_COMMENT, "%s", help);
6365 FC_FREE(help);
6366}
6367
6368/**********************************************************************/
6372static void show_help_command(struct connection *caller,
6373 enum command_id help_cmd,
6374 enum command_id id)
6375{
6376 const struct command *cmd = command_by_number(id);
6377
6378 if (command_short_help(cmd)) {
6379 cmd_reply(help_cmd, caller, C_COMMENT,
6380 /* TRANS: <untranslated name> - translated short help */
6381 _("Command: %s - %s"),
6382 command_name(cmd),
6383 command_short_help(cmd));
6384 } else {
6385 cmd_reply(help_cmd, caller, C_COMMENT,
6386 /* TRANS: <untranslated name> */
6387 _("Command: %s"),
6388 command_name(cmd));
6389 }
6390 if (command_synopsis(cmd)) {
6391 /* line up the synopsis lines: */
6392 const char *syn = _("Synopsis: ");
6393 size_t synlen = strlen(syn);
6394 char prefix[40];
6395
6396 fc_snprintf(prefix, sizeof(prefix), "%*s", (int) synlen, " ");
6397 cmd_reply_prefix(help_cmd, caller, C_COMMENT, prefix,
6398 "%s%s", syn, command_synopsis(cmd));
6399 }
6400 cmd_reply(help_cmd, caller, C_COMMENT,
6401 _("Level: %s"), cmdlevel_name(command_level(cmd)));
6402 {
6403 char *help = command_extra_help(cmd);
6404
6405 if (help) {
6407 cmd_reply(help_cmd, caller, C_COMMENT, _("Description:"));
6408 cmd_reply_prefix(help_cmd, caller, C_COMMENT, " ", " %s", help);
6409 FC_FREE(help);
6410 }
6411 }
6412}
6413
6414/**********************************************************************/
6418static void show_help_command_list(struct connection *caller,
6419 enum command_id help_cmd)
6420{
6421 enum command_id i;
6422
6423 cmd_reply(help_cmd, caller, C_COMMENT, horiz_line);
6424 cmd_reply(help_cmd, caller, C_COMMENT,
6425 _("The following server commands are available:"));
6426 cmd_reply(help_cmd, caller, C_COMMENT, horiz_line);
6427 if (!caller && con_get_style()) {
6428 for (i = 0; i < CMD_NUM; i++) {
6429 cmd_reply(help_cmd, caller, C_COMMENT, "%s", command_name_by_number(i));
6430 }
6431 } else {
6432 char buf[MAX_LEN_CONSOLE_LINE];
6433 int j;
6434
6435 buf[0] = '\0';
6436 for (i=0, j=0; i<CMD_NUM; i++) {
6437 if (may_use(caller, i)) {
6438 cat_snprintf(buf, sizeof(buf), "%-19s", command_name_by_number(i));
6439 if ((++j % 4) == 0) {
6440 cmd_reply(help_cmd, caller, C_COMMENT, "%s", buf);
6441 buf[0] = '\0';
6442 }
6443 }
6444 }
6445 if (buf[0] != '\0') {
6446 cmd_reply(help_cmd, caller, C_COMMENT, "%s", buf);
6447 }
6448 }
6449 cmd_reply(help_cmd, caller, C_COMMENT, horiz_line);
6450}
6451
6452/**********************************************************************/
6456static void cmd_reply_matches(enum command_id cmd,
6457 struct connection *caller,
6458 m_pre_accessor_fn_t accessor_fn,
6459 int *matches, int num_matches)
6460{
6461 char buf[MAX_LEN_MSG];
6462 const char *src, *end;
6463 char *dest;
6464 int i;
6465
6466 if (accessor_fn == NULL || matches == NULL || num_matches < 1) {
6467 return;
6468 }
6469
6470 dest = buf;
6471 end = buf + sizeof(buf) - 1;
6472
6473 for (i = 0; i < num_matches && dest < end; i++) {
6474 src = accessor_fn(matches[i]);
6475 if (!src) {
6476 continue;
6477 }
6478 if (dest != buf) {
6479 *dest++ = ' ';
6480 }
6481 while (*src != '\0' && dest < end) {
6482 *dest++ = *src++;
6483 }
6484 }
6485 *dest = '\0';
6486
6487 cmd_reply(cmd, caller, C_COMMENT, _("Possible matches: %s"), buf);
6488}
6489
6490/**************************************************************************
6491 Additional 'help' arguments
6492**************************************************************************/
6493#define SPECENUM_NAME help_general_args
6494#define SPECENUM_VALUE0 HELP_GENERAL_COMMANDS
6495#define SPECENUM_VALUE0NAME "commands"
6496#define SPECENUM_VALUE1 HELP_GENERAL_OPTIONS
6497#define SPECENUM_VALUE1NAME "options"
6498#define SPECENUM_COUNT HELP_GENERAL_COUNT
6499#include "specenum_gen.h"
6500
6501/**************************************************************************
6502 Unified indices for help arguments:
6503 CMD_NUM - Server commands
6504 HELP_GENERAL_NUM - General help arguments, above
6505 settings_number() - Server options
6506**************************************************************************/
6507#define HELP_ARG_NUM (CMD_NUM + HELP_GENERAL_COUNT + settings_number())
6508
6509/**********************************************************************/
6512static const char *helparg_accessor(int i)
6513{
6514 if (i < CMD_NUM) {
6515 return command_name_by_number(i);
6516 }
6517
6518 i -= CMD_NUM;
6519 if (i < HELP_GENERAL_COUNT) {
6520 return help_general_args_name((enum help_general_args) i);
6521 }
6522
6523 i -= HELP_GENERAL_COUNT;
6524 return optname_accessor(i);
6525}
6526
6527/**********************************************************************/
6530static bool show_help(struct connection *caller, char *arg)
6531{
6532 int matches[64], num_matches = 0;
6533 enum m_pre_result match_result;
6534 int ind;
6535
6537 /* no commands means no help, either */
6538
6540 fc_strncasecmp, NULL, arg, &ind, matches,
6541 ARRAY_SIZE(matches), &num_matches);
6542
6543 if (match_result == M_PRE_EMPTY) {
6544 show_help_intro(caller, CMD_HELP);
6545 return FALSE;
6546 }
6547 if (match_result == M_PRE_AMBIGUOUS) {
6548 cmd_reply(CMD_HELP, caller, C_FAIL,
6549 _("Help argument '%s' is ambiguous."), arg);
6551 matches, num_matches);
6552 return FALSE;
6553 }
6554 if (match_result == M_PRE_FAIL) {
6555 cmd_reply(CMD_HELP, caller, C_FAIL,
6556 _("No match for help argument '%s'."), arg);
6557 return FALSE;
6558 }
6559
6560 /* other cases should be above */
6561 fc_assert_ret_val(match_result < M_PRE_AMBIGUOUS, FALSE);
6562
6563 if (ind < CMD_NUM) {
6564 show_help_command(caller, CMD_HELP, ind);
6565 return TRUE;
6566 }
6567 ind -= CMD_NUM;
6568
6569 if (ind == HELP_GENERAL_OPTIONS) {
6571 return TRUE;
6572 }
6573 if (ind == HELP_GENERAL_COMMANDS) {
6575 return TRUE;
6576 }
6577 ind -= HELP_GENERAL_COUNT;
6578
6579 if (ind < settings_number()) {
6580 show_help_option(caller, CMD_HELP, ind);
6581 return TRUE;
6582 }
6583
6584 /* should have finished by now */
6585 log_error("Bug in show_help!");
6586 return FALSE;
6587}
6588
6589/**********************************************************************/
6592static void show_connections(struct connection *caller)
6593{
6594 char buf[MAX_LEN_CONSOLE_LINE];
6595
6596 cmd_reply(CMD_LIST, caller, C_COMMENT,
6597 _("List of connections to server:"));
6599
6600 if (conn_list_size(game.all_connections) == 0) {
6601 cmd_reply(CMD_LIST, caller, C_COMMENT, _("<no connections>"));
6602 } else {
6604 sz_strlcpy(buf, conn_description(pconn));
6605 if (pconn->established) {
6606 cat_snprintf(buf, sizeof(buf), " command access level %s",
6607 cmdlevel_name(pconn->access_level));
6608 }
6609 cmd_reply(CMD_LIST, caller, C_COMMENT, "%s", buf);
6611 }
6613}
6614
6615/**********************************************************************/
6618static void show_delegations(struct connection *caller)
6619{
6620 bool empty = TRUE;
6621
6622 cmd_reply(CMD_LIST, caller, C_COMMENT, _("List of all delegations:"));
6624
6625 players_iterate(pplayer) {
6626 const char *delegate_to = player_delegation_get(pplayer);
6627 if (delegate_to != NULL) {
6628 const char *owner =
6629 player_delegation_active(pplayer) ? pplayer->server.orig_username
6630 : pplayer->username;
6632 cmd_reply(CMD_LIST, caller, C_COMMENT,
6633 /* TRANS: last %s is either " (active)" or empty string */
6634 _("%s delegates control over player '%s' to user %s%s."),
6635 owner, player_name(pplayer), delegate_to,
6636 /* TRANS: preserve leading space */
6637 player_delegation_active(pplayer) ? _(" (active)") : "");
6638 empty = FALSE;
6639 }
6641
6642 if (empty) {
6643 cmd_reply(CMD_LIST, caller, C_COMMENT, _("No delegations defined."));
6644 }
6645
6647}
6648
6649/**********************************************************************/
6652static bool show_ignore(struct connection *caller)
6653{
6654 char buf[128];
6655 int n = 1;
6656
6657 if (NULL == caller) {
6658 cmd_reply(CMD_IGNORE, caller, C_FAIL,
6659 _("That would be rather silly, since you are not a player."));
6660 return FALSE;
6661 }
6662
6663 if (0 == conn_pattern_list_size(caller->server.ignore_list)) {
6664 cmd_reply(CMD_LIST, caller, C_COMMENT, _("Your ignore list is empty."));
6665 return TRUE;
6666 }
6667
6668 cmd_reply(CMD_LIST, caller, C_COMMENT, _("Your ignore list:"));
6670 conn_pattern_list_iterate(caller->server.ignore_list, ppattern) {
6671 conn_pattern_to_string(ppattern, buf, sizeof(buf));
6672 cmd_reply(CMD_LIST, caller, C_COMMENT, "%d: %s", n++, buf);
6675
6676 return TRUE;
6677}
6678
6679/**********************************************************************/
6682void show_players(struct connection *caller)
6683{
6684 cmd_reply(CMD_LIST, caller, C_COMMENT, _("List of players:"));
6686
6687 if (player_count() == 0) {
6688 cmd_reply(CMD_LIST, caller, C_COMMENT, _("<no players>"));
6689 } else {
6690 players_iterate(pplayer) {
6691 char buf[MAX_LEN_CONSOLE_LINE];
6692 int n;
6693
6694 /* Low access level callers don't get to see barbarians in list: */
6695 if (is_barbarian(pplayer) && caller
6696 && (caller->access_level < ALLOW_CTRL)) {
6697 continue;
6698 }
6699
6700 /* The output for each player looks like:
6701 *
6702 * <Player name> [color]: Team[, Nation][, Username][, Status]
6703 * AI/Barbarian/Human[, AI type, skill level][, Connections]
6704 * [Details for each connection]
6705 */
6706
6707 /* '<Player name> [color]: [Nation][, Username][, Status]' */
6708 buf[0] = '\0';
6709 cat_snprintf(buf, sizeof(buf), "%s [%s]: %s", player_name(pplayer),
6710 player_color_ftstr(pplayer),
6711 team_name_translation(pplayer->team));
6712 if (!game.info.is_new_game) {
6713 cat_snprintf(buf, sizeof(buf), ", %s",
6715 }
6716 if (strlen(pplayer->username) > 0
6717 && strcmp(pplayer->username, "nouser") != 0) {
6718 cat_snprintf(buf, sizeof(buf), _(", user %s"), pplayer->username);
6719 }
6720 if (S_S_INITIAL == server_state() && pplayer->is_connected) {
6721 if (pplayer->is_ready) {
6722 sz_strlcat(buf, _(", ready"));
6723 } else {
6724 /* Emphasizes this */
6725 n = strlen(buf);
6726 featured_text_apply_tag(_(", not ready"),
6727 buf + n, sizeof(buf) - n,
6729 ftc_changed);
6730 }
6731 } else if (!pplayer->is_alive) {
6732 sz_strlcat(buf, _(", Dead"));
6733 }
6734 cmd_reply(CMD_LIST, caller, C_COMMENT, "%s", buf);
6735
6736 /* ' AI/Barbarian/Human[, skill level][, Connections]' */
6737 buf[0] = '\0';
6738 if (is_barbarian(pplayer)) {
6739 sz_strlcat(buf, _("Barbarian"));
6740 } else if (is_ai(pplayer)) {
6741 sz_strlcat(buf, _("AI"));
6742 } else {
6743 sz_strlcat(buf, _("Human"));
6744 }
6745 if (is_ai(pplayer)) {
6746 cat_snprintf(buf, sizeof(buf), _(", %s"), ai_name(pplayer->ai));
6747 cat_snprintf(buf, sizeof(buf), _(", difficulty level %s"),
6748 ai_level_translated_name(pplayer->ai_common.skill_level));
6749 }
6750 n = conn_list_size(pplayer->connections);
6751 if (n > 0) {
6752 cat_snprintf(buf, sizeof(buf),
6753 PL_(", %d connection:", ", %d connections:", n), n);
6754 }
6755 cmd_reply(CMD_LIST, caller, C_COMMENT, " %s", buf);
6756
6757 /* ' [Details for each connection]' */
6758 conn_list_iterate(pplayer->connections, pconn) {
6759 fc_snprintf(buf, sizeof(buf),
6760 _("%s from %s (command access level %s), "
6761 "bufsize=%dkb"), pconn->username, pconn->addr,
6762 cmdlevel_name(pconn->access_level),
6763 (pconn->send_buffer->nsize >> 10));
6764 if (pconn->observer) {
6765 /* TRANS: preserve leading space */
6766 sz_strlcat(buf, _(" (observer mode)"));
6767 }
6768 cmd_reply(CMD_LIST, caller, C_COMMENT, " %s", buf);
6771 }
6773}
6774
6775/**********************************************************************/
6779static void show_rulesets(struct connection *caller)
6780{
6781 struct strvec *serv_list;
6782
6783 cmd_reply(CMD_LIST, caller, C_COMMENT,
6784 /* TRANS: don't translate text between '' */
6785 _("List of rulesets available with '%sread' command:"),
6786 (caller ? "/" : ""));
6788
6789 serv_list = get_init_script_choices();
6790 strvec_iterate(serv_list, s) {
6791 cmd_reply(CMD_LIST, caller, C_COMMENT, "%s", s);
6793 strvec_destroy(serv_list);
6794
6796}
6797
6798/**********************************************************************/
6801static void show_scenarios(struct connection *caller)
6802{
6803 char buf[MAX_LEN_CONSOLE_LINE];
6804 struct fileinfo_list *files;
6805
6806 cmd_reply(CMD_LIST, caller, C_COMMENT, _("List of scenarios available:"));
6808
6809 files = fileinfolist_infix(get_scenario_dirs(), ".sav", TRUE);
6810
6811 fileinfo_list_iterate(files, pfile) {
6812 struct section_file *sf = secfile_load_section(pfile->fullname, "scenario", TRUE);
6813
6814 if (secfile_lookup_bool_default(sf, TRUE, "scenario.is_scenario")) {
6815 fc_snprintf(buf, sizeof(buf), "%s", pfile->name);
6816 cmd_reply(CMD_LIST, caller, C_COMMENT, "%s", buf);
6817 }
6819 fileinfo_list_destroy(files);
6820
6822}
6823
6824/**********************************************************************/
6827static void show_nationsets(struct connection *caller)
6828{
6829 cmd_reply(CMD_LIST, caller, C_COMMENT,
6830 /* TRANS: don't translate text between '' */
6831 _("List of nation sets available for 'nationset' option:"));
6833
6834 nation_sets_iterate(pset) {
6835 const char *description = nation_set_description(pset);
6836 int num_nations = 0;
6837 nations_iterate(pnation) {
6838 if (is_nation_playable(pnation) && nation_is_in_set(pnation, pset)) {
6839 num_nations++;
6840 }
6842 cmd_reply(CMD_LIST, caller, C_COMMENT,
6843 /* TRANS: nation set description; %d refers to number of playable
6844 * nations in set */
6845 PL_(" %-10s %s (%d playable)",
6846 " %-10s %s (%d playable)", num_nations),
6848 num_nations);
6849 if (strlen(description) > 0) {
6850 static const char prefix[] = " ";
6851 char *translated = fc_strdup(_(description));
6853 cmd_reply_prefix(CMD_LIST, caller, C_COMMENT, prefix, "%s%s",
6854 prefix, translated);
6855 }
6857
6859}
6860
6861/**********************************************************************/
6864static void show_teams(struct connection *caller)
6865{
6866 /* Currently this just lists all teams (typically 32 of them) with their
6867 * names and # of players on the team. This could probably be improved. */
6868 cmd_reply(CMD_LIST, caller, C_COMMENT, _("List of teams:"));
6870
6871 teams_iterate(pteam) {
6872 const struct player_list *members = team_members(pteam);
6873
6874 /* PL_() is needed here because some languages may differentiate
6875 * between 2 and 3 (although English does not). */
6876 cmd_reply(CMD_LIST, caller, C_COMMENT,
6877 /* TRANS: There will always be at least 2 players here. */
6878 PL_("%2d : '%s' : %d player :",
6879 "%2d : '%s' : %d players :",
6880 player_list_size(members)),
6881 team_index(pteam), team_name_translation(pteam),
6882 player_list_size(members));
6883 player_list_iterate(members, pplayer) {
6884 cmd_reply(CMD_LIST, caller, C_COMMENT, " %s", player_name(pplayer));
6887
6889}
6890
6891/**********************************************************************/
6894static void show_mapimg(struct connection *caller, enum command_id cmd)
6895{
6896 int id;
6897
6898 if (mapimg_count() == 0) {
6899 cmd_reply(cmd, caller, C_OK, _("No map image definitions."));
6900 } else {
6901 cmd_reply(cmd, caller, C_COMMENT, _("List of map image definitions:"));
6902 cmd_reply(cmd, caller, C_COMMENT, horiz_line);
6903 for (id = 0; id < mapimg_count(); id++) {
6904 char str[MAX_LEN_MAPDEF] = "";
6905 mapimg_show(id, str, sizeof(str), FALSE);
6906 cmd_reply(cmd, caller, C_COMMENT, _("[%2d] %s"), id, str);
6907 }
6908 cmd_reply(cmd, caller, C_COMMENT, horiz_line);
6909 }
6910}
6911
6912/**********************************************************************/
6915static void show_colors(struct connection *caller)
6916{
6917 cmd_reply(CMD_LIST, caller, C_COMMENT, _("List of player colors:"));
6919 if (player_count() == 0) {
6920 cmd_reply(CMD_LIST, caller, C_COMMENT, _("<no players>"));
6921 } else {
6922 players_iterate(pplayer) {
6923 cmd_reply(CMD_LIST, caller, C_COMMENT, _("%s (user %s): [%s]"),
6924 player_name(pplayer), pplayer->username,
6925 player_color_ftstr(pplayer));
6927 }
6929}
6930
6931/**************************************************************************
6932 '/list' arguments
6933**************************************************************************/
6934#define SPECENUM_NAME list_args
6935#define SPECENUM_VALUE0 LIST_COLORS
6936#define SPECENUM_VALUE0NAME "colors"
6937#define SPECENUM_VALUE1 LIST_CONNECTIONS
6938#define SPECENUM_VALUE1NAME "connections"
6939#define SPECENUM_VALUE2 LIST_DELEGATIONS
6940#define SPECENUM_VALUE2NAME "delegations"
6941#define SPECENUM_VALUE3 LIST_IGNORE
6942#define SPECENUM_VALUE3NAME "ignored users"
6943#define SPECENUM_VALUE4 LIST_MAPIMG
6944#define SPECENUM_VALUE4NAME "map image definitions"
6945#define SPECENUM_VALUE5 LIST_PLAYERS
6946#define SPECENUM_VALUE5NAME "players"
6947#define SPECENUM_VALUE6 LIST_RULESETS
6948#define SPECENUM_VALUE6NAME "rulesets"
6949#define SPECENUM_VALUE7 LIST_SCENARIOS
6950#define SPECENUM_VALUE7NAME "scenarios"
6951#define SPECENUM_VALUE8 LIST_NATIONSETS
6952#define SPECENUM_VALUE8NAME "nationsets"
6953#define SPECENUM_VALUE9 LIST_TEAMS
6954#define SPECENUM_VALUE9NAME "teams"
6955#define SPECENUM_VALUE10 LIST_VOTES
6956#define SPECENUM_VALUE10NAME "votes"
6957#include "specenum_gen.h"
6958
6959/**********************************************************************/
6962static const char *list_accessor(int i)
6963{
6964 i = CLIP(0, i, list_args_max());
6965 return list_args_name((enum list_args) i);
6966}
6967
6968/**********************************************************************/
6971static bool show_list(struct connection *caller, char *arg)
6972{
6973 enum m_pre_result match_result;
6974 int ind_int;
6975 enum list_args ind;
6976
6978 match_result = match_prefix(list_accessor, list_args_max() + 1, 0,
6979 fc_strncasecmp, NULL, arg, &ind_int);
6980 ind = ind_int;
6981
6982 if (match_result > M_PRE_EMPTY) {
6983 cmd_reply(CMD_LIST, caller, C_SYNTAX,
6984 _("Bad list argument: '%s'. Try '%shelp list'."),
6985 arg, (caller ? "/" : ""));
6986 return FALSE;
6987 }
6988
6989 if (match_result == M_PRE_EMPTY) {
6990 ind = LIST_PLAYERS;
6991 }
6992
6993 switch (ind) {
6994 case LIST_COLORS:
6995 show_colors(caller);
6996 return TRUE;
6997 case LIST_CONNECTIONS:
6998 show_connections(caller);
6999 return TRUE;
7000 case LIST_DELEGATIONS:
7001 show_delegations(caller);
7002 return TRUE;
7003 case LIST_IGNORE:
7004 return show_ignore(caller);
7005 case LIST_MAPIMG:
7006 show_mapimg(caller, CMD_LIST);
7007 return TRUE;
7008 case LIST_PLAYERS:
7009 show_players(caller);
7010 return TRUE;
7011 case LIST_RULESETS:
7012 show_rulesets(caller);
7013 return TRUE;
7014 case LIST_SCENARIOS:
7015 show_scenarios(caller);
7016 return TRUE;
7017 case LIST_NATIONSETS:
7018 show_nationsets(caller);
7019 return TRUE;
7020 case LIST_TEAMS:
7021 show_teams(caller);
7022 return TRUE;
7023 case LIST_VOTES:
7024 show_votes(caller);
7025 return TRUE;
7026 }
7027
7028 cmd_reply(CMD_LIST, caller, C_FAIL,
7029 "Internal error: ind %d in show_list", ind);
7030 log_error("Internal error: ind %d in show_list", ind);
7031 return FALSE;
7032}
7033
7034#ifdef FREECIV_HAVE_LIBREADLINE
7035/********************* RL completion functions ***************************/
7036/* To properly complete both commands, player names, options and filenames
7037 there is one array per type of completion with the commands that
7038 the type is relevant for.
7039*/
7040
7041/**********************************************************************/
7049static char *generic_generator(const char *text, int state, int num,
7050 const char*(*index2str)(int))
7051{
7052 static int list_index, len;
7053 const char *name = ""; /* dummy non-NULL string */
7054 char *mytext = local_to_internal_string_malloc(text);
7055
7056 /* This function takes a string (text) in the local format and must return
7057 * a string in the local format. However comparisons are done against
7058 * names that are in the internal format (UTF-8). Thus we have to convert
7059 * the text function from the local to the internal format before doing
7060 * the comparison, and convert the string we return *back* to the
7061 * local format when returning it. */
7062
7063 /* If this is a new word to complete, initialize now. This includes
7064 saving the length of TEXT for efficiency, and initializing the index
7065 variable to 0. */
7066 if (state == 0) {
7067 list_index = 0;
7068 len = strlen(mytext);
7069 }
7070
7071 /* Return the next name which partially matches: */
7072 while ((num < 0 && name) || (list_index < num)) {
7073 name = index2str(list_index);
7074 list_index++;
7075
7076 if (name != NULL && fc_strncasecmp(name, mytext, len) == 0) {
7077 free(mytext);
7079 }
7080 }
7081 free(mytext);
7082
7083 /* If no names matched, then return NULL. */
7084 return ((char *)NULL);
7085}
7086
7087/**********************************************************************/
7090static char *command_generator(const char *text, int state)
7091{
7092 return generic_generator(text, state, CMD_NUM, command_name_by_number);
7093}
7094
7095/**********************************************************************/
7098static char *option_generator(const char *text, int state)
7099{
7100 return generic_generator(text, state, settings_number(), optname_accessor);
7101}
7102
7103/**********************************************************************/
7106static char *olevel_generator(const char *text, int state)
7107{
7108 return generic_generator(text, state, settings_number() + OLEVELS_NUM + 1,
7109 olvlname_accessor);
7110}
7111
7112/**********************************************************************/
7116static int completion_option;
7117static const char *option_value_accessor(int idx) {
7118 const struct setting *pset = setting_by_number(completion_option);
7119
7120 switch (setting_type(pset)) {
7121 case SST_ENUM:
7122 return setting_enum_val(pset, idx, FALSE);
7123 case SST_BITWISE:
7124 return setting_bitwise_bit(pset, idx, FALSE);
7125 default:
7127 }
7128
7129 return NULL;
7130}
7131
7132/**********************************************************************/
7136static char *option_value_generator(const char *text, int state)
7137{
7138 return generic_generator(text, state, -1, option_value_accessor);
7139}
7140
7141/**********************************************************************/
7144static const char *playername_accessor(int idx)
7145{
7146 const struct player_slot *pslot = player_slot_by_number(idx);
7147
7148 if (!player_slot_is_used(pslot)) {
7149 return NULL;
7150 }
7151
7152 return player_name(player_slot_get_player(pslot));
7153}
7154
7155/**********************************************************************/
7158static char *player_generator(const char *text, int state)
7159{
7160 return generic_generator(text, state, player_slot_count(),
7161 playername_accessor);
7162}
7163
7164/**********************************************************************/
7167static const char *connection_name_accessor(int idx)
7168{
7169 return conn_list_get(game.all_connections, idx)->username;
7170}
7171
7172/**********************************************************************/
7175static char *connection_generator(const char *text, int state)
7176{
7177 return generic_generator(text, state, conn_list_size(game.all_connections),
7178 connection_name_accessor);
7179}
7180
7181/**********************************************************************/
7184static const char *cmdlevel_arg1_accessor(int idx)
7185{
7186 return cmdlevel_name(idx);
7187}
7188
7189/**********************************************************************/
7192static char *cmdlevel_arg1_generator(const char *text, int state)
7193{
7194 return generic_generator(text, state, cmdlevel_max()+1,
7195 cmdlevel_arg1_accessor);
7196}
7197
7198/**********************************************************************/
7202static const char *cmdlevel_arg2_accessor(int idx)
7203{
7204 return ((idx == 0) ? "first" :
7205 (idx == 1) ? "new" :
7206 connection_name_accessor(idx - 2));
7207}
7208
7209/**********************************************************************/
7212static char *cmdlevel_arg2_generator(const char *text, int state)
7213{
7214 return generic_generator(text, state,
7215 /* "first", "new", connection names */
7216 2 + conn_list_size(game.all_connections),
7217 cmdlevel_arg2_accessor);
7218}
7219
7220/**********************************************************************/
7223static const char *aitype_accessor(int idx)
7224{
7225 return get_ai_type(idx)->name;
7226}
7227
7228/**********************************************************************/
7231static char *aitype_generator(const char *text, int state)
7232{
7233 return generic_generator(text, state, ai_type_get_count(),
7234 aitype_accessor);
7235}
7236
7237/**********************************************************************/
7240static char *reset_generator(const char *text, int state)
7241{
7242 return generic_generator(text, state, reset_args_max() + 1, reset_accessor);
7243}
7244
7245/**********************************************************************/
7248static char *vote_generator(const char *text, int state)
7249{
7250 return generic_generator(text, state, -1, vote_arg_accessor);
7251}
7252
7253/**********************************************************************/
7256static char *delegate_generator(const char *text, int state)
7257{
7258 return generic_generator(text, state, delegate_args_max() + 1,
7260}
7261
7262/**********************************************************************/
7265static char *mapimg_generator(const char *text, int state)
7266{
7267 return generic_generator(text, state, mapimg_args_max() + 1,
7269}
7270
7271/**********************************************************************/
7274static char *fcdb_generator(const char *text, int state)
7275{
7276 return generic_generator(text, state, FCDB_COUNT, fcdb_accessor);
7277}
7278
7279/**********************************************************************/
7282static char *lua_generator(const char *text, int state)
7283{
7284 return generic_generator(text, state, lua_args_max() + 1, lua_accessor);
7285}
7286
7287/**********************************************************************/
7290static char *help_generator(const char *text, int state)
7291{
7292 return generic_generator(text, state, HELP_ARG_NUM, helparg_accessor);
7293}
7294
7295/**********************************************************************/
7298static char *list_generator(const char *text, int state)
7299{
7300 return generic_generator(text, state, list_args_max() + 1, list_accessor);
7301}
7302
7303/**********************************************************************/
7307static bool contains_token_before_start(int start, int token, const char *arg,
7308 bool allow_fluff)
7309{
7310 char *str_itr = rl_line_buffer;
7311 int arg_len = strlen(arg);
7312
7313 /* Swallow unwanted tokens and their preceding delimiters */
7314 while (token--) {
7315 while (str_itr < rl_line_buffer + start && !fc_isalnum(*str_itr)) {
7316 str_itr++;
7317 }
7318 while (str_itr < rl_line_buffer + start && fc_isalnum(*str_itr)) {
7319 str_itr++;
7320 }
7321 }
7322
7323 /* Swallow any delimiters before the token we're interested in */
7324 while (str_itr < rl_line_buffer + start && !fc_isalnum(*str_itr)) {
7325 str_itr++;
7326 }
7327
7328 if (fc_strncasecmp(str_itr, arg, arg_len) != 0) {
7329 return FALSE;
7330 }
7331 str_itr += arg_len;
7332
7333 if (fc_isalnum(*str_itr)) {
7334 /* Not a distinct word. */
7335 return FALSE;
7336 }
7337
7338 if (!allow_fluff) {
7339 for (; str_itr < rl_line_buffer + start; str_itr++) {
7340 if (fc_isalnum(*str_itr)) {
7341 return FALSE;
7342 }
7343 }
7344 }
7345
7346 return TRUE;
7347}
7348
7349/**********************************************************************/
7354static bool contains_str_before_start(int start, const char *cmd,
7355 bool allow_fluff)
7356{
7357 return contains_token_before_start(start, 0, cmd, allow_fluff);
7358}
7359
7360/**********************************************************************/
7364static bool is_command(int start)
7365{
7366 char *str_itr;
7367
7368 if (contains_str_before_start(start, command_name_by_number(CMD_HELP), FALSE))
7369 return TRUE;
7370
7371 /* if there is only it is also OK */
7372 str_itr = rl_line_buffer;
7373 while (str_itr - rl_line_buffer < start) {
7374 if (fc_isalnum(*str_itr)) {
7375 return FALSE;
7376 }
7377 str_itr++;
7378 }
7379 return TRUE;
7380}
7381
7382/**********************************************************************/
7385static int num_tokens(int start)
7386{
7387 int res = 0;
7388 bool alnum = FALSE;
7389 char *chptr = rl_line_buffer;
7390
7391 while (chptr - rl_line_buffer < start) {
7392 if (fc_isalnum(*chptr)) {
7393 if (!alnum) {
7394 alnum = TRUE;
7395 res++;
7396 }
7397 } else {
7398 alnum = FALSE;
7399 }
7400 chptr++;
7401 }
7402
7403 return res;
7404}
7405
7406/**************************************************************************
7407 Commands that may be followed by a player name
7408**************************************************************************/
7409static const int player_cmd[] = {
7412 CMD_NOVICE,
7413 CMD_EASY,
7414 CMD_NORMAL,
7415 CMD_HARD,
7417#ifdef FREECIV_DEBUG
7418 CMD_EXPERIMENTAL,
7419#endif
7420 CMD_REMOVE,
7421 CMD_TEAM,
7423 -1
7424};
7425
7426/**********************************************************************/
7429static bool is_player(int start)
7430{
7431 int i = 0;
7432
7433 while (player_cmd[i] != -1) {
7434 if (contains_str_before_start(start, command_name_by_number(player_cmd[i]), FALSE)) {
7435 return TRUE;
7436 }
7437 i++;
7438 }
7439
7440 return FALSE;
7441}
7442
7443/**************************************************************************
7444 Commands that may be followed by a connection name
7445**************************************************************************/
7446static const int connection_cmd[] = {
7447 CMD_CUT,
7448 CMD_KICK,
7449 -1
7450};
7451
7452/**********************************************************************/
7455static bool is_connection(int start)
7456{
7457 int i = 0;
7458
7459 while (connection_cmd[i] != -1) {
7460 if (contains_str_before_start(start,
7461 command_name_by_number(connection_cmd[i]),
7462 FALSE)) {
7463 return TRUE;
7464 }
7465 i++;
7466 }
7467
7468 return FALSE;
7469}
7470
7471/**********************************************************************/
7474static bool is_cmdlevel_arg2(int start)
7475{
7476 return (contains_str_before_start(start, command_name_by_number(CMD_CMDLEVEL), TRUE)
7477 && num_tokens(start) == 2);
7478}
7479
7480/**********************************************************************/
7483static bool is_cmdlevel_arg1(int start)
7484{
7485 return contains_str_before_start(start, command_name_by_number(CMD_CMDLEVEL), FALSE);
7486}
7487
7488/**************************************************************************
7489 Commands that may be followed by a server option name
7490
7491 CMD_SHOW is handled by option_level_cmd, which is for both option levels
7492 and server options
7493**************************************************************************/
7494static const int server_option_cmd[] = {
7496 CMD_SET,
7498 -1
7499};
7500
7501/**********************************************************************/
7505static bool is_server_option(int start)
7506{
7507 int i = 0;
7508
7509 while (server_option_cmd[i] != -1) {
7510 if (contains_str_before_start(start, command_name_by_number(server_option_cmd[i]),
7511 FALSE)) {
7512 return TRUE;
7513 }
7514 i++;
7515 }
7516
7517 return FALSE;
7518}
7519
7520/**************************************************************************
7521 Commands that may be followed by an option level or server option
7522**************************************************************************/
7523static const int option_level_cmd[] = {
7524 CMD_SHOW,
7525 -1
7526};
7527
7528/**********************************************************************/
7532static bool is_option_level(int start)
7533{
7534 int i = 0;
7535
7536 while (option_level_cmd[i] != -1) {
7537 if (contains_str_before_start(start, command_name_by_number(option_level_cmd[i]),
7538 FALSE)) {
7539 return TRUE;
7540 }
7541 i++;
7542 }
7543
7544 return FALSE;
7545}
7546
7547/**********************************************************************/
7552static bool is_enum_option_value(int start, int *opt_p)
7553{
7554 if (contains_str_before_start(start, command_name_by_number(CMD_SET),
7555 TRUE)) {
7556 settings_iterate(SSET_ALL, pset) {
7557 if (setting_type(pset) != SST_ENUM
7558 && setting_type(pset) != SST_BITWISE) {
7559 continue;
7560 }
7561 /* Allow a single token for enum options, multiple for bitwise
7562 * (the separator | will separate tokens for these purposes) */
7563 if (contains_token_before_start(start, 1, setting_name(pset),
7564 setting_type(pset) == SST_BITWISE)) {
7565 *opt_p = setting_number(pset);
7566 /* Suppress appended space for bitwise options (user may want |) */
7567 rl_completion_suppress_append = (setting_type(pset) == SST_BITWISE);
7568 return TRUE;
7569 }
7571 }
7572 return FALSE;
7573}
7574
7575/**************************************************************************
7576 Commands that may be followed by a filename
7577**************************************************************************/
7578static const int filename_cmd[] = {
7579 CMD_LOAD,
7580 CMD_SAVE,
7583 -1
7584};
7585
7586/**********************************************************************/
7589static bool is_filename(int start)
7590{
7591 int i = 0;
7592
7593 while (filename_cmd[i] != -1) {
7594 if (contains_str_before_start(start, command_name_by_number(filename_cmd[i]), FALSE)) {
7595 return TRUE;
7596 }
7597 i++;
7598 }
7599
7600 return FALSE;
7601}
7602
7603/**********************************************************************/
7606static bool is_create_arg2(int start)
7607{
7608 return (contains_str_before_start(start, command_name_by_number(CMD_CREATE), TRUE)
7609 && num_tokens(start) == 2);
7610}
7611
7612/**********************************************************************/
7615static bool is_reset(int start)
7616{
7617 return contains_str_before_start(start,
7619 FALSE);
7620}
7621
7622/**********************************************************************/
7625static bool is_vote(int start)
7626{
7627 return contains_str_before_start(start,
7629 FALSE);
7630}
7631
7632/**********************************************************************/
7635static bool is_delegate_arg1(int start)
7636{
7637 return contains_str_before_start(start,
7639 FALSE);
7640}
7641
7642/**********************************************************************/
7645static bool is_mapimg(int start)
7646{
7647 return contains_str_before_start(start,
7649 FALSE);
7650}
7651
7652/**********************************************************************/
7655static bool is_fcdb(int start)
7656{
7657 return contains_str_before_start(start,
7659 FALSE);
7660}
7661
7662/**********************************************************************/
7665static bool is_lua(int start)
7666{
7667 return contains_str_before_start(start,
7669 FALSE);
7670}
7671
7672/**********************************************************************/
7675static bool is_help(int start)
7676{
7677 return contains_str_before_start(start, command_name_by_number(CMD_HELP), FALSE);
7678}
7679
7680/**********************************************************************/
7683static bool is_list(int start)
7684{
7685 return contains_str_before_start(start, command_name_by_number(CMD_LIST), FALSE);
7686}
7687
7688/**********************************************************************/
7695char **freeciv_completion(const char *text, int start, int end)
7696{
7697 char **matches = (char **)NULL;
7698
7699 if (is_help(start)) {
7700 matches = rl_completion_matches(text, help_generator);
7701 } else if (is_command(start)) {
7702 matches = rl_completion_matches(text, command_generator);
7703 } else if (is_list(start)) {
7704 matches = rl_completion_matches(text, list_generator);
7705 } else if (is_cmdlevel_arg2(start)) {
7706 matches = rl_completion_matches(text, cmdlevel_arg2_generator);
7707 } else if (is_cmdlevel_arg1(start)) {
7708 matches = rl_completion_matches(text, cmdlevel_arg1_generator);
7709 } else if (is_connection(start)) {
7710 matches = rl_completion_matches(text, connection_generator);
7711 } else if (is_player(start)) {
7712 matches = rl_completion_matches(text, player_generator);
7713 } else if (is_server_option(start)) {
7714 matches = rl_completion_matches(text, option_generator);
7715 } else if (is_option_level(start)) {
7716 matches = rl_completion_matches(text, olevel_generator);
7717 } else if (is_enum_option_value(start, &completion_option)) {
7718 matches = rl_completion_matches(text, option_value_generator);
7719 } else if (is_filename(start)) {
7720 /* This function we get from readline */
7721 matches = rl_completion_matches(text, rl_filename_completion_function);
7722 } else if (is_create_arg2(start)) {
7723 matches = rl_completion_matches(text, aitype_generator);
7724 } else if (is_reset(start)) {
7725 matches = rl_completion_matches(text, reset_generator);
7726 } else if (is_vote(start)) {
7727 matches = rl_completion_matches(text, vote_generator);
7728 } else if (is_delegate_arg1(start)) {
7729 matches = rl_completion_matches(text, delegate_generator);
7730 } else if (is_mapimg(start)) {
7731 matches = rl_completion_matches(text, mapimg_generator);
7732 } else if (is_fcdb(start)) {
7733 matches = rl_completion_matches(text, fcdb_generator);
7734 } else if (is_lua(start)) {
7735 matches = rl_completion_matches(text, lua_generator);
7736 } else {
7737 /* We have no idea what to do */
7738 matches = NULL;
7739 }
7740
7741 /* Don't automatically try to complete with filenames */
7742 rl_attempted_completion_over = 1;
7743
7744 return (matches);
7745}
7746
7747#endif /* FREECIV_HAVE_LIBREADLINE */
bool achievement_player_has(const struct achievement *pach, const struct player *pplayer)
int achievement_index(const struct achievement *pach)
#define achievements_iterate_end
#define achievements_iterate(_ach_)
const char * ai_name(const struct ai_type *ai)
Definition ai.c:329
struct ai_type * ai_type_by_name(const char *search)
Definition ai.c:284
int ai_type_get_count(void)
Definition ai.c:321
struct ai_type * get_ai_type(int id)
Definition ai.c:254
#define CALL_PLR_AI_FUNC(_func, _player,...)
Definition ai.h:374
const char * default_ai_type_name(void)
Definition aiiface.c:262
void astr_free(struct astring *astr)
Definition astring.c:153
const char * astr_build_or_list(struct astring *astr, const char *const *items, size_t number)
Definition astring.c:329
void astr_set(struct astring *astr, const char *format,...)
Definition astring.c:267
#define str
Definition astring.c:76
#define n
Definition astring.c:77
static const char * astr_str(const struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.h:93
#define ASTRING_INIT
Definition astring.h:44
#define BV_SET(bv, bit)
Definition bitvector.h:81
#define BV_ISSET(bv, bit)
Definition bitvector.h:78
#define BV_CLR(bv, bit)
Definition bitvector.h:86
#define SERVER_COMMAND_PREFIX
Definition chat.h:28
const char * city_name_get(const struct city *pcity)
Definition city.c:1115
#define city_list_iterate(citylist, pcity)
Definition city.h:488
static citizens city_size_get(const struct city *pcity)
Definition city.h:549
#define city_list_iterate_end
Definition city.h:490
enum cmd_echo command_echo(const struct command *pcommand)
Definition commands.c:787
const char * command_name_by_number(int i)
Definition commands.c:739
const char * command_name(const struct command *pcommand)
Definition commands.c:731
const struct command * command_by_number(int i)
Definition commands.c:722
const char * command_short_help(const struct command *pcommand)
Definition commands.c:755
char * command_extra_help(const struct command *pcommand)
Definition commands.c:764
enum cmdlevel command_level(const struct command *pcommand)
Definition commands.c:779
const char * command_synopsis(const struct command *pcommand)
Definition commands.c:747
command_id
Definition commands.h:34
@ CMD_NUM
Definition commands.h:105
@ CMD_IGNORE
Definition commands.h:77
@ CMD_METASERVER
Definition commands.h:57
@ CMD_END_GAME
Definition commands.h:83
@ CMD_DEFAULT
Definition commands.h:92
@ CMD_METAPATCHES
Definition commands.h:55
@ CMD_TEAM
Definition commands.h:52
@ CMD_DELEGATE
Definition commands.h:95
@ CMD_CHEATING
Definition commands.h:69
@ CMD_PLAYERCOLOR
Definition commands.h:79
@ CMD_LIST
Definition commands.h:38
@ CMD_AITOGGLE
Definition commands.h:58
@ CMD_CUT
Definition commands.h:40
@ CMD_EXPLAIN
Definition commands.h:43
@ CMD_SHOW
Definition commands.h:44
@ CMD_METAMESSAGE
Definition commands.h:54
@ CMD_RULESETDIR
Definition commands.h:53
@ CMD_HARD
Definition commands.h:68
@ CMD_RFCSTYLE
Definition commands.h:101
@ CMD_DETACH
Definition commands.h:61
@ CMD_NORMAL
Definition commands.h:67
@ CMD_RESTRICTED
Definition commands.h:64
@ CMD_VOTE
Definition commands.h:47
@ CMD_NOVICE
Definition commands.h:65
@ CMD_UNRECOGNIZED
Definition commands.h:106
@ CMD_TIMEOUT
Definition commands.h:75
@ CMD_CREATE
Definition commands.h:62
@ CMD_LOAD
Definition commands.h:88
@ CMD_AICMD
Definition commands.h:96
@ CMD_AMBIGUOUS
Definition commands.h:107
@ CMD_LUA
Definition commands.h:93
@ CMD_FCDB
Definition commands.h:97
@ CMD_CANCELVOTE
Definition commands.h:76
@ CMD_SAVE
Definition commands.h:86
@ CMD_SRVID
Definition commands.h:102
@ CMD_SCENSAVE
Definition commands.h:87
@ CMD_START_GAME
Definition commands.h:36
@ CMD_UNIGNORE
Definition commands.h:78
@ CMD_FIRSTLEVEL
Definition commands.h:74
@ CMD_WALL
Definition commands.h:45
@ CMD_EASY
Definition commands.h:66
@ CMD_KICK
Definition commands.h:94
@ CMD_WRITE_SCRIPT
Definition commands.h:90
@ CMD_CONNECTMSG
Definition commands.h:46
@ CMD_TAKE
Definition commands.h:59
@ CMD_HELP
Definition commands.h:37
@ CMD_REMOVE
Definition commands.h:85
@ CMD_OBSERVE
Definition commands.h:60
@ CMD_RESET
Definition commands.h:91
@ CMD_SURRENDER
Definition commands.h:84
@ CMD_METACONN
Definition commands.h:56
@ CMD_READ_SCRIPT
Definition commands.h:89
@ CMD_CMDLEVEL
Definition commands.h:73
@ CMD_SET
Definition commands.h:51
@ CMD_AWAY
Definition commands.h:63
@ CMD_QUIT
Definition commands.h:39
@ CMD_DEBUG
Definition commands.h:50
@ CMD_PLAYERNATION
Definition commands.h:80
@ CMD_MAPIMG
Definition commands.h:98
@ CMD_ECHO_ADMINS
Definition commands.h:21
@ CMD_ECHO_NONE
Definition commands.h:20
@ CMD_ECHO_ALL
Definition commands.h:22
bool connection_attach(struct connection *pconn, struct player *pplayer, bool observing)
bool connection_delegate_take(struct connection *pconn, struct player *dplayer)
struct player * find_uncontrolled_player(void)
void connection_close_server(struct connection *pconn, const char *reason)
void connection_detach(struct connection *pconn, bool remove_unused_player)
void conn_set_access(struct connection *pconn, enum cmdlevel new_level, bool granted)
Definition connecthand.c:68
bool connection_delegate_restore(struct connection *pconn)
size_t conn_pattern_to_string(const struct conn_pattern *ppattern, char *buf, size_t buf_len)
Definition connection.c:862
struct player * conn_get_player(const struct connection *pconn)
Definition connection.c:760
struct connection * conn_by_user_prefix(const char *user_name, enum m_pre_result *result)
Definition connection.c:397
struct connection * conn_by_user(const char *user_name)
Definition connection.c:376
void conn_list_compression_thaw(const struct conn_list *pconn_list)
Definition connection.c:729
void conn_pattern_destroy(struct conn_pattern *ppattern)
Definition connection.c:806
void conn_list_compression_freeze(const struct conn_list *pconn_list)
Definition connection.c:717
bool conn_controls_player(const struct connection *pconn)
Definition connection.c:742
const char * conn_description(const struct connection *pconn)
Definition connection.c:473
struct conn_pattern * conn_pattern_from_string(const char *pattern, enum conn_pattern_type prefer, char *error_buf, size_t error_buf_len)
Definition connection.c:875
enum cmdlevel conn_get_access(const struct connection *pconn)
Definition connection.c:772
#define conn_pattern_list_iterate_end
Definition connection.h:340
#define conn_list_iterate(connlist, pconn)
Definition connection.h:113
#define conn_pattern_list_iterate(plist, ppatern)
Definition connection.h:338
#define conn_list_iterate_end
Definition connection.h:115
bool con_get_style(void)
Definition console.c:264
void con_set_style(bool i)
Definition console.c:251
void con_write(enum rfc_status rfc_status, const char *message,...)
Definition console.c:203
rfc_status
Definition console.h:35
@ C_DISCONNECTED
Definition console.h:43
@ C_BOUNCE
Definition console.h:48
@ C_FAIL
Definition console.h:45
@ C_SYNTAX
Definition console.h:47
@ C_OK
Definition console.h:41
@ C_METAERROR
Definition console.h:46
@ C_GENFAIL
Definition console.h:49
@ C_COMMENT
Definition console.h:37
@ C_WARNING
Definition console.h:50
#define MAX_LEN_CONSOLE_LINE
Definition console.h:19
#define log_deprecation(message,...)
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:73
void set_ai_level_directer(struct player *pplayer, enum ai_level level)
Definition difficulty.c:39
int int id
Definition editgui_g.h:28
void free_tokens(char **tokens, size_t ntokens)
Definition fc_cmdline.c:203
int get_tokens(const char *str, char **tokens, size_t num_tokens, const char *delimiterset)
Definition fc_cmdline.c:166
#define RULESET_SUFFIX
Definition fc_types.h:384
#define MAX_LEN_NAME
Definition fc_types.h:66
#define LINE_BREAK
Definition fc_types.h:77
char * internal_to_local_string_malloc(const char *text)
char * local_to_internal_string_malloc(const char *text)
#define PL_(String1, String2, n)
Definition fcintl.h:71
#define _(String)
Definition fcintl.h:67
size_t featured_text_apply_tag(const char *text_source, char *featured_text, size_t featured_text_len, enum text_tag_type tag_type, ft_offset_t start_offset, ft_offset_t stop_offset,...)
const struct ft_color ftc_log
const struct ft_color ftc_command
const struct ft_color ftc_server
const struct ft_color ftc_any
VAR_ARG_CONST struct ft_color ftc_changed
const struct ft_color ftc_vote_team
const struct ft_color ftc_game_start
const struct ft_color ftc_server_prompt
const struct ft_color ftc_vote_public
#define FT_OFFSET_UNSET
@ TTT_COLOR
struct civ_game game
Definition game.c:57
struct world wld
Definition game.c:58
struct unit * game_unit_by_number(int id)
Definition game.c:111
@ DEBUG_FERRIES
Definition game.h:40
#define GAME_MAX_READ_RECURSION
Definition game.h:720
void send_scenario_description(struct conn_list *dest)
Definition gamehand.c:962
void send_scenario_info(struct conn_list *dest)
Definition gamehand.c:948
void send_game_info(struct conn_list *dest)
Definition gamehand.c:905
struct city * owner
Definition citydlg.c:219
const char * title
Definition repodlgs.c:1313
static GtkWidget * persistent
static char * leader_name
Definition dialogs.c:96
void handle_player_ready(struct player *pplayer, int player_no, bool is_ready)
Definition srv_main.c:2341
const char * name
Definition inputfile.c:127
#define fc_assert_ret(condition)
Definition log.h:191
#define __FC_LINE__
Definition log.h:40
#define log_verbose(message,...)
Definition log.h:109
#define fc_assert(condition)
Definition log.h:176
#define log_testmatic_alt(altlvl, message,...)
Definition log.h:124
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define fc_assert_action(condition, action)
Definition log.h:187
#define log_normal(message,...)
Definition log.h:107
@ LOG_NORMAL
Definition log.h:32
#define log_error(message,...)
Definition log.h:103
struct tile * map_pos_to_tile(const struct civ_map *nmap, int map_x, int map_y)
Definition map.c:417
int map_startpos_count(void)
Definition map.c:1656
bool map_is_empty(void)
Definition map.c:149
bool mapimg_id2str(int id, char *str, size_t str_len)
Definition mapimg.c:1310
bool mapimg_colortest(const char *savename, const char *path)
Definition mapimg.c:1435
struct mapdef * mapimg_isvalid(int id)
Definition mapimg.c:1120
bool mapimg_define(const char *maparg, bool check)
Definition mapimg.c:768
bool mapimg_delete(int id)
Definition mapimg.c:1203
int mapimg_count(void)
Definition mapimg.c:572
bool mapimg_create(struct mapdef *pmapdef, bool force, const char *savename, const char *path)
Definition mapimg.c:1331
const char * mapimg_error(void)
Definition mapimg.c:758
bool mapimg_show(int id, char *str, size_t str_len, bool detail)
Definition mapimg.c:1222
#define MAX_LEN_MAPDEF
Definition mapimg.h:65
#define FC_FREE(ptr)
Definition mem.h:41
#define fc_strdup(str)
Definition mem.h:43
#define fc_malloc(sz)
Definition mem.h:34
void set_meta_patches_string(const char *string)
Definition meta.c:171
bool is_metaserver_open(void)
Definition meta.c:482
char * meta_addr_port(void)
Definition meta.c:202
const char * default_meta_message_string(void)
Definition meta.c:90
void server_close_meta(void)
Definition meta.c:454
const char * default_meta_patches_string(void)
Definition meta.c:82
const char * get_meta_message_string(void)
Definition meta.c:114
bool send_server_info_to_metaserver(enum meta_flag flag)
Definition meta.c:490
bool server_open_meta(bool persistent)
Definition meta.c:463
const char * get_meta_patches_string(void)
Definition meta.c:106
void set_user_meta_message_string(const char *string)
Definition meta.c:187
#define DEFAULT_META_SERVER_ADDR
Definition meta.h:21
@ META_GOODBYE
Definition meta.h:28
@ META_INFO
Definition meta.h:26
#define translated
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:137
int nations_match(const struct nation_type *pnation1, const struct nation_type *pnation2, bool ignore_conflicts)
Definition nation.c:1205
struct nation_type * nation_of_unit(const struct unit *punit)
Definition nation.c:462
const char * nation_adjective_for_player(const struct player *pplayer)
Definition nation.c:168
const char * nation_set_name_translation(const struct nation_set *pset)
Definition nation.c:817
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:443
bool is_nation_playable(const struct nation_type *nation)
Definition nation.c:199
bool nation_is_in_set(const struct nation_type *pnation, const struct nation_set *pset)
Definition nation.c:836
const char * nation_set_description(const struct nation_set *pset)
Definition nation.c:827
int nation_set_count(void)
Definition nation.c:690
struct nation_type * nation_by_rule_name(const char *name)
Definition nation.c:120
const char * nation_plural_translation(const struct nation_type *pnation)
Definition nation.c:158
const char * nation_set_rule_name(const struct nation_set *pset)
Definition nation.c:806
const char * nation_plural_for_player(const struct player *pplayer)
Definition nation.c:177
struct government * init_government_of_nation(const struct nation_type *pnation)
Definition nation.c:658
struct nation_style * style_of_nation(const struct nation_type *pnation)
Definition nation.c:671
#define nation_sets_iterate_end
Definition nation.h:304
#define nation_sets_iterate(NAME_pset)
Definition nation.h:300
#define nations_iterate_end
Definition nation.h:335
#define nations_iterate(NAME_pnation)
Definition nation.h:332
#define NO_NATION_SELECTED
Definition nation.h:29
void notify_conn(struct conn_list *dest, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:238
void notify_team(const struct player *pplayer, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:345
void package_event(struct packet_chat_msg *packet, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:168
void event_cache_add_for_all(const struct packet_chat_msg *packet)
Definition notify.c:619
#define MAX_LEN_MSG
Definition packets.h:43
int send_packet_chat_msg(struct connection *pc, const struct packet_chat_msg *packet)
void dlsend_packet_game_load(struct conn_list *dest, bool load_successful, const char *load_filename)
void lsend_packet_achievement_info(struct conn_list *dest, const struct packet_achievement_info *packet)
struct city_list * cities
Definition packhand.c:117
int len
Definition packhand.c:125
bool player_slot_is_used(const struct player_slot *pslot)
Definition player.c:441
struct player * player_by_name_prefix(const char *name, enum m_pre_result *result)
Definition player.c:911
struct player * player_by_number(const int player_id)
Definition player.c:840
int player_count(void)
Definition player.c:808
int player_slot_count(void)
Definition player.c:411
struct player_slot * player_slot_by_number(int player_id)
Definition player.c:456
int player_number(const struct player *pplayer)
Definition player.c:828
const char * player_name(const struct player *pplayer)
Definition player.c:886
struct player * player_by_name(const char *name)
Definition player.c:872
struct player * player_by_user(const char *name)
Definition player.c:931
bool player_set_nation(struct player *pplayer, struct nation_type *pnation)
Definition player.c:852
struct player * player_slot_get_player(const struct player_slot *pslot)
Definition player.c:430
#define ai_level_cmd(_level_)
Definition player.h:565
#define players_iterate_end
Definition player.h:535
#define players_iterate(_pplayer)
Definition player.h:530
#define player_list_iterate(playerlist, pplayer)
Definition player.h:553
#define ANON_USER_NAME
Definition player.h:48
static bool is_barbarian(const struct player *pplayer)
Definition player.h:488
#define is_ai(plr)
Definition player.h:234
#define player_list_iterate_end
Definition player.h:555
#define set_as_human(plr)
Definition player.h:235
#define set_as_ai(plr)
Definition player.h:236
#define ANON_PLAYER_NAME
Definition player.h:43
@ PLAYER_DEBUG_DIPLOMACY
Definition player.h:217
@ PLAYER_DEBUG_TECH
Definition player.h:217
#define is_human(plr)
Definition player.h:233
void server_player_set_name(struct player *pplayer, const char *name)
Definition plrhand.c:2097
struct player * server_create_player(int player_id, const char *ai_tname, struct rgbcolor *prgbcolor, bool allow_ai_type_fallbacking)
Definition plrhand.c:1723
void player_status_add(struct player *plr, enum player_status pstatus)
Definition plrhand.c:3042
int normal_player_count(void)
Definition plrhand.c:3034
void player_set_under_human_control(struct player *pplayer)
Definition plrhand.c:3281
void server_player_set_color(struct player *pplayer, const struct rgbcolor *prgbcolor)
Definition plrhand.c:1652
void player_set_to_ai_mode(struct player *pplayer, enum ai_level skill_level)
Definition plrhand.c:3259
bool server_player_set_name_full(const struct connection *caller, struct player *pplayer, const struct nation_type *pnation, const char *name, char *error_buf, size_t error_buf_len)
Definition plrhand.c:1997
bool player_delegation_active(const struct player *pplayer)
Definition plrhand.c:3096
void player_info_thaw(void)
Definition plrhand.c:964
void player_info_freeze(void)
Definition plrhand.c:955
struct nation_type * pick_a_nation(const struct nation_list *choices, bool ignore_conflicts, bool needs_startpos, enum barbarian_type barb_type)
Definition plrhand.c:2282
const char * player_color_ftstr(struct player *pplayer)
Definition plrhand.c:1672
void send_player_info_c(struct player *src, struct conn_list *dest)
Definition plrhand.c:1000
bool player_status_check(struct player *plr, enum player_status pstatus)
Definition plrhand.c:3050
void player_delegation_set(struct player *pplayer, const char *username)
Definition plrhand.c:3080
void server_remove_player(struct player *pplayer)
Definition plrhand.c:1772
void server_player_init(struct player *pplayer, bool initmap, bool needs_team)
Definition plrhand.c:1450
bool player_color_changeable(const struct player *pplayer, const char **reason)
Definition plrhand.c:1546
void assign_player_colors(void)
Definition plrhand.c:1563
bool client_can_pick_nation(const struct nation_type *pnation)
Definition plrhand.c:2443
void send_player_diplstate_c(struct player *src, struct conn_list *dest)
Definition plrhand.c:1059
const char * player_delegation_get(const struct player *pplayer)
Definition plrhand.c:3067
bool nation_is_in_current_set(const struct nation_type *pnation)
Definition plrhand.c:2419
void reset_all_start_commands(bool plrchange)
Definition plrhand.c:2553
struct section_file * secfile_load(const char *filename, bool allow_duplicates)
Definition registry.c:50
const char * secfile_error(void)
void secfile_destroy(struct section_file *secfile)
void secfile_check_unused(const struct section_file *secfile)
struct section_file * secfile_load_section(const char *filename, const char *section, bool allow_duplicates)
bool secfile_lookup_bool_default(const struct section_file *secfile, bool def, const char *path,...)
struct research * research_get(const struct player *pplayer)
Definition research.c:126
bool rgbcolor_from_hex(struct rgbcolor **prgbcolor, const char *hex)
Definition rgbcolor.c:162
void rgbcolor_destroy(struct rgbcolor *prgbcolor)
Definition rgbcolor.c:74
bool rgbcolors_are_equal(const struct rgbcolor *c1, const struct rgbcolor *c2)
Definition rgbcolor.c:62
bool load_rulesets(const char *restore, const char *alt, bool compat_mode, rs_conversion_logger logger, bool act, bool buffer_script, bool load_luadata)
Definition ruleset.c:9227
bool reload_rulesets_settings(void)
Definition ruleset.c:9550
void send_rulesets(struct conn_list *dest)
Definition ruleset.c:9573
#define sanity_check()
Definition sanitycheck.h:43
void savegame_load(struct section_file *sfile)
Definition savemain.c:43
void save_game(const char *orig_filename, const char *save_reason, bool scenario)
Definition savemain.c:142
void script_fcdb_free(void)
bool script_fcdb_do_string(struct connection *caller, const char *str)
bool script_fcdb_call(const char *func_name,...)
bool script_fcdb_init(const char *fcdb_luafile)
bool script_server_unsafe_do_string(struct connection *caller, const char *str)
bool script_server_do_string(struct connection *caller, const char *str)
bool script_server_unsafe_do_file(struct connection *caller, const char *filename)
bool script_server_do_file(struct connection *caller, const char *filename)
bool setting_int_validate(const struct setting *pset, int val, struct connection *caller, char *reject_msg, size_t reject_msg_len)
Definition settings.c:3583
void setting_action(const struct setting *pset)
Definition settings.c:4168
const char * setting_default_name(const struct setting *pset, bool pretty, char *buf, size_t buf_len)
Definition settings.c:4102
bool setting_enum_validate(const struct setting *pset, const char *val, struct connection *caller, char *reject_msg, size_t reject_msg_len)
Definition settings.c:3861
struct setting * setting_by_name(const char *name)
Definition settings.c:3138
void setting_set_to_default(struct setting *pset)
Definition settings.c:4138
const char * setting_value_name(const struct setting *pset, bool pretty, char *buf, size_t buf_len)
Definition settings.c:4066
const char * setting_extra_help(const struct setting *pset, bool constant)
Definition settings.c:3179
int setting_number(const struct setting *pset)
Definition settings.c:3153
struct setting * setting_by_number(int id)
Definition settings.c:3130
bool setting_int_set(struct setting *pset, int val, struct connection *caller, char *reject_msg, size_t reject_msg_len)
Definition settings.c:3563
bool setting_is_visible(const struct setting *pset, struct connection *caller)
Definition settings.c:3326
bool setting_locked(const struct setting *pset)
Definition settings.c:4420
bool setting_non_default(const struct setting *pset)
Definition settings.c:4394
enum sset_type setting_type(const struct setting *pset)
Definition settings.c:3191
bool setting_str_set(struct setting *pset, const char *val, struct connection *caller, char *reject_msg, size_t reject_msg_len)
Definition settings.c:3634
int setting_int_max(const struct setting *pset)
Definition settings.c:3553
bool setting_bitwise_set(struct setting *pset, const char *val, struct connection *caller, char *reject_msg, size_t reject_msg_len)
Definition settings.c:4022
int settings_number(void)
Definition settings.c:5005
bool setting_str_validate(const struct setting *pset, const char *val, struct connection *caller, char *reject_msg, size_t reject_msg_len)
Definition settings.c:3654
const char * setting_enum_val(const struct setting *pset, int val, bool pretty)
Definition settings.c:3702
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:183
void setting_changed(struct setting *pset)
Definition settings.c:5341
enum setting_default_level setting_get_setdef(const struct setting *pset)
Definition settings.c:5349
bool setting_enum_set(struct setting *pset, const char *val, struct connection *caller, char *reject_msg, size_t reject_msg_len)
Definition settings.c:3832
const char * setting_short_help(const struct setting *pset)
Definition settings.c:3170
const char * setting_bitwise_bit(const struct setting *pset, int bit, bool pretty)
Definition settings.c:3887
int setting_int_min(const struct setting *pset)
Definition settings.c:3544
bool setting_bitwise_validate(const struct setting *pset, const char *val, struct connection *caller, char *reject_msg, size_t reject_msg_len)
Definition settings.c:4043
bool setting_bool_validate(const struct setting *pset, const char *val, struct connection *caller, char *reject_msg, size_t reject_msg_len)
Definition settings.c:3508
bool setting_is_changeable(const struct setting *pset, struct connection *caller, char *reject_msg, size_t reject_msg_len)
Definition settings.c:3289
void settings_reset(void)
Definition settings.c:4971
const char * setting_name(const struct setting *pset)
Definition settings.c:3162
bool settings_game_reset(void)
Definition settings.c:4935
void settings_list_update(void)
Definition settings.c:5270
bool setting_bool_set(struct setting *pset, const char *val, struct connection *caller, char *reject_msg, size_t reject_msg_len)
Definition settings.c:3477
void send_server_setting(struct conn_list *dest, const struct setting *pset)
Definition settings.c:5014
void send_server_settings(struct conn_list *dest)
Definition settings.c:5133
#define settings_iterate(_level, _pset)
Definition settings.h:177
#define settings_iterate_end
Definition settings.h:183
const char * fileinfoname(const struct strvec *dirs, const char *filename)
Definition shared.c:1094
void remove_trailing_spaces(char *s)
Definition shared.c:420
bool str_to_int(const char *str, int *pint)
Definition shared.c:512
const char * m_pre_description(enum m_pre_result result)
Definition shared.c:1564
struct strvec * fileinfolist(const struct strvec *dirs, const char *suffix)
Definition shared.c:1020
char * skip_leading_spaces(char *s)
Definition shared.c:387
enum m_pre_result match_prefix_full(m_pre_accessor_fn_t accessor_fn, size_t n_names, size_t max_len_name, m_pre_strncmp_fn_t cmp_fn, m_strlen_fn_t len_fn, const char *prefix, int *ind_result, int *matches, int max_matches, int *pnum_matches)
Definition shared.c:1604
const struct strvec * get_scenario_dirs(void)
Definition shared.c:971
void interpret_tilde(char *buf, size_t buf_size, const char *filename)
Definition shared.c:1711
const struct strvec * get_save_dirs(void)
Definition shared.c:934
void remove_leading_spaces(char *s)
Definition shared.c:402
enum m_pre_result match_prefix(m_pre_accessor_fn_t accessor_fn, size_t n_names, size_t max_len_name, m_pre_strncmp_fn_t cmp_fn, m_strlen_fn_t len_fn, const char *prefix, int *ind_result)
Definition shared.c:1581
bool is_safe_filename(const char *name)
Definition shared.c:252
void remove_leading_trailing_spaces(char *s)
Definition shared.c:443
const struct strvec * get_data_dirs(void)
Definition shared.c:886
struct fileinfo_list * fileinfolist_infix(const struct strvec *dirs, const char *infix, bool nodups)
Definition shared.c:1204
#define CLIP(lower, current, upper)
Definition shared.h:57
#define FC_MEMBER_SIZEOF(type, member)
Definition shared.h:90
#define ARRAY_SIZE(x)
Definition shared.h:85
#define MIN(x, y)
Definition shared.h:55
#define fileinfo_list_iterate(list, pnode)
Definition shared.h:176
#define MAX_LEN_PATH
Definition shared.h:32
m_pre_result
Definition shared.h:207
@ M_PRE_EXACT
Definition shared.h:208
@ M_PRE_ONLY
Definition shared.h:209
@ M_PRE_LAST
Definition shared.h:214
@ M_PRE_LONG
Definition shared.h:212
@ M_PRE_AMBIGUOUS
Definition shared.h:210
@ M_PRE_EMPTY
Definition shared.h:211
@ M_PRE_FAIL
Definition shared.h:213
#define fileinfo_list_iterate_end
Definition shared.h:178
const char *(* m_pre_accessor_fn_t)(int)
Definition shared.h:220
#define CITY_LOG(loglevel, pcity, msg,...)
Definition srv_log.h:83
#define UNIT_LOG(loglevel, punit, msg,...)
Definition srv_log.h:98
#define TIMING_RESULTS()
Definition srv_log.h:126
void server_game_init(bool keep_ruleset_value)
Definition srv_main.c:3382
void player_nation_defaults(struct player *pplayer, struct nation_type *pnation, bool set_name)
Definition srv_main.c:2510
bool force_end_of_sniff
Definition srv_main.c:185
void start_game(void)
Definition srv_main.c:1773
const char * aifill(int amount)
Definition srv_main.c:2395
void set_server_state(enum server_states newstate)
Definition srv_main.c:331
bool game_was_started(void)
Definition srv_main.c:339
struct server_arguments srvarg
Definition srv_main.c:173
void check_for_full_turn_done(void)
Definition srv_main.c:2145
void server_quit(void)
Definition srv_main.c:1810
enum server_states server_state(void)
Definition srv_main.c:323
void server_game_free(void)
Definition srv_main.c:3406
#define TOKEN_DELIMITERS
Definition srvdefs.h:20
static bool write_command(struct connection *caller, char *arg, bool check)
Definition stdinhand.c:1339
static const char * reset_accessor(int i)
Definition stdinhand.c:4764
static bool set_cmdlevel(struct connection *caller, struct connection *ptarget, enum cmdlevel level)
Definition stdinhand.c:1378
static struct setting * validate_setting_arg(enum command_id cmd, struct connection *caller, char *arg)
Definition stdinhand.c:2930
#define LOOKUP_OPTION_AMBIGUOUS
Definition stdinhand.c:1729
static const char * mapimg_accessor(int i)
Definition stdinhand.c:5613
void cmd_reply(enum command_id cmd, struct connection *caller, enum rfc_status rfc_status, const char *format,...)
Definition stdinhand.c:418
static bool read_init_script_real(struct connection *caller, char *script_filename, bool from_cmdline, bool check, int read_recursion)
Definition stdinhand.c:1185
bool read_init_script(struct connection *caller, char *script_filename, bool from_cmdline, bool check)
Definition stdinhand.c:1167
static bool set_command(struct connection *caller, char *str, bool check)
Definition stdinhand.c:2964
static enum command_id cmd_of_level(enum ai_level level)
Definition stdinhand.c:1981
static void show_delegations(struct connection *caller)
Definition stdinhand.c:6618
static char setting_status(struct connection *caller, const struct setting *pset)
Definition stdinhand.c:305
static void show_scenarios(struct connection *caller)
Definition stdinhand.c:6801
static bool delegate_command(struct connection *caller, char *arg, bool check)
Definition stdinhand.c:5123
static bool ignore_command(struct connection *caller, char *str, bool check)
Definition stdinhand.c:4013
static void show_help_command(struct connection *caller, enum command_id help_cmd, enum command_id id)
Definition stdinhand.c:6372
void set_running_game_access_level(void)
Definition stdinhand.c:1633
void notify_if_first_access_level_is_available(void)
Definition stdinhand.c:1449
static const char * lua_accessor(int i)
Definition stdinhand.c:4924
static const char * fcdb_accessor(int i)
Definition stdinhand.c:5921
static void show_help_command_list(struct connection *caller, enum command_id help_cmd)
Definition stdinhand.c:6418
void stdinhand_turn(void)
Definition stdinhand.c:254
static void show_connections(struct connection *caller)
Definition stdinhand.c:6592
static bool explain_option(struct connection *caller, char *str, bool check)
Definition stdinhand.c:1895
static struct kick_hash * kick_table_by_user
Definition stdinhand.c:116
bool conn_is_kicked(struct connection *pconn, int *time_remaining)
Definition stdinhand.c:6210
enum rfc_status create_command_newcomer(const char *name, const char *ai, bool check, struct nation_type *pnation, struct player **newplayer, char *buf, size_t buflen)
Definition stdinhand.c:810
static void show_colors(struct connection *caller)
Definition stdinhand.c:6915
static bool set_ai_level(struct connection *caller, const char *name, enum ai_level level, bool check)
Definition stdinhand.c:2028
static struct kick_hash * kick_table_by_addr
Definition stdinhand.c:115
static void show_help_option_list(struct connection *caller, enum command_id help_cmd)
Definition stdinhand.c:1859
void stdinhand_init(void)
Definition stdinhand.c:241
static bool take_command(struct connection *caller, char *str, bool check)
Definition stdinhand.c:3438
static bool wall(char *str, bool check)
Definition stdinhand.c:1928
static enum cmdlevel default_access_level
Definition stdinhand.c:97
static void show_ruleset_info(struct connection *caller, enum command_id cmd, bool check, int read_recursion)
Definition stdinhand.c:2125
void show_players(struct connection *caller)
Definition stdinhand.c:6682
static void cmd_reply_matches(enum command_id cmd, struct connection *caller, m_pre_accessor_fn_t accessor_fn, int *matches, int num_matches)
Definition stdinhand.c:6456
#define LOOKUP_OPTION_NO_RESULT
Definition stdinhand.c:1728
static const char * list_accessor(int i)
Definition stdinhand.c:6962
static int lookup_option(const char *name)
Definition stdinhand.c:1740
static bool reset_command(struct connection *caller, char *arg, bool check, int read_recursion)
Definition stdinhand.c:4774
static void show_teams(struct connection *caller)
Definition stdinhand.c:6864
static bool handle_stdin_input_real(struct connection *caller, char *str, bool check, int read_recursion)
Definition stdinhand.c:4389
enum rfc_status create_command_pregame(const char *name, const char *ai, bool check, struct player **newplayer, char *buf, size_t buflen)
Definition stdinhand.c:994
static bool metaconnection_command(struct connection *caller, char *arg, bool check)
Definition stdinhand.c:527
static bool show_serverid(struct connection *caller, char *arg)
Definition stdinhand.c:660
static void show_nationsets(struct connection *caller)
Definition stdinhand.c:6827
static const char * helparg_accessor(int i)
Definition stdinhand.c:6512
static bool metamessage_command(struct connection *caller, char *arg, bool check)
Definition stdinhand.c:608
static bool away_command(struct connection *caller, bool check)
Definition stdinhand.c:2084
enum cmdlevel access_level_for_next_connection(void)
Definition stdinhand.c:1435
static bool playercolor_command(struct connection *caller, char *str, bool check)
Definition stdinhand.c:4137
bool handle_stdin_input_free(struct connection *caller, char *str)
Definition stdinhand.c:4370
void set_ai_level_direct(struct player *pplayer, enum ai_level level)
Definition stdinhand.c:2003
static bool player_name_check(const char *name, char *buf, size_t buflen)
Definition stdinhand.c:191
static bool default_command(struct connection *caller, char *arg, bool check)
Definition stdinhand.c:4881
static bool create_command(struct connection *caller, const char *str, bool check)
Definition stdinhand.c:752
static void vcmd_reply_prefix(enum command_id cmd, struct connection *caller, enum rfc_status rfc_status, const char *prefix, const char *format, va_list ap)
Definition stdinhand.c:378
static const char * delegate_accessor(int i)
Definition stdinhand.c:5114
static void show_rulesets(struct connection *caller)
Definition stdinhand.c:6779
static bool surrender_command(struct connection *caller, char *str, bool check)
Definition stdinhand.c:4716
static bool end_command(struct connection *caller, char *str, bool check)
Definition stdinhand.c:4691
static const char * delegate_player_str(struct player *pplayer, bool observer)
Definition stdinhand.c:5574
static void cmd_reply_prefix(enum command_id cmd, struct connection *caller, enum rfc_status rfc_status, const char *prefix, const char *format,...) fc__attribute((__format__(__printf__
#define LOOKUP_OPTION_RULESETDIR
Definition stdinhand.c:1731
static bool timeout_command(struct connection *caller, char *str, bool check)
Definition stdinhand.c:1670
static void cmd_reply_no_such_player(enum command_id cmd, struct connection *caller, const char *name, enum m_pre_result match_result)
Definition stdinhand.c:431
static bool metaserver_command(struct connection *caller, char *arg, bool check)
Definition stdinhand.c:642
bool handle_stdin_input(struct connection *caller, char *str)
Definition stdinhand.c:4362
static bool cut_client_connection(struct connection *caller, char *name, bool check)
Definition stdinhand.c:6167
static void open_metaserver_connection(struct connection *caller, bool persistent)
Definition stdinhand.c:500
#define HELP_ARG_NUM
Definition stdinhand.c:6507
static time_t * time_duplicate(const time_t *t)
Definition stdinhand.c:6199
static void show_mapimg(struct connection *caller, enum command_id cmd)
Definition stdinhand.c:6894
static bool may_use_nothing(struct connection *caller)
Definition stdinhand.c:293
static void show_votes(struct connection *caller)
Definition stdinhand.c:2473
static const char *const vote_args[]
Definition stdinhand.c:2508
static bool show_settings(struct connection *caller, enum command_id called_as, char *str, bool check)
Definition stdinhand.c:2160
static bool a_connection_exists(void)
Definition stdinhand.c:1413
void stdinhand_free(void)
Definition stdinhand.c:262
static bool cmdlevel_command(struct connection *caller, char *str, bool check)
Definition stdinhand.c:1463
static bool is_first_access_level_taken(void)
Definition stdinhand.c:1421
#define LOOKUP_OPTION_LEVEL_NAME
Definition stdinhand.c:1730
static bool firstlevel_command(struct connection *caller, bool check)
Definition stdinhand.c:1606
static void start_cmd_reply(struct connection *caller, bool notify, char *msg)
Definition stdinhand.c:6033
static bool connectmsg_command(struct connection *caller, char *str, bool check)
Definition stdinhand.c:1940
static const char horiz_line[]
Definition stdinhand.c:175
static void cmd_reply_no_such_conn(enum command_id cmd, struct connection *caller, const char *name, enum m_pre_result match_result)
Definition stdinhand.c:466
static void cmd_reply_line(enum command_id cmd, struct connection *caller, enum rfc_status rfc_status, const char *prefix, const char *line)
Definition stdinhand.c:331
static void show_help_intro(struct connection *caller, enum command_id help_cmd)
Definition stdinhand.c:6339
static bool cancelvote_command(struct connection *caller, char *arg, bool check)
Definition stdinhand.c:2627
bool load_command(struct connection *caller, const char *filename, bool check, bool cmdline_load)
Definition stdinhand.c:3733
static void show_settings_one(struct connection *caller, enum command_id cmd, struct setting *pset)
Definition stdinhand.c:2332
static bool toggle_ai_command(struct connection *caller, char *arg, bool check)
Definition stdinhand.c:729
static bool playernation_command(struct connection *caller, char *str, bool check)
Definition stdinhand.c:4222
static bool team_command(struct connection *caller, char *str, bool check)
Definition stdinhand.c:2396
static bool quit_game(struct connection *caller, bool check)
Definition stdinhand.c:4349
static bool vote_command(struct connection *caller, char *str, bool check)
Definition stdinhand.c:2522
#define cmd_reply_show(string)
bool start_command(struct connection *caller, bool check, bool notify)
Definition stdinhand.c:6044
static const char * optname_accessor(int i)
Definition stdinhand.c:1646
static bool show_help(struct connection *caller, char *arg)
Definition stdinhand.c:6530
#define OPTION_NAME_SPACE
Definition stdinhand.c:95
struct strvec * get_init_script_choices(void)
Definition stdinhand.c:1268
static bool remove_player_command(struct connection *caller, char *arg, bool check)
Definition stdinhand.c:1121
static bool debug_command(struct connection *caller, char *str, bool check)
Definition stdinhand.c:2722
static enum command_id command_named(const char *token, bool accept_ambiguity)
Definition stdinhand.c:221
static bool fcdb_command(struct connection *caller, char *arg, bool check)
Definition stdinhand.c:5930
static bool set_rulesetdir(struct connection *caller, char *str, bool check, int read_recursion)
Definition stdinhand.c:3920
static bool mapimg_command(struct connection *caller, char *arg, bool check)
Definition stdinhand.c:5622
static bool unignore_command(struct connection *caller, char *str, bool check)
Definition stdinhand.c:4048
static bool observe_command(struct connection *caller, char *str, bool check)
Definition stdinhand.c:3290
static bool show_list(struct connection *caller, char *arg)
Definition stdinhand.c:6971
static bool scensave_command(struct connection *caller, char *arg, bool check)
Definition stdinhand.c:688
static enum sset_level lookup_option_level(const char *name)
Definition stdinhand.c:1714
static bool may_use(struct connection *caller, enum command_id cmd)
Definition stdinhand.c:281
static bool kick_command(struct connection *caller, char *name, bool check)
Definition stdinhand.c:6258
static const char * vote_arg_accessor(int i)
Definition stdinhand.c:2514
static bool is_restricted(struct connection *caller)
Definition stdinhand.c:182
const char * script_extension
Definition stdinhand.c:113
static bool show_command(struct connection *caller, char *str, bool check)
Definition stdinhand.c:2150
static bool lua_command(struct connection *caller, char *arg, bool check, int read_recursion)
Definition stdinhand.c:4933
static bool write_init_script(char *script_filename)
Definition stdinhand.c:1278
static enum cmdlevel first_access_level
Definition stdinhand.c:98
static bool detach_command(struct connection *caller, char *str, bool check)
Definition stdinhand.c:3627
static bool aicmd_command(struct connection *caller, char *arg, bool check)
Definition stdinhand.c:5847
static void close_metaserver_connection(struct connection *caller)
Definition stdinhand.c:514
static bool save_command(struct connection *caller, char *arg, bool check)
Definition stdinhand.c:671
static bool set_ai_level_named(struct connection *caller, const char *name, const char *level_name, bool check)
Definition stdinhand.c:2017
static bool metapatches_command(struct connection *caller, char *arg, bool check)
Definition stdinhand.c:583
static bool show_ignore(struct connection *caller)
Definition stdinhand.c:6652
static bool is_allowed_to_take(struct connection *requester, struct connection *taker, struct player *pplayer, bool will_obs, char *msg, size_t msg_len)
Definition stdinhand.c:3148
void toggle_ai_player_direct(struct connection *caller, struct player *pplayer)
Definition stdinhand.c:704
static void show_help_option(struct connection *caller, enum command_id help_cmd, int id)
Definition stdinhand.c:1769
static bool read_command(struct connection *caller, char *arg, bool check, int read_recursion)
Definition stdinhand.c:1158
void strvec_destroy(struct strvec *psv)
#define strvec_iterate(psv, str)
#define strvec_iterate_end
Definition ai.h:50
struct ai_type::@14 funcs
void(* gained_control)(struct player *pplayer)
Definition ai.h:108
char name[MAX_LEN_NAME]
Definition ai.h:51
void(* player_console)(struct player *pplayer, const char *cmd)
Definition ai.h:105
Definition city.h:309
struct city::@17::@19 server
bool debug
Definition city.h:433
int kick_time
Definition game.h:152
struct civ_game::@30::@34 server
char start_units[MAX_LEN_STARTUNIT]
Definition game.h:188
bool debug[DEBUG_LAST]
Definition game.h:201
char connectmsg[MAX_LEN_MSG]
Definition game.h:218
char * ruleset_summary
Definition game.h:84
struct conn_list * est_connections
Definition game.h:97
struct packet_game_info info
Definition game.h:89
int min_players
Definition game.h:165
int timeoutcounter
Definition game.h:206
char rulesetdir[MAX_LEN_NAME]
Definition game.h:236
struct packet_scenario_info scenario
Definition game.h:87
int timeoutint
Definition game.h:202
struct conn_list * all_connections
Definition game.h:96
char save_name[MAX_LEN_NAME]
Definition game.h:219
int timeoutincmult
Definition game.h:204
int timeoutinc
Definition game.h:203
char allow_take[MAX_LEN_ALLOW_TAKE]
Definition game.h:238
bool start_city
Definition game.h:189
int max_players
Definition game.h:155
int timeoutintinc
Definition game.h:205
Definition colors.h:20
struct player * playing
Definition connection.h:156
struct connection::@57::@63::@64 delegation
enum cmdlevel access_level
Definition connection.h:182
struct conn_list * self
Definition connection.h:168
bool observer
Definition connection.h:152
char username[MAX_LEN_NAME]
Definition connection.h:169
struct connection::@57::@63 server
enum auth_status status
Definition connection.h:222
char ipaddr[MAX_LEN_ADDR]
Definition connection.h:226
struct conn_pattern_list * ignore_list
Definition connection.h:232
struct player * player
Definition nation.h:117
char message[MAX_LEN_MSG]
enum ai_level skill_level
enum ai_level skill_level
Definition player.h:122
bool random_name
Definition player.h:295
struct player_ai ai_common
Definition player.h:288
bv_pstatus status
Definition player.h:318
bool is_male
Definition player.h:257
struct government * target_government
Definition player.h:259
char username[MAX_LEN_NAME]
Definition player.h:252
bool is_connected
Definition player.h:296
struct government * government
Definition player.h:258
bool was_created
Definition player.h:294
const struct ai_type * ai
Definition player.h:289
struct unit_list * units
Definition player.h:282
struct conn_list * connections
Definition player.h:298
bool is_alive
Definition player.h:268
struct nation_style * style
Definition player.h:279
struct player::@69::@71 server
bv_debug debug
Definition player.h:332
struct rgbcolor * rgb
Definition player.h:308
bool unassigned_user
Definition player.h:253
char metaserver_addr[256]
Definition srv_main.h:29
char load_filename[512]
Definition srv_main.h:44
char * saves_pathname
Definition srv_main.h:46
char * script_filename
Definition srv_main.h:45
char serverid[256]
Definition srv_main.h:49
char *const value
Definition settings.c:143
Definition map.c:41
Definition tile.h:49
struct unit_list * units
Definition tile.h:57
Definition timing.c:81
Definition unit.h:138
bool debug
Definition unit.h:231
struct unit::@80::@83 server
Definition voting.h:46
int caller_id
Definition voting.h:47
char cmdline[512]
Definition voting.h:49
int vote_no
Definition voting.h:52
struct civ_map map
struct nation_style * style_by_rule_name(const char *name)
Definition style.c:117
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:787
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189
int cat_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:995
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition support.c:896
FILE * fc_fopen(const char *filename, const char *opentype)
Definition support.c:506
int fc_break_lines(char *str, size_t desired_len)
Definition support.c:1144
int fc_stat(const char *filename, struct stat *buf)
Definition support.c:575
bool is_reg_file_for_access(const char *name, bool write_access)
Definition support.c:1129
bool fc_isalnum(char c)
Definition support.c:1205
int fc_strncasecmp(const char *str0, const char *str1, size_t n)
Definition support.c:238
#define sz_strlcpy(dest, src)
Definition support.h:167
#define fc__attribute(x)
Definition support.h:89
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
#define sz_strlcat(dest, src)
Definition support.h:168
#define fc__fallthrough
Definition support.h:109
int team_index(const struct team *pteam)
Definition team.c:383
const char * team_name_translation(const struct team *pteam)
Definition team.c:420
struct team_slot * team_slot_by_number(int team_id)
Definition team.c:175
const char * team_slot_name_translation(const struct team_slot *tslot)
Definition team.c:253
bool team_add_player(struct player *pplayer, struct team *pteam)
Definition team.c:467
struct team * team_new(struct team_slot *tslot)
Definition team.c:317
struct team_slot * team_slot_by_rule_name(const char *team_name)
Definition team.c:189
const struct player_list * team_members(const struct team *pteam)
Definition team.c:456
void team_remove_player(struct player *pplayer)
Definition team.c:502
#define teams_iterate_end
Definition team.h:85
#define teams_iterate(_pteam)
Definition team.h:80
void init_tech(struct research *research, bool update)
Definition techtools.c:1070
void send_research_info(const struct research *presearch, const struct conn_list *dest)
Definition techtools.c:273
void give_initial_techs(struct research *presearch, int num_random_techs)
Definition techtools.c:1162
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
struct timer * timer_new(enum timer_timetype type, enum timer_use use)
Definition timing.c:157
void timer_destroy(struct timer *t)
Definition timing.c:191
void timer_start(struct timer *t)
Definition timing.c:224
double timer_read_seconds(struct timer *t)
Definition timing.c:344
@ TIMER_ACTIVE
Definition timing.h:45
@ TIMER_CPU
Definition timing.h:40
@ TIMER_USER
Definition timing.h:41
#define unit_owner(_pu)
Definition unit.h:394
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_end
Definition unitlist.h:33
const char * unit_name_translation(const struct unit *punit)
Definition unittype.c:1621
int describe_vote(struct vote *pvote, char *buf, int buflen)
Definition voting.c:748
bool conn_can_vote(const struct connection *pconn, const struct vote *pvote)
Definition voting.c:248
struct vote * vote_new(struct connection *caller, const char *allargs, int command_id)
Definition voting.c:338
bool vote_is_team_only(const struct vote *pvote)
Definition voting.c:235
struct vote * get_vote_by_no(int vote_no)
Definition voting.c:301
int vote_number_sequence
Definition voting.c:42
bool vote_would_pass_immediately(const struct connection *caller, int command_id)
Definition voting.c:391
void clear_all_votes(void)
Definition voting.c:219
struct vote * get_vote_by_caller(const struct connection *caller)
Definition voting.c:319
void connection_vote(struct connection *pconn, struct vote *pvote, enum vote_type type)
Definition voting.c:663
struct vote_list * vote_list
Definition voting.c:41
const struct connection * vote_get_caller(const struct vote *pvote)
Definition voting.c:891
int count_voters(const struct vote *pvote)
Definition voting.c:48
void remove_vote(struct vote *pvote)
Definition voting.c:205
bool conn_can_see_vote(const struct connection *pconn, const struct vote *pvote)
Definition voting.c:272
@ VOTE_ABSTAIN
Definition voting.h:27
@ VOTE_YES
Definition voting.h:27
@ VOTE_NUM
Definition voting.h:27
@ VOTE_NO
Definition voting.h:27
#define vote_list_iterate_end
Definition voting.h:65
@ VCF_NODISSENT
Definition voting.h:20
#define vote_list_iterate(alist, pvote)
Definition voting.h:63