]> git.treefish.org Git - mtxbot.git/commitdiff
discover room uninvite
authorAlexander Schmidt <alex@treefish.org>
Sat, 3 Oct 2020 10:44:51 +0000 (12:44 +0200)
committerAlexander Schmidt <alex@treefish.org>
Sat, 3 Oct 2020 10:44:51 +0000 (12:44 +0200)
src/presence.py

index 4c7e966a5d9cd4de89439d4662a4fb425a772c5a..2f348d65f2280fe2870ce7eb59d3458fa57f9b27 100644 (file)
@@ -8,10 +8,12 @@ class Presence:
     def __init__(self, room_name, fifo_path):
         self._room_name = room_name
         self._fifo_path = fifo_path
+        self._is_member = False
 
     async def run(self, client):
         self._log(logging.INFO, "Presence running")
         self._stop = False
+        asyncio.create_task( self._do_watch(client) )
         while not self._stop:
             room_id = await self._enter_room(client)
             try:
@@ -27,25 +29,17 @@ class Presence:
 
     def _do_stop(self):
         self._stop = True
-        fifo = -1
-        try:
-            fifo = posix.open(self._fifo_path, posix.O_WRONLY | posix.O_NONBLOCK)
-            posix.write(fifo, "\n".encode())
-        except OSError as e:
-            if e.errno == errno.ENXIO:
-                pass
-        finally:
-            if fifo != -1:
-                posix.close(fifo)
+        self._tickle_fifo()
 
     async def _enter_room(self, client):
         while not self._stop:
             await client.sync()
 
-            for room_id, room in client.rooms.items():
-                if room.display_name == self._room_name:
-                    self._log(logging.INFO, "A room member already")
-                    return room_id
+            room_id = await self._joined_room_id(client)
+            if room_id != None:
+                self._log(logging.INFO, "A room member already")
+                self._is_member = True
+                return room_id
 
             self._log(logging.INFO, "Not yet a room member")
 
@@ -62,11 +56,37 @@ class Presence:
 
             await asyncio.sleep(3.0)
 
+    async def _do_watch(self, client):
+        while not self._stop:
+            self._log(logging.INFO, "WATCHING...")
+
+            await client.sync()
+
+            if await self._joined_room_id(client) == None:
+                self._is_member = False
+                self._tickle_fifo()
+            else:
+                self._is_member = True
+
+            await asyncio.sleep(3.0)
+
+    def _tickle_fifo(self):
+        fifo = -1
+        try:
+            fifo = posix.open(self._fifo_path, posix.O_WRONLY | posix.O_NONBLOCK)
+            posix.write(fifo, "\n".encode())
+        except OSError as e:
+            if e.errno == errno.ENXIO:
+                pass
+        finally:
+            if fifo != -1:
+                posix.close(fifo)
+
     async def _process_messages(self, client, room_id):
-        if self._stop:
+        if self._stop or not self._is_member:
             return
         async for line in self._read_fifo(self._fifo_path):
-            if self._stop:
+            if self._stop or not self._is_member:
                 break
             await client.room_send(
                 room_id=room_id,
@@ -77,6 +97,14 @@ class Presence:
                 }
             )
 
+    async def _joined_room_id(self, client):
+        joined_room_ids = ( await client.joined_rooms() ).rooms
+        for room_id, room in client.rooms.items():
+            if room.display_name == self._room_name:
+                if room_id in joined_room_ids:
+                    return room_id
+        return None
+
     def _log(self, level, msg, *args, **kwargs):
         logging.log(level, "P{%s}: %s" % (self._room_name, msg), *args, **kwargs)