Add room name validation

Rooms must match a defined set of active rooms.

Add !add !del !list commands in order to manage the room list.
Also add a !clean command to remove session entries from a given
set of rooms. All commands take a list of rooms as argument.

Change-Id: I4130c49cc2a3985c440d5b9205f1900daaf46d3d
This commit is contained in:
Thierry Carrez 2017-06-13 11:22:16 +02:00
parent 2668ea8f6e
commit cca73956da
2 changed files with 52 additions and 3 deletions

View File

@ -90,6 +90,13 @@ class PTGBot(irc.bot.SingleServerIRCBot):
def usage(self, channel): def usage(self, channel):
self.send(channel, "Format is '@ROOM [now|next] SESSION'") self.send(channel, "Format is '@ROOM [now|next] SESSION'")
def send_room_list(self, channel):
rooms = self.data.list_rooms()
if rooms:
self.send(channel, "Active rooms: %s" % str.join(' ', rooms))
else:
self.send(channel, "There are no active rooms defined yet")
def on_pubmsg(self, c, e): def on_pubmsg(self, c, e):
if not self.identify_msg_cap: if not self.identify_msg_cap:
self.log.debug("Ignoring message because identify-msg " self.log.debug("Ignoring message because identify-msg "
@ -104,13 +111,19 @@ class PTGBot(irc.bot.SingleServerIRCBot):
self.channels[chan].is_oper(nick)): self.channels[chan].is_oper(nick)):
self.send(chan, "%s: Need voice to issue commands" % (nick,)) self.send(chan, "%s: Need voice to issue commands" % (nick,))
return return
words = msg.split() words = msg.split()
if len(words) < 3: if len(words) < 3:
self.send(chan, "%s: Incorrect number of arguments" % (nick,)) self.send(chan, "%s: Incorrect number of arguments" % (nick,))
self.usage(chan) self.usage(chan)
return return
room = words[0][1:].lower() room = words[0][1:].lower()
# TODO: Add test for room/day/person match if not self.data.is_room_valid(room):
self.send(chan, "%s: unknown room '%s'" % (nick, room))
self.send_room_list(chan)
return
adverb = words[1].lower() adverb = words[1].lower()
session = str.join(' ', words[2:]) session = str.join(' ', words[2:])
if adverb == 'now': if adverb == 'now':
@ -131,6 +144,14 @@ class PTGBot(irc.bot.SingleServerIRCBot):
command = words[0][1:].lower() command = words[0][1:].lower()
if command == 'wipe': if command == 'wipe':
self.data.wipe() self.data.wipe()
elif command == 'list':
self.send_room_list(chan)
return
elif command in ('clean', 'add', 'del'):
if len(words) < 2:
self.send(chan, "this command takes one or more arguments")
return
getattr(self.data, command + '_rooms')(words[1:])
else: else:
self.send(chan, "%s: unknown command '%s'" % (nick, command)) self.send(chan, "%s: unknown command '%s'" % (nick, command))
return return

View File

@ -21,6 +21,8 @@ import requests
class PTGDataBase(): class PTGDataBase():
BASE = {'rooms': [], 'ethercalc': [], 'now': {}, 'next': {}}
def __init__(self, filename, ethercalc_url, ethercalc_cells): def __init__(self, filename, ethercalc_url, ethercalc_cells):
self.filename = filename self.filename = filename
self.ethercalc_url = ethercalc_url self.ethercalc_url = ethercalc_url
@ -29,7 +31,7 @@ class PTGDataBase():
with open(filename, 'r') as fp: with open(filename, 'r') as fp:
self.data = json.load(fp) self.data = json.load(fp)
else: else:
self.data = {'ethercalc': [], 'now': {}, 'next': {}} self.data = self.BASE
def add_now(self, room, session): def add_now(self, room, session):
self.data['now'][room] = session self.data['now'][room] = session
@ -43,6 +45,32 @@ class PTGDataBase():
self.data['next'][room].append(session) self.data['next'][room].append(session)
self.save() self.save()
def is_room_valid(self, room):
return room in self.data['rooms']
def list_rooms(self):
return self.data['rooms']
def add_rooms(self, rooms):
for room in rooms:
if room not in self.data['rooms']:
self.data['rooms'].append(room)
self.save()
def del_rooms(self, rooms):
for room in rooms:
if room in self.data['rooms']:
self.data['rooms'].remove(room)
self.save()
def clean_rooms(self, rooms):
for room in rooms:
if room in self.data['now']:
del self.data['now'][room]
if room in self.data['next']:
del self.data['next'][room]
self.save()
def from_ethercalc(self): def from_ethercalc(self):
if self.ethercalc_url: if self.ethercalc_url:
ethercalc = requests.get(self.ethercalc_url).json() ethercalc = requests.get(self.ethercalc_url).json()
@ -55,7 +83,7 @@ class PTGDataBase():
self.data['ethercalc'].append(msg) self.data['ethercalc'].append(msg)
def wipe(self): def wipe(self):
self.data = {'now': {}, 'next': {}} self.data = self.BASE
self.save() self.save()
def save(self): def save(self):