/*
 *  mboxsend.c by Davide Libenzi ( deliver mail to mbox files )
 *  Copyright (C) 1999,..,2002  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>
#include <sys/file.h>



#define STD_MBOXES_PATH "/var/spool/mail/"

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




static void usage(char const *prg) {

    fprintf(stderr, "use: %s [--input fnin] [--output fnout] [--from sndr]\n", prg);
}


int main(int argc, char * argv[]) {
	int i, line, rcode;
	time_t tcur = time(NULL);
	FILE *pfin = stdin, *pfout;
	char *fnin = NULL, *fnout = NULL, *from = NULL, *user;
	char buffer[2048], stdmbox[320], 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], "--from") == 0) {
			if (++i < argc)
				from = argv[i];
			continue;
		} else {
            usage(argv[0]);
            return 1;
        }
	}
    if (!fnout) {
        if (!(user = getenv("USER"))) {
            usage(argv[0]);
            return 2;
        }
        sprintf(stdmbox, "%s%s", STD_MBOXES_PATH, user);
        fnout = stdmbox;
        return 3;
    }
	if (fnin && !(pfin = fopen(fnin, "rb"))) {
        perror(fnin);
        return 4;
    }
	if (!(pfout = fopen(fnout, "a+b"))) {
        perror(fnout);
        if (pfin != stdin) fclose(pfin);
        return 5;
    }
    if (flock(fileno(pfout), LOCK_EX)) {
        perror(fnout);
        fclose(pfout);
        if (pfin != stdin) fclose(pfin);
        return 6;
    }
    rcode = 7;
    for (line = 0; fgets(buffer, sizeof(buffer) - 1, pfin); line++) {
        if (!line && strncasecmp(buffer, "From ", 5)) {
            if (!from) {
                fprintf(stderr, "Input not in mbox format. Please add \"--from sender\".\n");
                rcode = 8;
                goto err_exit;
            }
            strcpy(astime, ctime(&tcur));
            astime[strlen(astime) - 1] = '\0';
            fprintf(pfout, "From %s %s\n", from, astime);
        } else {
            if (!strncasecmp(buffer, "From ", 5))
                fputs(">", pfout);
            if (fputs(buffer, pfout) == EOF)
                goto err_exit;
        }
    }
    rcode = 0;
err_exit:
	fflush(pfout);
    flock(fileno(pfout), LOCK_UN);
    fclose(pfout);
    if (pfin != stdin) fclose(pfin);
    return rcode;
}

