# HG changeset patch # User Ben Schmidt # Date 1325204952 -39600 # Node ID 5dc52f70e76b16640afbd4f6e6e58218018069ac # Parent c9237f9a346570430221a34c29643cc79550590c Abstract away operations on list texts in preparation for more processing. diff -r c9237f9a3465 -r 5dc52f70e76b include/prepstdreply.h --- a/include/prepstdreply.h Fri Dec 30 00:20:21 2011 +1100 +++ b/include/prepstdreply.h Fri Dec 30 11:29:12 2011 +1100 @@ -25,9 +25,16 @@ #ifndef PREPSTDREPLY_H #define PREPSTDREPLY_H +struct text; +typedef struct text text; + char *substitute(const char *line, const char *listaddr, const char *listdelim, size_t datacount, char **data, const char *listdir); -int open_listtext(const char *listdir, const char *filename); +text *open_text_file(const char *listdir, const char *filename); +text *open_text(const char *listdir, const char *purpose, const char *action, + const char *reason, const char *type, const char *compat); +char *get_text_line(text *txt); +void close_text(text *txt); char *prepstdreply(const char *listdir, const char *purpose, const char *action, const char *reason, const char *type, const char *compat, const char *from, const char *to, const char *replyto, diff -r c9237f9a3465 -r 5dc52f70e76b src/prepstdreply.c --- a/src/prepstdreply.c Fri Dec 30 00:20:21 2011 +1100 +++ b/src/prepstdreply.c Fri Dec 30 11:29:12 2011 +1100 @@ -46,6 +46,11 @@ #include "unistr.h" +struct text { + int fd; +}; + + static char *alphanum_token(char *token) { char *pos; if (*token == '\0') return NULL; @@ -186,30 +191,79 @@ } -int open_listtext(const char *listdir, const char *filename) +text *open_text_file(const char *listdir, const char *filename) { char *tmp; - int fd; + text *txt; + + txt = mymalloc(sizeof(text)); tmp = concatstr(3, listdir, "/text/", filename); - fd = open(tmp, O_RDONLY); + txt->fd = open(tmp, O_RDONLY); myfree(tmp); - if (fd >= 0) - return fd; + if (txt->fd >= 0) return txt; tmp = concatstr(2, DEFAULTTEXTDIR "/default/", filename); - fd = open(tmp, O_RDONLY); + txt->fd = open(tmp, O_RDONLY); myfree(tmp); - if (fd >= 0) - return fd; + if (txt->fd >= 0) return txt; tmp = concatstr(2, DEFAULTTEXTDIR "/en/", filename); - fd = open(tmp, O_RDONLY); + txt->fd = open(tmp, O_RDONLY); myfree(tmp); - if (fd >= 0) - return fd; + if (txt->fd >= 0) return txt; + + return NULL; +} + + +text *open_text(const char *listdir, const char *purpose, const char *action, + const char *reason, const char *type, const char *compat) +{ + size_t filenamelen, len; + char *filename; + text *txt; - return -1; + filename = concatstr(7,purpose,"-",action,"-",reason,"-",type); + filenamelen = strlen(filename); + do { + if ((txt = open_text_file(listdir, filename)) != NULL) break; + len = type ? strlen(type) : 0; + filename[filenamelen-len-1] = '\0'; + if ((txt = open_text_file(listdir, filename)) != NULL) break; + filename[filenamelen-len-1] = '-'; + filenamelen -= len + 1; + len = reason ? strlen(reason) : 0; + filename[filenamelen-len-1] = '\0'; + if ((txt = open_text_file(listdir, filename)) != NULL) break; + filename[filenamelen-len-1] = '-'; + filenamelen -= len + 1; + len = action ? strlen(action) : 0; + filename[filenamelen-len-1] = '\0'; + if ((txt = open_text_file(listdir, filename)) != NULL) break; + filename[filenamelen-len-1] = '-'; + filenamelen -= len + 1; + if ((txt = open_text_file(listdir, compat)) != NULL) { + myfree(filename); + filename = mystrdup(compat); + break; + } + log_error(LOG_ARGS, "Could not open listtext '%s'", filename); + myfree(filename); + return NULL; + } while (0); + + return txt; +} + + +char *get_text_line(text *txt) { + return mygetline(txt->fd); +} + + +void close_text(text *txt) { + close(txt->fd); } @@ -218,42 +272,17 @@ const char *from, const char *to, const char *replyto, size_t tokencount, char **data, const char *mailname) { - size_t filenamelen, i, len; - int infd, outfd, mailfd; - char *filename, *listaddr, *listdelim, *tmp, *retstr = NULL; + size_t len, i; + int outfd, mailfd; + text *txt; + char *listaddr, *listdelim, *tmp, *retstr = NULL; char *listfqdn, *line, *utfline, *utfsub, *utfsub2; char *str = NULL; char **moredata; char *headers[10] = { NULL }; /* relies on NULL to flag end */ - filename = concatstr(7,purpose,"-",action,"-",reason,"-",type); - filenamelen = strlen(filename); - do { - if ((infd = open_listtext(listdir, filename)) >= 0) break; - len = type ? strlen(type) : 0; - filename[filenamelen-len-1] = '\0'; - if ((infd = open_listtext(listdir, filename)) >= 0) break; - filename[filenamelen-len-1] = '-'; - filenamelen -= len + 1; - len = reason ? strlen(reason) : 0; - filename[filenamelen-len-1] = '\0'; - if ((infd = open_listtext(listdir, filename)) >= 0) break; - filename[filenamelen-len-1] = '-'; - filenamelen -= len + 1; - len = action ? strlen(action) : 0; - filename[filenamelen-len-1] = '\0'; - if ((infd = open_listtext(listdir, filename)) >= 0) break; - filename[filenamelen-len-1] = '-'; - filenamelen -= len + 1; - if ((infd = open_listtext(listdir, compat)) >= 0) { - myfree(filename); - filename = mystrdup(compat); - break; - } - log_error(LOG_ARGS, "Could not open listtext '%s'", filename); - myfree(filename); - return NULL; - } while (0); + txt = open_text(listdir, purpose, action, reason, type, compat); + if (txt == NULL) return NULL; listaddr = getlistaddr(listdir); listdelim = getlistdelim(listdir); @@ -276,7 +305,7 @@ myfree(listdelim); myfree(listfqdn); myfree(retstr); - myfree(filename); + myfree(txt); return NULL; } @@ -316,10 +345,9 @@ } for(;;) { - line = mygetline(infd); + line = get_text_line(txt); if (!line) { - log_error(LOG_ARGS, "No body in '%s' listtext", - filename); + log_error(LOG_ARGS, "No body in listtext"); break; } if (*line == '\n') { @@ -356,8 +384,7 @@ } if (!*tmp) { log_error(LOG_ARGS, "No headers or invalid " - "header in '%s' listtext", - filename); + "header in listtext"); break; } tmp++; @@ -431,7 +458,7 @@ str = concatstr(2, line, "\n"); myfree(line); } else { - str = mygetline(infd); + str = get_text_line(txt); } while(str) { utfline = unistr_escaped_to_utf8(str); @@ -498,7 +525,7 @@ myfree(str); } - str = mygetline(infd); + str = get_text_line(txt); } fsync(outfd); @@ -515,7 +542,8 @@ } myfree(moredata); - myfree(filename); + close_text(txt); + myfree(txt); return retstr; } diff -r c9237f9a3465 -r 5dc52f70e76b src/send_digest.c --- a/src/send_digest.c Fri Dec 30 00:20:21 2011 +1100 +++ b/src/send_digest.c Fri Dec 30 11:29:12 2011 +1100 @@ -180,7 +180,8 @@ int send_digest(const char *listdir, int firstindex, int lastindex, int issue, const char *addr, const char *mlmmjsend) { - int i, fd, archivefd, status, hdrfd, txtfd; + int i, fd, archivefd, status, hdrfd; + text * txt; char buf[45]; char *tmp, *queuename = NULL, *archivename, *subject, *line = NULL; char *utfsub, *utfsub2, *utfline; @@ -224,8 +225,8 @@ listfqdn = genlistfqdn(listaddr); listdelim = getlistdelim(listdir); - txtfd = open_listtext(listdir, "digest"); - if (txtfd < 0) { + txt = open_text_file(listdir, "digest"); + if (txt == NULL) { log_error(LOG_ARGS, "Could not open listtext 'digest'"); } @@ -252,7 +253,7 @@ subst_data[8] = "digestthreads"; subst_data[9] = thread_list(listdir, firstindex, lastindex); - if ((txtfd < 0) || !(line = mygetline(txtfd)) || + if (txt == NULL || (line = get_text_line(txt)) == NULL || (strncasecmp(line, "Subject: ", 9) != 0)) { utfsub = mystrdup("Digest of $listaddr$ issue $digestissue$" @@ -307,8 +308,8 @@ myfree(subst_data[5]); myfree(subst_data[7]); myfree(subst_data[9]); - if (txtfd > 0) { - close(txtfd); + if (txt != NULL) { + close_text(txt); myfree(line); } if (hdrfd > 0) { @@ -317,7 +318,7 @@ return -1; } - if ((txtfd > 0) && !statctrl(listdir, "nodigesttext")) { + if ((txt != NULL) && !statctrl(listdir, "nodigesttext")) { tmp = concatstr(3, "\n--", boundary, "\nContent-Type: text/plain; charset=UTF-8" @@ -339,8 +340,8 @@ myfree(subst_data[5]); myfree(subst_data[7]); myfree(subst_data[9]); - if (txtfd > 0) { - close(txtfd); + if (txt != NULL) { + close_text(txt); myfree(line); } return -1; @@ -349,7 +350,7 @@ if (line && (strncasecmp(line, "Subject: ", 9) == 0)) { myfree(line); - line = mygetline(txtfd); + line = get_text_line(txt); if (line && (strcmp(line, "\n") == 0)) { /* skip empty line after Subject: */ line[0] = '\0'; @@ -372,12 +373,12 @@ break; } myfree(tmp); - } while ((line = mygetline(txtfd))); + } while ((line = get_text_line(txt))); } - close(txtfd); - } else if (txtfd > 0) { - close(txtfd); + close_text(txt); + } else if (txt != NULL) { + close_text(txt); } myfree(line);