from core.util import logs from django.core.management.base import BaseCommand from slixmpp.componentxmpp import ComponentXMPP from django.conf import settings from core.models import User, Person, PersonIdentifier, ChatSession from redis import asyncio as aioredis from asgiref.sync import sync_to_async from django.utils.timezone import now import asyncio import msgpack from core.lib import deferred from core.clients import signalapi from slixmpp.xmlstream import register_stanza_plugin from slixmpp.plugins.xep_0085.stanza import Active, Composing, Paused, Inactive, Gone from slixmpp.stanza import Message from slixmpp.xmlstream.stanzabase import ElementBase, ET import aiohttp from core.messaging import history log = logs.get_logger("component") redis = aioredis.from_url("unix://var/run/gia-redis.sock", db=10) class Attachment(ElementBase): name = "attachment" namespace = "urn:xmpp:attachments" plugin_attrib = "attachment" interfaces = {"url", "filename", "content_type"} async def stream(**kwargs): pubsub = redis.pubsub() await pubsub.subscribe("component") while True: message = await pubsub.get_message(ignore_subscribe_messages=True) if message is not None: try: data = message["data"] unpacked = msgpack.unpackb(data, raw=False) log.info(f"Unpacked: {unpacked}") except TypeError: continue if "type" in unpacked.keys(): if unpacked["type"] == "def": await deferred.process_deferred( unpacked, **kwargs ) await asyncio.sleep(0.01) class Command(BaseCommand): def handle(self, *args, **options): xmpp = EchoComponent( jid=settings.XMPP_JID, secret=settings.XMPP_SECRET, server=settings.XMPP_ADDRESS, port=settings.XMPP_PORT, ) xmpp.register_plugin('xep_0030') # Service Discovery xmpp.register_plugin('xep_0004') # Data Forms xmpp.register_plugin('xep_0060') # PubSub xmpp.register_plugin('xep_0199') # XMPP Ping xmpp.register_plugin("xep_0085") # Chat State Notifications xmpp.register_plugin('xep_0363') # HTTP File Upload loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.create_task(stream(xmpp=xmpp)) # Connect to the XMPP server and start processing XMPP stanzas. xmpp.connect() xmpp.process() try: while True: pass # Keep the component running except (KeyboardInterrupt, SystemExit): log.info("XMPP Component terminating")