/*	:ts=4
 *	newgroup - this is a C implementation for the Amiga, in an attempt to
 *	reduce the dependency on shell scripts (they aren't capable enough).
 *
 *	Expects two command line arguments,
 *		"news.group" and "moderated/unmoderated"
 *
 *	$Id$
 */

#include <stdio.h>
#include <string.h>

#ifdef __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif /* __STDC__ */

#include "libc.h"
#include "news.h"
#include "config.h"
#include "system.h"

static void read_article(FILE *fp);
static void mail(char *to, char *msg, ...);

char approved[128], sender[128], from[128];
char *progname;

main(int argc, char **argv)
{
	FILE *active;
	long pos;
	char *ptr, *who;
	char flag;
	struct system *me;
	static char buff[BUFSIZ];

	if ((progname=strchr(*argv,'/')) || (progname=strchr(*argv,':')))
		progname++;
	else
		progname = *argv;

	read_article(stdin);
	if (ptr = strchr(approved, '\n'))			*ptr = '\0';
	if (*sender)
		if (ptr = strchr(who = sender, '\n'))	*ptr = '\0';
	else
		if (ptr = strchr(who = from, '\n'))		*ptr = '\0';

	if (!*approved) {
		mail(newsmaster(), "Unapproved `newgroup' request from %s", who);
		exit( 10 );
	}
	if (argc > 1 && !strcmp(argv[2], "moderated")) flag = 'm';
	else flag = 'y';

	if (active = fopen(ctlfile("active"), "r+")) {
		int len = strlen(argv[1]);
		char ch = '!';

		ptr = buff + len;
		while (pos = ftell(active), fgets(buff, sizeof(buff), active))
			if (isspace(*ptr) && !strncmp(buff, argv[1], len)) {
				ch = strchr(ptr, '\n')[-1];
				if (flag != ch && ch != '=') {
					fseek(active, pos, SEEK_SET);
					fputs(buff, active);
					mail(newsmaster(),
						"newgroup modified %s per %s", argv[1], who);
				}
				break;
			}
		if (ch != '!') {
			fclose(active);
			exit( 0 );			/* Matching line found, all done */
		}
	} else {
		error("Couldn't open `active' file", "");
		exit( 10 );
	}
	if ((me = oursys()) == NULL) {
		mail(newsmaster(), "Couldn't find \"ME\" in sys file!");
		exit( 10 );
	}
	if (ngmatch(me->sy_ngs, argv[1])) {
		fseek(active, 0L, SEEK_END);
		fprintf(active, "%s 0000000000 0000000000 %c\n", argv[1], flag);
		fclose(active);
		if (active = fopen(ctlfile("active.times"), "a+")) {
			time_t tim = time(0L);

			fprintf(active, "%s %s %s", argv[1], who, ctime(&tim));
			fclose(active);
		}
		ptr = argv[1];
		while (ptr = strchr(ptr, NGDELIM))
			*ptr++ = FNDELIM;
		mkdir(argv[1]);
		ptr = argv[1];
		while (ptr = strchr(ptr, FNDELIM))
			*ptr++ = NGDELIM;
		mail(newsmaster(), "newsgroup %s created by %s\n", argv[1], who);
		return( 0 );
	}
	mail(newsmaster(),
		"newsgroup %s rejected: not in subscription list\n", argv[1]);
	return( 5 );
}

static void read_article(FILE *fp)
{
	static char Approved[] = "Approved: ";
	static char Sender[] = "Sender: ";
	static char From[] = "From: ";
	static char buff[BUFSIZ];

	while (fgets(buff, sizeof(buff), fp) && *buff != '\n') {
		if (!strncmp(buff, Approved, STRLEN(Approved)))
			strcpy(approved, buff + STRLEN(Approved));
		else if (!strncmp(buff, Sender, STRLEN(Sender)))
			strcpy(sender, buff + STRLEN(Sender));
		else if (!strncmp(buff, From, STRLEN(From)))
			strcpy(from, buff + STRLEN(From));
	}
}

/*
 *	And what to do about this???
 */
static void mail(char *who, char *msg, ...)
{
	FILE *fp;

	if (fp = fopen(ctlfile("errlog"), "a+")) {
		va_list args;

		fprintf(fp, "\nTo: %s\nSubject: %s error\n\n", who, progname);
		va_start(args, msg);
		vfprintf(fp, msg, args);
		va_end(args);
		fclose(fp);
	}
}
