]> 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-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 #if !defined(_WIN32)
38 #include <unistd.h>
39 #elif defined(__GNUC__)
40 #include <io.h>
41 #endif
42 #include "tel.h"
43 #include "commands.h"
44 #include "optlist.h"
45
46 /*
47  * Enable / disable logins and set the message of the day.
48  */
49 int
50 turn(void)
51 {
52     FILE *fptr;
53     struct telstr tgm;
54     char *p;
55     char buf[1024];
56     char msgbuf[MAXTELSIZE + 1]; /* UTF-8 */
57     char *msgfilepath;
58
59     p = getstarg(player->argp[1], "on, off or motd? ", buf);
60     if (!p)
61         return RET_SYN;
62     if (strcmp(p, "off") == 0) {
63         msgfilepath = downfil;
64     } else if (strcmp(p, "on") == 0) {
65         pr("Removing no-login message and re-enabling logins.\n");
66         if ((unlink(downfil) == -1) && (errno != ENOENT)) {
67             pr("Could not remove no-login file, logins still disabled.\n");
68             logerror("Could not remove no-login file (%s).\n", downfil);
69             return RET_SYS;
70         }
71         return RET_OK;
72     } else {
73         msgfilepath = motdfil;
74     }
75
76     if (msgfilepath == downfil)
77         pr("Enter a message shown to countries trying to log in.\n");
78     else
79         pr("Enter a new message of the day.\n");
80
81     time(&tgm.tel_date);
82     tgm.tel_length = getele("The World", msgbuf);
83
84     if (tgm.tel_length < 0) {
85         pr("Ignored\n");
86         if (msgfilepath == downfil)
87             pr("NOT disabling logins.\n");
88         return RET_SYN;
89     } else if (tgm.tel_length == 0) {
90         if (msgfilepath == motdfil) {
91             pr("Removing exsting motd.\n");
92             if ((unlink(msgfilepath) == -1) && (errno != ENOENT)) {
93                 pr("Could not remove motd.\n");
94                 logerror("Could not remove motd file (%s).\n",
95                          msgfilepath);
96                 return RET_SYS;
97             }
98             return RET_OK;
99         } else
100             pr("Writing empty no-login message.\n");
101     }
102
103     fptr = fopen(msgfilepath, "wb");
104     if (fptr == NULL) {
105         pr("Something went wrong opening the message file.\n");
106         logerror("Could not open message file (%s).\n", msgfilepath);
107         return RET_SYS;
108     }
109
110     if (msgfilepath == downfil)
111         pr("Logins disabled.\n");
112
113     if ((fwrite(&tgm, sizeof(tgm), 1, fptr) != 1) ||
114         (fwrite(msgbuf, tgm.tel_length, 1, fptr) != 1)) {
115         fclose(fptr);
116         pr("Something went wrong writing the message file.\n");
117         logerror("Could not properly write message file (%s).\n",
118             msgfilepath);
119         return RET_SYS;
120     }
121     if (fclose(fptr)) {
122         pr("Something went wrong closing the message.\n");
123         logerror("Could not properly close message file (%s).\n",
124             msgfilepath);
125         return RET_SYS;
126     }
127
128     pr("\n");
129
130     return RET_OK;
131 }