import os import sys import traceback import time from matrix_client.client import MatrixClient from time import sleep from yaml import load, dump class Bot(object): def __init__(self): self.conns = {} self.persist_dir = os.path.abspath('persist') if not os.path.exists(self.persist_dir): os.mkdir(self.persist_dir) bot = Bot() sys.path += ['plugins'] # bootstrap the reloader eval(compile(open(os.path.join('core', 'reload.py'), 'U').read(), os.path.join('core', 'reload.py'), 'exec'), globals()) reload(init=True) print "matrix has u" config = {} with open("./config.yml", "r") as fin: config = load(fin.read()) client = MatrixClient(config["me"]["homeserver"]) token = client.login_with_password(username=config["me"]["user"], password=config["me"]["password"]) rooms = client.get_rooms() def room_callback(event): room = rooms[event[u'room_id']] if u'user_id' in event and event[u'type'] == "m.room.message": print room.name, "<"+event[u'user_id']+">", event[u'content'][u'body'] if event[u'user_id'] == config["me"]["user"]: return else: content = event[u'content'] body = content[u'body'] if body.startswith("."): body = body.replace(".", "", 1) splitstuff = body.split() command = splitstuff[0] args = " ".join(splitstuff[1:]) cmd = match_command(command) if cmd in bot.commands: room.send_text(bot.commands[cmd][0](args)) else: for func, args in bot.plugs["regex"]: m = args['re'].search(body) if m: room.send_text(func(m)) for room in rooms: rooms[room].add_listener(room_callback) while True: client.listen_for_events() sleep(0.25)