93 lines
3.3 KiB
Python
93 lines
3.3 KiB
Python
import requests
|
|
import logging
|
|
import yaml
|
|
import sys
|
|
import time
|
|
import shlex
|
|
import json
|
|
import re
|
|
import pnutpy
|
|
|
|
from matrix_client.client import MatrixClient
|
|
from matrix_client.api import MatrixHttpApi
|
|
from matrix_client.api import MatrixError, MatrixRequestError
|
|
from models import *
|
|
|
|
class MonkeyBot:
|
|
|
|
txId = 0
|
|
|
|
def __init__(self):
|
|
with open("config.yaml", "rb") as config_file:
|
|
self.config = yaml.load(config_file)
|
|
self.api = MatrixHttpApi(self.config['MATRIX_HOST'], self.config['MATRIX_AS_TOKEN'])
|
|
self.pnut_token = self.config['MATRIX_PNUT_TOKEN']
|
|
pnutpy.api.add_authorization_token(self.pnut_token)
|
|
|
|
def on_invite(self, event):
|
|
logging.debug("<__on_invite__>")
|
|
logging.debug(event)
|
|
room = self.api.join_room(event['room_id'])
|
|
|
|
def on_message(self, event):
|
|
logging.debug("<__on_message__>")
|
|
logging.debug(event)
|
|
|
|
if event['type'] == 'm.room.message':
|
|
if event['content']['msgtype'] == 'm.text':
|
|
argv = shlex.split(event['content']['body'])
|
|
cmd = argv[0]
|
|
args = argv[1:]
|
|
self._parse_cmd(event, cmd, args)
|
|
|
|
def _parse_cmd(self, event, cmd, args):
|
|
logging.debug("<__parse_cmd__>")
|
|
logging.debug("<cmd> " + cmd)
|
|
logging.debug(args)
|
|
|
|
if cmd.lower() == 'help':
|
|
self.api.send_notice(event['room_id'], self._help())
|
|
|
|
elif cmd.lower() == 'set_access_token':
|
|
token = args[0]
|
|
pnutpy.api.add_authorization_token(token)
|
|
try:
|
|
response, meta = pnutpy.api.get_user('me')
|
|
|
|
user = MatrixUser(matrix_id=event['user_id'], room_id=event['room_id'],
|
|
pnut_id=response['username'], pnut_token=token)
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
reply = "Token verified, you are now linked as " + response['username']
|
|
|
|
except pnut.api.PnutAuthAPIException as e:
|
|
reply = "Your account is not authorized."
|
|
|
|
except Exception as e:
|
|
reply = "Something went wrong...\n"
|
|
reply += str(e)
|
|
logging.exception('::set_access_token::')
|
|
|
|
self.api.send_notice(event['room_id'], reply)
|
|
pnutpy.api.add_authorization_token(self.pnut_token)
|
|
|
|
elif cmd.lower() == 'drop_access_token':
|
|
user = MatrixUser.query.filter_by(matrix_id=event['user_id']).first()
|
|
db.session.delete(user)
|
|
db.session.commit()
|
|
reply = "Your token has been removed."
|
|
self.api.send_notice(event['room_id'], reply)
|
|
|
|
else:
|
|
self.api.send_notice(event['room_id'], self._help())
|
|
|
|
def _help(self):
|
|
reply = "Visit the following URL to authorize pnut-matrix with your account on pnut.io.\n\n"
|
|
reply += "https://pnut.io/oauth/authenticate?client_id=6SeCRCpCZkmZOKFLFGWbcdAeq2fX1M5t&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=write_post,presence,messages&response_type=token\n\n"
|
|
reply += "The following commands are available.\n\n"
|
|
reply += "set_access_token <token>\n"
|
|
reply += " - Set your access token for matrix -> pnut.io account puppeting\n\n"
|
|
reply += "drop_access_token\n"
|
|
reply += " - Drop your access token to remove puppeting\n\n"
|
|
return reply
|
|
|