Browse Source

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
changes/57/473757/2
Thierry Carrez 4 years ago
parent
commit
cca73956da
2 changed files with 52 additions and 3 deletions
  1. +22
    -1
      ptgbot/bot.py
  2. +30
    -2
      ptgbot/db.py

+ 22
- 1
ptgbot/bot.py View File

@ -90,6 +90,13 @@ class PTGBot(irc.bot.SingleServerIRCBot):
def usage(self, channel):
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):
if not self.identify_msg_cap:
self.log.debug("Ignoring message because identify-msg "
@ -104,13 +111,19 @@ class PTGBot(irc.bot.SingleServerIRCBot):
self.channels[chan].is_oper(nick)):
self.send(chan, "%s: Need voice to issue commands" % (nick,))
return
words = msg.split()
if len(words) < 3:
self.send(chan, "%s: Incorrect number of arguments" % (nick,))
self.usage(chan)
return
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()
session = str.join(' ', words[2:])
if adverb == 'now':
@ -131,6 +144,14 @@ class PTGBot(irc.bot.SingleServerIRCBot):
command = words[0][1:].lower()
if command == '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:
self.send(chan, "%s: unknown command '%s'" % (nick, command))
return


+ 30
- 2
ptgbot/db.py View File

@ -21,6 +21,8 @@ import requests
class PTGDataBase():
BASE = {'rooms': [], 'ethercalc': [], 'now': {}, 'next': {}}
def __init__(self, filename, ethercalc_url, ethercalc_cells):
self.filename = filename
self.ethercalc_url = ethercalc_url
@ -29,7 +31,7 @@ class PTGDataBase():
with open(filename, 'r') as fp:
self.data = json.load(fp)
else:
self.data = {'ethercalc': [], 'now': {}, 'next': {}}
self.data = self.BASE
def add_now(self, room, session):
self.data['now'][room] = session
@ -43,6 +45,32 @@ class PTGDataBase():
self.data['next'][room].append(session)
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):
if self.ethercalc_url:
ethercalc = requests.get(self.ethercalc_url).json()
@ -55,7 +83,7 @@ class PTGDataBase():
self.data['ethercalc'].append(msg)
def wipe(self):
self.data = {'now': {}, 'next': {}}
self.data = self.BASE
self.save()
def save(self):


Loading…
Cancel
Save