-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
259 lines (214 loc) · 6.45 KB
/
index.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// results - programmatic handling of plugin results
'use strict'
const config = require('haraka-config')
const util = require('util')
// see docs in docs/Results.md
const append_lists = ['msg', 'pass', 'fail', 'skip', 'err']
const overwrite_lists = ['hide', 'order']
const log_opts = ['emit', 'human', 'human_html']
const all_opts = append_lists.concat(overwrite_lists, log_opts)
let cfg
class ResultStore {
constructor(conn) {
this.conn = conn
this.store = {}
cfg = config.get('results.ini', {
booleans: ['+main.redis_publish'],
})
}
has(plugin, list, search) {
const name = this.resolve_plugin_name(plugin)
const result = this.store[name]
if (!result || !result[list]) return false
if (typeof result[list] === 'string') {
return this._has_string(result[list], search)
}
if (Array.isArray(result[list])) {
return this._has_array(result[list], search)
}
return false
}
_has_string(msg, search) {
if (typeof search === 'string' && search === msg) return true
if (typeof search === 'object' && msg.match(search)) return true
return false
}
_has_array(msg, search) {
for (const item of msg) {
switch (typeof search) {
case 'string':
case 'number':
case 'boolean':
if (search === item) return true
break
case 'object':
if (item.match(search)) return true
break
}
}
return false
}
redis_publish(name, obj) {
if (!cfg.main.redis_publish) return
if (!this.conn.server?.notes?.redis) return
const channel = `result-${this.conn.transaction ? this.conn.transaction.uuid : this.conn.uuid}`
this.conn.server.notes.redis.publish(
channel,
JSON.stringify({ plugin: name, result: obj }),
)
}
add(plugin, obj) {
const name = this.resolve_plugin_name(plugin)
let result = this.store[name]
if (!result) {
result = default_result()
this.store[name] = result
}
this.redis_publish(name, obj)
// these are arrays each invocation appends to
for (const key of append_lists) {
if (!obj[key]) continue
result[key] = this._append_to_array(result[key], obj[key])
}
// these arrays are overwritten when passed
for (const key of overwrite_lists) {
if (!obj[key]) continue
result[key] = obj[key]
}
// anything else is an arbitrary key/val to store
for (const key in obj) {
if (all_opts.includes(key)) continue // weed out our keys
if (obj[key] === undefined) continue // ignore keys w/undef value
result[key] = obj[key] // save the rest
}
return this._log(plugin, result, obj)
}
_append_to_array(array, item) {
if (Array.isArray(item)) return array.concat(item)
array.push(item)
return array
}
incr(plugin, obj) {
const name = this.resolve_plugin_name(plugin)
let result = this.store[name]
if (!result) {
result = default_result()
this.store[name] = result
}
const pub = {}
for (const key in obj) {
let val = parseFloat(obj[key]) || 0
if (isNaN(val)) val = 0
if (isNaN(result[key])) result[key] = 0
result[key] = parseFloat(result[key]) + parseFloat(val)
pub[key] = result[key]
}
this.redis_publish(name, pub)
}
push(plugin, obj) {
const name = this.resolve_plugin_name(plugin)
let result = this.store[name]
if (!result) {
result = default_result()
this.store[name] = result
}
this.redis_publish(name, obj)
for (const key in obj) {
if (!result[key]) result[key] = []
result[key] = this._append_to_array(result[key], obj[key])
}
return this._log(plugin, result, obj)
}
collate(plugin) {
const name = this.resolve_plugin_name(plugin)
const result = this.store[name]
if (!result) return
return this.private_collate(result, name).join(', ')
}
get(plugin_or_name) {
return this.store[this.resolve_plugin_name(plugin_or_name)]
}
resolve_plugin_name(thing) {
if (typeof thing === 'string') return thing
if (typeof thing === 'object' && thing.name) return thing.name
return
}
get_all() {
return this.store
}
private_collate(result, name) {
const r = []
const order = this._get_order(cfg[name])
const hide = this._get_hide(cfg[name])
// anything not predefined in the result was purposeful, show it first
for (const key in result) {
if (!this._pre_defined(key, result[key], hide)) continue
r.push(`${key}: ${result[key]}`)
}
// and then supporting information
let array = append_lists // default
if (order && order.length) array = order // config file
if (result.order && result.order.length) array = result.order // caller
for (const key of array) {
if (!result[key]) continue
if (!result[key].length) continue
if (hide && hide.length && hide.indexOf(key) !== -1) continue
r.push(`${key}:${result[key].join(', ')}`)
}
return r
}
_pre_defined(key, res, hide) {
if (key[0] === '_') return false // 'private' keys
if (all_opts.indexOf(key) !== -1) return false // these get shown later.
if (hide.length && hide.indexOf(key) !== -1) return false
if (typeof res === 'object') {
if (Array.isArray(res)) {
if (res.length === 0) return false
} else {
return false
}
}
return true
}
_get_order(c) {
if (!c || !c.order) return []
return c.order.trim().split(/[,; ]+/)
}
_get_hide(c) {
if (!c || !c.hide) return []
return c.hide
.trim()
.trim()
.split(/[,; ]+/)
}
_log(plugin, result, obj) {
const name = plugin.name
// collate results
result.human = obj.human
if (!result.human) {
const r = this.private_collate(result, name)
result.human = r.join(', ')
result.human_html = r.join(', \t ')
}
// logging results
if (obj.emit) this.conn.loginfo(plugin, result.human) // by request
if (obj.err) {
// Handle error objects by logging the message
if (util.isError(obj.err)) {
this.conn.logerror(plugin, obj.err.message)
} else {
this.conn.logerror(plugin, obj.err)
}
}
if (!obj.emit && !obj.err) {
// by config
const pic = cfg[name]
if (pic && pic.debug) this.conn.logdebug(plugin, result.human)
}
return this.human
}
}
function default_result() {
return { pass: [], fail: [], msg: [], err: [], skip: [] }
}
module.exports = ResultStore