/*
		rgsmtp - V1.0, Henning Peters, faroul@beyond.hb.north.de
		
		generated from:

				rsmtp.c - BSMTP frontend for sendmail v8
				Copyright (C) 1994 Erik Heinz <erik@jena.thur.de>
				Version: 07/09/94

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, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.	If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>

const char version[]="\0$VER: rcsmtp V1.0\0";

#define MAXLINLEN 2048

typedef struct userlist {
	char *name;
	char *host;
	struct userlist *next;
} userlist;

char *sending_host, *from_adress, rline[MAXLINLEN], *HOST,
		*out, *OUT, buf[64];
FILE *inp;
time_t Time;


char *get_smtp_arg(char *smtp_cmd) {		/* check for command an get arguments */
	char *c1, *c2; int i;

	while (*fgets(rline, MAXLINLEN, inp) == '\n') ;
	rline[strlen(rline)-1] = '\0';
	if (rline[strlen(rline)-1] == '\r')
		rline[strlen(rline)-1] = '\0';

	if ((c1=strstr(rline, smtp_cmd)) == NULL)
		return NULL; 
	else {
		c2 = strdup(c1 + strlen(smtp_cmd));
		i = strlen(c2)-1;
		if ((c2[0] == '<') && (c2[i] == '>')) c2++[i]='\0';
		return strdup(c2);
	}
}

void uncompress() {
	FILE *fout;
	char *temp;

	temp=strdup(tmpnam((char *)NULL));
	OUT=strdup(tmpnam((char *)NULL));

	if (!(fout=fopen(temp,"w")))
		return;

	while (!feof(stdin))
		fputc(getchar(),fout);
	
	fclose(fout);

	sprintf(buf,"gzip -fcd %s > %s", temp, OUT);
	system(buf);
	sprintf(buf,"delete %s", temp);
	system(buf);
}

int handle_one_mail() {
	FILE *rmail;
	userlist *ul, *UL;
	char *rcpt, *s;

	out=strdup(tmpnam((char *)NULL));
	if ((rmail=fopen(out,"w"))) {
		UL=NULL;
		while ((rcpt=get_smtp_arg("RCPT TO:")) != NULL) {
			ul=malloc(sizeof(userlist));
			if ((s=strchr(rcpt,'@'))) {
				ul->host=strdup(++s);
				s--[0]='\0';
				ul->name=strdup(rcpt);
			} else {
				ul->name=strdup(rcpt);
				ul->host=NULL;
			}
			ul->next=UL;
			UL=ul;
		}

		if (strstr(rline, "DATA") == NULL) {
			fclose(rmail);
			return -1;
		}

		Time=time(0);
		fprintf(rmail,"From %s %s remote from %s\n",
									from_adress, ctime(&Time), sending_host);

		while ((strcmp(fgets(rline, MAXLINLEN, inp), ".\n") != 0) 
						&& (!feof(inp))) {
			if (strcmp(rline, "..\n") == 0)
				strcpy(rline, ".\n");
			if (fputs(rline,rmail) == EOF) {
				fclose(rmail);
				return -1;
			}
		}
		fclose(rmail);
		for (ul=UL; ul; ul=ul->next) {	/* Send mail to any receipient */
			if (stricmp(ul->host, HOST) == 0) /* This is local mail */
				sprintf(rline, "rmail <%s %s", out, ul->name);
			else
				sprintf(rline, "rmail <%s %s@%s", out, ul->name, ul->host);
			system(rline);
		}
		sprintf(rline, "delete %s", out);
		system(rline);

		do {
			ul=UL->next;
			free(UL->name);
			if (UL->host)
				free(UL->host);
			free(UL);
			UL=ul;
		} while (UL);

		return 0;
	}
	return -1;
}


char *getenv(const char *name) {
FILE *ENV;
char env[50];

	sprintf(env,"ENV:%s",name);
	if ((ENV=fopen(env,"r"))) {
		(void) fgets(env, 200, ENV);
		fclose(ENV);
	} else return 0;

	if (strncmp("ENV:",env,4)==0) return 0;
	return strdup(env);
}

int main(int argc, char *argv[]) {
	uncompress();

	if ((inp=fopen(OUT,"r"))) {
		if ((sending_host = get_smtp_arg("HELO ")) == NULL) exit(10); 

		HOST=getenv("HOSTNAME");

		while ((from_adress = get_smtp_arg("MAIL FROM:")) != NULL) {
			if (handle_one_mail() < 0) exit(10);
		}
		fclose(inp);
		sprintf(rline, "delete %s", OUT);
		system(rline);
		return 0;
	}	
	sprintf(rline, "delete %s", OUT);
	system(rline);
	return 10;
}

