/*
 * W-NEWS       A simple NEWS processing package.
 *		moderate.c - functions for checking moderated groups
 *
 * Version:     @(#)moderate.c     4.50    31-Jan-1994
 *
 * Authors:	Edvard Tuinder, <ed@ow.org>
 *		Miquel van Smoorenburg, <miquels@drinkel.ow.org>
 *              Copyright 1993 MicroWalt Corporation
 *		Copyright 1994,1995 Cistron Software C.V.
 *
 *              This program is free software; you can redistribute it and/or
 *              modify it under the terms of the GNU General Public License
 *              as published by the Free Software Foundation; either version
 *              2 of the License, or (at your option) any later version.
 */
#include "libwnews.h"


_PRO( int moderated, (char *group, FILE *fp));
_PRO( static int mail_art, (char *moderator, char *group, FILE *artfp));


/*
 * Check the Newsgroups line to see if there are any moderated or
 * non-posting groups in it. For a non-post group, give an error if
 * it's the only group, elsewise just print a warning message.
 * If moderated, mail the article to the moderator.
 * In both cases, delete the newsgroup from the Newsgroups line.
 */
int
check_group(group, artname, approved)
char *group;
char *artname;
char *approved;
{
  ACT *a;
  char *sp, *start;
  char buff[256];
  int f;
  int status = OK;
  FILE *fp;

  if ((fp = fopen(artname, "r")) == (FILE *)NULL) {
	logmsg(LOGERR, "cannot re-open %s\n", artname);
	return(ERROR);
  }

  /* Loop over the Newsgroups: field */
  sp = group;
  while(*sp) {
	/* Gather newsgroup name. */
	f = 0;
	start = sp;
	while(*sp && *sp != ' ' && *sp != '\t' && *sp != ',')
		buff[f++] = *sp++;
	buff[f] = 0;

	/* Skip to begin of next newsgroup name */
	while(*sp && strchr(" \t,", *sp)) sp++;

	/* Get info about this group. */
	a = get_group(buff, NO_LOCK);
	if (a == (ACT *)NULL) continue; 	/* Will get into JUNK later */
	if (a->type == 'y') continue;		/* Posting allowed. */
	if (a->type == 'm' && approved)
		continue;			/* Moderated but approved. */

	/* Delete this group! */
	strcpy(start, sp);
	sp = start;

	/* Error? */
	if (a->type == 'n') {
		logmsg(LOGERR, "%s: posting to this group not allowed.\n", buff);
		status = ERROR;
		continue;
	}

	/* Try to post to the moderator. */
	if (moderated(buff, fp) == ERROR) status = ERROR;
  }

  /* If newsgroups left, return OK anyway. */
  (void) fclose(fp);
  if (group[0]) return(OK);
  return(status);  
}


/* Mail an article to some moderator. */
static int
mail_art(moderator, group, artfp)
char *moderator;
char *group;
FILE *artfp;
{
  FILE *fp;
  char cmd[256];
  int c, err;
 
  /* Log the fact. */
  logmsg(LOGINFO, "local	ng %s mailed to moderator %s\n",
	group, moderator);
	
  /* Build the command line. */
  sprintf(cmd, "%s %s", RMAIL, moderator);

  if ((fp = wpopen(cmd, "w")) == (FILE *)NULL) {
	logmsg(LOGSYS|LOGERR, "mail to moderator failed\n");
	return(ERROR);
  }

  /* Now pipe the rest of the file into the command. */
  fprintf(fp, "To: %s\n", moderator);
  while((c = fgetc(artfp)) != EOF) fputc(c, fp);

  /* Close pipe. */
  if ((err = wpclose(fp)) != 0) {
	logmsg(LOGSYS|LOGERR, "mail to moderator failed with status %d\n",
		err);
  }
  return(err);
}


/* Scan the "moderators" file and mail article to moderator. */
int
moderated(group, artfp)
char *group;
FILE *artfp;
{
  FILE *fp;
  char buff[256];
  char temp[256];
  char moderator[256];
  char *mod, *pat;
  char *p;

  /* We need to scan the moderators file.. */
  sprintf (buff, "%s/%s", CONFDIR, MODERATORS);
  if ((fp = fopen(buff, "r")) == (FILE *)NULL) {
	logmsg(LOGERR, "can't open moderators file `%s'\n", buff);
	return(ERROR);
  }

  /* Read line-by-line. */
  while(fgets(buff, 256, fp) != (char *)NULL) {

	/* Skip comments and empty lines. */
	for(p = buff; *p == ' ' || *p == '\t'; p++)
		;
	if (*p == '#' || *p == '\n' || *p == 0) continue;

	/* Split the line at the ':' */
	pat = strtok(buff, ":");
	mod = strtok(NULL, "\n");
	if (mod == (char *)NULL || pat == (char *)NULL) {
		logmsg(LOGERR, "moderators file format error\n");
		continue;
	}

	/* Delete white space at the end of the pattern. */
	for(p = pat; *p && *p != ' ' && *p != '\t'; p++)
		;
	*p = 0;

	/* Delete white space at the beginning of moderator. */
	while(*mod == ' ' || *mod == '\t') mod++;

	/* Now try to match this entry. */
	if (! mod_match(group, pat)) continue;
	
	/* Mail article to moderator. */
	strcpy(temp, group);
	for(p = temp; *p; p++) if (*p == '.') *p = '-';
	sprintf(moderator, mod, temp);
	(void) fclose(fp);
	return(mail_art(moderator, group, artfp));
  }
  (void) fclose(fp);
  logmsg(LOGERR, "no moderator for %s\n", group);
  return(ERROR);
}

