Debian Codegraph @

lualib/lua_verdict.lua

raw
1 --[[
2 Copyright (c) 2022, Vsevolod Stakhov <[email protected]>
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 ]]--
16
17 local exports = {}
18
19 ---[[[
20 -- @function lua_verdict.get_default_verdict(task)
21 -- Returns verdict for a task + score if certain, must be called from idempotent filters only
22 -- Returns string:
23 -- * `spam`: if message have over reject threshold and has more than one positive rule
24 -- * `junk`: if a message has between score between [add_header/rewrite subject] to reject thresholds and has more than two positive rules
25 -- * `passthrough`: if a message has been passed through some short-circuit rule
26 -- * `ham`: if a message has overall score below junk level **and** more than three negative rule, or negative total score
27 -- * `uncertain`: all other cases
28 --]]
29 local function default_verdict_function(task)
30 local result = task:get_metric_result()
31
32 if result then
33
34 if result.passthrough then
35 return 'passthrough', nil
36 end
37
38 local score = result.score
39
40 local action = result.action
41
42 if action == 'reject' and result.npositive > 1 then
43 return 'spam', score
44 elseif action == 'no action' then
45 if score < 0 or result.nnegative > 3 then
46 return 'ham', score
47 end
48 else
49 -- All colors of junk
50 if action == 'add header' or action == 'rewrite subject' then
51 if result.npositive > 2 then
52 return 'junk', score
53 end
54 end
55 end
56
57 return 'uncertain', score
58 end
59 end
60
61 local default_possible_verdicts = {
62 passthrough = {
63 can_learn = false,
64 description = 'message has passthrough result',
65 },
66 spam = {
67 can_learn = 'spam',
68 description = 'message is likely spam',
69 },
70 junk = {
71 can_learn = 'spam',
72 description = 'message is likely possible spam',
73 },
74 ham = {
75 can_learn = 'ham',
76 description = 'message is likely ham',
77 },
78 uncertain = {
79 can_learn = false,
80 description = 'not certainty in verdict'
81 }
82 }
83
84 -- Verdict functions specific for modules
85 local specific_verdicts = {
86 default = {
87 callback = default_verdict_function,
88 possible_verdicts = default_possible_verdicts
89 }
90 }
91
92 local default_verdict = specific_verdicts.default
93
94 exports.get_default_verdict = default_verdict.callback
95 exports.set_verdict_function = function(func, what)
96 assert(type(func) == 'function')
97 if not what then
98 -- Default verdict
99 local existing = specific_verdicts.default.callback
100 specific_verdicts.default.callback = func
101 exports.get_default_verdict = func
102
103 return existing
104 else
105 local existing = specific_verdicts[what]
106
107 if not existing then
108 specific_verdicts[what] = {
109 callback = func,
110 possible_verdicts = default_possible_verdicts
111 }
112 else
113 existing = existing.callback
114 end
115
116 specific_verdicts[what].callback = func
117 return existing
118 end
119 end
120
121 exports.set_verdict_table = function(verdict_tbl, what)
122 assert(type(verdict_tbl) == 'table' and
123 type(verdict_tbl.callback) == 'function' and
124 type(verdict_tbl.possible_verdicts) == 'table')
125
126 if not what then
127 -- Default verdict
128 local existing = specific_verdicts.default
129 specific_verdicts.default = verdict_tbl
130 exports.get_default_verdict = specific_verdicts.default.callback
131
132 return existing
133 else
134 local existing = specific_verdicts[what]
135 specific_verdicts[what] = verdict_tbl
136 return existing
137 end
138 end
139
140 exports.get_specific_verdict = function(what, task)
141 if specific_verdicts[what] then
142 return specific_verdicts[what].callback(task)
143 end
144
145 return exports.get_default_verdict(task)
146 end
147
148 exports.get_possible_verdicts = function(what)
149 local lua_util = require "lua_util"
150 if what then
151 if specific_verdicts[what] then
152 return lua_util.keys(specific_verdicts[what].possible_verdicts)
153 end
154 else
155 return lua_util.keys(specific_verdicts.default.possible_verdicts)
156 end
157
158 return nil
159 end
160
161 exports.can_learn = function(verdict, what)
162 if what then
163 if specific_verdicts[what] and specific_verdicts[what].possible_verdicts[verdict] then
164 return specific_verdicts[what].possible_verdicts[verdict].can_learn
165 end
166 else
167 if specific_verdicts.default.possible_verdicts[verdict] then
168 return specific_verdicts.default.possible_verdicts[verdict].can_learn
169 end
170 end
171
172 return nil -- To distinguish from `false` that could happen in can_learn
173 end
174
175 exports.describe = function(verdict, what)
176 if what then
177 if specific_verdicts[what] and specific_verdicts[what].possible_verdicts[verdict] then
178 return specific_verdicts[what].possible_verdicts[verdict].description
179 end
180 else
181 if specific_verdicts.default.possible_verdicts[verdict] then
182 return specific_verdicts.default.possible_verdicts[verdict].description
183 end
184 end
185
186 return nil
187 end
188
189 return exports