2021-01-09 09:58:56 -05:00
|
|
|
import logging
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
|
2021-01-29 10:07:49 -05:00
|
|
|
from aprsd import plugin, 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")
|
|
|
|
|
|
|
|
|
2021-08-19 11:39:29 -04:00
|
|
|
class FortunePlugin(plugin.APRSDRegexCommandPluginBase):
|
2021-01-09 09:58:56 -05:00
|
|
|
"""Fortune."""
|
|
|
|
|
|
|
|
version = "1.0"
|
|
|
|
command_regex = "^[fF]"
|
|
|
|
command_name = "fortune"
|
|
|
|
|
2021-11-10 11:01:10 -05:00
|
|
|
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
|
2021-11-10 11:01:10 -05:00
|
|
|
|
2021-01-29 10:07:49 -05:00
|
|
|
@trace.trace
|
2021-08-19 11:39:29 -04:00
|
|
|
def process(self, packet):
|
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
|