1
0
mirror of https://github.com/craigerl/aprsd.git synced 2024-10-31 15:07:13 -04:00
aprsd/aprsd/plugins/fortune.py

58 lines
1.3 KiB
Python
Raw Normal View History

import logging
import shutil
import subprocess
from aprsd import plugin, trace
LOG = logging.getLogger("APRSD")
class FortunePlugin(plugin.APRSDRegexCommandPluginBase):
"""Fortune."""
version = "1.0"
command_regex = "^[fF]"
command_name = "fortune"
fortune_path = None
def setup(self):
self.fortune_path = shutil.which("fortune")
if not self.fortune_path:
self.enabled = False
2021-11-10 11:51:21 -05:00
else:
self.enabled = True
@trace.trace
def process(self, packet):
LOG.info("FortunePlugin")
# fromcall = packet.get("from")
# message = packet.get("message_text", None)
# ack = packet.get("msgNo", "0")
reply = None
try:
cmnd = [self.fortune_path, "-s", "-n 60"]
command = " ".join(cmnd)
output = subprocess.check_output(
command,
shell=True,
timeout=3,
universal_newlines=True,
)
2021-01-17 11:02:45 -05:00
output = (
output.replace("\r", "")
.replace("\n", "")
.replace(" ", "")
.replace("\t", " ")
)
except subprocess.CalledProcessError as ex:
reply = f"Fortune command failed '{ex.output}'"
else:
reply = output
return reply