Skip to content

Commit

Permalink
core.py: fully implement Connection class
Browse files Browse the repository at this point in the history
  • Loading branch information
Annika committed May 25, 2020
1 parent 56360fb commit a58c901
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
4 changes: 3 additions & 1 deletion config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@
"manageRank": "#",
"roomRanksInOrder": ["+", "%", "@", "*", "#"],
"rooms": [],
"separator": ","
"separator": ",",
"modules": {},
"commandCharacter": "~"
}
7 changes: 5 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

CONFIG_PATH = 'config.json'
CONFIG_VARS = ['username', 'password', 'websocketURL', 'loglevel', 'sysops', 'broadcastRank',
'addfactRank', 'hostgameRank', 'manageRank', 'roomRanksInOrder', 'rooms', 'separator']
'addfactRank', 'hostgameRank', 'manageRank', 'roomRanksInOrder', 'rooms', 'separator', 'modules',
'commandCharacter']

def loadConfig():
configData = json.load(open(CONFIG_PATH, 'r'))
Expand All @@ -23,5 +24,7 @@ def loadConfig():
returnValue.append(configData[configItem])
return returnValue

username, password, websocketURL, loglevel, sysops, broadcastRank, addfactRank, hostgameRank, manageRank, roomRanksInOrder, rooms, separator = loadConfig()
username, password, websocketURL, loglevel, sysops, broadcastRank, addfactRank, \
hostgameRank, manageRank, roomRanksInOrder, rooms, separator, modules, \
commandCharacter = loadConfig()
# the order of these needs to be the same as in CONFIG_VARS
30 changes: 27 additions & 3 deletions core.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def can(self, action, room):

def PM(self, message):
'''PMs the user the given message'''
self.connection.send('|/pm {user},{message}'.format(user = self.id, message = message))
self.connection.whisper(self.id, message)

###################
## Message Class ##
Expand All @@ -114,7 +114,13 @@ def __init__(self, raw, connection):
self.type = 'chat'
self.room = connection.getRoomByID(split[0].strip('>').strip('\n'))
self.time = split[2]
self.sender = User(split[3], self.connection)

username = split[3].strip()
if username[0] in config.roomRanksInOrder:
rank = username[0]
username = username[1:]
self.room.auth[rank].append(username)
self.sender = User(username, self.connection)
self.body = "|".join(split[4:]).strip('\n')
self.arguments = self.body.split(config.separator)
log("DEBUG: Message(): body = " + self.body)
Expand Down Expand Up @@ -142,6 +148,11 @@ def __init__(self):
on_error = self.onError,
on_close = self.onClose, on_open = self.onOpen)
self.roomList = []
self.commands = {}
for module in config.modules:
self.commands.update(module.commands)
# Note: if multiple modules have the same command then the later module will overwrite the earlier.
log("I: Connection(): Loaded the following commands: " + ", ".join(self.commands.keys()))

def onError(self, ws, error):
log("E: Connection.onError(): websocket error: {error}".format(error = error))
Expand All @@ -156,6 +167,10 @@ def onMessage(self, rawMessage):
message = Message(rawMessage, self)
if message.challstr:
self.login(message.challstr)
elif message.type in ['chat', 'pm'] and message.body[0] == config.commandCharacter:
potentialCommand = message.body.split(' ')[0].strip(config.commandCharacter)
if potentialCommand in self.commands:
self.commands[potentialCommand](message) # Invoke the command

def login(self, challstr):
'''logs in'''
Expand All @@ -176,7 +191,7 @@ def send(self, message):
def getRoomByID(self, id):
'''Gets the Room object corresponding to the given ID'''
objects = [room for room in self.roomList if room.id == id]
if len(objects) == 1:
if len(objects) == 0:
return None
elif len(objects) > 1:
log("W: Connection.getRoomByID(): more than 1 Room object for room " + id)
Expand All @@ -185,6 +200,15 @@ def getRoomByID(self, id):
def getRoomByName(self, name):
'''Gets the Room object for the room of the given name'''
return self.getRoomByID(toID(name))

def sayIn(self, room, message):
'''Sends the given message to the given room. Both arguments are strings.'''
self.websocket.send(room + "|" + message)

def whisper(self, userid, message):
'''PMs the given message to the given userid'''
self.websocket.send("|/pm {user}, {message}".format(user = userid, message = message))


if __name__ == "__main__":
connection = Connection()
Expand Down

0 comments on commit a58c901

Please sign in to comment.