]> git.pond.sub.org Git - empserver/blob - src/lib/commands/turn.c
Update copyright notice
[empserver] / src / lib / commands / turn.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2012, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  turn.c: Turn the game on, off, or set the login message.
28  *
29  *  Known contributors to this file:
30  *     Marc Olzheim, 2004
31  *     Ron Koenderink, 2005-2007
32  *     Markus Armbruster, 2005-2009
33  */
34
35 #include <config.h>
36
37 #include <errno.h>
38 #include <unistd.h>
39 #include "game.h"
40 #include "tel.h"
41 #include "commands.h"
42 #include "optlist.h"
43
44 int
45 turn(void)
46 {
47     FILE *fptr;
48     struct telstr tgm;
49     char *p;
50     char buf[1024];
51     char msgbuf[MAXTELSIZE + 1]; /* UTF-8 */
52     char *msgfilepath;
53     int len, down;
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         pr("Enter a message explaining the down time.\n");
61         len = getele("The World", msgbuf);
62         down = 1;
63     } else if (strcmp(p, "on") == 0) {
64         msgfilepath = downfil;
65         len = 0;
66         down = 0;
67     } else {
68         msgfilepath = motdfil;
69         pr("Enter a new message of the day.\n");
70         len = getele("The World", msgbuf);
71         down = -1;
72     }
73
74     if (len < 0)
75         return RET_FAIL;
76     if (len == 0) {
77         if (unlink(msgfilepath) < 0 && (errno != ENOENT)) {
78             pr("Could not remove %s file.\n", msgfilepath);
79             logerror("Could not remove %s file (%s)",
80                      msgfilepath, strerror(errno));
81             return RET_FAIL;
82         }
83     } else {
84         fptr = fopen(msgfilepath, "wb");
85         if (fptr == NULL) {
86             pr("Something went wrong opening the message file.\n");
87             logerror("Could not open message file (%s).\n", msgfilepath);
88             return RET_FAIL;
89         }
90         memset(&tgm, 0, sizeof(tgm));
91         time(&tgm.tel_date);
92         tgm.tel_length = len;
93         if (fwrite(&tgm, sizeof(tgm), 1, fptr) != 1 ||
94             fwrite(msgbuf, 1, tgm.tel_length, fptr) != tgm.tel_length) {
95             fclose(fptr);
96             pr("Something went wrong writing the message file.\n");
97             logerror("Could not properly write message file (%s).\n",
98                      msgfilepath);
99             return RET_FAIL;
100         }
101         if (fclose(fptr)) {
102             pr("Something went wrong closing the message.\n");
103             logerror("Could not properly close message file (%s).\n",
104                      msgfilepath);
105             return RET_FAIL;
106         }
107     }
108
109     if (down >= 0)
110         game_ctrl_play(!down);
111
112     /* "The game is down" will be printed automatically */
113     return RET_OK;
114 }