/*
 *  econv by Davide Libenzi (XMail spool to email format converter)
 *  Copyright (C) 1999..2008  Davide Libenzi
 *
 *  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.
 *
 *  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; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Davide Libenzi <davidel@xmailserver.org>
 *
 */

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


#define MAIL_DATA_TAG	"<<MAIL-DATA>>"
#define MAIL_FROM	"MAIL FROM:"

#ifdef _MSC_VER
#define strncasecmp	strnicmp
#define strcasecmp	stricmp
#endif	/* #ifdef _MSC_VER */


char *extract_address(char const *str, char *addr, int asize) {
	int alen;
	char const *pat, *base, *end;

	if (!(pat = strchr(str, '@')))
		return NULL;
	for (base = pat - 1; base >= str && !strchr(":,;<> \t\r\n\"'", *base); base--);
	base++;
	for (end = pat + 1; *end != '\0' && !strchr(":,;<> \t\r\n\"'", *end); end++);
	alen = (int) (end - base);
	if (alen > asize)
		alen = asize - 1;
	strncpy(addr, base, alen);
	addr[alen] = '\0';
	return addr;
}


int main(int argc, char * argv[]) {
	int i, rcode = 1, uxmode = 0, mbox = 0;
	time_t tcur = time(NULL);
	FILE *pfin = stdin, *pfout = stdout;
	char *fnin = NULL, *fnout = NULL;
	char buffer[2048], from[256] = "", astime[128];

	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], "--input") == 0) {
			if (++i < argc)
				fnin = argv[i];
			continue;
		} else if (strcmp(argv[i], "--output") == 0) {
			if (++i < argc)
				fnout = argv[i];
			continue;
		} else if (strcmp(argv[i], "--unix") == 0) {
			++uxmode;
			continue;
		} else if (strcmp(argv[i], "--mbox") == 0) {
			++mbox;
			continue;
		}
	}
	if (fnin && !(pfin = fopen(fnin, "rb")))
		pfin = stdin;
	if (fnout && !(pfout = fopen(fnout, "wb")))
		pfout = stdout;
	while (fgets(buffer, sizeof(buffer) - 1, pfin) &&
	       strncmp(buffer, MAIL_DATA_TAG, sizeof(MAIL_DATA_TAG) - 1))
		if (mbox && !strncasecmp(buffer, MAIL_FROM, sizeof(MAIL_FROM) - 1))
			extract_address(buffer, from, sizeof(from) - 1);
	if (feof(pfin))
		goto clean_exit;
	if (mbox) {
		rcode = 2;
		if (!strlen(from))
			goto clean_exit;
		strcpy(astime, ctime(&tcur));
		astime[strlen(astime) - 1] = '\0';
		if (!uxmode)
			fprintf(pfout, "From %s %s\r\n", from, astime);
		else
			fprintf(pfout, "From %s %s\n", from, astime);
	}
	rcode = 3;
	if (!uxmode) {
		do {
			unsigned int rsize = fread(buffer, 1, sizeof(buffer), pfin);

			if (rsize && fwrite(buffer, 1, rsize, pfout) != rsize)
				goto clean_exit;
		} while (!feof(pfin));
	} else {
		while (fgets(buffer, sizeof(buffer) - 1, pfin)) {
			if ((i = strlen(buffer)) >= 2 && buffer[i - 1] == '\n' &&
			    buffer[i - 2] == '\r')
				buffer[--i - 1] = '\n';
			if (i && fwrite(buffer, 1, i, pfout) != i)
				goto clean_exit;
		}
	}
	fflush(pfout);
	rcode = 0;
clean_exit:
	if (pfin != stdin) fclose(pfin);
	if (pfout != stdout) fclose(pfout);
	return rcode;
}

