|
| 1 | +from collections import defaultdict |
| 2 | + |
| 3 | +from oxenmq import AuthLevel |
| 4 | + |
| 5 | +from . import config |
| 6 | +from . import model |
| 7 | +from .omq import omq |
| 8 | + |
| 9 | +from binascii import hexlify |
| 10 | + |
| 11 | +from oxenc import bt_serialize as serialize |
| 12 | +from oxenc import bt_deserialize as deserialize |
| 13 | + |
| 14 | +from requests import request as http_request |
| 15 | + |
| 16 | +# pools for event propagation |
| 17 | +_pools = defaultdict(list) |
| 18 | + |
| 19 | +status_OK = 'OK' |
| 20 | +status_ERR = 'ERROR' |
| 21 | + |
| 22 | +# the events we are able to subscribe to |
| 23 | +EVENTS = ('message', 'joined', 'parted', 'banned', 'unbanned', 'deleted', 'uploaded') |
| 24 | + |
| 25 | + |
| 26 | +def event_name_valid(eventname): |
| 27 | + """ return True if this event name is something well formed """ |
| 28 | + return eventname in EVENTS |
| 29 | + |
| 30 | + |
| 31 | +def _user_from_conn(conn, automod=True): |
| 32 | + """ |
| 33 | + make a model.User from a connection using it's curve pubkey as the session id, if automod is |
| 34 | + True we auto promote the bot to a moderator if it isn't already |
| 35 | + """ |
| 36 | + user = model.User(session_id='05' + hexlify(conn.pubkey).decode()) |
| 37 | + if automod and not user.global_moderator: |
| 38 | + user.set_moderator() |
| 39 | + return user |
| 40 | + |
| 41 | + |
| 42 | +def _sub_conn_to_event(name, conn): |
| 43 | + """ subscribe a connection to an event type """ |
| 44 | + if not event_name_valid(name): |
| 45 | + raise Exception('invalid event name: {}'.format(name)) |
| 46 | + global _pools |
| 47 | + _pools[name].append(conn) |
| 48 | + |
| 49 | + |
| 50 | +def _unsub_conn_from_event(name, conn): |
| 51 | + if not event_name_valid(name): |
| 52 | + raise Exception(f"invalid event: {name}") |
| 53 | + global _pool |
| 54 | + _pools[name].remove(conn) |
| 55 | + |
| 56 | + |
| 57 | +def _handle_subscribe_request(msg): |
| 58 | + """ handle a request to subscribe to events as a bot""" |
| 59 | + parts = msg.data() |
| 60 | + if len(parts) == 0: |
| 61 | + raise Exception("no events selected") |
| 62 | + for name in parts: |
| 63 | + _sub_conn_to_event(name.decode(), msg.conn) |
| 64 | + |
| 65 | + |
| 66 | +def _handle_unsubscribe_request(msg): |
| 67 | + """ unusb from events """ |
| 68 | + parts = msg.data() |
| 69 | + if len(parts) == 0: |
| 70 | + raise Exception("no events selected") |
| 71 | + for name in parts: |
| 72 | + _unsub_conn_from_event(name.decode(), msg.conn) |
| 73 | + |
| 74 | + |
| 75 | +def _propagate_event(eventname, *args): |
| 76 | + """ propagate an event to everyone who cares about it """ |
| 77 | + assert event_name_valid(eventname) |
| 78 | + global omq, _pools |
| 79 | + ev = (eventname,) + args |
| 80 | + for conn in _pools[eventname]: |
| 81 | + omq.send(conn, 'sogs.event', *ev) |
| 82 | + |
| 83 | + |
| 84 | +def _handle_request(handler, msg): |
| 85 | + """safely handle a request handler and catch any exceptions that happen and propagate them to |
| 86 | + the remote connection""" |
| 87 | + try: |
| 88 | + retval = handler(msg) |
| 89 | + if retval is None: |
| 90 | + msg.reply(status_OK) |
| 91 | + elif isinstance(retval, tuple): |
| 92 | + msg.reply(status_OK, *retval) |
| 93 | + else: |
| 94 | + msg.reply(status_OK, serialize(retval)) |
| 95 | + except Exception as ex: |
| 96 | + msg.reply(status_ERR, '{}'.format(ex)) |
| 97 | + |
| 98 | + |
| 99 | +def _handle_api_request(msg): |
| 100 | + """ do an http api request """ |
| 101 | + parts = msg.parts() |
| 102 | + if len(parts) < 3: |
| 103 | + raise Exception("invalid number of parts {}".format(len(parts))) |
| 104 | + kwargs = dict() |
| 105 | + if len(parts) >= 4: |
| 106 | + kwargs['body'] = parts[3] |
| 107 | + |
| 108 | + headers = deserialize(parts[2]) |
| 109 | + if not isinstance(headers, dict): |
| 110 | + raise ValueError("headers is not a dict") |
| 111 | + |
| 112 | + kwargs['headers'] = headers |
| 113 | + |
| 114 | + path = parts[1].decode() |
| 115 | + if not path.startswith("/"): |
| 116 | + raise ValueError("invalid request path") |
| 117 | + method = parts[0].decode() |
| 118 | + resp = http_request(method, config.URL_BASE + path, **kwargs) |
| 119 | + |
| 120 | + headers = dict() |
| 121 | + for k, v in resp.headers.items(): |
| 122 | + headers[k] = v |
| 123 | + |
| 124 | + return serialize(resp.status_code), serialize(headers), resp.content |
| 125 | + |
| 126 | + |
| 127 | +def setup_rpc(): |
| 128 | + """ set up bot api using an existing oxenmq instance """ |
| 129 | + _bot_category = omq.add_category('sogs', AuthLevel.none) |
| 130 | + _bot_category.add_request_command( |
| 131 | + 'sub', lambda msg: _handle_request(_handle_subscribe_request, msg) |
| 132 | + ) |
| 133 | + _bot_category.add_request_command( |
| 134 | + 'unsub', lambda msg: _handle_request(_handle_unsubscribe_request, msg) |
| 135 | + ) |
| 136 | + _bot_category.add_request_command( |
| 137 | + 'request', lambda msg: _handle_request(_handle_api_request, msg) |
| 138 | + ) |
| 139 | + |
| 140 | + |
| 141 | +class _Notify: |
| 142 | + """ Holder type for all event notification functions """ |
| 143 | + |
| 144 | + |
| 145 | +notify = _Notify() |
| 146 | + |
| 147 | +# set up event notifiers |
| 148 | +for ev in EVENTS: |
| 149 | + setattr(notify, ev, lambda *args: _propagate_event(ev, *args)) |
0 commit comments