summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/test/resources/filterTestUtils.js
blob: c34d1147c0adad9a80635df5b3fd03e8e04aa340 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0/. */

// Utility functions for testing interactions with filters.

var contains = Ci.nsMsgSearchOp.Contains;
// This maps strings to a filter attribute (excluding the parameter)
var ATTRIB_MAP = {
  // Template : [attrib, op, field of value, otherHeader]
  subject: [Ci.nsMsgSearchAttrib.Subject, contains, "str", null],
  from: [Ci.nsMsgSearchAttrib.Sender, contains, "str", null],
  date: [Ci.nsMsgSearchAttrib.Date, Ci.nsMsgSearchOp.Is, "date", null],
  size: [Ci.nsMsgSearchAttrib.Size, Ci.nsMsgSearchOp.Is, "size", null],
  "message-id": [
    Ci.nsMsgSearchAttrib.OtherHeader + 1,
    contains,
    "str",
    "Message-ID",
  ],
  "user-agent": [
    Ci.nsMsgSearchAttrib.OtherHeader + 2,
    contains,
    "str",
    "User-Agent",
  ],
};
// And this maps strings to filter actions
var ACTION_MAP = {
  // Template : [action, auxiliary attribute field, auxiliary value]
  priority: [Ci.nsMsgFilterAction.ChangePriority, "priority", 6],
  delete: [Ci.nsMsgFilterAction.Delete],
  read: [Ci.nsMsgFilterAction.MarkRead],
  unread: [Ci.nsMsgFilterAction.MarkUnread],
  kill: [Ci.nsMsgFilterAction.KillThread],
  watch: [Ci.nsMsgFilterAction.WatchThread],
  flag: [Ci.nsMsgFilterAction.MarkFlagged],
  stop: [Ci.nsMsgFilterAction.StopExecution],
  tag: [Ci.nsMsgFilterAction.AddTag, "strValue", "tag"],
};

/**
 * Creates a filter and appends it to the nsIMsgFilterList.
 *
 * @param {nsIMsgFilter} list - An nsIMsgFilter to which the new filter will be appended.
 * @param {string} trigger - A key of ATTRIB_MAP that represents the filter trigger.
 * @param {string} value - The value of the filter trigger.
 * @param {nsMsgFilterAction} action - A key of ACTION_MAP that represents the action to be taken.
 */
function createFilter(list, trigger, value, action) {
  var filter = list.createFilter(trigger + action + "Test");
  filter.filterType = Ci.nsMsgFilterType.NewsRule;

  var searchTerm = filter.createTerm();
  searchTerm.matchAll = false;
  if (trigger in ATTRIB_MAP) {
    let information = ATTRIB_MAP[trigger];
    searchTerm.attrib = information[0];
    if (information[3] != null) {
      searchTerm.arbitraryHeader = information[3];
    }
    searchTerm.op = information[1];
    var oldValue = searchTerm.value;
    oldValue.attrib = information[0];
    oldValue[information[2]] = value;
    searchTerm.value = oldValue;
  } else {
    throw new Error("Unknown trigger " + trigger);
  }
  searchTerm.booleanAnd = true;
  filter.appendTerm(searchTerm);

  var filterAction = filter.createAction();
  if (action in ACTION_MAP) {
    let information = ACTION_MAP[action];
    filterAction.type = information[0];
    if (1 in information) {
      filterAction[information[1]] = information[2];
    }
  } else {
    throw new Error("Unknown action " + action);
  }
  filter.appendAction(filterAction);

  filter.enabled = true;

  // Add to the end
  list.insertFilterAt(list.filterCount, filter);
}