summaryrefslogtreecommitdiffstats
path: root/lib/smtpapi_headers.js
blob: 4bd8a1506a1739f3a5a61a942b99bb9833ad0567 (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
var _ = require('underscore');

function SmtpapiHeaders() {
  this.to = [];
  this.sub = {};
  this.unique_args = {};
  this.category = [];
  this.filters = {};
}

SmtpapiHeaders.prototype.addTo = function(to) {
  if (_.isArray(to)) {
    this.to = this.to.concat(to);
  } else {
    this.to.push(to);
  }
}

SmtpapiHeaders.prototype.addSubVal = function(key, val) {
  if (_.isArray(val)) {
    this.sub[key] = val;
  } else {
    this.sub[key] = [val];
  }
}

SmtpapiHeaders.prototype.setUniqueArgs = function(val) {
  if (_.isObject(val)) {
    this.unique_args = val;
  }
}

SmtpapiHeaders.prototype.addUniqueArgs = function(val) {
  if (_.isObject(val)) {
    _.extend(this.unique_args, val);
  }
}

SmtpapiHeaders.prototype.setCategory = function(val) {
  this.category = val;
}

SmtpapiHeaders.prototype.addCategory = function(val) {
  if (_.isArray(val)) {
    this.category.concat(val);
  } else {
    this.category.push(val);
  }
}

SmtpapiHeaders.prototype.setFilterSetting = function(filters) {
  this.filters = filters;
}

SmtpapiHeaders.prototype.addFilterSetting = function(filter, setting, val) {
  if (!this.filters[filter]) {
    this.filters[filter] = {};
  }

  if (!this.filters[filter]['settings']) {
    this.filters[filter]['settings'] = {};
  }

  this.filters[filter]['settings'][setting] = val;
}

SmtpapiHeaders.prototype.toJson = function() {
  var json = JSON.stringify(this);
  return json.replace(/(["\]}])([,:])(["\[{])/, '$1$2 $3');
}

module.exports = SmtpapiHeaders;