2021-01-09 09:58:56 -05:00
|
|
|
import logging
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
|
2022-12-14 22:03:21 -05:00
|
|
|
from aprsd import packets, plugin
|
2022-07-07 10:47:34 -04:00
|
|
|
from aprsd.utils import trace
|
2021-01-09 09:58:56 -05:00
|
|
|
|
2021-08-23 12:14:19 -04:00
|
|
|
|
2021-01-09 09:58:56 -05:00
|
|
|
LOG = logging.getLogger("APRSD")
|
|
|
|
|
2024-01-08 20:30:43 -05:00
|
|
|
DEFAULT_FORTUNE_PATH = '/usr/games/fortune'
|
|
|
|
|
2021-01-09 09:58:56 -05:00
|
|
|
|
2021-08-19 11:39:29 -04:00
|
|
|
class FortunePlugin(plugin.APRSDRegexCommandPluginBase):
|
2021-01-09 09:58:56 -05:00
|
|
|
"""Fortune."""
|
|
|
|
|
2022-12-29 14:18:38 -05:00
|
|
|
command_regex = r"^([f]|[f]\s|fortune)"
|
2021-01-09 09:58:56 -05:00
|
|
|
command_name = "fortune"
|
2021-11-12 11:36:22 -05:00
|
|
|
short_description = "Give me a fortune"
|
2021-01-09 09:58:56 -05:00
|
|
|
|
2021-11-10 11:01:10 -05:00
|
|
|
fortune_path = None
|
|
|
|
|
|
|
|
def setup(self):
|
2024-01-08 20:30:43 -05:00
|
|
|
self.fortune_path = shutil.which(DEFAULT_FORTUNE_PATH)
|
|
|
|
LOG.info(f"Fortune path {self.fortune_path}")
|
2021-11-10 11:01:10 -05:00
|
|
|
if not self.fortune_path:
|
|
|
|
self.enabled = False
|
2021-11-10 11:51:21 -05:00
|
|
|
else:
|
|
|
|
self.enabled = True
|
2021-11-10 11:01:10 -05:00
|
|
|
|
2021-01-29 10:07:49 -05:00
|
|
|
@trace.trace
|
2022-12-14 22:03:21 -05:00
|
|
|
def process(self, packet: packets.MessagePacket):
|
2021-01-09 09:58:56 -05:00
|
|
|
LOG.info("FortunePlugin")
|
2021-07-14 20:50:41 -04:00
|
|
|
|
|
|
|
# fromcall = packet.get("from")
|
|
|
|
# message = packet.get("message_text", None)
|
|
|
|
# ack = packet.get("msgNo", "0")
|
|
|
|
|
2021-01-09 09:58:56 -05:00
|
|
|
reply = None
|
|
|
|
|
|
|
|
try:
|
2021-11-10 11:01:10 -05:00
|
|
|
cmnd = [self.fortune_path, "-s", "-n 60"]
|
2021-01-11 14:13:20 -05:00
|
|
|
command = " ".join(cmnd)
|
|
|
|
output = subprocess.check_output(
|
|
|
|
command,
|
|
|
|
shell=True,
|
|
|
|
timeout=3,
|
|
|
|
universal_newlines=True,
|
2021-01-09 09:58:56 -05:00
|
|
|
)
|
2021-01-17 11:02:45 -05:00
|
|
|
output = (
|
|
|
|
output.replace("\r", "")
|
|
|
|
.replace("\n", "")
|
|
|
|
.replace(" ", "")
|
|
|
|
.replace("\t", " ")
|
|
|
|
)
|
2021-01-11 14:13:20 -05:00
|
|
|
except subprocess.CalledProcessError as ex:
|
2021-08-23 12:14:19 -04:00
|
|
|
reply = f"Fortune command failed '{ex.output}'"
|
2021-01-11 14:13:20 -05:00
|
|
|
else:
|
|
|
|
reply = output
|
2021-01-09 09:58:56 -05:00
|
|
|
|
|
|
|
return reply
|