]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
Bye bye FILE*, welcome fd
authormmj <none@none>
Fri, 4 Jun 2004 10:41:57 +0000 (20:41 +1000)
committermmj <none@none>
Fri, 4 Jun 2004 10:41:57 +0000 (20:41 +1000)
include/mlmmj-send.h
src/dumpfd2fd.c
src/mail-functions.c
src/mlmmj-send.c
src/send_help.c

index 67fb07b3fdf74bd27646f41ded994a9e2930697f..59d448f040412e471a858618ed58b5b0a2c72d14 100644 (file)
@@ -13,7 +13,7 @@ int send_mail(int sockfd, const char *from, const char *to,
              const char *replyto, int mailfd,
              const char *listdir, const char *mlmmjbounce);
 int send_mail_many(int sockfd, const char *from, const char *replyto,
-                  int mailfd, FILE *subfile, const char *listaddr,
+                  int mailfd, int subfd, const char *listaddr,
                   const char *archivefilename, const char *listdir,
                   const char *mlmmjbounce);
 int initsmtp(int *sockfd, const char *relayhost);
index a0d19a4f6d62401ac1687f0c19cb029d287147e5..48c824cae0c084e7834e16ef06d945728486ef92 100644 (file)
@@ -14,7 +14,7 @@
 
 #include "wrappers.h"
 
