From cb4704c68992fe6e12cd153a693508b04bc7a470 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Tue, 7 Nov 2006 01:57:52 +0100 Subject: [PATCH] Simplications go on. make mutt_parse_rfc822_line more functionnal, that makes the API less awkward. Signed-off-by: Pierre Habouzit --- lib-lib/buffer.h | 4 + lib-mime/mime.c | 2 + lib-mime/mime.h | 8 +- lib-mime/rfc822address.c | 19 +++ lib-mime/rfc822parse.c | 272 +++++++++++++++++---------------------- protos.h | 2 - url.c | 4 +- 7 files changed, 149 insertions(+), 162 deletions(-) diff --git a/lib-lib/buffer.h b/lib-lib/buffer.h index 50e3904..41a508f 100644 --- a/lib-lib/buffer.h +++ b/lib-lib/buffer.h @@ -63,4 +63,8 @@ static inline void mutt_buffer_addch(BUFFER *b, char c) { mutt_buffer_add(b, &c, 1); } +static inline void mutt_buffer_reset(BUFFER *b) { + *(b->dptr = b->data) = '\0'; +} + #endif /* MUTT_LIB_LIB_BUFFER_H */ diff --git a/lib-mime/mime.c b/lib-mime/mime.c index 238bdbb..2311083 100644 --- a/lib-mime/mime.c +++ b/lib-mime/mime.c @@ -19,6 +19,8 @@ #include "mime-types.h" +#include "mutt.h" + const char MimeSpecials[] = "@.,;:<>[]\\\"()?/= \t"; const char *BodyTypes[] = { diff --git a/lib-mime/mime.h b/lib-mime/mime.h index 160a7c9..b235acc 100644 --- a/lib-mime/mime.h +++ b/lib-mime/mime.h @@ -64,12 +64,14 @@ BODY *mutt_read_mime_header (FILE *, int); void mutt_parse_part(FILE *, BODY *); BODY *mutt_parse_messageRFC822(FILE *, BODY *); BODY *mutt_parse_multipart(FILE *, const char *, off_t, int); -void mutt_parse_rfc822_line(ENVELOPE *, HEADER *, char *line, char *p, - short user_hdrs, short weed, short do_2047, - LIST **); +LIST **mutt_parse_rfc822_line(ENVELOPE *, HEADER *, char *line, char *p, + short weed, short do_2047, LIST **); + +ENVELOPE *mutt_read_rfc822_header(FILE *, HEADER *, short, short); /*** addresses ***/ +address_t *mutt_parse_adrlist (address_t *, const char *); address_t *address_dup(address_t *addr); address_t *address_list_dup(address_t *addr); void rfc822_qualify(address_t *, const char *); diff --git a/lib-mime/rfc822address.c b/lib-mime/rfc822address.c index 6a2e0ee..e4bf976 100644 --- a/lib-mime/rfc822address.c +++ b/lib-mime/rfc822address.c @@ -462,3 +462,22 @@ rfc822_write_address(char *buf, ssize_t buflen, address_t *addr, int display) return pos; } +address_t *mutt_parse_adrlist(address_t *p, const char *s) +{ + /* check for a simple whitespace separated list of addresses */ + char *q = strpbrk(s, "\"<>():;,\\"); + char tmp[HUGE_STRING]; + + if (q) + return rfc822_parse_adrlist(p, s); + + m_strcpy(tmp, sizeof(tmp), s); + q = tmp; + while ((q = strtok(q, " \t"))) { + p = rfc822_parse_adrlist(p, q); + q = NULL; + } + + return p; +} + diff --git a/lib-mime/rfc822parse.c b/lib-mime/rfc822parse.c index ebd7b82..5509b8c 100644 --- a/lib-mime/rfc822parse.c +++ b/lib-mime/rfc822parse.c @@ -785,9 +785,8 @@ time_t mutt_parse_date(const char *s, HEADER *h) #include "rfc822hdrs.h" -void mutt_parse_rfc822_line(ENVELOPE *e, HEADER *hdr, char *line, char *p, - short user_hdrs, short weed, short do_2047, - LIST **lastp) +LIST **mutt_parse_rfc822_line(ENVELOPE *e, HEADER *hdr, char *line, char *p, + short weed, short do_2047, LIST **user_hdrs) { switch (rfc822_well_known(line)) { case HDR_APPARENTLY_FROM: @@ -1038,33 +1037,27 @@ void mutt_parse_rfc822_line(ENVELOPE *e, HEADER *hdr, char *line, char *p, break; default: - if (!user_hdrs) { - return; - } + if (!user_hdrs) + break; + /* restore the original line */ line[m_strlen(line)] = ':'; if (weed && option(OPTWEED) && mutt_matches_ignore(line, Ignore) && !mutt_matches_ignore(line, UnIgnore)) { - return; - } - - if (*lastp) { - (*lastp)->next = mutt_new_list(); - (*lastp) = (*lastp)->next; - } else { - (*lastp) = e->userhdrs = mutt_new_list (); + break; } - (*lastp)->data = m_strdup(line); + *user_hdrs = mutt_new_list(); + (*user_hdrs)->data = m_strdup(line); if (do_2047) - rfc2047_decode(&(*lastp)->data); + rfc2047_decode(&(*user_hdrs)->data); + (*user_hdrs)->next = mutt_new_list(); + return &(*user_hdrs)->next; } -} - -/*** XXX: MC READ MARK ***/ - + return user_hdrs; +} /* mutt_read_rfc822_header() -- parses a RFC822 header * @@ -1084,162 +1077,131 @@ void mutt_parse_rfc822_line(ENVELOPE *e, HEADER *hdr, char *line, char *p, * Returns: newly allocated envelope structure. You should free it by * envelope_delete() when envelope stay unneeded. */ -ENVELOPE *mutt_read_rfc822_header (FILE * f, HEADER * hdr, short user_hdrs, - short weed) +ENVELOPE * +mutt_read_rfc822_header(FILE *f, HEADER *hdr, short user_hdrs, short weed) { - ENVELOPE *e = envelope_new(); - LIST *last = NULL; - char *line = p_new(char, LONG_STRING); - char *p; - off_t loc; - ssize_t linelen = LONG_STRING; - char buf[LONG_STRING + 1]; - - if (hdr) { - if (hdr->content == NULL) { - hdr->content = mutt_new_body (); - - /* set the defaults from RFC1521 */ - hdr->content->type = TYPETEXT; - hdr->content->subtype = m_strdup("plain"); - hdr->content->encoding = ENC7BIT; - hdr->content->length = -1; - - /* RFC 2183 says this is arbitrary */ - hdr->content->disposition = DISPINLINE; - } - } + ENVELOPE *e = envelope_new(); + LIST **last = user_hdrs ? &e->userhdrs : NULL; - while ((loc = ftello (f)), - mutt_read_rfc822_line (f, &line, &linelen)) - { - if ((p = strpbrk (line, ": \t")) == NULL || *p != ':') { - char return_path[LONG_STRING]; - time_t t; - - /* some bogus MTAs will quote the original "From " line */ - if (m_strncmp(">From ", line, 6) == 0) - continue; /* just ignore */ - else if (is_from (line, return_path, sizeof (return_path), &t)) { - /* MH somtimes has the From_ line in the middle of the header! */ - if (hdr && !hdr->received) - hdr->received = t - mutt_local_tz (t); - continue; - } + char *line = p_new(char, LONG_STRING); + ssize_t linelen = LONG_STRING; + off_t loc; - fseeko (f, loc, 0); - break; /* end of header */ + if (hdr && !hdr->content) { + hdr->content = mutt_new_body (); + + /* set the defaults from RFC1521 */ + hdr->content->type = TYPETEXT; + hdr->content->subtype = m_strdup("plain"); + hdr->content->encoding = ENC7BIT; + hdr->content->length = -1; + + /* RFC 2183 says this is arbitrary */ + hdr->content->disposition = DISPINLINE; } - *buf = '\0'; - - if (mutt_match_spam_list (line, SpamList, buf, sizeof (buf))) { - if (!rx_list_match (NoSpamList, line)) { - - /* if spam tag already exists, figure out how to amend it */ - if (e->spam && *buf) { - /* If SpamSep defined, append with separator */ - if (SpamSep) { - mutt_buffer_addstr (e->spam, SpamSep); - mutt_buffer_addstr (e->spam, buf); - } - - /* else overwrite */ - else { - e->spam->dptr = e->spam->data; - *e->spam->dptr = '\0'; - mutt_buffer_addstr (e->spam, buf); - } - } + while ((loc = ftello(f)), + mutt_read_rfc822_line(f, &line, &linelen)) + { + char buf[LONG_STRING + 1] = ""; + char *p; + + p = strpbrk(line, ": \t"); + if (!p || *p != ':') { + char return_path[LONG_STRING]; + time_t t; + + /* some bogus MTAs will quote the original "From " line */ + if (!m_strncmp(">From ", line, 6)) + continue; /* just ignore */ + + if (is_from(line, return_path, sizeof(return_path), &t)) { + /* MH somtimes has the From_ line in the middle of the header! */ + if (hdr && !hdr->received) + hdr->received = t - mutt_local_tz(t); + continue; + } - /* spam tag is new, and match expr is non-empty; copy */ - else if (!e->spam && *buf) { - e->spam = mutt_buffer_from (NULL, buf); + fseeko(f, loc, 0); + break; /* end of header */ } - /* match expr is empty; plug in null string if no existing tag */ - else if (!e->spam) { - e->spam = mutt_buffer_from (NULL, ""); + if (mutt_match_spam_list(line, SpamList, buf, sizeof(buf))) { + if (!rx_list_match(NoSpamList, line)) { + /* if spam tag already exists, figure out how to amend it */ + if (e->spam && *buf) { + if (SpamSep) { + /* If SpamSep defined, append with separator */ + mutt_buffer_addstr(e->spam, SpamSep); + mutt_buffer_addstr(e->spam, buf); + } else { + /* else overwrite */ + mutt_buffer_reset(e->spam); + mutt_buffer_addstr(e->spam, buf); + } + } + else if (!e->spam && *buf) { + /* spam tag is new, and match expr is non-empty; copy */ + e->spam = mutt_buffer_from(NULL, buf); + } + else if (!e->spam) { + /* match expr is empty; plug in null string if no existing tag */ + e->spam = mutt_buffer_from(NULL, ""); + } + } } - if (e->spam && e->spam->data) - debug_print (5, ("spam = %s\n", e->spam->data)); - } - } - - *p++ = 0; - p = vskipspaces(p); - if (!*p) - continue; /* skip empty header fields */ - - mutt_parse_rfc822_line (e, hdr, line, p, user_hdrs, weed, 1, &last); - } + *p++ = '\0'; + p = vskipspaces(p); + if (!*p) + continue; /* skip empty header fields */ - p_delete(&line); - - if (hdr) { - hdr->content->hdr_offset = hdr->offset; - hdr->content->offset = ftello (f); - rfc2047_decode_envelope(e); - /* check for missing or invalid date */ - if (hdr->date_sent <= 0) { - debug_print (1, ("no date found, using received " - "time from msg separator\n")); - hdr->date_sent = hdr->received; + last = mutt_parse_rfc822_line(e, hdr, line, p, weed, 1, last); } - } - return (e); -} + p_delete(&line); -address_t *mutt_parse_adrlist (address_t * p, const char *s) -{ - const char *q; - - /* check for a simple whitespace separated list of addresses */ - if ((q = strpbrk (s, "\"<>():;,\\")) == NULL) { - char tmp[HUGE_STRING]; - char *r; - - m_strcpy(tmp, sizeof(tmp), s); - r = tmp; - while ((r = strtok (r, " \t")) != NULL) { - p = rfc822_parse_adrlist (p, r); - r = NULL; + if (hdr) { + hdr->content->hdr_offset = hdr->offset; + hdr->content->offset = ftello(f); + rfc2047_decode_envelope(e); + /* check for missing or invalid date */ + if (hdr->date_sent <= 0) { + debug_print(1, ("no date found, using received " + "time from msg separator\n")); + hdr->date_sent = hdr->received; + } } - } - else - p = rfc822_parse_adrlist (p, s); - return p; + return e; } +/* -------------------- XXX: MC READ MARK ------------- */ /* Compares mime types to the ok and except lists */ -int count_body_parts_check(LIST **checklist, BODY *b, int dflt) { - LIST *type; - ATTACH_MATCH *a; - - /* If list is null, use default behavior. */ - if (! *checklist) { - /*return dflt;*/ - return 0; - } - - for (type = *checklist; type; type = type->next) { - a = (ATTACH_MATCH *)type->data; - debug_print(5, ("cbpc: %s %d/%s ?? %s/%s [%d]... ", - dflt ? "[OK] " : "[EXCL] ", - b->type, b->subtype, a->major, a->minor, a->major_int)); - if ((a->major_int == TYPEANY || a->major_int == b->type) && - !regexec(&a->minor_rx, b->subtype, 0, NULL, 0)) { - debug_print(5, ("yes\n")); - return 1; - } else { - debug_print(5, ("no\n")); +static int count_body_parts_check(LIST **checklist, BODY *b, int dflt) +{ + LIST *type; + ATTACH_MATCH *a; + + /* If list is null, use default behavior. */ + if (!*checklist) + return 0; + + for (type = *checklist; type; type = type->next) { + a = (ATTACH_MATCH *)type->data; + debug_print(5, ("cbpc: %s %d/%s ?? %s/%s [%d]... ", + dflt ? "[OK] " : "[EXCL] ", + b->type, b->subtype, a->major, a->minor, a->major_int)); + if ((a->major_int == TYPEANY || a->major_int == b->type) && + !regexec(&a->minor_rx, b->subtype, 0, NULL, 0)) { + debug_print(5, ("yes\n")); + return 1; + } else { + debug_print(5, ("no\n")); + } } - } - return 0; + return 0; } #define AT_COUNT(why) { shallcount = 1; } diff --git a/protos.h b/protos.h index d87c1f3..1f1e206 100644 --- a/protos.h +++ b/protos.h @@ -53,7 +53,6 @@ int mutt_option_value (const char* val, char* dst, size_t dstlen); address_t *mutt_default_from (void); address_t *mutt_remove_duplicates (address_t *); -address_t *mutt_parse_adrlist (address_t *, const char *); BODY *mutt_make_file_attach (const char *); BODY *mutt_make_message_attach (CONTEXT *, HEADER *, int); @@ -65,7 +64,6 @@ CONTENT *mutt_get_content_info (const char *fname, BODY * b); LIST *mutt_make_references (ENVELOPE * e); -ENVELOPE *mutt_read_rfc822_header (FILE *, HEADER *, short, short); HEADER *mutt_dup_header (HEADER *); int mutt_cmp_header (const HEADER*, const HEADER*); diff --git a/url.c b/url.c index 81b47c7..39bbec8 100644 --- a/url.c +++ b/url.c @@ -207,7 +207,7 @@ int url_parse_mailto (ENVELOPE * e, char **body, const char *src) int taglen; - LIST *last = NULL; + LIST **last = &e->userhdrs; if (!(t = strchr (src, ':'))) return -1; @@ -244,7 +244,7 @@ int url_parse_mailto (ENVELOPE * e, char **body, const char *src) #undef SAVEPFX scratch[taglen] = '\0'; value = vskipspaces(&scratch[taglen + 1]); - mutt_parse_rfc822_line (e, NULL, scratch, value, 1, 0, 0, &last); + last = mutt_parse_rfc822_line (e, NULL, scratch, value, 0, 0, last); /* if $strict_mailto is set, force editing headers to let * users have a look at what we got */ if (!option (OPTSTRICTMAILTO)) { -- 2.20.1