changeset 11:8e1ebc68ee62

do_all_the_voodo_here introducton plus a massive cleanup
author mmj
date Thu, 22 Apr 2004 22:10:37 +1000
parents c9df40f76802
children dd4349f8129d
files src/Makefile.am src/Makefile.in src/do_all_the_voodo_here.c src/find_email_adr.c src/gethdrline.c src/mlmmj-process.c src/mlmmj-recieve.c src/mygetline.c
diffstat 8 files changed, 288 insertions(+), 135 deletions(-) [+]
line wrap: on
line diff
--- a/src/Makefile.am	Thu Apr 22 22:10:12 2004 +1000
+++ b/src/Makefile.am	Thu Apr 22 22:10:37 2004 +1000
@@ -19,13 +19,14 @@
 
 mlmmj_recieve_SOURCES = mlmmj-recieve.c writen.c strip_file_to_fd.c \
                         random-int.c header_token.c getlistaddr.c chomp.c \
-			strgen.c print-version.c
+			strgen.c print-version.c mygetline.c
 
 mlmmj_process_SOURCES = mlmmj-process.c writen.c strip_file_to_fd.c \
                         find_email_adr.c incindexfile.c itoa.c \
 			getlistaddr.c chomp.c mylocking.c listcontrol.c \
 			random-int.c strgen.c header_token.c print-version.c \
-			send_help.c
+			send_help.c do_all_the_voodo_here.c mygetline.c \
+			gethdrline.c
 
 mlmmj_subscribe_SOURCES = mlmmj-subscribe.c writen.c mylocking.c \
 			getlistaddr.c chomp.c random-int.c strgen.c \
--- a/src/Makefile.in	Thu Apr 22 22:10:12 2004 +1000
+++ b/src/Makefile.in	Thu Apr 22 22:10:37 2004 +1000
@@ -59,13 +59,15 @@
 	incindexfile.$(OBJEXT) itoa.$(OBJEXT) getlistaddr.$(OBJEXT) \
 	chomp.$(OBJEXT) mylocking.$(OBJEXT) listcontrol.$(OBJEXT) \
 	random-int.$(OBJEXT) strgen.$(OBJEXT) header_token.$(OBJEXT) \
-	print-version.$(OBJEXT) send_help.$(OBJEXT)
+	print-version.$(OBJEXT) send_help.$(OBJEXT) \
+	do_all_the_voodo_here.$(OBJEXT) mygetline.$(OBJEXT) \
+	gethdrline.$(OBJEXT)
 mlmmj_process_OBJECTS = $(am_mlmmj_process_OBJECTS)
 mlmmj_process_LDADD = $(LDADD)
 am_mlmmj_recieve_OBJECTS = mlmmj-recieve.$(OBJEXT) writen.$(OBJEXT) \
 	strip_file_to_fd.$(OBJEXT) random-int.$(OBJEXT) \
 	header_token.$(OBJEXT) getlistaddr.$(OBJEXT) chomp.$(OBJEXT) \
-	strgen.$(OBJEXT) print-version.$(OBJEXT)
+	strgen.$(OBJEXT) print-version.$(OBJEXT) mygetline.$(OBJEXT)
 mlmmj_recieve_OBJECTS = $(am_mlmmj_recieve_OBJECTS)
 mlmmj_recieve_LDADD = $(LDADD)
 am_mlmmj_send_OBJECTS = mlmmj-send.$(OBJEXT) writen.$(OBJEXT) \
@@ -96,7 +98,10 @@
 depcomp = $(SHELL) $(top_srcdir)/depcomp
 am__depfiles_maybe = depfiles
 @AMDEP_TRUE@DEP_FILES = ./$(DEPDIR)/checkwait_smtpreply.Po \
-@AMDEP_TRUE@	./$(DEPDIR)/chomp.Po ./$(DEPDIR)/find_email_adr.Po \
+@AMDEP_TRUE@	./$(DEPDIR)/chomp.Po \
+@AMDEP_TRUE@	./$(DEPDIR)/do_all_the_voodo_here.Po \
+@AMDEP_TRUE@	./$(DEPDIR)/find_email_adr.Po \
+@AMDEP_TRUE@	./$(DEPDIR)/gethdrline.Po \
 @AMDEP_TRUE@	./$(DEPDIR)/getlistaddr.Po \
 @AMDEP_TRUE@	./$(DEPDIR)/header_token.Po \
 @AMDEP_TRUE@	./$(DEPDIR)/incindexfile.Po \
@@ -108,7 +113,7 @@
 @AMDEP_TRUE@	./$(DEPDIR)/mlmmj-send.Po \
 @AMDEP_TRUE@	./$(DEPDIR)/mlmmj-subscribe.Po \
 @AMDEP_TRUE@	./$(DEPDIR)/mlmmj-unsubscribe.Po \
-@AMDEP_TRUE@	./$(DEPDIR)/mylocking.Po \
+@AMDEP_TRUE@	./$(DEPDIR)/mygetline.Po ./$(DEPDIR)/mylocking.Po \
 @AMDEP_TRUE@	./$(DEPDIR)/print-version.Po \
 @AMDEP_TRUE@	./$(DEPDIR)/random-int.Po ./$(DEPDIR)/readln.Po \
 @AMDEP_TRUE@	./$(DEPDIR)/send_help.Po ./$(DEPDIR)/strgen.Po \
@@ -208,13 +213,14 @@
 
 mlmmj_recieve_SOURCES = mlmmj-recieve.c writen.c strip_file_to_fd.c \
                         random-int.c header_token.c getlistaddr.c chomp.c \
-			strgen.c print-version.c
+			strgen.c print-version.c mygetline.c
 
 mlmmj_process_SOURCES = mlmmj-process.c writen.c strip_file_to_fd.c \
                         find_email_adr.c incindexfile.c itoa.c \
 			getlistaddr.c chomp.c mylocking.c listcontrol.c \
 			random-int.c strgen.c header_token.c print-version.c \
-			send_help.c
+			send_help.c do_all_the_voodo_here.c mygetline.c \
+			gethdrline.c
 
 mlmmj_subscribe_SOURCES = mlmmj-subscribe.c writen.c mylocking.c \
 			getlistaddr.c chomp.c random-int.c strgen.c \
@@ -323,7 +329,9 @@
 
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/checkwait_smtpreply.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/chomp.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/do_all_the_voodo_here.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/find_email_adr.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gethdrline.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/getlistaddr.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/header_token.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/incindexfile.Po@am__quote@
@@ -336,6 +344,7 @@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mlmmj-send.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mlmmj-subscribe.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mlmmj-unsubscribe.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mygetline.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mylocking.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/print-version.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/random-int.Po@am__quote@
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/do_all_the_voodo_here.c	Thu Apr 22 22:10:37 2004 +1000
@@ -0,0 +1,124 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "mlmmj.h"
+#include "mygetline.h"
+#include "gethdrline.h"
+#include "strgen.h"
+#include "chomp.h"
+#include "do_all_the_voodo_here.h"
+
+int findit(const char *line, const char **headers)
+{
+	int i = 0;
+	size_t len;
+
+	while(headers[i]) {
+		len = strlen(headers[i]);
+		if(strncmp(line, headers[i], len) == 0)
+			return 1;
+		i++;
+	}
+
+	return 0;
+}
+
+void getinfo(const char *line, struct mailhdr *readhdrs)
+{
+	int i = 0;
+	size_t tokenlen, linelen, valuelen;
+
+	while(readhdrs[i].token) {
+		tokenlen = strlen(readhdrs[i].token);
+		linelen = strlen(line);
+		if(strncmp(line, readhdrs[i].token, tokenlen) == 0) {
+			valuelen = linelen - tokenlen + 1;
+			if(!readhdrs[i].value) {
+				readhdrs[i].value = malloc(valuelen + 1);
+				memcpy(readhdrs[i].value, line+tokenlen,
+						valuelen);
+			}
+			chomp(readhdrs[i].value);
+		}
+		i++;
+	}
+}
+
+void do_all_the_voodo_here(FILE *in, FILE *out, FILE *hdradd, FILE *footers,
+		 const char **delhdrs, struct mailhdr *readhdrs)
+{
+	char *hdrline, *line;
+
+	while((hdrline = gethdrline(in))) {
+		/* Done with headers? Then add extra if wanted*/
+		if((strlen(hdrline) == 1) && (hdrline[0] == '\n')){
+			if(hdradd) {
+				fflush(out);
+				while((line = mygetline(hdradd))) {
+					fputs(line, out);
+					free(line);
+				}
+				fflush(out);
+			}
+			fputs(hdrline, out);
+			fflush(out);
+			free(hdrline);
+			break;
+		}
+		/* Do we want info from hdrs? Get it before it's gone */
+		if(readhdrs)
+			getinfo(hdrline, readhdrs);
+		
+		/* Should it be stripped? */
+		if(delhdrs)
+			if(!findit(hdrline, delhdrs))
+				fputs(hdrline, out);
+
+		free(hdrline);
+	}
+
+	/* Just print the rest of the mail */
+	while((line = mygetline(in))) {
+		fputs(line, out);
+		free(line);
+	}
+
+	fflush(out);
+
+	/* No more, lets add the footer if one*/
+	if(footers) {
+		while((line = mygetline(footers))) {
+			fputs(line, out);
+			free(line);
+		}
+	}
+
+	fflush(out);
+}
+#if 0
+int main(int argc, char **argv)
+{
+	int i = 0;
+	FILE *hdrfile = fopen(argv[1], "r");
+	FILE *footfile = fopen(argv[2], "r");
+	const char *badhdrs[] = {"From ", "Received:", NULL};
+	struct mailhdr readhdrs[] = {
+		{"MIME-Version: ", NULL},
+		{"Date: ", NULL},
+		{ NULL , NULL }
+	};
+	
+	do_all_the_voodo_here(stdin, stdout, hdrfile, footfile, badhdrs,
+ 				readhdrs);
+
+	while(readhdrs[i].token) {
+		printf(	"readhdrs[%d].token = [%s]\n"
+			"readhdrs[%d].value = [%s]\n", i, readhdrs[i].token,
+						     i, readhdrs[i].value);
+		i++;
+	}
+
+	return 0;
+}
+#endif
--- a/src/find_email_adr.c	Thu Apr 22 22:10:12 2004 +1000
+++ b/src/find_email_adr.c	Thu Apr 22 22:10:37 2004 +1000
@@ -17,16 +17,14 @@
 {
 	size_t len;
 	char *index_atsign;
-	char *tempstr;
+	char *tempstr = strdup(str);
 	char *c, *first_char = 0, *last_char = 0;
 	
-	tempstr = malloc(strlen(str) + 1);
-	snprintf(tempstr, strlen(str) + 1, "%s", str);
-
 	retstruct->emailcount = 0;
 	retstruct->emaillist = NULL;
 	
-	while((index_atsign = index(tempstr, '@'))) {
+	index_atsign = strchr(tempstr, '@');
+	while(index_atsign) {
 		c = index_atsign;
 		retstruct->emailcount++;
 		while(*c != '<' && *c != ' ' && *c != ',' && *c != ';'
@@ -41,15 +39,14 @@
 				&& *c != '}' && *c != 0) {
 			c++;
 		}
-		*c = 0;
 		last_char = --c;
 
 		len = last_char - first_char + 2;
 		
-		retstruct->emaillist = realloc(retstruct->emaillist,
+		retstruct->emaillist = (char **)realloc(retstruct->emaillist,
 				sizeof(char **) * retstruct->emailcount);
 		retstruct->emaillist[retstruct->emailcount-1] =
-			malloc(len);
+				(char *)malloc(len + 1);
 		snprintf(retstruct->emaillist[retstruct->emailcount-1], len,
 			 "%s", first_char);
 #if 0
@@ -57,6 +54,7 @@
 				retstruct->emaillist[retstruct->emailcount-1]);
 #endif
 		*index_atsign = 'A'; /* Clear it so we don't find it again */
+		index_atsign = strchr(tempstr, '@');
 	}
 	free(tempstr);
 	return retstruct;
--- a/src/gethdrline.c	Thu Apr 22 22:10:12 2004 +1000
+++ b/src/gethdrline.c	Thu Apr 22 22:10:37 2004 +1000
@@ -3,7 +3,7 @@
 
 #include "mygetline.h"
 #include "gethdrline.h"
-#include "stringfuncs.h"
+#include "strgen.h"
 
 char *gethdrline(FILE *infile)
 {
@@ -14,16 +14,17 @@
 		line = mygetline(infile);
 		if(line == NULL)
 			return NULL;
-		ch = getc(infile);
-		if(ch == '\t') {
-			ungetc(ch, infile);
+		ch = getc(infile); ungetc(ch, infile);
+		if(ch == '\t' || ch == ' ') {
 			nextline = mygetline(infile);
 			tmp = retstr;
 			retstr = concatstr(3, retstr, line, nextline);
 			free(tmp); free(line); free(nextline);
 			tmp = line = nextline = NULL;
+			ch = getc(infile); ungetc(ch, infile);
+			if(!(ch == '\t' || ch == ' '))
+				return retstr;
 		} else {
-			ungetc(ch, infile);
 			tmp = retstr;
 			retstr = concatstr(3, retstr, line, nextline);
 			free(tmp);
--- a/src/mlmmj-process.c	Thu Apr 22 22:10:12 2004 +1000
+++ b/src/mlmmj-process.c	Thu Apr 22 22:10:37 2004 +1000
@@ -15,48 +15,36 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 
-#include "mlmmj-process.h"
 #include "mlmmj.h"
 #include "wrappers.h"
-#include "strip_file_to_fd.h"
-#include "header_token.h"
 #include "find_email_adr.h"
 #include "incindexfile.h"
 #include "getlistaddr.h"
 #include "listcontrol.h"
 #include "strgen.h"
-
-
-void free_str_array(char **to_free)
-{
-	int i = 0;
-
-	while(to_free[i])
-		free(to_free[i++]);
-	free(to_free);
-}
+#include "do_all_the_voodo_here.h"
 
 static void print_help(const char *prg)
 {
-	        printf("Usage: %s -L /path/to/chat-list\n"
+	        printf("Usage: %s [-P] -L /path/to/chat-list\n"
 		       "          -m mailfile\n", prg);
 		exit(EXIT_SUCCESS);
 }
 
-
 int main(int argc, char **argv)
 {
-	char *donemailname = 0;
-	const char *badheaders[] = {"From ", "Return-Path:", 0};
-	int donemailfd, opt;
-	char *listdir = 0;
-	char listadr[READ_BUFSIZE];
-	char tovalue[READ_BUFSIZE];
-	char *mailfile = 0;
-	char *headerfilename = 0;
-	char *footerfilename = 0;
-	FILE *headerfile, *footerfile, *rawmailfile;
-	struct email_container toemails;
+	int fd, opt, i, noprocess = 0;
+	char *listdir = NULL, *mailfile = NULL, *headerfilename = NULL;
+	char *footerfilename = NULL, *donemailname = NULL;
+	char *randomstr = random_str();
+	FILE *headerfile, *footerfile, *rawmailfile, *donemailfile;
+	struct email_container toemails = { 0, NULL };
+	const char *badheaders[] = { "From ", "Return-Path:", NULL };
+	struct mailhdr readhdrs[] = {
+		{ "To:", NULL },
+		{ "Cc:", NULL },
+		{ NULL, NULL }
+	};
 	
 	while ((opt = getopt(argc, argv, "hVm:L:")) != -1) {
 		switch(opt) {
@@ -69,37 +57,51 @@
 		case 'h':
 			print_help(argv[0]);
 			break;
+		case 'P':
+			noprocess = 1;
+			break;
 		case 'V':
 			print_version(argv[0]);
 			exit(0);
 		}
 	}
-	if(listdir == 0 || mailfile == 0) {
+	if(listdir == NULL || mailfile == NULL) {
 		fprintf(stderr, "You have to specify -L and -m\n");
 		fprintf(stderr, "%s -h for help\n", argv[0]);
 		exit(EXIT_FAILURE);
 	}
-	/* get the list address */
-	getlistaddr(listadr, listdir);
 
-	donemailname = concatstr(3, listdir, "/queue/", random_str());
-	donemailfd = open(donemailname, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
-	while(donemailfd == -1 && errno == EEXIST) {
-		donemailname = concatstr(3, listdir, "/queue/", random_str());
-		donemailfd = open(donemailname, O_RDWR|O_CREAT|O_EXCL,
-				  S_IRUSR|S_IWUSR);
+	donemailname = concatstr(3, listdir, "/queue/", randomstr);
+	free(randomstr);
+	fd = open(donemailname, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
+	while(fd == -1 && errno == EEXIST) {
+		free(donemailname);
+		randomstr = random_str();
+		donemailname = concatstr(3, listdir, "/queue/", randomstr);
+		free(randomstr);
+		fd = open(donemailname, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
 	}
 	
-	if(donemailfd == -1) {
+	if(fd == -1) {
 		free(donemailname);
-		perror("Cannot open queuemailfile");
+		fprintf(stderr, "%s:%d could not get fd in %s: ",
+                                __FILE__, __LINE__, donemailname);
 		exit(EXIT_FAILURE);
 	}
 
-	printf("%s\n", donemailname);
+	if((donemailfile = fdopen(fd, "w")) == NULL) {
+		free(donemailname);
+		fprintf(stderr, "%s:%d could not fdopen",
+				__FILE__, __LINE__);
+		exit(EXIT_FAILURE);
+	}
+
+	printf("[%s]\n", donemailname);
 
 	if((rawmailfile = fopen(mailfile, "r")) == NULL) {
-		perror("Cannot open mailfile");
+		free(donemailname);
+		fprintf(stderr, "%s:%d could not open %s: ",
+                                __FILE__, __LINE__, mailfile);
 		exit(EXIT_FAILURE);
 	}
 
@@ -111,26 +113,52 @@
 	footerfile = fopen(footerfilename, "r");
 	free(footerfilename);
 	
-	strip_file_to_fd(rawmailfile, donemailfd, badheaders, headerfile,
-			 footerfile, tovalue);
-	close(donemailfd);
+	do_all_the_voodo_here(rawmailfile, donemailfile, headerfile,
+				footerfile, badheaders, readhdrs);
 
 	fclose(rawmailfile);
-	unlink(mailfile);
+	/*XXX: unlink(mailfile);*/
+	close(fd);
+	fclose(donemailfile);
 
-	if(tovalue) {
-		find_email_adr(tovalue, &toemails);
-		if(index(toemails.emaillist[0], RECIPDELIM))
-			listcontrol(donemailname, listdir, toemails.emaillist[0]);
-	}
-	close(donemailfd);
 	if(headerfile)
 		fclose(headerfile);
+	if(footerfile)
+		fclose(footerfile);
+
+	if(readhdrs[0].value) {
+		find_email_adr(readhdrs[0].value, &toemails);
+#if 0
+		for(i = 0; i < toemails.emailcount; i++)
+			printf("toemails.emaillist[%d] = %s\n", i,
+					toemails.emaillist[i]);
+	}
+	if(readhdrs[1].value) {
+		find_email_adr(readhdrs[1].value, &ccemails);
+		for(i = 0; i < ccemails.emailcount; i++)
+			printf("ccemails.emaillist[%d] = %s\n", i,
+					ccemails.emaillist[i]);
+#endif
+	}
+
+	if(strchr(toemails.emaillist[0], RECIPDELIM)) {
+		printf("listcontrol(%s, %s, %s)\n", donemailname, listdir,
+						toemails.emaillist[0]);
+		listcontrol(donemailname, listdir, toemails.emaillist[0]);
+		return EXIT_SUCCESS;
+	}
+
+	if(noprocess) {
+		free(donemailname);
+		/* XXX: toemails and ccemails etc. have to be free() */
+		exit(EXIT_SUCCESS);
+	}
 	
 	execlp(BINDIR"mlmmj-send", "mlmmj-send",
 				"-L", listdir,
 				"-m", donemailname, 0);
-	fprintf(stderr, "%s:%d execlp() of "BINDIR"mlmmj-send failed: ", __FILE__, __LINE__);
+	fprintf(stderr, "%s:%d execlp() of mlmmj-send failed: ",
+			__FILE__, __LINE__);
 	perror(NULL);
 	return EXIT_FAILURE;
 }
--- a/src/mlmmj-recieve.c	Thu Apr 22 22:10:12 2004 +1000
+++ b/src/mlmmj-recieve.c	Thu Apr 22 22:10:37 2004 +1000
@@ -16,38 +16,25 @@
 #include <fcntl.h>
 
 #include "mlmmj.h"
-#include "mlmmj-recieve.h"
 #include "wrappers.h"
-#include "strip_file_to_fd.h"
-#include "header_token.h"
-#include "getlistaddr.h"
+#include "mygetline.h"
 #include "strgen.h"
 
-void free_str_array(char **to_free)
-{
-	int i = 0;
-
-	while(to_free[i])
-		free(to_free[i++]);
-	free(to_free);
-}
+extern char *optarg;
 
 static void print_help(const char *prg)
 {
-	        printf("Usage: %s -L /path/to/chat-list\n", prg);
+        printf("Usage: %s -L /path/to/chat-list [-V] [-P]\n", prg);
 		exit(EXIT_SUCCESS);
 }
 
-
 int main(int argc, char **argv)
 {
-	char *infilename;
-	int mailfd;
-	int opt, ch, process = 1;
-	char *listdir = 0;
-	char listadr[READ_BUFSIZE];
+	char *infilename = NULL, *listdir = NULL, *line = NULL;
+	char *randomstr = random_str();
+	int fd, opt, noprocess = 0;
 	
-	while ((opt = getopt(argc, argv, "hVPL:")) != -1) {
+	while ((opt = getopt(argc, argv, "hPVL:")) != -1) {
 		switch(opt) {
 		case 'h':
 			print_help(argv[0]);
@@ -56,52 +43,59 @@
 			listdir = optarg;
 			break;
 		case 'P':
-			process = 0;
+			noprocess = 1;
 			break;
 		case 'V':
 			print_version(argv[0]);
 			exit(0);
 		}
 	}
-	if(listdir == 0) {
+	if(listdir == NULL) {
 		fprintf(stderr, "You have to specify -L\n");
 		fprintf(stderr, "%s -h for help\n", argv[0]);
 		exit(EXIT_FAILURE);
 	}
-	/* get the list address */
-	getlistaddr(listadr, listdir);
 	
-	infilename = concatstr(3, listdir, "/incoming/", random_str());
-	mailfd = open(infilename, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
-	while(mailfd == -1 && errno == EEXIST) {
+	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);
-		infilename = concatstr(3, listdir, "/incoming/", random_str());
-		mailfd = open(infilename, O_RDWR|O_CREAT|O_EXCL,
-				S_IRUSR|S_IWUSR);
+		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(mailfd == -1) {
+	if(fd < 0) {
+		fprintf(stderr, "%s:%d could not get fd in %s: ",
+				__FILE__, __LINE__, infilename);
 		free(infilename);
-		perror(infilename);
 		exit(EXIT_FAILURE);
 	}
 
-	printf("%s\n", infilename);
+	while((line = mygetline(stdin))) {
+		writen(fd, line, strlen(line));
+		fsync(fd);
+		free(line);
+	}
 	
-	while((ch = getc(stdin)) != EOF)
-		writen(mailfd, &ch, 1);
+	printf("mlmmj-recieve: wrote %s\n", infilename);
 
-	close(mailfd);
+	close(fd);
 
-	if(!process)
-		return EXIT_SUCCESS;
+	if(noprocess) {
+		free(infilename);
+		exit(EXIT_SUCCESS);
+	}
 
-	execlp(BINDIR"mlmmj-process", "mlmmj-process",
+	execlp("mlmmj-process", "mlmmj-process",
 				"-L", listdir,
 				"-m", infilename, 0);
 
-	fprintf(stderr, "%s:%d execlp() of "BINDIR"mlmmj-send failed: ",
+	fprintf(stderr, "%s:%d execlp() of mlmmj-process failed: ",
 			__FILE__, __LINE__);
 	perror(NULL);
+
 	return EXIT_FAILURE;
 }
--- a/src/mygetline.c	Thu Apr 22 22:10:12 2004 +1000
+++ b/src/mygetline.c	Thu Apr 22 22:10:37 2004 +1000
@@ -1,35 +1,34 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-
 #include "mygetline.h"
 
 char *mygetline(FILE *infile)
 {
-	char *buf = malloc(BUFSIZE);
-	char *str = malloc(BUFSIZE);
-	size_t lenbuf, lenstr, i = 1;
+	size_t buf_size = BUFSIZE;  /* initial buffer size */
+	size_t buf_used;
+	char *buf = malloc(buf_size);
 	
-	buf[0] = str[0] = 0;
+	buf[0] = '\0';
 	for(;;) {
-		if(fgets(buf, BUFSIZE, infile) != NULL) {
-			if(i == 1) {
-				free(buf);
-				free(str);
-				return NULL;
+		buf_used = strlen(buf);
+		if (fgets(buf+buf_used, buf_size-buf_used, infile) == NULL) {
+			if (buf[0]) {
+				 return buf;
 			} else {
 				free(buf);
-				return str;
+				return NULL;
 			}
 		}
-		lenbuf = strlen(buf);
-		lenstr = strlen(str);
-		realloc(str, lenbuf + lenstr + 1);
-		strcat(str, buf);
-		if(!((lenbuf == BUFSIZE - 1) && (buf[BUFSIZE - 1] != 'n'))) {
-			free(buf);
-			return str;
+
+		if ((strlen(buf) < buf_size-1) || (buf[buf_size-1] == '\n')) {
+			return buf;
 		}
+
+		/* grow buffer */
+		buf_size *= 2;
+		buf = realloc(buf, buf_size);
+
 	}
 }
 #if 0
@@ -42,7 +41,6 @@
 		free(str);
 	}
 
-	free(str);
 	return 0;
 }
 #endif