/
umud/DOC/
umud/DOC/examples/
umud/DOC/internals/
umud/DOC/wizard/
umud/MISC/
umud/MISC/dbchk/
umud/RWHO/rwhod/
/*
	Copyright (C) 1991, Marcus J. Ranum. All rights reserved.
*/

#ifndef	lint
static	char	RCSid[] = "$Header: /usr/users/mjr/hacks/umud/CMD/RCS/news.c,v 1.3 91/09/23 16:10:05 mjr Exp $";
#endif

/* configure all options BEFORE including system stuff. */
#include	"config.h"


#ifdef NOSYSTYPES_H
#include	<types.h>
#else
#include	<sys/types.h>
#endif

#include	<stdio.h>
#include	<ctype.h>

#include	"mud.h"
#include	"vars.h"


static	int
deletepost(who,aswho,num)
char	*who;
char	*aswho;
int	num;
{
	char	nfbuf[640];

	if(!ut_flagged(aswho,var_wiz)){
		say(who,"Only a wizard can delete a news item.\n",(char *)0);
		return(1);
	}
	(void)sprintf(nfbuf,"NEWS/%d",num);
	if(unlink(nfbuf)){
		say(who,"Could not delete news item!\n",(char *)0);
		return(1);
	}
	say(who,"Deleted.\n",(char *)0);
	return(0);
}

static	int
post(who,message)
char	*who;
char	*message;
{
	FILE	*fp;
	int	newart;
	char	nfbuf[640];
	time_t	now;
	extern	char	*ctime();

	/* access and increment the system top article ID */
	if(ut_getnum(system_object,var_newsart,&newart))
		newart = 0;

	newart = newart + 1;

	/* try to open the file for output */
	(void)sprintf(nfbuf,"NEWS/%d",newart);
	if((fp = fopen(nfbuf,"w")) == (FILE *)0) {
		say(who,"Cannot create new article\n",(char *)0);
		return(1);
	}

	/* if the file open worked, increment the system article count. */ 
	if(ut_setnum(who,system_object,var_newsart,newart)) {
		(void)fclose(fp);
		(void)unlink(nfbuf);
		say(who,"System error. Could not post.\n",(char *)0);
		return(1);
	}

	/* we now have the article # and the file open. go for it. */
	(void)time(&now);

	/* naughty, naughty */
	(void)fprintf(fp," -- Article #%d, posted by %s(%s), %s%s\n",
		newart,ut_name(who),who,ctime(&now),message);

	(void)fclose(fp);
	say(who,"Posted.\n",(char *)0);
	return(0);
}



/* ARGSUSED */
cmd_NEWS(argc,argv,who,aswho)
int	argc;
char	*argv[];
char	*who;
char	*aswho;
{
	int	anum;
	int	nnum;
	int	topart;
	char	*ap;
	char	nfbuf[640];
	char	nxbuf[64];
	int	setflg = 0;

	if((ap = ut_getatt(who,0,typ_int,var_newsart,(char *)0)) != (char *)0)
		anum = atoi(ap);
	else
		anum = 0;


	if(argc == 1)
		nnum = anum + 1, setflg++;
	else
	if(argc == 2 && !strncmp(argv[1],"next",strlen(argv[1])))
		nnum = anum + 1, setflg++;
	else
	if(argc == 2 && !strncmp(argv[1],"previous",strlen(argv[1])))
		nnum = anum - 1;
	else
	if(argc >= 2 && !strncmp(argv[1],"post",strlen(argv[1]))) {
		if(argc != 3) {
			say(who,"Usage: ",argv[0]," post message-text\n",(char *)0);
			return(1);
		}
		return(post(who,argv[2]));
	}
	else
	if(argc >= 2 && !strncmp(argv[1],"goto",strlen(argv[1]))) {
		if(argc != 3) {
			say(who,"Usage: ",argv[0]," goto message-number\n",(char *)0);
			return(1);
		}
		nnum = atoi(argv[2]);
		setflg++;
	}
	else
	if(argc >= 2 && !strncmp(argv[1],"delete",strlen(argv[1]))) {
		if(argc != 3) {
			say(who,"Usage: ",argv[0]," delete message-number\n",(char *)0);
			return(1);
		}
		nnum = atoi(argv[2]);
		return(deletepost(who,aswho,nnum));
	}
	else
	if(argc == 2 && isdigit(argv[1][0]))
		nnum = atoi(argv[1]);
	else {
		say(who,argv[0]," [next]\n",(char *)0);
		say(who,argv[0]," previous\n",(char *)0);
		say(who,argv[0]," post message-text\n",(char *)0);
		say(who,argv[0]," delete message-number\n",(char *)0);
		say(who,argv[0]," [goto] message-number\n",(char *)0);
		return(1);
	}

	if(nnum <= 0) {
		say(who,itoa(nnum,nxbuf)," isn't a valid article.\n",(char *)0);
		return(1);
	}

	if(ut_getnum(system_object,var_newsart,&topart)){
		say(who,"There's no news.\n",(char *)0);
		return(0);
	}
	do{
		(void)sprintf(nfbuf,"NEWS/%d",nnum);
		if(say_file(who,nfbuf)) {
			say(who,"no article ",itoa(nnum,nxbuf),"\n",(char *)0);
			nnum++;
		} else {
			say(who," -- End Article ",itoa(nnum,nxbuf),"\n",(char *)0);
			break;
		}
	} while(nnum <= topart && setflg);

	nnum = (nnum > topart ? topart : nnum);

	/* update ".newsrc" */
	if(setflg) {
		if(ut_set(who,who,typ_int,var_newsart,itoa(nnum,nxbuf)))
			return(1);
	}
	return(0);
}