X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=score.c;h=7bf381192fd26dc663fdcfaa4f16244682f8afea;hp=a29e35713ac79a32feb5403ada0982d366c526e5;hb=366c938a78d928880c77bfe31a8715184c918d41;hpb=6833ce8bdca2d64e14485118f2a4417b7e1cb1b1 diff --git a/score.c b/score.c index a29e357..7bf3811 100644 --- a/score.c +++ b/score.c @@ -1,57 +1,54 @@ /* + * Copyright notice from original mutt: * Copyright (C) 1996-2000 Michael R. Elkins - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. - */ + * + * This file is part of mutt-ng, see http://www.muttng.org/. + * It's licensed under the GNU General Public License, + * please see the file GPL in the top level source directory. + */ + +#include #include "mutt.h" #include "sort.h" -#include -#include +#include "pattern.h" -typedef struct score_t -{ +typedef struct score_t { char *str; pattern_t *pat; int val; - int exact; /* if this rule matches, don't evaluate any more */ + int exact; /* if this rule matches, don't evaluate any more */ struct score_t *next; -} SCORE; +} score_t; +DO_INIT(score_t, score); +static void score_wipe(score_t *sc) +{ + pattern_list_wipe(&sc->pat); + p_delete(&sc->str); +} +DO_NEW(score_t, score); +DO_DELETE(score_t, score); +DO_SLIST(score_t, score, score_delete); -SCORE *Score = NULL; +static score_t *Score = NULL; -void mutt_check_rescore (CONTEXT *ctx) +void mutt_check_rescore (CONTEXT * ctx) { int i; - if (option (OPTNEEDRESCORE) && option (OPTSCORE)) - { + if (option (OPTNEEDRESCORE) && option (OPTSCORE)) { if ((Sort & SORT_MASK) == SORT_SCORE || - (SortAux & SORT_MASK) == SORT_SCORE) - { + (SortAux & SORT_MASK) == SORT_SCORE) { set_option (OPTNEEDRESORT); if ((Sort & SORT_MASK) == SORT_THREADS) - set_option (OPTSORTSUBTHREADS); + set_option (OPTSORTSUBTHREADS); } /* must redraw the index since the user might have %N in it */ set_option (OPTFORCEREDRAWINDEX); set_option (OPTFORCEREDRAWPAGER); - for (i = 0; ctx && i < ctx->msgcount; i++) - { + for (i = 0; ctx && i < ctx->msgcount; i++) { mutt_score_message (ctx, ctx->hdrs[i], 1); ctx->hdrs[i]->pair = 0; } @@ -59,79 +56,67 @@ void mutt_check_rescore (CONTEXT *ctx) unset_option (OPTNEEDRESCORE); } -int mutt_parse_score (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err) +int mutt_parse_score (BUFFER * buf, BUFFER * s, + unsigned long data __attribute__ ((unused)), + BUFFER * err) { - SCORE *ptr, *last; char *pattern, *pc; struct pattern_t *pat; + score_t **last; mutt_extract_token (buf, s, 0); - if (!MoreArgs (s)) - { - strfcpy (err->data, _("score: too few arguments"), err->dsize); + if (!MoreArgs (s)) { + m_strcpy(err->data, err->dsize, _("score: too few arguments")); return (-1); } pattern = buf->data; - memset (buf, 0, sizeof (BUFFER)); + p_clear(buf, 1); mutt_extract_token (buf, s, 0); - if (MoreArgs (s)) - { - FREE (&pattern); - strfcpy (err->data, _("score: too many arguments"), err->dsize); + if (MoreArgs (s)) { + p_delete(&pattern); + m_strcpy(err->data, err->dsize, _("score: too many arguments")); return (-1); } /* look for an existing entry and update the value, else add it to the end of the list */ - for (ptr = Score, last = NULL; ptr; last = ptr, ptr = ptr->next) - if (mutt_strcmp (pattern, ptr->str) == 0) + for (last = &Score; *last; last = &(*last)->next) { + if (m_strcmp(pattern, (*last)->str) == 0) break; - if (!ptr) - { - if ((pat = mutt_pattern_comp (pattern, 0, err)) == NULL) - { - FREE (&pattern); + } + if (!*last) { + if (!(pat = mutt_pattern_comp(pattern, 0, err))) { + p_delete(&pattern); return (-1); } - ptr = safe_calloc (1, sizeof (SCORE)); - if (last) - last->next = ptr; - else - Score = ptr; - ptr->pat = pat; - ptr->str = pattern; + *last = score_new(); + (*last)->pat = pat; + (*last)->str = pattern; } pc = buf->data; - if (*pc == '=') - { - ptr->exact = 1; - pc++; - } - ptr->val = atoi (pc); - set_option (OPTNEEDRESCORE); + pc += (*last)->exact = (*pc == '='); + (*last)->val = atoi(pc); + set_option(OPTNEEDRESCORE); return 0; } -void mutt_score_message (CONTEXT *ctx, HEADER *hdr, int upd_ctx) +void mutt_score_message (CONTEXT * ctx, HEADER * hdr, int upd_ctx) { - SCORE *tmp; - - hdr->score = 0; /* in case of re-scoring */ - for (tmp = Score; tmp; tmp = tmp->next) - { - if (mutt_pattern_exec (tmp->pat, 0, NULL, hdr) > 0) - { - if (tmp->exact || tmp->val == 9999 || tmp->val == -9999) - { - hdr->score = tmp->val; - break; + score_t *tmp; + + hdr->score = 0; /* in case of re-scoring */ + for (tmp = Score; tmp; tmp = tmp->next) { + if (mutt_pattern_exec (tmp->pat, M_MATCH_FULL_ADDRESS, NULL, hdr) > 0) { + if (tmp->exact || tmp->val == 9999 || tmp->val == -9999) { + hdr->score = tmp->val; + break; } hdr->score += tmp->val; } } if (hdr->score < 0) hdr->score = 0; - + if (hdr->score <= ScoreThresholdDelete) _mutt_set_flag (ctx, hdr, M_DELETE, 1, upd_ctx); if (hdr->score <= ScoreThresholdRead) @@ -140,42 +125,26 @@ void mutt_score_message (CONTEXT *ctx, HEADER *hdr, int upd_ctx) _mutt_set_flag (ctx, hdr, M_FLAG, 1, upd_ctx); } -int mutt_parse_unscore (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err) +int mutt_parse_unscore(BUFFER * buf, BUFFER * s, + unsigned long data __attribute__ ((unused)), + BUFFER * err __attribute__ ((unused))) { - SCORE *tmp, *last = NULL; - - while (MoreArgs (s)) - { - mutt_extract_token (buf, s, 0); - if (!mutt_strcmp ("*", buf->data)) - { - for (tmp = Score; tmp; ) - { - last = tmp; - tmp = tmp->next; - mutt_pattern_free (&last->pat); - FREE (&last); - } - Score = NULL; - } - else - { - for (tmp = Score; tmp; last = tmp, tmp = tmp->next) - { - if (!mutt_strcmp (buf->data, tmp->str)) - { - if (last) - last->next = tmp->next; - else - Score = tmp->next; - mutt_pattern_free (&tmp->pat); - FREE (&tmp); - /* there should only be one score per pattern, so we can stop here */ - break; - } - } + while (MoreArgs(s)) { + mutt_extract_token(buf, s, 0); + if (!m_strcmp("*", buf->data)) { + score_list_wipe(&Score); + } else { + score_t **last; + + for (last = &Score; *last; last = &(*last)->next) { + if (!m_strcmp(buf->data, (*last)->str)) { + score_t *tmp = score_list_pop(last); + score_delete(&tmp); + break; + } + } + } } - } - set_option (OPTNEEDRESCORE); - return 0; + set_option (OPTNEEDRESCORE); + return 0; }