h2/main.py

77 lines
2.1 KiB
Python
Raw Normal View History

import fuckit
2015-11-29 06:42:25 +00:00
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']]
reload()
2015-11-29 16:39:56 +00:00
if event[u'type'] == "m.room.message":
2015-11-29 06:42:25 +00:00
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))
2015-11-29 16:39:56 +00:00
elif u'invite_room_state' in event:
for user in config["masters"]:
if event[u'user_id'] == user:
client.join_room(event[u'room_id'])
print user, "asked me to join", event[u'room_id']
#else:
# print dump(event)
2015-11-29 06:42:25 +00:00
for room in rooms:
rooms[room].add_listener(room_callback)
while True:
client.listen_for_events()
sleep(0.25)