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")
 
  24         asyncio.get_running_loop().call_soon_threadsafe(
 
  25             Presence._do_stop, self
 
  32             fifo = posix.open(self._fifo_path, posix.O_WRONLY | posix.O_NONBLOCK)
 
  33             posix.write(fifo, "\n".encode())
 
  35             if e.errno == errno.ENXIO:
 
  41     async def _enter_room(self, client):
 
  45             for room_id, room in client.rooms.items():
 
  46                 if room.display_name == self._room_name:
 
  47                     self._log(logging.INFO, "A room member already")
 
  50             self._log(logging.INFO, "Not yet a room member")
 
  53             for room_id, room in client.invited_rooms.items():
 
  54                 if room.display_name == self._room_name:
 
  56                     self._log(logging.INFO, "Joining room")
 
  57                     await client.join(room_id)
 
  61                 self._log(logging.INFO, "Got no room invite yet")
 
  63             await asyncio.sleep(3.0)
 
  65     async def _process_messages(self, client, room_id):
 
  68         async for line in self._read_fifo(self._fifo_path):
 
  71             await client.room_send(
 
  73                 message_type="m.room.message",
 
  76                     "body": line.rstrip("\n")
 
  80     def _log(self, level, msg, *args, **kwargs):
 
  81         logging.log(level, "P{%s}: %s" % (self._room_name, msg), *args, **kwargs)
 
  84     async def _read_fifo(file_path):
 
  86             async with aiofiles.open(file_path) as fifo:
 
  87                 async for line in fifo: