score.c 4.84 KB
Newer Older
1
/*
2
 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 *     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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */ 

#if HAVE_CONFIG_H
# include "config.h"
#endif
22 23

#include "mutt.h"
24
#include "mutt_menu.h"
25
#include "sort.h"
26 27
#include <string.h>
#include <stdlib.h>
28

29
typedef struct score_t
30 31
{
  char *str;
32
  pattern_t *pat;
33
  int val;
34 35 36
  int exact;		/* if this rule matches, don't evaluate any more */
  struct score_t *next;
} SCORE;
37

38
static SCORE *Score = NULL;
39

40
void mutt_check_rescore (CONTEXT *ctx)
41 42 43
{
  int i;

44
  if (option (OPTNEEDRESCORE) && option (OPTSCORE))
45
  {
46 47
    if ((Sort & SORT_MASK) == SORT_SCORE ||
	(SortAux & SORT_MASK) == SORT_SCORE)
48
    {
49
      set_option (OPTNEEDRESORT);
50
      if ((Sort & SORT_MASK) == SORT_THREADS)
51
	set_option (OPTSORTSUBTHREADS);
52 53 54
    }

    /* must redraw the index since the user might have %N in it */
55 56
    mutt_set_menu_redraw_full (MENU_MAIN);
    mutt_set_menu_redraw_full (MENU_PAGER);
57 58 59

    for (i = 0; ctx && i < ctx->msgcount; i++)
    {
60
      mutt_score_message (ctx, ctx->hdrs[i], 1);
61 62 63
      ctx->hdrs[i]->pair = 0;
    }
  }
64
  unset_option (OPTNEEDRESCORE);
65 66
}

67
int mutt_parse_score (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
68
{
69 70 71
  SCORE *ptr, *last;
  char *pattern, *pc;
  struct pattern_t *pat;
72

73 74
  mutt_extract_token (buf, s, 0);
  if (!MoreArgs (s))
75
  {
76 77
    strfcpy (err->data, _("score: too few arguments"), err->dsize);
    return (-1);
78 79
  }
  pattern = buf->data;
80 81 82
  mutt_buffer_init (buf);
  mutt_extract_token (buf, s, 0);
  if (MoreArgs (s))
83
  {
84 85 86
    FREE (&pattern);
    strfcpy (err->data, _("score: too many arguments"), err->dsize);
    return (-1);
87 88 89 90 91
  }

  /* 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)
92
    if (mutt_strcmp (pattern, ptr->str) == 0)
93 94 95
      break;
  if (!ptr)
  {
96
    if ((pat = mutt_pattern_comp (pattern, 0, err)) == NULL)
97
    {
98 99
      FREE (&pattern);
      return (-1);
100
    }
101
    ptr = safe_calloc (1, sizeof (SCORE));
102 103 104 105 106 107
    if (last)
      last->next = ptr;
    else
      Score = ptr;
    ptr->pat = pat;
    ptr->str = pattern;
108
  } else
109 110 111 112
    /* 'buf' arg was cleared and 'pattern' holds the only reference;
     * as here 'ptr' != NULL -> update the value only in which case
     * ptr->str already has the string, so pattern should be freed.
     */
113
    FREE (&pattern);
114 115 116 117 118 119
  pc = buf->data;
  if (*pc == '=')
  {
    ptr->exact = 1;
    pc++;
  }
120
  if (mutt_atoi (pc, &ptr->val) < 0)
121
  {
122 123 124
    FREE (&pattern);
    strfcpy (err->data, _("Error: score: invalid number"), err->dsize);
    return (-1);
125
  }
126
  set_option (OPTNEEDRESCORE);
127 128 129
  return 0;
}

130
void mutt_score_message (CONTEXT *ctx, HEADER *hdr, int upd_ctx)
131
{
132 133
  SCORE *tmp;
  pattern_cache_t cache;
134

135
  memset (&cache, 0, sizeof (cache));
136 137 138
  hdr->score = 0; /* in case of re-scoring */
  for (tmp = Score; tmp; tmp = tmp->next)
  {
139
    if (mutt_pattern_exec (tmp->pat, MUTT_MATCH_FULL_ADDRESS, NULL, hdr, &cache) > 0)
140 141 142
    {
      if (tmp->exact || tmp->val == 9999 || tmp->val == -9999)
      {
143 144
	hdr->score = tmp->val;
	break;
145 146 147 148 149 150
      }
      hdr->score += tmp->val;
    }
  }
  if (hdr->score < 0)
    hdr->score = 0;
151
  
152
  if (hdr->score <= ScoreThresholdDelete)
153
    _mutt_set_flag (ctx, hdr, MUTT_DELETE, 1, upd_ctx);
154
  if (hdr->score <= ScoreThresholdRead)
155
    _mutt_set_flag (ctx, hdr, MUTT_READ, 1, upd_ctx);
156
  if (hdr->score >= ScoreThresholdFlag)
157
    _mutt_set_flag (ctx, hdr, MUTT_FLAG, 1, upd_ctx);
158 159
}

160
int mutt_parse_unscore (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
161
{
162
  SCORE *tmp, *last = NULL;
163

164
  while (MoreArgs (s))
165
  {
166 167
    mutt_extract_token (buf, s, 0);
    if (!mutt_strcmp ("*", buf->data))
168
    {
169
      for (tmp = Score; tmp; )
170
      {
171 172 173 174
	last = tmp;
	tmp = tmp->next;
	mutt_pattern_free (&last->pat);
	FREE (&last);
175 176 177 178 179 180 181
      }
      Score = NULL;
    }
    else
    {
      for (tmp = Score; tmp; last = tmp, tmp = tmp->next)
      {
182 183 184 185 186 187 188 189 190 191 192
	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;
	}
193 194 195
      }
    }
  }
196
  set_option (OPTNEEDRESCORE);
197 198
  return 0;
}