changeset 189:cb46caab2621

New function to prepare a standard mail in an easy way, and make send_help use it. More to follow :)
author mmj
date Mon, 07 Jun 2004 22:36:39 +1000
parents 2da457be244c
children c37f95babe75
files src/Makefile.am src/prepstdreply.c src/send_help.c
diffstat 3 files changed, 101 insertions(+), 73 deletions(-) [+]
line wrap: on
line diff
--- a/src/Makefile.am	Mon Jun 07 22:36:23 2004 +1000
+++ b/src/Makefile.am	Mon Jun 07 22:36:39 2004 +1000
@@ -23,21 +23,23 @@
 mlmmj_process_SOURCES = mlmmj-process.c writen.c find_email_adr.c \
 			incindexfile.c itoa.c getlistaddr.c chomp.c \
 			mylocking.c listcontrol.c random-int.c strgen.c \
-			print-version.c send_help.c \
+			print-version.c send_help.c prepstdreply.c \
 			do_all_the_voodo_here.c mygetline.c gethdrline.c \
 			log_error.c statctrl.c ctrlvalue.c dumpfd2fd.c
 
 mlmmj_sub_SOURCES = mlmmj-sub.c writen.c mylocking.c \
 			getlistaddr.c chomp.c random-int.c strgen.c \
 			subscriberfuncs.c print-version.c \
-			log_error.c mygetline.c
+			log_error.c mygetline.c prepstdreply.c
 
 mlmmj_unsub_SOURCES = mlmmj-unsub.c writen.c mylocking.c \
 			getlistaddr.c chomp.c subscriberfuncs.c random-int.c \
-			strgen.c print-version.c log_error.c mygetline.c
+			strgen.c print-version.c log_error.c mygetline.c \
+			prepstdreply.c
 
 mlmmj_bounce_SOURCES = mlmmj-bounce.c print-version.c log_error.c \
-		       subscriberfuncs.c strgen.c random-int.c writen.c
+		       subscriberfuncs.c strgen.c random-int.c writen.c \
+		       prepstdreply.c mygetline.c
 
 mlmmj_maintd_SOURCES = mlmmj-maintd.c print-version.c log_error.c mygetline.c \
 		       strgen.c random-int.c chomp.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/prepstdreply.c	Mon Jun 07 22:36:39 2004 +1000
