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