mutt_enter_string is only used for _mutt_get_field for real.
[apps/madmutt.git] / score.cpkg
index 40adf78..1495ed8 100644 (file)
 #include "score.h"
 @import  "lib-lua/base.cpkg"
 
+typedef struct score_t {
+    char *str;
+    pattern_t *pat;
+    int val;
+    int exact;                    /* if this rule matches, don't evaluate any more */
+    struct score_t *next;
+} 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);
+
+static score_t *Score = NULL;
+
 @package mod_score {
     bool enable = 1;
     /*
      ** Madmutt scores are always greater than or equal to zero, the default setting
      ** of this variable will never mark a message read.
      */
-};
-
-typedef struct score_t {
-  char *str;
-  pattern_t *pat;
-  int val;
-  int exact;                    /* if this rule matches, don't evaluate any more */
-  struct score_t *next;
-} 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);
-
-static score_t *Score = NULL;
 
-void mutt_check_rescore (CONTEXT * ctx)
-{
-  int i;
-
-  if (option (OPTNEEDRESCORE) && mod_score.enable) {
-    if ((Sort & SORT_MASK) == SORT_SCORE ||
-        (SortAux & SORT_MASK) == SORT_SCORE) {
-      set_option (OPTNEEDRESORT);
-      if ((Sort & SORT_MASK) == SORT_THREADS)
-        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++) {
-      mutt_score_message (ctx, ctx->hdrs[i], 1);
-      ctx->hdrs[i]->pair = 0;
-    }
-  }
-  unset_option (OPTNEEDRESCORE);
-}
+    void score(string_t pattern, int value) {
+        const char *pc;
+        score_t **last;
 
-int mutt_parse_score (BUFFER * buf, BUFFER * s,
-                      unsigned long data __attribute__ ((unused)),
-                      BUFFER * err)
-{
-  char *pattern, *pc;
-  struct pattern_t *pat;
-  score_t **last;
-
-  mutt_extract_token (buf, s, 0);
-  if (!MoreArgs (s)) {
-    m_strcpy(err->data, err->dsize, _("score: too few arguments"));
-    return (-1);
-  }
-  pattern = buf->data;
-  p_clear(buf, 1);
-  mutt_extract_token (buf, s, 0);
-  if (MoreArgs (s)) {
-    p_delete(&pattern);
-    m_strcpy(err->data, err->dsize, _("score: too many arguments"));
-    return (-1);
-  }
+        for (last = &Score; *last; last = &(*last)->next) {
+            if (!m_strcmp(pattern, (*last)->str))
+                break;
+        }
+        if (!*last) {
+            /* FIXME need a buffer for error */
+            struct pattern_t *pat = mutt_pattern_comp(pattern, 0, NULL);
 
-  /* look for an existing entry and update the value, else add it to the end
-     of the list */
-  for (last = &Score; *last; last = &(*last)->next) {
-    if (m_strcmp(pattern, (*last)->str) == 0)
-      break;
-  }
-  if (!*last) {
-    if (!(pat = mutt_pattern_comp(pattern, 0, err))) {
-      p_delete(&pattern);
-      return (-1);
-    }
-    *last = score_new();
-    (*last)->pat = pat;
-    (*last)->str = pattern;
-  }
-  pc = buf->data;
-  pc += (*last)->exact = (*pc == '=');
-  (*last)->val = atoi(pc);
-  set_option(OPTNEEDRESCORE);
-  return 0;
-}
+            if (!pat) {
+                p_delete(&pattern);
+                RETURN();
+            }
+            *last = score_new();
+            (*last)->pat = pat;
+            (*last)->str = pattern;
+        }
+        pc  = pattern;
+        pc += (*last)->exact = (*pc == '=');
+        (*last)->val = value;
+        set_option(OPTNEEDRESCORE);
+        RETURN();
+    };
+
+    void unscore(const string_t pattern) {
+        if (!m_strcmp(pattern, "*")) {
+            score_list_wipe(&Score);
+            set_option(OPTNEEDRESCORE);
+            RETURN();
+        }
+        for (score_t **last = &Score; *last; last = &(*last)->next) {
+            if (!m_strcmp(pattern, (*last)->str)) {
+                score_t *tmp = score_list_pop(last);
+                score_delete(&tmp);
+                set_option(OPTNEEDRESCORE);
+                break;
+            }
+        }
+        RETURN();
+    };
+};
 
 void mutt_score_message (CONTEXT * ctx, HEADER * hdr, int upd_ctx)
 {
-  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;
+    hdr->score = 0;               /* in case of re-scoring */
+    for (score_t *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 <= mod_score.threshold_delete)
-    _mutt_set_flag (ctx, hdr, M_DELETE, 1, upd_ctx);
-  if (hdr->score <= mod_score.threshold_flag)
-    _mutt_set_flag (ctx, hdr, M_READ, 1, upd_ctx);
-  if (hdr->score >= mod_score.threshold_flag)
-    _mutt_set_flag (ctx, hdr, M_FLAG, 1, upd_ctx);
+    if (hdr->score < 0)
+        hdr->score = 0;
+
+    if (hdr->score <= mod_score.threshold_delete)
+        _mutt_set_flag(ctx, hdr, M_DELETE, 1, upd_ctx);
+    if (hdr->score <= mod_score.threshold_read)
+        _mutt_set_flag(ctx, hdr, M_READ, 1, upd_ctx);
+    if (hdr->score >= mod_score.threshold_flag)
+        _mutt_set_flag(ctx, hdr, M_FLAG, 1, upd_ctx);
 }
 
-int mutt_parse_unscore(BUFFER * buf, BUFFER * s,
-                       unsigned long data __attribute__ ((unused)),
-                       BUFFER * err __attribute__ ((unused)))
+void mutt_check_rescore (CONTEXT * ctx)
 {
-    while (MoreArgs(s)) {
-        mutt_extract_token(buf, s, 0);
-        if (!m_strcmp("*", buf->data)) {
-            score_list_wipe(&Score);
-        } else {
-            score_t **last;
+    if (option(OPTNEEDRESCORE) && mod_score.enable) {
+        if (((Sort | SortAux) & SORT_MASK) == SORT_SCORE) {
+            set_option(OPTNEEDRESORT);
+            if ((Sort & SORT_MASK) == SORT_THREADS)
+                set_option(OPTSORTSUBTHREADS);
+        }
 
-            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;
-                }
-            }
+        /* must redraw the index since the user might have %N in it */
+        set_option(OPTFORCEREDRAWINDEX);
+        set_option(OPTFORCEREDRAWPAGER);
+
+        for (int i = 0; ctx && i < ctx->msgcount; i++) {
+            mutt_score_message(ctx, ctx->hdrs[i], 1);
+            ctx->hdrs[i]->pair = 0;
         }
     }
-    set_option (OPTNEEDRESCORE);
-    return 0;
+    unset_option(OPTNEEDRESCORE);
 }
 
 /* vim:set ft=c: */