src/lib/commands/turn.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  turn.c: Turn the game on, off, or set the login message.
00029  * 
00030  *  Known contributors to this file:
00031  *     Marc Olzheim, 2004
00032  */
00033 
00034 #include <config.h>
00035 
00036 #include <errno.h>
00037 #include <unistd.h>
00038 #include "tel.h"
00039 #include "commands.h"
00040 #include "optlist.h"
00041 
00042 /*
00043  * Enable / disable logins and set the message of the day.
00044  */
00045 int
00046 turn(void)
00047 {
00048     FILE *fptr;
00049     struct telstr tgm;
00050     char *p;
00051     char buf[1024];
00052     char msgbuf[MAXTELSIZE + 1]; /* UTF-8 */
00053     char *msgfilepath;
00054 
00055     p = getstarg(player->argp[1], "on, off or motd? ", buf);
00056     if (!p)
00057         return RET_SYN;
00058     if (strcmp(p, "off") == 0) {
00059         msgfilepath = downfil;
00060     } else if (strcmp(p, "on") == 0) {
00061         pr("Removing no-login message and re-enabling logins.\n");
00062         if ((unlink(downfil) == -1) && (errno != ENOENT)) {
00063             pr("Could not remove no-login file, logins still disabled.\n");
00064             logerror("Could not remove no-login file (%s).\n", downfil);
00065             return RET_SYS;
00066         }
00067         return RET_OK;
00068     } else {
00069         msgfilepath = motdfil;
00070     }
00071 
00072     if (msgfilepath == downfil)
00073         pr("Enter a message shown to countries trying to log in.\n");
00074     else
00075         pr("Enter a new message of the day.\n");
00076 
00077     time(&tgm.tel_date);
00078     tgm.tel_length = getele("The World", msgbuf);
00079 
00080     if (tgm.tel_length < 0) {
00081         pr("Ignored\n");
00082         if (msgfilepath == downfil)
00083             pr("NOT disabling logins.\n");
00084         return RET_SYN;
00085     } else if (tgm.tel_length == 0) {
00086         if (msgfilepath == motdfil) {
00087             pr("Removing exsting motd.\n");
00088             if ((unlink(msgfilepath) == -1) && (errno != ENOENT)) {
00089                 pr("Could not remove motd.\n");
00090                 logerror("Could not remove motd file (%s).\n",
00091                          msgfilepath);
00092                 return RET_SYS;
00093             }
00094             return RET_OK;
00095         } else
00096             pr("Writing empty no-login message.\n");
00097     }
00098 
00099     fptr = fopen(msgfilepath, "wb");
00100     if (fptr == NULL) {
00101         pr("Something went wrong opening the message file.\n");
00102         logerror("Could not open message file (%s).\n", msgfilepath);
00103         return RET_SYS;
00104     }
00105 
00106     if (msgfilepath == downfil)
00107         pr("Logins disabled.\n");
00108 
00109     if ((fwrite(&tgm, sizeof(tgm), 1, fptr) != 1) ||
00110         (fwrite(msgbuf, tgm.tel_length, 1, fptr) != 1)) {
00111         fclose(fptr);
00112         pr("Something went wrong writing the message file.\n");
00113         logerror("Could not properly write message file (%s).\n",
00114             msgfilepath);
00115         return RET_SYS;
00116     }
00117     if (fclose(fptr)) {
00118         pr("Something went wrong closing the message.\n");
00119         logerror("Could not properly close message file (%s).\n",
00120             msgfilepath);
00121         return RET_SYS;
00122     }
00123 
00124     pr("\n");
00125 
00126     return RET_OK;
00127 }

Generated on Fri Mar 28 11:01:13 2008 for empserver by  doxygen 1.5.2