-#define DUMPBUF 1024
+#define DUMPBUF 4096
 
 int dumpfd2fd(int infd, int outfd)
 {
index 781192c3d4eb55e270c08f4aedcd966ca6d31d01..e321e9faed19d9520ab1a3ff0045454b02815fb2 100644 (file)
@@ -59,7 +59,7 @@ int write_mail_from(int sockfd, const char *from_addr)
        snprintf(mail_from, len, "MAIL FROM: <%s>\r\n", from_addr);
        len = strlen(mail_from);
 
-#ifdef MLMMJ_DEBUG
+#if 0
        fprintf(stderr, "\nwrite_mail_from, mail_from = [%s]\n", mail_from);
 #endif
        bytes_written = writen(sockfd, mail_from, len);
@@ -87,7 +87,7 @@ int write_rcpt_to(int sockfd, const char *rcpt_addr)
        snprintf(rcpt_to, len, "RCPT TO: <%s>\r\n", rcpt_addr);
        len = strlen(rcpt_to);
 
-#ifdef MLMMJ_DEBUG
+#if 0
        fprintf(stderr, "\nwrite_rcpt_to, rcpt_to = [%s]\n", rcpt_to);
 #endif
        bytes_written = writen(sockfd, rcpt_to, len);
@@ -121,12 +121,14 @@ int write_mailbody_from_fd(int sockfd, int fd)
        /* keep writing chunks of line (max WRITE_BUFSIZE) */
        for(;;) {
                bufp = buf+1;
-               errno = 0;  /* We must reset errno, otherwise we can't
-                            * determine if we hit EOF or an error
-                            * occurred */
-               if(read(fd, bufp, WRITE_BUFSIZE) < 0) {
+
+               len = read(fd, bufp, WRITE_BUFSIZE);
+
+               if(len == 0)
+                       return 0;
+                       
+               if(len < 0) {
                        if (errno == EINTR) {
-                               errno = 0;
                                continue;
                        } else {
                                return errno;
@@ -140,7 +142,6 @@ int write_mailbody_from_fd(int sockfd, int fd)
                }
 
                /* fix newlines */
-               len = strlen(bufp);
                if((len > 0) && (bufp[len-1] == '\n')) {
                        bufp[len-1] = '\r';
                        bufp[len] = '\n';
@@ -151,7 +152,7 @@ int write_mailbody_from_fd(int sockfd, int fd)
                        full_line = 0;
                }
 
-#ifdef MLMMJ_DEBUG
+#if 0
                fprintf(stderr, "write_mailbody_from_file = [%s]\n", bufp);
 #endif
                bytes_written = writen(sockfd, bufp, len);
index 9adb2eab2825e8680e37ec621a1aeae5f353d95c..c97651802a5b334b7da6f722ada3919106850641 100644 (file)
@@ -34,6 +34,7 @@
 #include "strgen.h"
 #include "log_error.h"
 #include "mygetline.h"
+#include "wrappers.h"
 
 char *bounce_from_adr(const char *recipient, const char *listadr,
                      const char *mailfilename)
@@ -278,15 +279,15 @@ int endsmtp(int *sockfd)
 }
 
 int send_mail_many(int sockfd, const char *from, const char *replyto,
-                  int mailfd, FILE *subfile, const char *listaddr,
+                  int mailfd, int subfd, const char *listaddr,
                   const char *archivefilename, const char *listdir,
                   const char *mlmmjbounce)
 {
-       int sendres = 0;
+       int sendres = 0, addrfd;
        char *bounceaddr, *addr, *index, *dirname, *addrfilename;
-       FILE *addrfile;
+       size_t len;
 
-       while((addr = myfgetline(subfile))) {
+       while((addr = mygetline(subfd))) {
                chomp(addr);
                if(from)
                        sendres = send_mail(sockfd, from, addr, replyto,
@@ -313,8 +314,9 @@ int send_mail_many(int sockfd, const char *from, const char *replyto,
                        }
                        addrfilename = concatstr(2, dirname, "/subscribers");
                        free(dirname);
-                       addrfile = fopen(addrfilename, "a");
-                       if(addrfile == NULL) {
+                       addrfd = open(addrfilename, O_WRONLY|O_CREAT|O_APPEND,
+                                                       S_IRUSR|S_IWUSR);
+                       if(addrfd < 0) {
                                log_error(LOG_ARGS, "Could not write to %s",
                                                    addrfilename);
                                free(addrfilename);
@@ -322,20 +324,22 @@ int send_mail_many(int sockfd, const char *from, const char *replyto,
                                return 1;
                        } else { /* dump the remaining addresses */
                                do {
-                                       if(fputs(addr, addrfile) < 0)
+                                       /* Dirty hack to add newline. */
+                                       len = strlen(addr);
+                                       addr[len] = '\n';
+                                       if(writen(addrfd, addr, len+1) < 0)
                                                log_error(LOG_ARGS,
                                                        "Could not add [%s] "
                                                        "to requeue address "
                                                        "file.", addr);
-                                       fputc('\n', addrfile);
                                        free(addr);
-                                       addr = myfgetline(subfile);
+                                       addr = mygetline(subfd);
                                } while(addr);
                        }
                        
                        free(addr);
                        free(addrfilename);
-                       fclose(addrfile);
+                       close(addrfd);
 
                        return 1;
                }
@@ -368,7 +372,7 @@ static void print_help(const char *prg)
 int main(int argc, char **argv)
 {
        size_t len = 0;
-       int sockfd = 0, mailfd = 0, opt, mindex;
+       int sockfd = 0, mailfd = 0, opt, mindex, subfd, tmpfd;
        int deletewhensent = 1, sendres, archive = 1;
        char *listaddr, *mailfilename = NULL, *subfilename = NULL;
        char *replyto = NULL, *bounceaddr = NULL, *to_addr = NULL;
@@ -376,7 +380,6 @@ int main(int argc, char **argv)
        char *listctrl = NULL, *subddirname = NULL, *listdir = NULL;
        char *mlmmjbounce = NULL, *bindir;
        DIR *subddir;
-       FILE *subfile = NULL, *tmpfile;
        struct dirent *dp;
        
        log_set_name(argv[0]);
@@ -465,7 +468,7 @@ int main(int argc, char **argv)
                break;
        case '2': /* Moderators */
                subfilename = concatstr(2, listdir, "/moderators");
-               if((subfile = fopen(subfilename, "r")) == NULL) {
+               if((subfd = open(subfilename, O_RDONLY)) < 0) {
                        log_error(LOG_ARGS, "Could not open '%s':",
                                            subfilename);
                        free(subfilename);
@@ -476,7 +479,7 @@ int main(int argc, char **argv)
                }
                break;
        case '3': /* resending earlier failed mails */
-               if((subfile = fopen(subfilename, "r")) == NULL) {
+               if((subfd = open(subfilename, O_RDONLY)) < 0) {
                        log_error(LOG_ARGS, "Could not open '%s':",
                                            subfilename);
                        exit(EXIT_FAILURE);
@@ -509,28 +512,41 @@ int main(int argc, char **argv)
                        deletewhensent = 0;
                        /* dump date we want when resending */
                        tmpstr = concatstr(2, mailfilename, ".mailfrom");
-                       tmpfile = fopen(tmpstr, "w");
+                       tmpfd = open(tmpstr, O_WRONLY|O_CREAT|O_TRUNC,
+                                               S_IRUSR|S_IWUSR);
                        free(tmpstr);
-                       fputs(bounceaddr, tmpfile);
-                       fclose(tmpfile);
+                       if(tmpfd >= 0) {
+                               writen(tmpfd, to_addr, strlen(to_addr));
+                               fsync(tmpfd);
+                       }
+                       close(tmpfd);
                        tmpstr = concatstr(2, mailfilename, ".reciptto");
-                       tmpfile = fopen(tmpstr, "w");
+                       tmpfd = open(tmpstr, O_WRONLY|O_CREAT|O_TRUNC,
+                                               S_IRUSR|S_IWUSR);
                        free(tmpstr);
-                       fputs(to_addr, tmpfile);
-                       fclose(tmpfile);
+                       if(tmpfd >= 0) {
+                               writen(tmpfd, bounceaddr, strlen(bounceaddr));
+                               fsync(tmpfd);
+                       }
+                       close(tmpfd);
                        if(replyto) {
                                tmpstr = concatstr(2, mailfilename,
                                                      ".reply-to");
-                               tmpfile = fopen(tmpstr, "w");
+                               tmpfd = open(tmpstr, O_WRONLY|O_CREAT|O_TRUNC,
+                                                       S_IRUSR|S_IWUSR);
                                free(tmpstr);
-                               fputs(replyto, tmpfile);
-                               fclose(tmpfile);
+                               if(tmpfd >= 0) {
+                                       writen(tmpfd, replyto,
+                                               strlen(replyto));
+                                       fsync(tmpfd);
+                               }
+                               close(tmpfd);
                        }
                }
                break;
        case '2': /* Moderators */
                initsmtp(&sockfd, relayhost);
-               if(send_mail_many(sockfd, bounceaddr, NULL, mailfd, subfile,
+               if(send_mail_many(sockfd, bounceaddr, NULL, mailfd, subfd,
                               NULL, NULL, listdir, NULL))
                        close(sockfd);
                else
@@ -538,7 +554,7 @@ int main(int argc, char **argv)
                break;
        case '3': /* resending earlier failed mails */
                initsmtp(&sockfd, relayhost);
-               if(send_mail_many(sockfd, NULL, NULL, mailfd, subfile,
+               if(send_mail_many(sockfd, NULL, NULL, mailfd, subfd,
                                listaddr, mailfilename, listdir, mlmmjbounce))
                        close(sockfd);
                else
@@ -562,7 +578,7 @@ int main(int argc, char **argv)
                                continue;
                        subfilename = concatstr(3, listdir, "/subscribers.d/",
                                                dp->d_name);
-                       if((subfile = fopen(subfilename, "r")) == NULL) {
+                       if((subfd = open(subfilename, O_RDONLY)) < 0) {
                                log_error(LOG_ARGS, "Could not open '%s'",
                                                    subfilename);
                                free(subfilename);
@@ -573,7 +589,7 @@ int main(int argc, char **argv)
 
                        initsmtp(&sockfd, relayhost);
                        sendres = send_mail_many(sockfd, NULL, NULL, mailfd,
-                                       subfile, listaddr, archivefilename,
+                                       subfd, listaddr, archivefilename,
                                        listdir, mlmmjbounce);
                        if (sendres) {
                                /* If send_mail_many() failed we close the
@@ -584,7 +600,7 @@ int main(int argc, char **argv)
                        } else {
                                endsmtp(&sockfd);
                        }
-                       fclose(subfile);
+                       close(subfd);
                }
                closedir(subddir);
                break;
index dce9526dd8f4e28ad95061e8b6e40f7ab887d859..12de2f0afe8c75a9e9ce7df817a1851e0d4c1a72 100644 (file)
@@ -9,8 +9,10 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <strings.h>
 #include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #include "mlmmj.h"
 #include "send_help.h"
 #include "find_email_adr.h"
 #include "getlistaddr.h"
 #include "log_error.h"
+#include "chomp.h"
+#include "wrappers.h"
+#include "mygetline.h"
 
 void send_help(const char *listdir, const char *emailaddr,
               const char *mlmmjsend)
 {
-       FILE *helpfile, *queuefile;
-       char buf[READ_BUFSIZE];
-       char *listaddr, *bufres, *helpaddr, *fromaddr, *fromstr, *tostr;
-       char *subjectstr, *helpfilename, *queuefilename, *listname;
-       char *randomstr, *listfqdn;
-       size_t len;
+       int helpfd, queuefd;
+       char *listaddr, *buf, *fromaddr;
+       char *helpfilename, *queuefilename, *listname;
+       char *randomstr, *listfqdn, *s1;
 
         listaddr = getlistaddr(listdir);
+       chomp(listaddr);
 
        helpfilename = concatstr(2, listdir, "/text/listhelp");
 
-       if((helpfile = fopen(helpfilename, "r")) == NULL) {
+       if((helpfd = open(helpfilename, O_RDONLY)) < 0) {
                log_error(LOG_ARGS, "Could not open text/help");
                free(helpfilename);
                exit(EXIT_FAILURE);
@@ -46,9 +50,9 @@ void send_help(const char *listdir, const char *emailaddr,
        randomstr = random_str();
 
        queuefilename = concatstr(3, listdir, "/queue/", randomstr);
-       printf("%s\n", queuefilename);
        
-       if((queuefile = fopen(queuefilename, "w")) == NULL) {
+       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);
@@ -56,50 +60,56 @@ void send_help(const char *listdir, const char *emailaddr,
        }
        free(randomstr);
 
-       len = strlen(listname) + strlen(listfqdn) + strlen("+help@") + 1;
-       helpaddr = malloc(len);
-       snprintf(helpaddr, len, "%s+help@%s", listname, listfqdn);
+       fromaddr = concatstr(3, listname, "+bounces-help@", listfqdn);
 
-       len += strlen("+bounces");
-       fromaddr = malloc(len);
-       snprintf(fromaddr, len, "%s+bounces-help@%s", listname, listfqdn);
+       s1 = concatstr(11, "From: ", listname, "+owner@", listfqdn, "\n"
+                       "To: ", emailaddr, "\n", "Subject: Help for ",
+                       listaddr, "\n\n");
 
-       fromstr = headerstr("From: ", helpaddr);
-       fputs(fromstr, queuefile);
-       free(helpaddr);
-
-       tostr = headerstr("To: ", emailaddr);
-       fputs(tostr, queuefile);
+       if(writen(queuefd, s1, strlen(s1)) < 0) {
+               log_error(LOG_ARGS, "Could not write help mail");
+               exit(EXIT_FAILURE);
+       }
 
-       subjectstr = headerstr("Subject: Help for ", listaddr);
-       fputs(subjectstr, queuefile);
-       fputc('\n', queuefile);
+       free(s1);
 
-       while((bufres = fgets(buf, READ_BUFSIZE, helpfile))) {
+       while((buf = mygetline(helpfd)) != NULL) {
                if(strncmp(buf, "*UNSUBADDR*", 11) == 0) {
-                       fputs(listname, queuefile);
-                       fputs("+unsubscribe@", queuefile);
-                       fputs(listfqdn, queuefile);
-               }
-               else if(strncmp(buf, "*SUBADDR*", 9) == 0) {
-                       fputs(listname, queuefile);
-                       fputs("+subscribe@", queuefile);
-                       fputs(listfqdn, queuefile);
+                       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);
                }
-               else if(strncmp(buf, "*HLPADDR*", 9) == 0) {
-                       fputs(listname, queuefile);
-                       fputs("+help@", queuefile);
-                       fputs(listfqdn, queuefile);
-               } else
-                       fputs(buf, queuefile);
+               free(buf);
        }
        
-       free(tostr);
-       free(subjectstr);
        free(listname);
        free(listfqdn);
-       fclose(helpfile);
-       fclose(queuefile);
+       close(helpfd);
+       close(queuefd);
 
        execlp(mlmmjsend, mlmmjsend,
                                "-l", "1",