(turn): Change the command to allow for both an MOTD and a no-login

message.  Clean 'register' and 's_char' remnants on the way.
Circumvents bug #813167.
This commit is contained in:
Marc Olzheim 2004-12-16 14:17:08 +00:00
parent b3f62f7d66
commit d9f0f475da
7 changed files with 119 additions and 66 deletions

View file

@ -99,7 +99,7 @@ int start_unit_type[];
#endif #endif
/* Variables that get values derived from econfig */ /* Variables that get values derived from econfig */
extern char *upfil; extern char *motdfil;
extern char *downfil; extern char *downfil;
extern char *disablefil; extern char *disablefil;
extern char *banfil; extern char *banfil;

View file

@ -261,7 +261,7 @@ extern int natbyname(s_char *, natid *);
extern int natpass(int, s_char *); extern int natpass(int, s_char *);
/* player.c */ /* player.c */
extern struct player *player; /* current player's context */ extern struct player *player; /* current player's context */
extern s_char *praddr(struct player *); extern char *praddr(struct player *);
extern void player_main(struct player *); extern void player_main(struct player *);
extern int match_user(char *, struct player *); extern int match_user(char *, struct player *);
extern int status(void); extern int status(void);

View file

@ -1,9 +1,10 @@
.TH Command TURN .TH Command TURN
.NA turn "Allow/disallow logins. Change login message." .NA turn "Allow/disallow logins. Change login message."
.LV Expert .LV Expert
.SY "turn <on|off|mess>" .SY "turn <on|off|motd>"
Use "turn on" or "turn off" to enable/disable logins. If you just Use "turn on" or "turn off" to enable/disable logins. If you want to
want to change the login message, then use "turn mess". In all three change the login message, then use "turn motd". With "turn motd" and
cases, you will be put into the telegram prompter to enter a new motd "turn off", you will be put into the telegram prompter to enter a new
(message of the day) which is printed when the player logs in. motd (message of the day) which is printed when the player logs in, or
an additional "no-login" message printed while logins are disabled.
.SA "Deity" .SA "Deity"

View file

@ -37,52 +37,94 @@
#include "commands.h" #include "commands.h"
#include "optlist.h" #include "optlist.h"
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
/*
* Enable / disable logins and set the message of the day.
*/
int int
turn(void) turn(void)
{ {
int fd; int fd;
struct telstr tgm; struct telstr tgm;
register s_char *p; char *p;
s_char buf[MAXTELSIZE]; char buf[MAXTELSIZE];
char *msgfilepath;
p = getstarg(player->argp[1], "on, off or message? ", buf); p = getstarg(player->argp[1], "on, off or motd? ", buf);
if (!p) if (!p)
return RET_SYN; return RET_SYN;
if (strcmp(p, "off") == 0) { if (strcmp(p, "off") == 0) {
(void)unlink(upfil); msgfilepath = downfil;
#if !defined(_WIN32)
fd = open(downfil, O_RDWR | O_CREAT | O_TRUNC, 0660);
#else
fd = open(downfil, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0660);
#endif
pr("off ");
} else if (strcmp(p, "on") == 0) { } else if (strcmp(p, "on") == 0) {
(void)unlink(downfil); pr("Removing no-login message and re-enabling logins.\n");
#if !defined(_WIN32) if ((unlink(downfil) == -1) && (errno != ENOENT))
fd = open(upfil, O_RDWR | O_CREAT | O_TRUNC, 0660); {
#else pr("Could not remove no-login file, logins still disabled.\n");
fd = open(upfil, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0660); logerror("Could not remove no-login file (%s).\n", downfil);
#endif return RET_SYS;
pr("on "); }
return RET_OK;
} else { } else {
#if !defined(_WIN32) msgfilepath = motdfil;
fd = open(upfil, O_RDWR | O_CREAT | O_TRUNC, 0660);
#else
fd = open(upfil, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0660);
#endif
pr("motd ");
} }
(void)time(&tgm.tel_date);
if ((tgm.tel_length = getele("The World", buf)) <= 0) { if (msgfilepath == downfil)
pr("Enter a message shown to countries trying to log in.\n");
else
pr("Enter a new message of the day.\n");
time(&tgm.tel_date);
tgm.tel_length = getele("The World", buf);
if (tgm.tel_length < 0)
{
pr("Ignored\n"); pr("Ignored\n");
close(fd); if (msgfilepath == downfil)
pr("NOT disabling logins.\n");
return RET_SYN; return RET_SYN;
} else if (tgm.tel_length == 0)
{
if (msgfilepath == motdfil)
{
pr("Removing exsting motd.\n");
if ((unlink(msgfilepath) == -1) && (errno != ENOENT))
{
pr("Could not remove motd.\n");
logerror("Could not remove motd file (%s).\n", msgfilepath);
return RET_SYS;
}
return RET_OK;
} else
pr("Writing empty no-login message.\n");
}
#if !defined(_WIN32)
fd = open(msgfilepath, O_RDWR | O_CREAT | O_TRUNC, 0660);
#else
fd = open(msgfilepath, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0660);
#endif
if (fd == -1)
{
pr("Something went wrong opening the message file.\n");
logerror("Could not open message file (%s).\n", msgfilepath);
return RET_SYS;
}
if (msgfilepath == downfil)
pr("Logins disabled.\n");
if ((write(fd, &tgm, sizeof(tgm)) < sizeof(tgm)) ||
(write(fd, buf, tgm.tel_length) < tgm.tel_length) ||
(close(fd) == -1))
{
pr("Something went wrong writing the message file.\n");
logerror("Could not properly write message file (%s).\n",
msgfilepath);
return RET_SYS;
} }
(void)write(fd, (s_char *)&tgm, sizeof(tgm));
(void)write(fd, buf, tgm.tel_length);
(void)close(fd);
pr("\n"); pr("\n");
return RET_OK; return RET_OK;
} }

