]> git.pond.sub.org Git - empserver/blob - src/lib/commands/turn.c
1cbfade7217a5cd3456c9ce599fb512e8b754ec1
[empserver] / src / lib / commands / turn.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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  *     Ron Koenderink, 2005-2007
33  *     Markus Armbruster, 2005-2009
34  */
35
36 #include <config.h>
37
38 #include <errno.h>
39 #include <unistd.h>
40 #include "game.h"
41 #include "tel.h"
42 #include "commands.h"
43 #include "optlist.h"
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     int len, down;
55
56     p = getstarg(player->argp[1], "on, off or motd? ", buf);
57     if (!p)
58         return RET_SYN;
59     if (strcmp(p, "off") == 0) {
60         msgfilepath = downfil;
61         pr("Enter a message explaining the down time.\n");
62         len = getele("The World", msgbuf);
63         down = 1;
64     } else if (strcmp(p, "on") == 0) {
65         msgfilepath = downfil;
66         len = 0;
67         down = 0;
68     } else {
69         msgfilepath = motdfil;
70         pr("Enter a new message of the day.\n");
71         len = getele("The World", msgbuf);
72         down = -1;
73     }
74
75     if (len < 0)
76         return RET_FAIL;
77     if (len == 0) {
78         if (unlink(msgfilepath) < 0 && (errno != ENOENT)) {
79             pr("Could not remove %s file.\n", msgfilepath);
80             logerror("Could not remove %s file (%s)",
81                      msgfilepath, strerror(errno));
82             return RET_FAIL;
83         }
84     } else {
85         fptr = fopen(msgfilepath, "wb");
86         if (fptr == NULL) {
87             pr("Something went wrong opening the message file.\n");
88             logerror("Could not open message file (%s).\n", msgfilepath);
89             return RET_FAIL;
90         }
91         memset(&tgm, 0, sizeof(tgm));
92         time(&tgm.tel_date);
93         tgm.tel_length = len;
94         if (fwrite(&tgm, sizeof(tgm), 1, fptr) != 1 ||
95             fwrite(msgbuf, 1, tgm.tel_length, fptr) != tgm.tel_length) {
96             fclose(fptr);
97             pr("Something went wrong writing the message file.\n");
98             logerror("Could not properly write message file (%s).\n",
99                      msgfilepath);
100             return RET_FAIL;
101         }
102         if (fclose(fptr)) {
103             pr("Something went wrong closing the message.\n");
104             logerror("Could not properly close message file (%s).\n",
105                      msgfilepath);
106             return RET_FAIL;
107         }
108     }
109
110     if (down >= 0)
111         game_ctrl_play(!down);
112
113     /* "The game is down" will be printed automatically */
114     return RET_OK;
115 }