Merge "Recognise when user forgets # prefix for a track check-in"

This commit is contained in:
Zuul 2019-04-30 21:31:43 +00:00 committed by Gerrit Code Review
commit 932cb5df03
1 changed files with 28 additions and 8 deletions

View File

@ -111,6 +111,31 @@ class PTGBot(SASL, SSL, irc.bot.SingleServerIRCBot):
self.send_priv_or_pub(nick, None,
"Recognised commands: in, out, seen")
# Checks location against known tracks. If prefixed with # then
# insists it must match a known track. If not #-prefixed but
# matches a known track then the # prefix is added. Returns the
# normalized location to check into, or None if a valid one was
# not established. When matching/matched against a known track,
# it will be lower-cased. This assumes that all registered tracks
# are lower-case.
def normalize_location(self, reply_to, nick, location):
tracks = self.data.list_tracks()
if location.startswith('#'):
track = location[1:].lower()
if track in tracks:
return location.lower()
else:
self.send_priv_or_pub(
reply_to, nick, "Unrecognised track #%s" % track)
return None
else:
if location.lower() in tracks:
return '#' + location.lower()
else:
# Free-form location
return location
def check_in(self, reply_to, nick, words):
if len(words) == 0:
self.send_priv_or_pub(
@ -119,14 +144,9 @@ class PTGBot(SASL, SSL, irc.bot.SingleServerIRCBot):
return
location = " ".join(words)
if location.startswith('#'):
track = location[1:].lower()
tracks = self.data.list_tracks()
if track not in tracks:
self.send_priv_or_pub(
reply_to, nick, "Unrecognised track #%s" % track)
return
location = self.normalize_location(reply_to, nick, location)
if location is None:
return
self.data.check_in(nick, location)
self.send_priv_or_pub(