From 7a0c9b87e1d08c2de137999aca3ba98412399d4b Mon Sep 17 00:00:00 2001 From: skizzerz Date: Tue, 28 Jun 2016 12:48:07 -0500 Subject: [PATCH] Expiration date fixes for fwarn - Always use UTC (time issued was already UTC, but expiration was not) - Remove some duplicate code about parsing expiration date - Round dates to the nearest minute --- messages/en.json | 3 +-- src/wolfgame.py | 49 +++++++++++++++++++++++------------------------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/messages/en.json b/messages/en.json index 4266b69..e788ba3 100644 --- a/messages/en.json +++ b/messages/en.json @@ -811,8 +811,7 @@ "fwarn_add_syntax": "Usage: fwarn add [@] [~expiry] [sanctions] <:reason> [| notes]", "fwarn_page_invalid": "Invalid page, must be a number 1 or greater.", "fwarn_points_invalid": "Invalid points, must be a number above 0.", - "fwarn_expiry_invalid": "Invalid expiration amount, must be a number above 0 or 'never' for a warning that never expires.", - "fwarn_expiry_invalid_suffix": "Invalid expiration suffix, must use either d, h, or m.", + "fwarn_expiry_invalid": "Invalid expiration, must be a number above 0 followed by either d, h, or m, or 'never' for a warning that never expires.", "fwarn_cannot_add": "Cannot add warning, double-check your parameters (the nick might be wrong or you are not joined to the channel).", "fwarn_added": "Added warning {0}.", "fwarn_done": "Done.", diff --git a/src/wolfgame.py b/src/wolfgame.py index 4a49814..873822c 100644 --- a/src/wolfgame.py +++ b/src/wolfgame.py @@ -7975,15 +7975,23 @@ def add_warning(target, amount, actor, reason, notes=None, expires=None, need_ac exp_amount = int(expires[:-1]) if exp_suffix == "d": - expires = datetime.now() + timedelta(days=exp_amount) + expires = datetime.utcnow() + timedelta(days=exp_amount) elif exp_suffix == "h": - expires = datetime.now() + timedelta(hours=exp_amount) + expires = datetime.utcnow() + timedelta(hours=exp_amount) elif exp_suffix == "m": - expires = datetime.now() + timedelta(minutes=exp_amount) + expires = datetime.utcnow() + timedelta(minutes=exp_amount) else: raise ValueError("Invalid expiration string") elif isinstance(expires, int): - expires = datetime.now() + timedelta(days=expires) + expires = datetime.utcnow() + timedelta(days=expires) + + # Round expires to the nearest minute (30s rounds up) + if isinstance(expires, datetime): + round_add = 0 + if expires.second >= 30: + round_add = 1 + expires -= timedelta(seconds=expires.second, microseconds=expires.microsecond) + expires += timedelta(minutes=round_add) # determine if we need to automatically add any sanctions prev = db.get_warning_points(tacc, thm) @@ -8523,9 +8531,15 @@ def fwarn(cli, nick, chan, rest): elif suffix == "m": expires = issued + timedelta(minutes=amount) else: - reply(cli, nick, chan, messages["fwarn_expiry_invalid_suffix"]) + reply(cli, nick, chan, messages["fwarn_expiry_invalid"]) return + round_add = 0 + if expires.second >= 30: + round_add = 1 + expires -= timedelta(seconds=expires.second, microseconds=expires.microsecond) + expires += timedelta(minutes=round_add) + # maintain existing reason if none was specified if not reason: reason = warning["reason"] @@ -8627,29 +8641,12 @@ def fwarn(cli, nick, chan, rest): if expires.lower() in messages["never_aliases"]: expires = None - else: - suffix = expires[-1] - try: - amount = int(expires[:-1]) - except ValueError: - reply(cli, nick, chan, messages["fwarn_expiry_invalid"]) - return - if amount <= 0: - reply(cli, nick, chan, messages["fwarn_expiry_invalid"]) - return + try: + warn_id = add_warning(target, points, nick, reason, notes, expires, need_ack, sanctions) + except ValueError: + reply(cli, nick, chan, messages["fwarn_expiry_invalid"]) - if suffix == "d": - expires = datetime.now() + timedelta(days=amount) - elif suffix == "h": - expires = datetime.now() + timedelta(hours=amount) - elif suffix == "m": - expires = datetime.now() + timedelta(minutes=amount) - else: - reply(cli, nick, chan, messages["fwarn_expiry_invalid_suffix"]) - return - - warn_id = add_warning(target, points, nick, reason, notes, expires, need_ack, sanctions) if warn_id is False: reply(cli, nick, chan, messages["fwarn_cannot_add"]) else: