Implement XMPP relaying

This commit is contained in:
2025-02-21 21:34:47 +00:00
parent 018d2f87c7
commit 8d2f28f571
17 changed files with 941 additions and 77 deletions

View File

@@ -22,6 +22,9 @@ SIGNAL_URL = "signal:8080"
log = logs.get_logger("processing")
redis = aioredis.from_url("unix://var/run/gia-redis.sock", db=10)
class HandleMessage(Command):
async def handle(self, c: Context):
msg = {
@@ -36,9 +39,18 @@ class HandleMessage(Command):
"mentions": c.message.mentions,
"raw_message": c.message.raw_message
}
dest = c.message.raw_message.get("envelope", {}).get("syncMessage", {}).get("sentMessage", {}).get("destinationUuid")
account = c.message.raw_message.get("account", "")
source_name = msg["raw_message"].get("envelope", {}).get("sourceName", "")
log.info("1")
raw = json.loads(c.message.raw_message)
print(json.dumps(c.message.raw_message, indent=2))
#dest = c.message.raw_message.get("envelope", {}).get("syncMessage", {}).get("sentMessage", {}).get("destinationUuid")
dest = raw.get("envelope", {}).get("syncMessage", {}).get("sentMessage", {}).get("destinationUuid")
#account = c.message.raw_message.get("account", "")
account = raw.get("account", "")
log.info("2")
#source_name = msg["raw_message"].get("envelope", {}).get("sourceName", "")
source_name = raw.get("envelope", {}).get("sourceName", "")
log.info("3")
source_number = c.message.source_number
source_uuid = c.message.source_uuid
@@ -58,6 +70,21 @@ class HandleMessage(Command):
# Determine the identifier to use
identifier_uuid = dest if is_from_bot else source_uuid
cast = {
"type": "def",
"method": "xmpp",
"service": "signal",
# "sender": source_uuid,
"identifier": identifier_uuid,
"msg": text,
"detail": {
"reply_to_self": reply_to_self,
"reply_to_others": reply_to_others,
"is_outgoing_message": is_outgoing_message,
}
}
packed = msgpack.packb(cast, use_bin_type=True)
await redis.publish("component", packed)
# TODO: Permission checks
manips = await sync_to_async(list)(
@@ -116,8 +143,14 @@ class HandleMessage(Command):
text=result,
ts=ts + 1,
)
log.info("NOT SENDING CHECK CODE IS OK")
# log.info("NOT SENDING CHECK CODE IS OK")
# await natural.natural_send_message(c, result)
tss = await natural.natural_send_message(
result,
c.send,
c.start_typing,
c.stop_typing,
)
elif manip.mode == "notify":
title = f"[GIA] Suggested message to {person_identifier.person.name}"
manip.user.sendmsg(result, title=title)
@@ -180,8 +213,7 @@ class HandleMessage(Command):
)
#
async def pubsub():
redis = aioredis.from_url("unix://var/run/gia-redis.sock", db=10)
async def stream():
pubsub = redis.pubsub()
await pubsub.subscribe("processing")
@@ -211,7 +243,7 @@ class Command(BaseCommand):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
bot._event_loop = loop
loop.create_task(pubsub())
loop.create_task(stream())
bot.start()
try:
loop.run_forever()