Debian Codegraph @

lualib/rspamadm/grep.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 argparse = require "argparse"
18
19
20 -- Define command line options
21 local parser = argparse()
22 :name "rspamadm grep"
23 :description "Search for patterns in rspamd logs"
24 :help_description_margin(30)
25 parser:mutex(
26 parser:option "-s --string"
27 :description('Plain string to search (case-insensitive)')
28 :argname "<str>",
29 parser:option "-p --pattern"
30 :description('Pattern to search for (regex)')
31 :argname "<re>"
32 )
33 parser:flag "-l --lua"
34 :description('Use Lua patterns in string search')
35
36 parser:argument "input":args "*"
37 :description('Process specified inputs')
38 :default("stdin")
39 parser:flag "-S --sensitive"
40 :description('Enable case-sensitivity in string search')
41 parser:flag "-o --orphans"
42 :description('Print orphaned logs')
43 parser:flag "-P --partial"
44 :description('Print partial logs')
45
46 local function handler(args)
47
48 local rspamd_regexp = require 'rspamd_regexp'
49 local res = parser:parse(args)
50
51 if not res['string'] and not res['pattern'] then
52 parser:error('string or pattern options must be specified')
53 end
54
55 if res['string'] and res['pattern'] then
56 parser:error('string and pattern options are mutually exclusive')
57 end
58
59 local buffer = {}
60 local matches = {}
61
62 local pattern = res['pattern']
63 local re
64 if pattern then
65 re = rspamd_regexp.create(pattern)
66 if not re then
67 io.stderr:write("Couldn't compile regex: " .. pattern .. '\n')
68 os.exit(1)
69 end
70 end
71
72 local plainm = true
73 if res['lua'] then
74 plainm = false
75 end
76 local orphans = res['orphans']
77 local search_str = res['string']
78 local sensitive = res['sensitive']
79 local partial = res['partial']
80 if search_str and not sensitive then
81 search_str = string.lower(search_str)
82 end
83 local inputs = res['input'] or { 'stdin' }
84
85 for _, n in ipairs(inputs) do
86 local h, err
87 if string.match(n, '%.xz$') then
88 h, err = io.popen('xzcat ' .. n, 'r')
89 elseif string.match(n, '%.bz2$') then
90 h, err = io.popen('bzcat ' .. n, 'r')
91 elseif string.match(n, '%.gz$') then
92 h, err = io.popen('zcat ' .. n, 'r')
93 elseif string.match(n, '%.zst$') then
94 h, err = io.popen('zstdcat ' .. n, 'r')
95 elseif n == 'stdin' then
96 h = io.input()
97 else
98 h, err = io.open(n, 'r')
99 end
100 if not h then
101 if err then
102 io.stderr:write("Couldn't open file (" .. n .. '): ' .. err .. '\n')
103 else
104 io.stderr:write("Couldn't open file (" .. n .. '): no error\n')
105 end
106 else
107 for line in h:lines() do
108 local hash = string.match(line, '<(%x+)>')
109 local already_matching = false
110 if hash then
111 if matches[hash] then
112 table.insert(matches[hash], line)
113 already_matching = true
114 else
115 if buffer[hash] then
116 table.insert(buffer[hash], line)
117 else
118 buffer[hash] = { line }
119 end
120 end
121 end
122 local ismatch = false
123 if re then
124 ismatch = re:match(line)
125 elseif sensitive and search_str then
126 ismatch = string.find(line, search_str, 1, plainm)
127 elseif search_str then
128 local lwr = string.lower(line)
129 ismatch = string.find(lwr, search_str, 1, plainm)
130 end
131 if ismatch then
132 if not hash then
133 if orphans then
134 print('*** orphaned ***')
135 print(line)
136 print()
137 end
138 elseif not already_matching then
139 matches[hash] = buffer[hash]
140 end
141 end
142 local is_end = string.match(line, '<%x+>; task; rspamd_protocol_http_reply:')
143 if is_end then
144 buffer[hash] = nil
145 if matches[hash] then
146 for _, v in ipairs(matches[hash]) do
147 print(v)
148 end
149 print()
150 matches[hash] = nil
151 end
152 end
153 end
154 if partial then
155 for k, v in pairs(matches) do
156 print('*** partial ***')
157 for _, vv in ipairs(v) do
158 print(vv)
159 end
160 print()
161 matches[k] = nil
162 end
163 else
164 matches = {}
165 end
166 end
167 end
168 end
169
170 return {
171 handler = handler,
172 description = parser._description,
173 name = 'grep'
174 }