2014-07-05 15:05:17 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
This plugins allows commands to be sent to poezio via a named pipe.
|
|
|
|
|
2014-12-08 16:49:31 +00:00
|
|
|
You can run the same commands that you would in the poezio input
|
|
|
|
(e.g. ``echo '/message toto@example.tld Hi' >> /tmp/poezio.fifo``).
|
|
|
|
|
|
|
|
Configuration
|
|
|
|
-------------
|
|
|
|
|
|
|
|
.. glossary::
|
|
|
|
:sorted:
|
|
|
|
|
|
|
|
pipename
|
|
|
|
**Default:** :file:`/tmp/poezio.fifo`
|
|
|
|
|
|
|
|
The path to the fifo which will receive commands.
|
|
|
|
|
2014-07-05 15:05:17 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2016-06-27 23:10:52 +00:00
|
|
|
from poezio.plugin import BasePlugin
|
2014-07-05 15:05:17 +00:00
|
|
|
import os
|
|
|
|
import stat
|
|
|
|
import logging
|
2014-08-01 15:56:43 +00:00
|
|
|
import asyncio
|
2014-07-05 15:05:17 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
PIPENAME = "/tmp/poezio.fifo"
|
|
|
|
|
|
|
|
class Plugin(BasePlugin):
|
|
|
|
def init(self):
|
|
|
|
self.stop = False
|
|
|
|
|
|
|
|
self.pipename = self.config.get("pipename", PIPENAME)
|
|
|
|
|
|
|
|
if not os.path.exists(self.pipename):
|
|
|
|
os.mkfifo(self.pipename)
|
|
|
|
|
|
|
|
if not stat.S_ISFIFO(os.stat(self.pipename).st_mode):
|
2014-08-01 15:56:43 +00:00
|
|
|
raise TypeError("File %s is not a fifo file" % self.pipename)
|
|
|
|
|
|
|
|
self.fd = os.open(self.pipename, os.O_RDONLY|os.O_NONBLOCK)
|
|
|
|
|
|
|
|
self.data = b""
|
|
|
|
asyncio.get_event_loop().add_reader(self.fd, self.read_from_fifo)
|
|
|
|
|
|
|
|
def read_from_fifo(self):
|
|
|
|
data = os.read(self.fd, 512)
|
|
|
|
if not data:
|
|
|
|
# EOF, close the fifo. And reopen it
|
|
|
|
asyncio.get_event_loop().remove_reader(self.fd)
|
|
|
|
os.close(self.fd)
|
|
|
|
self.fd = os.open(self.pipename, os.O_RDONLY|os.O_NONBLOCK)
|
|
|
|
asyncio.get_event_loop().add_reader(self.fd, self.read_from_fifo)
|
|
|
|
self.data = b''
|
|
|
|
else:
|
|
|
|
self.data += data
|
|
|
|
l = self.data.split(b'\n', 1)
|
|
|
|
if len(l) == 2:
|
|
|
|
line, self.data = l
|
|
|
|
log.debug("run: %s" % (line.decode().strip()))
|
|
|
|
self.api.run_command(line.decode().strip())
|
2014-07-05 15:05:17 +00:00
|
|
|
|
|
|
|
def cleanup(self):
|
2014-08-01 15:56:43 +00:00
|
|
|
asyncio.get_event_loop().remove_reader(self.fd)
|
|
|
|
os.close(self.fd)
|