@@ -0,0 +1,73 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+#include <errno.h>
+
+#include "prepstdreply.h"
+#include "strgen.h"
+#include "chomp.h"
+#include "log_error.h"
+#include "mygetline.h"
+#include "wrappers.h"
+
+char *prepstdreply(const char *listdir, const char *filename, const char *from,
+		   const char *to, const char *replyto, const char *subject,
+		   size_t tokencount, char **data)
+{
+	int infd, outfd;
+	size_t i;
+	char *str, *tmp, *retstr;
+
+	tmp = concatstr(3, listdir, "/text/", filename);
+	infd = open(tmp, O_RDONLY);
+	free(tmp);
+	if(infd < 0) {
+		log_error(LOG_ARGS, "Could not open std mail %s", filename);
+		return NULL;
+	}
+
+	tmp = concatstr(6, "From: ", from, "\nTo: ", to, "\nSubject: ", subject);
+	if(replyto)
+		str = concatstr(3, tmp, "\nReply-To: ", replyto, "\n\n");
+	else
+		str = concatstr(2, tmp, "\n\n");
+		
+	free(tmp);
+
+	tmp = random_str();
+	retstr = concatstr(3, listdir, "/queue/", random);
+	free(tmp);
+	outfd = open(retstr, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
+	if(outfd < 0) {
+		log_error(LOG_ARGS, "Could not open std mail %s", tmp);
+		return NULL;
+	}
+
+	if(writen(outfd, str, strlen(str)) < 0) {
+		log_error(LOG_ARGS, "Could not write std mail");
+		return NULL;
+	}
+	free(str);
+
+	while((str = mygetline(infd))) {
+		for(i = 0; i < tokencount; i++) {
+			if(strncmp(str, data[i*2], strlen(data[i*2])) == 0) {
+				free(str);
+				str = strdup(data[(i*2)+1]);
+			}
+		}
+		if(writen(outfd, str, strlen(str)) < 0) {
+			log_error(LOG_ARGS, "Could not write std mail");
+			return NULL;
+		}
+		free(str);
+	}
+	
+	fsync(outfd);
+	close(outfd);
+
+	return retstr;
+}	
--- a/src/send_help.c	Mon Jun 07 22:36:23 2004 +1000
+++ b/src/send_help.c	Mon Jun 07 22:36:39 2004 +1000
@@ -23,99 +23,52 @@
 #include "chomp.h"
 #include "wrappers.h"
 #include "mygetline.h"
+#include "prepstdreply.h"
 
 void send_help(const char *listdir, const char *emailaddr,
 	       const char *mlmmjsend)
 {
-	int helpfd, queuefd;
-	char *listaddr, *buf, *fromaddr;
-	char *helpfilename, *queuefilename, *listname;
-	char *randomstr, *listfqdn, *s1;
+	char *queuefilename, *listaddr, *listname, *listfqdn, *fromaddr;
+	char *fromstr, *subject;
+	char *maildata[] = { "*UNSUBADDR*", NULL, "*SUBADDR*", NULL,
+			     "*HLPADDR*", NULL };
 
         listaddr = getlistaddr(listdir);
 	chomp(listaddr);
 
-	helpfilename = concatstr(2, listdir, "/text/listhelp");
-
-	if((helpfd = open(helpfilename, O_RDONLY)) < 0) {
-		log_error(LOG_ARGS, "Could not open text/help");
-		free(helpfilename);
-		exit(EXIT_FAILURE);
-	}
-
-	free(helpfilename);
-
 	listname = genlistname(listaddr);
 	listfqdn = genlistfqdn(listaddr);
-	randomstr = random_str();
-
-	queuefilename = concatstr(3, listdir, "/queue/", randomstr);
-	
-	queuefd = open(queuefilename, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
-	if(queuefd < 0) {
-		log_error(LOG_ARGS, "Could not open '%s'", queuefilename);
-		free(queuefilename);
-		free(randomstr);
-		exit(EXIT_FAILURE);
-	}
-	free(randomstr);
 
 	fromaddr = concatstr(3, listname, "+bounces-help@", listfqdn);
 
-	s1 = concatstr(9, "From: ", listname, "+owner@", listfqdn,
-			"\nTo: ", emailaddr, "\nSubject: Help for ",
-			listaddr, "\n\n");
+        maildata[1] = concatstr(3, listname, "+unsubscribe@", listfqdn);
+	maildata[3] = concatstr(3, listname, "+subscribe@", listfqdn);
+	maildata[5] = concatstr(3, listname, "+help@", listfqdn);
+	fromstr = concatstr(3, listname, "+owner@", listfqdn);
+	subject = concatstr(2, "Help for ", listaddr);
 
-	if(writen(queuefd, s1, strlen(s1)) < 0) {
-		log_error(LOG_ARGS, "Could not write help mail");
+	queuefilename = prepstdreply(listdir, "listhelp", fromstr, emailaddr,
+				NULL, subject, 3, maildata);
+	if(queuefilename == NULL) {
+		log_error(LOG_ARGS, "Could not prepare help mail");
 		exit(EXIT_FAILURE);
 	}
 
-	free(s1);
-
-	while((buf = mygetline(helpfd)) != NULL) {
-		if(strncmp(buf, "*UNSUBADDR*", 11) == 0) {
-			s1 = concatstr(3, listname, "+unsubscribe@", listfqdn);
-			if(writen(queuefd, s1, strlen(s1)) < 0) {
-				log_error(LOG_ARGS,
-						"Could not write help mail");
-				exit(EXIT_FAILURE);
-			}
-			free(s1);
-		} else if(strncmp(buf, "*SUBADDR*", 9) == 0) {
-			s1 = concatstr(3, listname, "+subscribe@", listfqdn);
-			if(writen(queuefd, s1, strlen(s1)) < 0) {
-				log_error(LOG_ARGS,
-						"Could not write help mail");
-				exit(EXIT_FAILURE);
-			}
-			free(s1);
-		} else if(strncmp(buf, "*HLPADDR*", 9) == 0) {
-			s1 = concatstr(3, listname, "+help@", listfqdn);
-			if(writen(queuefd, s1, strlen(s1)) < 0) {
-				log_error(LOG_ARGS,
-						"Could not write help mail");
-				exit(EXIT_FAILURE);
-			}
-			free(s1);
-		} else if(writen(queuefd, buf, strlen(buf)) < 0) {
-				log_error(LOG_ARGS,
-						"Could not write help mail");
-				exit(EXIT_FAILURE);
-		}
-		free(buf);
-	}
-	
+	free(fromstr);
+	free(listaddr);
 	free(listname);
 	free(listfqdn);
-	close(helpfd);
-	close(queuefd);
+	free(maildata[1]);
+	free(maildata[3]);
+	free(maildata[5]);
+	free(subject);
 
 	execlp(mlmmjsend, mlmmjsend,
 				"-l", "1",
 				"-T", emailaddr,
 				"-F", fromaddr,
 				"-m", queuefilename, 0);
+
 	log_error(LOG_ARGS, "execlp() of '%s' failed", mlmmjsend);
 	exit(EXIT_FAILURE);
 }