1
0
mirror of https://github.com/mail-in-a-box/mailinabox.git synced 2025-04-20 02:52:11 +00:00

Fixed #1894 log date over year change, START_DATE < END_DATE now.

This commit is contained in:
Jarek 2021-01-15 20:28:21 +01:00
parent 879467d358
commit dcf3e646c5

View File

@ -44,9 +44,8 @@ TIME_DELTAS = OrderedDict([
('today', datetime.datetime.now() - datetime.datetime.now().replace(hour=0, minute=0, second=0)) ('today', datetime.datetime.now() - datetime.datetime.now().replace(hour=0, minute=0, second=0))
]) ])
# Start date > end date! END_DATE = NOW = datetime.datetime.now()
START_DATE = datetime.datetime.now() START_DATE = None
END_DATE = None
VERBOSE = False VERBOSE = False
@ -121,7 +120,7 @@ def scan_mail_log(env):
pass pass
print("Scanning logs from {:%Y-%m-%d %H:%M:%S} to {:%Y-%m-%d %H:%M:%S}".format( print("Scanning logs from {:%Y-%m-%d %H:%M:%S} to {:%Y-%m-%d %H:%M:%S}".format(
END_DATE, START_DATE) START_DATE, END_DATE)
) )
# Scan the lines in the log files until the date goes out of range # Scan the lines in the log files until the date goes out of range
@ -253,7 +252,7 @@ def scan_mail_log(env):
if collector["postgrey"]: if collector["postgrey"]:
msg = "Greylisted Email {:%Y-%m-%d %H:%M:%S} and {:%Y-%m-%d %H:%M:%S}" msg = "Greylisted Email {:%Y-%m-%d %H:%M:%S} and {:%Y-%m-%d %H:%M:%S}"
print_header(msg.format(END_DATE, START_DATE)) print_header(msg.format(START_DATE, END_DATE))
print(textwrap.fill( print(textwrap.fill(
"The following mail was greylisted, meaning the emails were temporarily rejected. " "The following mail was greylisted, meaning the emails were temporarily rejected. "
@ -291,7 +290,7 @@ def scan_mail_log(env):
if collector["rejected"]: if collector["rejected"]:
msg = "Blocked Email {:%Y-%m-%d %H:%M:%S} and {:%Y-%m-%d %H:%M:%S}" msg = "Blocked Email {:%Y-%m-%d %H:%M:%S} and {:%Y-%m-%d %H:%M:%S}"
print_header(msg.format(END_DATE, START_DATE)) print_header(msg.format(START_DATE, END_DATE))
data = OrderedDict(sorted(collector["rejected"].items(), key=email_sort)) data = OrderedDict(sorted(collector["rejected"].items(), key=email_sort))
@ -345,19 +344,19 @@ def scan_mail_log_line(line, collector):
# Replaced the dateutil parser for a less clever way of parser that is roughly 4 times faster. # Replaced the dateutil parser for a less clever way of parser that is roughly 4 times faster.
# date = dateutil.parser.parse(date) # date = dateutil.parser.parse(date)
# date = datetime.datetime.strptime(date, '%b %d %H:%M:%S') # strptime fails on Feb 29 with ValueError: day is out of range for month if correct year is not provided.
# date = date.replace(START_DATE.year) # See https://bugs.python.org/issue26460
date = datetime.datetime.strptime(str(NOW.year) + ' ' + date, '%Y %b %d %H:%M:%S')
# strptime fails on Feb 29 if correct year is not provided. See https://bugs.python.org/issue26460 # if log date in future, step back a year
date = datetime.datetime.strptime(str(START_DATE.year) + ' ' + date, '%Y %b %d %H:%M:%S') if date > NOW:
date = date.replace(year = NOW.year - 1)
#print("date:", date) #print("date:", date)
# Check if the found date is within the time span we are scanning # Check if the found date is within the time span we are scanning
# END_DATE < START_DATE if date > END_DATE:
if date > START_DATE:
# Don't process, and halt # Don't process, and halt
return False return False
elif date < END_DATE: elif date < START_DATE:
# Don't process, but continue # Don't process, but continue
return True return True
@ -838,12 +837,12 @@ if __name__ == "__main__":
args = parser.parse_args() args = parser.parse_args()
if args.startdate is not None: if args.startdate is not None:
START_DATE = args.startdate END_DATE = args.startdate
if args.timespan == 'today': if args.timespan == 'today':
args.timespan = 'day' args.timespan = 'day'
print("Setting start date to {}".format(START_DATE)) print("Setting start date to {}".format(END_DATE))
END_DATE = START_DATE - TIME_DELTAS[args.timespan] START_DATE = END_DATE - TIME_DELTAS[args.timespan]
VERBOSE = args.verbose VERBOSE = args.verbose