#include "listserv.h"

static char rcsid[] = "$Header: /usr/local/src/mail/listserv/RCS/faq.c,v 1.1 92/09/14 11:03:46 andy Exp Locker: andy $";

extern FILE *mailer;

sendfaq(from, command, outsider)
char *from, *command;
int outsider;
	{
	int i;
	char grp[256], tmp[512];

	printf("called sendfaq with %s %s %s\n", from, command, outsider);
	i = sscanf(command, "%s%s", tmp, grp);
	if(i != 2)
		{
		callmailer("", from, command);
		sprintf(tmp, "%s/FAQHELP", SERVDIR);
		mailcat(tmp, "\t");
		fflush(mailer);
		pclose(mailer);
		return;
		}
	
	sprintf(tmp, "%s/%s.pub", SERVDIR, grp);
	callmailer("", from, command);
	if(access(tmp,R_OK) == 0)
		{
		sprintf(tmp, "%s/%s.faq", SERVDIR, grp);
		if(access(tmp, R_OK) == 0)
			{
			mailcat(tmp, "\t");
			fflush(mailer);
			pclose(mailer);
			return;
			}
		else
			{
			fprintf(mailer, "There is no FAQ available for");
			fprintf(mailer, " mailing list \"%s\".\n", grp);
			fflush(mailer);
			pclose(mailer);
			return;
			}
		}
	else if(outsider)
		{
		fprintf(mailer,"The mailing list \"%s\" could not be found.\n",
			grp);
		fprintf(mailer,"You may use the INDEX command to get a listing\n");
		fprintf(mailer,"of available mailing lists.\n");
		fflush(mailer);
		pclose(mailer);
		return;
		}
	else	{
		sprintf(tmp, "%s/%s.faq", SERVDIR, grp);
		if(access(tmp, R_OK) == 0)
			{
			mailcat(tmp, "\t");
			fflush(mailer);
			pclose(mailer);
			return;
			}
		else
			{
			fprintf(mailer, "There is no FAQ available for");
			fprintf(mailer, " mailing list \"%s\".\n", grp);
			fflush(mailer);
			pclose(mailer);
			return;
			}
		}

	}
		

faqindex(from,request)
char *from,*request;
	{
	FILE *ls;
	char tmp[128];
	char buf[128];
	int i;
	printf("called faqindex with %s %s\n", from,request);

	sprintf(tmp,"cd %s; ls *.faq | sed -e 's/.faq//", SERVDIR);
	ls = popen(tmp,"r");
	if (ls == NULL)
		{
		perror(tmp);
		exit(1);
		}
	callmailer("", from, request);
	fprintf(mailer,"Index of Frequently Asked Questions\n");

	/* read the result of the ls */
	while (fgets(tmp, sizeof(tmp), ls))
		{
		while (tmp[(i=strlen(tmp)-1)] == '\n')
			tmp[i] = '\0';
		fprintf(mailer, "%s\n", tmp);
		}
	pclose(ls);
	pclose(mailer);
	return;
	}

	
		
		
