-
Notifications
You must be signed in to change notification settings - Fork 0
/
followmail.py
336 lines (271 loc) · 8.38 KB
/
followmail.py
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
# vim: se ts=4 et syn=python:
# created by: matteoguadrini
# followmail -- followmail
#
# Copyright (C) 2024 Matteo Guadrini <matteo.guadrini@hotmail.it>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Main module of followmail program."""
# region imports
import argparse
import gzip
import os
import re
from collections import namedtuple
from tablib import Dataset
# endregion
# region globals
__version__ = "1.0.0"
LogLine = namedtuple(
"LogLine", ["date", "time", "server", "queue", "smtpid", "message"]
)
# endregion
# region functions
def get_args():
"""Get command line arguments"""
parser = argparse.ArgumentParser(
description="postfix log parser to follow a mail addresses",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--version",
"-V",
help="print version",
action="version",
version="%(prog)s " + __version__,
)
parser.add_argument(
"--verbose",
"-v",
help="print with verbosity",
action="store_true",
)
parser.add_argument(
"--to",
"-t",
help="email address into to field",
metavar="mail_address",
type=str,
action="store",
)
parser.add_argument(
"--from",
"-f",
help="email address into from field",
dest="from_",
metavar="mail_address",
type=str,
action="store",
)
parser.add_argument(
"-l",
"--maillog",
help="input maillog file",
metavar="path",
type=str,
default="/var/log/maillog",
)
parser.add_argument(
"-q",
"--queue",
metavar="queue_name",
help="name of postfix queue",
default="postfix",
)
parser.add_argument(
"-m",
"--max-lines",
type=int,
help="max lines to print",
)
parser.add_argument(
"-D",
"--sortby-date",
help="sort lines by date",
action="store_true",
)
parser.add_argument(
"-c",
"--csv",
help="print in csv format",
action="store_true",
)
parser.add_argument(
"-j",
"--json",
help="print in json format",
action="store_true",
)
args = parser.parse_args()
email_pattern = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
# Check if max lines is less than one
if args.max_lines is not None and args.max_lines <= 0:
parser.error("max lines is must greater than zero")
# Check maillog file exists
if not os.path.isfile(args.maillog):
parser.error(f'maillog file "{args.maillog}" does not exists')
# Check flags
if not args.to and not args.from_:
parser.error('unspecified filter "--to" or "--from"')
# Validate email address
if args.to and not re.findall(email_pattern, args.to):
parser.error("specified a valid email address in 'to' field")
if args.from_ and not re.findall(email_pattern, args.from_):
parser.error("specified a valid email address in 'from' field")
return args
def print_verbose(verbosity: bool, *messages: str):
"""Print verbose messages
:param verbosity: boolean to activate verbose print
"""
if verbosity:
print("debug:", *messages)
def report_issue(exc):
"""Report issue
:param: exc: Exception object
"""
print(
"followmail: error: {0} on line {1}, with error {2}".format(
type(exc).__name__, exc.__traceback__.tb_lineno, str(exc)
)
)
exit(1)
def open_log(log: str):
"""Open maillog file
:param log: maillog file path
:return: opened log
"""
# Define function to open log
open_method = gzip.open if log.endswith("gz") else open
return open_method(log, "rt")
def make_logline(line: str, pattern: re.Pattern):
"""Make LogLine object from string
:param line: maillog line
:param pattern: regexp pattern object
:return: LogLine
"""
# Split line into single variable
line = re.findall(pattern, line)
# Skip line if not in pattern
if line:
line = [part for part in line[0]]
# Make a LogLine object
logline = LogLine(
date=line[0],
time=line[1],
server=line[2],
queue=line[3],
smtpid=line[4],
message=line[5],
)
return logline
def search_by_smtpid(smtpid: str, log: str, pattern: re.Pattern):
"""Search log lines by smtp id
:param smtpid: string of smtp is
:param log: opened log file
:param pattern: regexp pattern
:return: List[tuple]
"""
rows = list()
# Process log file
with open_log(log) as maillog_file:
for line in maillog_file:
logline = make_logline(line, pattern)
if logline:
# Match smtp id
if smtpid == logline.smtpid:
rows.append(logline)
# End of flow
if "removed" in logline.message:
break
return rows
def print_data(data: Dataset, csv: bool = False, json: bool = False):
"""Print Dataset to stdout
:param data: Dataset object
:param csv: print to csv format, defaults to False
:param json: print to json format, defaults to False
"""
# Check if data is not empty...
if not data:
print("no data found")
return
# ...everything else, print!
if csv:
print(data.export("csv"))
elif json:
print(data.export("json"))
else:
print(data)
# region scripts
def main():
"""Main function"""
# Define global for a script
args = get_args()
verbose = args.verbose
# Empty Dataset
data = Dataset(headers=("date", "time", "server", "queue", "smtpid", "message"))
print_verbose(verbose, "start followmail")
# Define filters
to = args.to
if to:
print_verbose(verbose, f"add {to} into 'to' filters")
from_ = args.from_
if from_:
print_verbose(verbose, f"add {from_} into 'from' filters")
maillog = args.maillog
print_verbose(verbose, f"add {maillog} into filters")
queue = args.queue
print_verbose(verbose, f"add {queue} into filters")
# Define pattern regexp
pattern = re.compile(
r"(^[A-Za-z]{3}\s\s?\d{1,2})\s(\d{2}:\d{2}:\d{2})\s(\w+)\s(.*/.*\[\d+]):\s(\w{10,15}):\s(.*)"
)
# Process log file
with open_log(maillog) as maillog_file:
for line in maillog_file:
logline = make_logline(line, pattern)
if logline:
# Filter queue
if queue not in logline.queue:
continue
# Filter to and from
if (f"to=<{to}>" not in logline.message and
f"from=<{from_}>" not in logline.message):
continue
print_verbose(verbose, f"found a log line {logline}")
# Find lines through smtp id
lines = search_by_smtpid(logline.smtpid, maillog, pattern)
# Add logline into Dataset
data.extend(lines)
# Sort by smtpid
if args.sortby_date:
data.sort("date")
else:
data.sort("smtpid")
if args.max_lines:
limited_data = data[:args.max_lines]
# Create a new empty Dataset
data = Dataset(headers=("date", "time", "server", "queue", "smtpid", "message"))
# Extend dataset with limited data
data.extend(limited_data)
# Print data
print_data(data, csv=args.csv, json=args.json)
# endregion
if __name__ == "__main__":
try:
main()
except Exception as err:
report_issue(err)
# endregion