View file

@ -173,7 +173,7 @@ struct otherfiles {
/* list of other well known files... -maybe tailor these oneday /* list of other well known files... -maybe tailor these oneday
* anyway - meantime they are all relative to datadir */ * anyway - meantime they are all relative to datadir */
static struct otherfiles ofiles[] = { static struct otherfiles ofiles[] = {
{&upfil, "up"}, {&motdfil, "motd"},
{&downfil, "down"}, {&downfil, "down"},
{&disablefil, "disable"}, {&disablefil, "disable"},
{&banfil, "ban"}, {&banfil, "ban"},

View file

@ -39,7 +39,7 @@ char *infodir = EMPDIR "/info.nr";
char *datadir = EMPDIR "/data"; char *datadir = EMPDIR "/data";
char *teldir = EMPDIR "/data/tel"; char *teldir = EMPDIR "/data/tel";
char *upfil = EMPDIR "/data/up"; char *motdfil = EMPDIR "/data/motd";
char *downfil = EMPDIR "/data/down"; char *downfil = EMPDIR "/data/down";
char *disablefil = EMPDIR "/data/disable"; char *disablefil = EMPDIR "/data/disable";
char *telfil = EMPDIR "/data/tel/tel"; char *telfil = EMPDIR "/data/tel/tel";

View file

@ -56,6 +56,7 @@
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#include <stdio.h> #include <stdio.h>
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
struct player *player; struct player *player;
@ -65,12 +66,12 @@ player_main(struct player *p)
{ {
struct natstr *natp; struct natstr *natp;
int secs; int secs;
s_char buf[128]; char buf[128];
p->state = PS_PLAYING; p->state = PS_PLAYING;
player = p; player = p;
time(&player->lasttime); time(&player->lasttime);
(void)time(&player->curup); time(&player->curup);
showvers(CLIENTPROTO); showvers(CLIENTPROTO);
show_motd(); show_motd();
if (init_nats() < 0) if (init_nats() < 0)
@ -102,9 +103,9 @@ player_main(struct player *p)
natp->nat_last_logout ? ctime(&natp-> natp->nat_last_logout ? ctime(&natp->
nat_last_logout) : "?"); nat_last_logout) : "?");
pr(" by: %s@%s\n", pr(" by: %s@%s\n",
*natp->nat_userid ? natp->nat_userid : (s_char *)"nobody", *natp->nat_userid ? natp->nat_userid : "nobody",
*natp->nat_hostname ? natp->nat_hostname : *natp-> *natp->nat_hostname ? natp->nat_hostname : *natp->
nat_hostaddr ? natp->nat_hostaddr : (s_char *)"nowhere"); nat_hostaddr ? natp->nat_hostaddr : "nowhere");
} }
} }
if (*player->userid) if (*player->userid)
@ -158,8 +159,8 @@ int
command(void) command(void)
{ {
unsigned int x; unsigned int x;
s_char *redir; char *redir;
s_char scanspace[1024]; char scanspace[1024];
if (getcommand(player->combuf) < 0) if (getcommand(player->combuf) < 0)
return 0; return 0;
@ -171,7 +172,7 @@ command(void)
/* DONT USE IT!!!! alarm and sleep may and dont work /* DONT USE IT!!!! alarm and sleep may and dont work
together -- Sasha */ together -- Sasha */
/* alarm((unsigned int)60*60); 1 hour */ /* alarm((unsigned int)60*60); 1 hour */
if (player->condarg != (s_char *)0) if (player->condarg != NULL)
for (x = 0; x < strlen(player->condarg); x++) for (x = 0; x < strlen(player->condarg); x++)
if (isupper(*(player->condarg + x))) if (isupper(*(player->condarg + x)))
*(player->condarg + x) = *(player->condarg + x) =
@ -188,7 +189,7 @@ status(void)
struct natstr *natp; struct natstr *natp;
int minute; int minute;
struct sctstr sect; struct sctstr sect;
s_char buf[128]; char buf[128];
if (player->state == PS_SHUTDOWN) if (player->state == PS_SHUTDOWN)
return 0; return 0;
@ -236,7 +237,7 @@ status(void)
if (natp->nat_flags & NF_SACKED) if (natp->nat_flags & NF_SACKED)
player->nstat &= ~CAP; /* No capital yet */ player->nstat &= ~CAP; /* No capital yet */
player->ncomstat = player->nstat; player->ncomstat = player->nstat;
(void)time(&player->curup); time(&player->curup);
minute = (player->curup - player->lasttime) / 60; minute = (player->curup - player->lasttime) / 60;
if (minute > 0) { if (minute > 0) {
player->minleft -= minute; player->minleft -= minute;
@ -303,18 +304,18 @@ status(void)
int int
execute(void) execute(void)
{ {
s_char buf[512]; char buf[512];
int failed; int failed;
s_char *p; char *p;
s_char *redir; char *redir;
s_char scanspace[1024]; char scanspace[1024];
failed = 0; failed = 0;
redir = 0; redir = 0;
p = getstarg(player->argp[1], "File? ", buf); p = getstarg(player->argp[1], "File? ", buf);
if (p == (s_char *)0 || *p == '\0') if (p == NULL || *p == '\0')
return RET_SYN; return RET_SYN;
prexec(player->argp[1]); prexec(player->argp[1]);
@ -342,31 +343,40 @@ execute(void)
int int
show_motd(void) show_motd(void)
{ {
int upf; int motdf;
struct telstr tgm; struct telstr tgm;
s_char buf[MAXTELSIZE]; char buf[MAXTELSIZE];
#if !defined(_WIN32) #if !defined(_WIN32)
if ((upf = open(upfil, O_RDONLY, 0)) < 0) if ((motdf = open(motdfil, O_RDONLY, 0)) < 0)
#else #else
if ((upf = open(upfil, O_RDONLY | O_BINARY, 0)) < 0) if ((motdf = open(motdfil, O_RDONLY | O_BINARY, 0)) < 0)
#endif #endif
return RET_FAIL; {
if (read(upf, (s_char *)&tgm, sizeof(tgm)) != sizeof(tgm)) { if (errno == ENOENT)
logerror("bad header on login message (upfil)"); return RET_OK;
close(upf); else
{
pr ("Could not open motd.\n");
logerror("Could not open motd (%s).\n", motdfil);
return RET_SYS;
}
}
if (read(motdf, &tgm, sizeof(tgm)) != sizeof(tgm)) {
logerror("bad header on login message (motdfil)");
close(motdf);
return RET_FAIL; return RET_FAIL;
} }
if (read(upf, buf, tgm.tel_length) != tgm.tel_length) { if (read(motdf, buf, tgm.tel_length) != tgm.tel_length) {
logerror("bad length %ld on login message", tgm.tel_length); logerror("bad length %ld on login message", tgm.tel_length);
close(upf); close(motdf);
return RET_FAIL; return RET_FAIL;
} }
if (tgm.tel_length >= (long)sizeof(buf)) if (tgm.tel_length >= (long)sizeof(buf))
tgm.tel_length = sizeof(buf) - 1; tgm.tel_length = sizeof(buf) - 1;
buf[tgm.tel_length] = 0; buf[tgm.tel_length] = 0;
prnf(buf); prnf(buf);
(void)close(upf); (void)close(motdf);
return RET_OK; return RET_OK;
} }
@ -375,8 +385,8 @@ match_user(char *file, struct player *p)
{ {
FILE *fp; FILE *fp;
int match = 0; int match = 0;
s_char host[256]; char host[256];
s_char user[256]; char user[256];
if ((fp = fopen(file, "r")) == NULL) { if ((fp = fopen(file, "r")) == NULL) {
/*logerror("Cannot find file %s", file); */ /*logerror("Cannot find file %s", file); */
@ -408,7 +418,7 @@ quit(void)
return RET_OK; return RET_OK;
} }
s_char * char *
praddr(struct player *p) praddr(struct player *p)
{ {
return prbuf("%s@%s", p->userid, return prbuf("%s@%s", p->userid,