From: Pierre Habouzit Date: Sun, 19 Nov 2006 02:13:55 +0000 (+0100) Subject: exit SPAM_LIST, just extend rx_t with the needed informations. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=9fde23d2b9a4ba8076eb425a1af4342de1e485b5;p=apps%2Fmadmutt.git exit SPAM_LIST, just extend rx_t with the needed informations. push some things into the rx lib. horrors--. Signed-off-by: Pierre Habouzit --- diff --git a/globals.h b/globals.h index 47bed4c..1eb9b1c 100644 --- a/globals.h +++ b/globals.h @@ -183,7 +183,7 @@ WHERE rx_t *MailLists INITVAL (0); WHERE rx_t *UnMailLists INITVAL (0); WHERE rx_t *SubscribedLists INITVAL (0); WHERE rx_t *UnSubscribedLists INITVAL (0); -WHERE SPAM_LIST *SpamList INITVAL (0); +WHERE rx_t *SpamList INITVAL (0); WHERE rx_t *NoSpamList INITVAL (0); /* bit vector for boolean variables */ diff --git a/init.c b/init.c index c3db611..d4b367c 100644 --- a/init.c +++ b/init.c @@ -637,101 +637,46 @@ add_to_rx_list(rx_t **list, const char *s, int flags, BUFFER *err) return 0; } -static int add_to_spam_list (SPAM_LIST ** list, const char *pat, +static int add_to_spam_list(rx_t **list, const char *pat, const char *templ, BUFFER * err) { - SPAM_LIST *t = NULL, *last = NULL; - rx_t* rx; - int n; - const char *p; - - if (!pat || !*pat || !templ) - return 0; + rx_t **last, *rx; - if (!(rx = rx_compile (pat, REG_ICASE))) { - snprintf (err->data, err->dsize, _("Bad regexp: %s"), pat); - return -1; - } + if (!pat || !*pat || !templ) + return 0; - /* check to make sure the item is not already on this list */ - for (last = *list; last; last = last->next) { - if (ascii_strcasecmp (rx->pattern, last->rx->pattern) == 0) { - /* Already on the list. Formerly we just skipped this case, but - * now we're supporting removals, which means we're supporting - * re-adds conceptually. So we probably want this to imply a - * removal, then do an add. We can achieve the removal by freeing - * the template, and leaving t pointed at the current item. - */ - t = last; - p_delete(&t->template); - break; + if (!(rx = rx_compile (pat, REG_ICASE))) { + snprintf (err->data, err->dsize, _("Bad regexp: %s"), pat); + return -1; } - if (!last->next) - break; - } - - /* If t is set, it's pointing into an extant SPAM_LIST* that we want to - * update. Otherwise we want to make a new one to link at the list's end. - */ - if (!t) { - t = mutt_new_spam_list (); - t->rx = rx; - if (last) - last->next = t; - else - *list = t; - } - - /* Now t is the SPAM_LIST* that we want to modify. It is prepared. */ - t->template = m_strdup(templ); - /* Find highest match number in template string */ - t->nmatch = 0; - for (p = templ; *p;) { - if (*p == '%') { - n = atoi (++p); - if (n > t->nmatch) - t->nmatch = n; - while (*p && isdigit ((int) *p)) - ++p; + /* check to make sure the item is not already on this list */ + for (last = list; *last; last = &(*last)->next) { + if (!ascii_strcasecmp(rx->pattern, (*last)->pattern) == 0) { + rx_t *tmp = rx_list_pop(last); + rx_delete(&tmp); + last = rx_list_last(last); + break; + } } - else - ++p; - } - t->nmatch++; /* match 0 is always the whole expr */ - return 0; + *last = rx; + rx_set_template(rx, templ); + return 0; } -static int remove_from_spam_list (SPAM_LIST ** list, const char *pat) +static int remove_from_spam_list (rx_t ** list, const char *pat) { - SPAM_LIST *spam, *prev; int nremoved = 0; - /* Being first is a special case. */ - spam = *list; - if (!spam) - return 0; - if (spam->rx && !m_strcmp(spam->rx->pattern, pat)) { - *list = spam->next; - rx_delete(&spam->rx); - p_delete(&spam->template); - p_delete(&spam); - return 1; - } - - prev = spam; - for (spam = prev->next; spam;) { - if (!m_strcmp(spam->rx->pattern, pat)) { - prev->next = spam->next; - rx_delete(&spam->rx); - p_delete(&spam->template); - p_delete(&spam); - spam = prev->next; - ++nremoved; - } - else - spam = spam->next; + while (*list) { + if (!m_strcmp((*list)->pattern, pat)) { + rx_t *spam = rx_list_pop(list); + rx_delete(&spam); + nremoved++; + } else { + list = &(*list)->next; + } } return nremoved; @@ -983,7 +928,7 @@ static int parse_spam_list (BUFFER * buf, BUFFER * s, unsigned long data, /* "*" is a special case. */ if (!m_strcmp(buf->data, "*")) { - mutt_free_spam_list (&SpamList); + rx_list_wipe(&SpamList); rx_list_wipe(&NoSpamList); return 0; } @@ -1792,12 +1737,12 @@ static int parse_set (BUFFER * tmp, BUFFER * s, unsigned long data, DTYPE (option->type == DT_SYN)) { struct option_t* newopt = hash_find (ConfigOptions, (char*) option->data); - syn_t* tmp = syn_new(); - tmp->f = m_strdup(CurRCFile); - tmp->l = CurRCLine; - tmp->n = newopt; - tmp->o = option; - syn_list_push(&Synonyms, tmp); + syn_t* syn = syn_new(); + syn->f = m_strdup(CurRCFile); + syn->l = CurRCLine; + syn->n = newopt; + syn->o = option; + syn_list_push(&Synonyms, syn); option = newopt; } diff --git a/lib-lib/rx.c b/lib-lib/rx.c index d0a3942..8604ad5 100644 --- a/lib-lib/rx.c +++ b/lib-lib/rx.c @@ -38,11 +38,32 @@ rx_t *rx_compile(const char *s, int flags) return pp; } +void rx_set_template(rx_t *rx, const char *tpl) +{ + const char *p = tpl; + + m_strreplace(&rx->template, tpl); + rx->nmatch = 0; + + while ((p = strchr(p, '%'))) { + if (isdigit(*++p)) { + int n = strtol(p, (char **)&p, 10); + rx->nmatch = MAX(n, rx->nmatch); + } else { + if (*p == '%') + p++; + } + } + + rx->nmatch++; /* match 0 is always the whole expr */ +} + void rx_delete(rx_t **p) { p_delete(&(*p)->pattern); regfree((*p)->rx); p_delete(&(*p)->rx); + p_delete(&(*p)->template); p_delete(p); } diff --git a/lib-lib/rx.h b/lib-lib/rx.h index 14709d1..a827b82 100644 --- a/lib-lib/rx.h +++ b/lib-lib/rx.h @@ -43,9 +43,13 @@ typedef struct rx_t { char *pattern; /* printable version */ regex_t *rx; /* compiled expression */ int not; /* do not match */ + + int nmatch; /* nb matches */ + char *template; /* out template */ } rx_t; -rx_t* rx_compile (const char*, int); +rx_t* rx_compile(const char*, int); +void rx_set_template(rx_t *, const char*); void rx_delete(rx_t **); DO_SLIST(rx_t, rx, rx_delete); diff --git a/mutt.h b/mutt.h index b7ab1b6..7ddd8b1 100644 --- a/mutt.h +++ b/mutt.h @@ -462,17 +462,6 @@ enum { #define toggle_option(x) mutt_bit_toggle(Options,x) #define option(x) mutt_bit_isset(Options,x) -typedef struct spam_list_t { - rx_t *rx; - int nmatch; - char *template; - struct spam_list_t *next; -} SPAM_LIST; - - -#define mutt_new_spam_list() p_new(SPAM_LIST, 1) -void mutt_free_spam_list (SPAM_LIST **); - int mutt_matches_ignore (const char *, string_list_t *); void mutt_init (int, string_list_t *); diff --git a/muttlib.c b/muttlib.c index 81a0788..4b13526 100644 --- a/muttlib.c +++ b/muttlib.c @@ -250,13 +250,12 @@ void mutt_pretty_mailbox (char *s) } } -void mutt_expand_file_fmt(char *dest, ssize_t destlen, - const char *fmt, const char *src) +void +mutt_expand_file_fmt(char *dst, ssize_t n, const char *fmt, const char *src) { char tmp[LONG_STRING]; - mutt_quote_filename(tmp, sizeof(tmp), src); - m_snsubst(dest, destlen, fmt, tmp); + m_snsubst(dst, n, fmt, tmp); } /* return 0 on success, -1 on abort, 1 on error */ @@ -293,9 +292,8 @@ int mutt_check_overwrite (const char *attname, const char *path, } } else - if ((rc = - mutt_yesorno (_("File is a directory, save under it?"), - M_YES)) != M_YES) + if ((rc = mutt_yesorno(_("File is a directory, save under it?"), + M_YES)) != M_YES) return (rc == M_NO) ? 1 : -1; if (!attname || !attname[0]) { @@ -559,16 +557,6 @@ void mutt_FormatString (char *dest, /* output buffer */ } } *wptr = 0; - -#if 0 - if (flags & M_FORMAT_MAKEPRINT) { - /* Make sure that the string is printable by changing all non-printable - chars to dots, or spaces for non-printable whitespace */ - for (cp = dest; *cp; cp++) - if (!isprint(*cp) && !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX))) - *cp = isspace ((unsigned char) *cp) ? ' ' : '.'; - } -#endif } /* returns 0 if OK to proceed, -1 to abort, 1 to retry */ @@ -612,8 +600,7 @@ int mutt_save_confirm (const char *s, struct stat *st) mutt_error (_("%s is not a mailbox!"), s); return 1; } - } - else { + } else { if (magic != M_IMAP) { st->st_mtime = 0; @@ -627,8 +614,7 @@ int mutt_save_confirm (const char *s, struct stat *st) else if (rc == -1) ret = -1; } - } - else { + } else { mutt_perror (s); return 1; } @@ -681,22 +667,7 @@ const char *mutt_make_version (int full) return vstring; } -void mutt_free_spam_list (SPAM_LIST ** list) -{ - SPAM_LIST *p; - - if (!list) - return; - while (*list) { - p = *list; - *list = (*list)->next; - rx_delete(&p->rx); - p_delete(&p->template); - p_delete(&p); - } -} - -int mutt_match_spam_list (const char *s, SPAM_LIST * l, char *text, int x) +int mutt_match_spam_list (const char *s, rx_t * l, char *text, int x) { static regmatch_t *pmatch = NULL; static int nmatch = 0; @@ -716,8 +687,7 @@ int mutt_match_spam_list (const char *s, SPAM_LIST * l, char *text, int x) } /* Does this pattern match? */ - if (regexec(l->rx->rx, s, l->nmatch, (regmatch_t *)pmatch, (int) 0) == 0) - { + if (regexec(l->rx, s, l->nmatch, (regmatch_t *)pmatch, (int) 0) == 0) { /* Copy template into text, with substitutions. */ for (p = l->template; *p;) { if (*p == '%') { diff --git a/protos.h b/protos.h index 5e0a1a3..5b5395d 100644 --- a/protos.h +++ b/protos.h @@ -129,7 +129,7 @@ int mutt_invoke_mta (address_t *, address_t *, address_t *, address_t *, const c int mutt_is_list_cc (int, address_t *, address_t *); int mutt_is_list_recipient (int, address_t *, address_t *); int mutt_lookup_mime_type (BODY *, const char *); -int mutt_match_spam_list (const char *, SPAM_LIST *, char *, int); +int mutt_match_spam_list (const char *, rx_t *, char *, int); int mutt_num_postponed (int); int mutt_parse_bind (BUFFER *, BUFFER *, unsigned long, BUFFER *); int mutt_parse_exec (BUFFER *, BUFFER *, unsigned long, BUFFER *);