|
| 1 | +from collections import defaultdict |
| 2 | + |
| 3 | +from oxenmq import AuthLevel |
| 4 | + |
| 5 | +from . import model |
| 6 | +from .omq import omq |
| 7 | +from .web import app |
| 8 | + |
| 9 | +from binascii import hexlify |
| 10 | +from types import Iterable |
| 11 | + |
| 12 | +from oxenc import bt_serialize |
| 13 | + |
| 14 | +from .routes.subrequest import make_subrequest |
| 15 | + |
| 16 | +from flask import g |
| 17 | + |
| 18 | +# pools for event propagation |
| 19 | +_pools = defaultdict(list) |
| 20 | + |
| 21 | +status_OK = 'OK' |
| 22 | +status_ERR = 'ERROR' |
| 23 | + |
| 24 | +# the events we are able to subscribe to |
| 25 | +EVENTS = ('message', 'joined', 'parted', 'banned', 'unbanned', 'deleted', 'uploaded') |
| 26 | + |
| 27 | + |
| 28 | +def event_name_valid(eventname): |
| 29 | + """ return True if this event name is something well formed """ |
| 30 | + return eventname in EVENTS |
| 31 | + |
| 32 | + |
| 33 | +def _user_from_conn(conn): |
| 34 | + """ |
| 35 | + make a model.User from a connection using it's curve pubkey as the session id. |
| 36 | + """ |
| 37 | + return model.User(session_id='05' + hexlify(conn.pubkey).decode()) |
| 38 | + |
| 39 | + |
| 40 | +def _propagate_event(eventname, *args): |
| 41 | + """ propagate an event to everyone who cares about it """ |
| 42 | + assert event_name_valid(eventname) |
| 43 | + global omq, _pools |
| 44 | + sent = 0 |
| 45 | + for conn in _pools[eventname]: |
| 46 | + omq.send(conn, f'sogs.event.{eventname}', *(bt_serialize(a) for a in args)) |
| 47 | + sent += 1 |
| 48 | + if sent: |
| 49 | + app.logger.info(f"sent {eventname} to {sent} subscribers") |
| 50 | + |
| 51 | + |
| 52 | +_category = omq.add_category('sogs', AuthLevel.basic) |
| 53 | + |
| 54 | + |
| 55 | +def api(f, *, name=None, minargs=None): |
| 56 | + """ set up a request handler for zmq for a function with name of the endpoint """ |
| 57 | + assert name is not None |
| 58 | + |
| 59 | + def _handle_request(msg): |
| 60 | + try: |
| 61 | + if minargs and len(msg.data) < minargs: |
| 62 | + raise ValueError(f"Not enough arguments, got {len(msg.data)} expected 2 or more") |
| 63 | + app.logger.debug(f"zmq request: {name} for {msg.conn}") |
| 64 | + g.user = _user_from_conn(msg.conn) |
| 65 | + retval = f(*msg.data, conn=msg.conn) |
| 66 | + if retval is None: |
| 67 | + msg.reply(status_OK) |
| 68 | + elif isinstance(retval, tuple): |
| 69 | + msg.reply(status_OK, *retval) |
| 70 | + else: |
| 71 | + msg.reply(status_OK, bt_serialize(retval)) |
| 72 | + except Exception as ex: |
| 73 | + app.logger.error(f"{f.__name__} raised exception: {ex}") |
| 74 | + msg.reply(status_ERR, f'{ex}') |
| 75 | + finally: |
| 76 | + g.user = None |
| 77 | + |
| 78 | + global _category |
| 79 | + _category.add_request_command(name, _handle_request) |
| 80 | + app.logger.info(f"register zmq api handler: sogs.{name}") |
| 81 | + return f |
| 82 | + |
| 83 | + |
| 84 | +def _collect_bytes(iterable: Iterable[bytes]): |
| 85 | + """ collect all bytes from an iterable of bytes and put it into one big bytes instance """ |
| 86 | + data = bytes() |
| 87 | + for part in iterable: |
| 88 | + data += part |
| 89 | + return data |
| 90 | + |
| 91 | + |
| 92 | +@api(name='sub', minargs=1) |
| 93 | +def handle_subscribe(*events, conn=None): |
| 94 | + """ subscribe connection to many events """ |
| 95 | + sub = set() |
| 96 | + for ev in events: |
| 97 | + name = ev.decode('ascii') |
| 98 | + if not event_name_valid(name): |
| 99 | + raise Exception(f"invalid event type: {name}") |
| 100 | + sub += name |
| 101 | + |
| 102 | + global _pools |
| 103 | + for name in sub: |
| 104 | + _pools[name].append(conn) |
| 105 | + app.logger.debug(f"sub {conn} to {len(sub)} events") |
| 106 | + |
| 107 | + |
| 108 | +@api(name='unsub', minargs=1) |
| 109 | +def handle_unsubscribe(*events, conn=None): |
| 110 | + """ unsub connection to many events """ |
| 111 | + unsub = set() |
| 112 | + for ev in events: |
| 113 | + name = ev.decode('ascii') |
| 114 | + if not event_name_valid(name): |
| 115 | + raise Exception(f"invalid event type: {name}") |
| 116 | + unsub += name |
| 117 | + |
| 118 | + global _pools |
| 119 | + for name in unsub: |
| 120 | + _pools[name].remove(conn) |
| 121 | + app.logger.debug(f"unsub {conn} to {len(unsub)} events") |
| 122 | + |
| 123 | + |
| 124 | +@api(name="request", minargs=2) |
| 125 | +def handle_rpc_call(method, path, body=None, *, conn=None): |
| 126 | + """ make a sub request via zmq """ |
| 127 | + ctype = None |
| 128 | + # guess content type |
| 129 | + if body: |
| 130 | + if body[0] in (b'{', b'['): |
| 131 | + ctype = 'application/json' |
| 132 | + else: |
| 133 | + ctype = 'application/octet-stream' |
| 134 | + |
| 135 | + resp = make_subrequest( |
| 136 | + method.decode('ascii'), path.decode('ascii'), content_type=ctype, body=body |
| 137 | + ) |
| 138 | + return resp.status_code, _collect_bytes(resp.response) |
| 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