]> git.pond.sub.org Git - empserver/blob - src/lib/commands/turn.c
2594341238908b4f89bdabb74e8559dc87c338fb
[empserver] / src / lib / commands / turn.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  turn.c: Turn the game on, off, or set the login message.
29  * 
30  *  Known contributors to this file:
31  *     Marc Olzheim, 2004
32  */
33
34 #include <config.h>
35
36 #include <errno.h>
37 #include <unistd.h>
38 #include "tel.h"
39 #include "commands.h"
40 #include "optlist.h"
41
42 /*
43  * Enable / disable logins and set the message of the day.
44  */
45 int
46 turn(void)
47 {
48     FILE *fptr;
49     struct telstr tgm;
50     char *p;
51     char buf[1024];
52     char msgbuf[MAXTELSIZE + 1]; /* UTF-8 */
53     char *msgfilepath;
54
55     p = getstarg(player->argp[1], "on, off or motd? ", buf);
56     if (!p)
57         return RET_SYN;
58     if (strcmp(p, "off") == 0) {
59         msgfilepath = downfil;
60     } else if (strcmp(p, "on") == 0) {
61         pr("Removing no-login message and re-enabling logins.\n");
62         if ((unlink(downfil) == -1) && (errno != ENOENT)) {
63             pr("Could not remove no-login file, logins still disabled.\n");
64             logerror("Could not remove no-login file (%s).\n", downfil);
65             return RET_SYS;
66         }
67         return RET_OK;
68     } else {
69         msgfilepath = motdfil;
70     }
71
72     if (msgfilepath == downfil)
73         pr("Enter a message shown to countries trying to log in.\n");
74     else
75         pr("Enter a new message of the day.\n");
76
77     time(&tgm.tel_date);
78     tgm.tel_length = getele("The World", msgbuf);
79
80     if (tgm.tel_length < 0) {
81         pr("Ignored\n");
82         if (msgfilepath == downfil)
83             pr("NOT disabling logins.\n");
84         return RET_SYN;
85     } else if (tgm.tel_length == 0) {
86         if (msgfilepath == motdfil) {
87             pr("Removing exsting motd.\n");
88             if ((unlink(msgfilepath) == -1) && (errno != ENOENT)) {
89                 pr("Could not remove motd.\n");
90                 logerror("Could not remove motd file (%s).\n",
91                          msgfilepath);
92                 return RET_SYS;
93             }
94             return RET_OK;
95         } else
96             pr("Writing empty no-login message.\n");
97     }
98
99     fptr = fopen(msgfilepath, "wb");
100     if (fptr == NULL) {
101         pr("Something went wrong opening the message file.\n");
102         logerror("Could not open message file (%s).\n", msgfilepath);
103         return RET_SYS;
104     }
105
106     if (msgfilepath == downfil)
107         pr("Logins disabled.\n");
108
109     if ((fwrite(&tgm, sizeof(tgm), 1, fptr) != 1) ||
110         (fwrite(msgbuf, tgm.tel_length, 1, fptr) != 1)) {
111         fclose(fptr);
112         pr("Something went wrong writing the message file.\n");
113         logerror("Could not properly write message file (%s).\n",
114             msgfilepath);
115         return RET_SYS;
116     }
117     if (fclose(fptr)) {
118         pr("Something went wrong closing the message.\n");
119         logerror("Could not properly close message file (%s).\n",
120             msgfilepath);
121         return RET_SYS;
122     }
123
124     pr("\n");
125
126     return RET_OK;
127 }