From: Ben Schmidt Date: Fri, 30 Dec 2011 00:29:12 +0000 (+1100) Subject: Abstract away operations on list texts in preparation for more processing. X-Git-Tag: RELEASE_1_2_18a1~58 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2ee6a9844f0e97edd1c8f06f490c900deda9a396;p=thirdparty%2Fmlmmj.git Abstract away operations on list texts in preparation for more processing. --- diff --git a/include/prepstdreply.h b/include/prepstdreply.h index 45a6b66e..4d6d8df1 100644 --- a/include/prepstdreply.h +++ b/include/prepstdreply.h @@ -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 --git a/src/prepstdreply.c b/src/prepstdreply.c index 90595ecd..394e1589 100644 --- a/src/prepstdreply.c +++ b/src/prepstdreply.c @@ -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,66 +191,59 @@ char *substitute(const char *line, const char *listaddr, const char *listdelim, } -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 -1; + return NULL; } -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, - size_t tokencount, char **data, const char *mailname) +text *open_text(const char *listdir, const char *purpose, const char *action, + const char *reason, const char *type, const char *compat) { - size_t filenamelen, i, len; - int infd, outfd, mailfd; - char *filename, *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 */ + size_t filenamelen, len; + char *filename; + text *txt; filename = concatstr(7,purpose,"-",action,"-",reason,"-",type); filenamelen = strlen(filename); do { - if ((infd = open_listtext(listdir, filename)) >= 0) break; + if ((txt = open_text_file(listdir, filename)) != NULL) break; len = type ? strlen(type) : 0; filename[filenamelen-len-1] = '\0'; - if ((infd = open_listtext(listdir, filename)) >= 0) break; + 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 ((infd = open_listtext(listdir, filename)) >= 0) break; + 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 ((infd = open_listtext(listdir, filename)) >= 0) break; + if ((txt = open_text_file(listdir, filename)) != NULL) break; filename[filenamelen-len-1] = '-'; filenamelen -= len + 1; - if ((infd = open_listtext(listdir, compat)) >= 0) { + if ((txt = open_text_file(listdir, compat)) != NULL) { myfree(filename); filename = mystrdup(compat); break; @@ -255,6 +253,37 @@ char *prepstdreply(const char *listdir, const char *purpose, const char *action, return NULL; } while (0); + return txt; +} + + +char *get_text_line(text *txt) { + return mygetline(txt->fd); +} + + +void close_text(text *txt) { + close(txt->fd); +} + + +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, + size_t tokencount, char **data, const char *mailname) +{ + 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 */ + + txt = open_text(listdir, purpose, action, reason, type, compat); + if (txt == NULL) return NULL; + listaddr = getlistaddr(listdir); listdelim = getlistdelim(listdir); listfqdn = genlistfqdn(listaddr); @@ -276,7 +305,7 @@ char *prepstdreply(const char *listdir, const char *purpose, const char *action, myfree(listdelim); myfree(listfqdn); myfree(retstr); - myfree(filename); + myfree(txt); return NULL; } @@ -316,10 +345,9 @@ char *prepstdreply(const char *listdir, const char *purpose, const char *action, } 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 @@ char *prepstdreply(const char *listdir, const char *purpose, const char *action, } if (!*tmp) { log_error(LOG_ARGS, "No headers or invalid " - "header in '%s' listtext", - filename); + "header in listtext"); break; } tmp++; @@ -431,7 +458,7 @@ char *prepstdreply(const char *listdir, const char *purpose, const char *action, 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 @@ char *prepstdreply(const char *listdir, const char *purpose, const char *action, myfree(str); } - str = mygetline(infd); + str = get_text_line(txt); } fsync(outfd); @@ -515,7 +542,8 @@ freeandreturn: } myfree(moredata); - myfree(filename); + close_text(txt); + myfree(txt); return retstr; } diff --git a/src/send_digest.c b/src/send_digest.c index 6106668a..57f03e35 100644 --- a/src/send_digest.c +++ b/src/send_digest.c @@ -180,7 +180,8 @@ static char *thread_list(const char *listdir, int firstindex, int lastindex) 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 @@ int send_digest(const char *listdir, int firstindex, int lastindex, 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 @@ int send_digest(const char *listdir, int firstindex, int lastindex, 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 @@ errdighdrs: 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 @@ errdighdrs: 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 @@ errdighdrs: 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 @@ errdighdrs: 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 @@ errdighdrs: 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);