#include "listserv.h"

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

extern FILE *msg;
extern FILE *mailer;

sendhelp(from,request)
char *from, *request;
	{
	printf("called sendhelp with %s %s\n", from, request);
	callmailer("", from, request);
	mailcat(HELPFILE,"");
	pclose(mailer);
	return;
	}

listhelp(from,grp,request,outsider)
char *from,*grp,*request;
int outsider;
	{
	FILE *commands;
	int i;
	char tmp[128], hlp[128], cmd[128];
	printf("called listhelp with %s %s %s\n", from,grp,request);
	
	callmailer("", from, request);
	strcpy(tmp, COMMANDFILE);
	commands = fopen(tmp, "r");
	if( commands == NULL)
		{
		perror(tmp);
		exit(1);
		}
	while(i = fscanf(commands, "%s %s",cmd, hlp) > 0)
		if (!strcasecmp(cmd, grp))
			{
 			sprintf(tmp, "%s/%s", SERVDIR, hlp);
			mailcat(tmp, "\t");
			fflush(mailer);
			pclose(mailer);
			fclose(commands);
			return;
			}
	fclose(commands);

	if (outsider)
		sprintf(tmp,"%s/%s.pub", SERVDIR, grp);
	else
		sprintf(tmp,"%s/%s.info", SERVDIR, grp);
	if (access(tmp,R_OK) != 0)
		{
		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;
		}

	fprintf(mailer,"Mailing list \"%s\":\n", grp);
	mailcat(tmp,"\t");
	pclose(mailer);
	return;
	}

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

	if (outsider)
		sprintf(tmp,"cd %s; ls *.pub | sed -e 's/.pub//", SERVDIR);
	else
		sprintf(tmp,"cd %s; ls *.info | sed -e 's/.info//", SERVDIR);
	ls = popen(tmp,"r");
	if (ls == NULL)
		{
		perror(tmp);
		exit(1);
		}
	callmailer("", from, request);
	fprintf(mailer,"Index of mailing lists\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);

		/* if he wanted the long listing, cat the .info file */
		if (l)
			{
			if (outsider)
				sprintf(buf, "%s/%s.pub", SERVDIR, tmp);
			else
				sprintf(buf, "%s/%s.info", SERVDIR, tmp);
			mailcat(buf,"\t");
			fprintf(mailer, "\n");
			}
		}
	pclose(ls);
	pclose(mailer);
	return;
	}

	
		
		
