view src/mlmmj-recieve.c @ 18:f54f95a4c53e

changed to use log_error() all over
author mortenp
date Thu, 22 Apr 2004 23:15:53 +1000
parents 8e1ebc68ee62
children 65d01831c0eb
line wrap: on
line source

/* Copyright (C) 2002, 2003 Mads Martin Joergensen <mmj at mmj.dk>
 *
 * $Id$
 *
 * This file is redistributable under version 2 of the GNU General
 * Public License as described at http://www.gnu.org/licenses/gpl.txt
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "mlmmj.h"
#include "wrappers.h"
#include "mygetline.h"
#include "strgen.h"

#include "log_error.c"

extern char *optarg;

static void print_help(const char *prg)
{
        printf("Usage: %s -L /path/to/chat-list [-V] [-P]\n", prg);
	exit(EXIT_SUCCESS);
}

int main(int argc, char **argv)
{
	char *infilename = NULL, *listdir = NULL, *line = NULL;
	char *randomstr = random_str();
	int fd, opt, noprocess = 0;
	
	while ((opt = getopt(argc, argv, "hPVL:")) != -1) {
		switch(opt) {
		case 'h':
			print_help(argv[0]);
			break;
		case 'L':
			listdir = optarg;
			break;
		case 'P':
			noprocess = 1;
			break;
		case 'V':
			print_version(argv[0]);
			exit(0);
		}
	}
	if(listdir == NULL) {
		fprintf(stderr, "You have to specify -L\n");
		fprintf(stderr, "%s -h for help\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	
	infilename = concatstr(3, listdir, "/incoming/", randomstr);
	free(randomstr);
	fd = open(infilename, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
	while(fd < 0 && errno == EEXIST) {
		free(infilename);
		randomstr = random_str();
		infilename = concatstr(3, listdir, "/incoming/", randomstr);
		free(randomstr);
		fd = open(infilename, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
	}

	if(fd < 0) {
		log_error("could not create mail file in incoming directory");
		free(infilename);
		exit(EXIT_FAILURE);
	}
	
	while((line = mygetline(stdin))) {
		writen(fd, line, strlen(line));
		fsync(fd);
		free(line);
	}

	printf("mlmmj-recieve: wrote %s\n", infilename);

	close(fd);

	if(noprocess) {
		free(infilename);
		exit(EXIT_SUCCESS);
	}

	execlp(BINDIR"mlmmj-process", "mlmmj-process",
				"-L", listdir,
				"-m", infilename, 0);
	log_error("execlp() of "BINDIR"mlmmj-process failed");

	return EXIT_FAILURE;
}