8 def __init__(self, room_name, fifo_path):
9 self._room_name = room_name
10 self._fifo_path = fifo_path
12 async def run(self, client):
13 self._log(logging.INFO, "Presence running")
16 room_id = await self._enter_room(client)
18 await self._process_messages(client, room_id)
19 except Exception as e:
20 self._log(logging.ERROR, "Error processing messages: %s", str(e))
21 self._log(logging.INFO, "Presence stopped")
27 fifo = posix.open(self._fifo_path, posix.O_WRONLY | posix.O_NONBLOCK)
28 posix.write(fifo, "\n".encode())
30 if e.errno == errno.ENXIO:
36 async def _enter_room(self, client):
40 for room_id, room in client.rooms.items():
41 if room.display_name == self._room_name:
42 self._log(logging.INFO, "A room member already")
45 self._log(logging.INFO, "Not yet a room member")
48 for room_id, room in client.invited_rooms.items():
49 if room.display_name == self._room_name:
51 self._log(logging.INFO, "Joining room")
52 await client.join(room_id)
56 self._log(logging.INFO, "Got no room invite yet")
58 await asyncio.sleep(3.0)
60 async def _process_messages(self, client, room_id):
63 async for line in self._read_fifo(self._fifo_path):
66 await client.room_send(
68 message_type="m.room.message",
71 "body": line.rstrip("\n")
75 def _log(self, level, msg, *args, **kwargs):
76 logging.log(level, "P{%s}: %s" % (self._room_name, msg), *args, **kwargs)
79 async def _read_fifo(file_path):
81 async with aiofiles.open(file_path) as fifo:
82 async for line in fifo: