1
0
mirror of https://github.com/craigerl/aprsd.git synced 2025-09-02 13:17:54 -04:00

Update Listen command

This patch updates the aprsd listen command to add the packet-plugins
argument which allows enabling a single plugin to work against the
packets recieved from the aprsis network.
This commit is contained in:
Hemna 2023-04-17 10:51:17 -04:00
parent 8a456cac48
commit 483afce5ad
4 changed files with 57 additions and 10 deletions

View File

@ -101,8 +101,6 @@ def test_plugin(
pm = plugin.PluginManager() pm = plugin.PluginManager()
if load_all: if load_all:
pm.setup_plugins() pm.setup_plugins()
else:
pm._init()
obj = pm._create_class(plugin_path, plugin.APRSDPluginBase) obj = pm._create_class(plugin_path, plugin.APRSDPluginBase)
if not obj: if not obj:
click.echo(ctx.get_help()) click.echo(ctx.get_help())
@ -116,7 +114,7 @@ def test_plugin(
obj.__class__, obj.version, obj.__class__, obj.version,
), ),
) )
pm._pluggy_pm.register(obj) pm.register_msg(obj)
packet = packets.MessagePacket( packet = packets.MessagePacket(
from_call=fromcall, from_call=fromcall,

View File

@ -15,7 +15,7 @@ from rich.console import Console
# local imports here # local imports here
import aprsd import aprsd
from aprsd import cli_helper, client, packets, stats, threads from aprsd import cli_helper, client, packets, plugin, stats, threads
from aprsd.aprsd import cli from aprsd.aprsd import cli
from aprsd.threads import rx from aprsd.threads import rx
@ -40,9 +40,12 @@ def signal_handler(sig, frame):
class APRSDListenThread(rx.APRSDRXThread): class APRSDListenThread(rx.APRSDRXThread):
def __init__(self, packet_queue, packet_filter=None): def __init__(self, packet_queue, packet_filter=None, plugin_manager=None):
super().__init__(packet_queue) super().__init__(packet_queue)
self.packet_filter = packet_filter self.packet_filter = packet_filter
self.plugin_manager = plugin_manager
if self.plugin_manager:
LOG.info(f"Plugins {self.plugin_manager.get_message_plugins()}")
def process_packet(self, *args, **kwargs): def process_packet(self, *args, **kwargs):
packet = self._client.decode_packet(*args, **kwargs) packet = self._client.decode_packet(*args, **kwargs)
@ -59,8 +62,17 @@ class APRSDListenThread(rx.APRSDRXThread):
filter_class = filters[self.packet_filter] filter_class = filters[self.packet_filter]
if isinstance(packet, filter_class): if isinstance(packet, filter_class):
packet.log(header="RX") packet.log(header="RX")
if self.plugin_manager:
# Don't do anything with the reply
# This is the listen only command.
self.plugin_manager.run(packet)
else: else:
packet.log(header="RX") if self.plugin_manager:
# Don't do anything with the reply.
# This is the listen only command.
self.plugin_manager.run(packet)
else:
packet.log(header="RX")
packets.PacketList().rx(packet) packets.PacketList().rx(packet)
@ -94,6 +106,11 @@ class APRSDListenThread(rx.APRSDRXThread):
), ),
help="Filter by packet type", help="Filter by packet type",
) )
@click.option(
"--packet-plugins",
default=None,
help="CSV, List of aprsd plugins to enable",
)
@click.argument( @click.argument(
"filter", "filter",
nargs=-1, nargs=-1,
@ -106,6 +123,7 @@ def listen(
aprs_login, aprs_login,
aprs_password, aprs_password,
packet_filter, packet_filter,
packet_plugins,
filter, filter,
): ):
"""Listen to packets on the APRS-IS Network based on FILTER. """Listen to packets on the APRS-IS Network based on FILTER.
@ -162,10 +180,20 @@ def listen(
keepalive = threads.KeepAliveThread() keepalive = threads.KeepAliveThread()
keepalive.start() keepalive.start()
LOG.info(f"Packet plugins {packet_plugins}")
pm = None
if packet_plugins:
LOG.info(f"Load plugins! {packet_plugins}")
pm = plugin.PluginManager()
for plugin_path in packet_plugins.split(","):
pm._load_plugin(plugin_path)
LOG.debug("Create APRSDListenThread") LOG.debug("Create APRSDListenThread")
listen_thread = APRSDListenThread( listen_thread = APRSDListenThread(
packet_queue=threads.packet_queue, packet_queue=threads.packet_queue,
packet_filter=packet_filter, packet_filter=packet_filter,
plugin_manager=pm,
) )
LOG.debug("Start APRSDListenThread") LOG.debug("Start APRSDListenThread")
listen_thread.start() listen_thread.start()

View File

@ -183,6 +183,16 @@ class Packet(metaclass=abc.ABCMeta):
self.prepare() self.prepare()
return self.raw return self.raw
def __repr__(self):
"""Build the repr version of the packet."""
repr = (
f"{self.__class__.__name__}:"
f" From: {self.from_call} "
" To: "
)
return repr
@dataclass @dataclass
class PathPacket(Packet): class PathPacket(Packet):

View File

@ -338,6 +338,7 @@ class PluginManager:
return cls._instance return cls._instance
def __init__(self, config=None): def __init__(self, config=None):
self._init()
self.obj_list = [] self.obj_list = []
if config: if config:
self.config = config self.config = config
@ -422,10 +423,10 @@ class PluginManager:
self._watchlist_pm.register(plugin_obj) self._watchlist_pm.register(plugin_obj)
else: else:
LOG.warning(f"Plugin {plugin_obj.__class__.__name__} is disabled") LOG.warning(f"Plugin {plugin_obj.__class__.__name__} is disabled")
else: elif isinstance(plugin_obj, APRSDRegexCommandPluginBase):
if plugin_obj.enabled: if plugin_obj.enabled:
LOG.info( LOG.info(
"Registering plugin '{}'({}) -- {}".format( "Registering Regex plugin '{}'({}) -- {}".format(
plugin_name, plugin_name,
plugin_obj.version, plugin_obj.version,
plugin_obj.command_regex, plugin_obj.command_regex,
@ -434,6 +435,17 @@ class PluginManager:
self._pluggy_pm.register(plugin_obj) self._pluggy_pm.register(plugin_obj)
else: else:
LOG.warning(f"Plugin {plugin_obj.__class__.__name__} is disabled") LOG.warning(f"Plugin {plugin_obj.__class__.__name__} is disabled")
elif isinstance(plugin_obj, APRSDPluginBase):
if plugin_obj.enabled:
LOG.info(
"Registering Base plugin '{}'({})".format(
plugin_name,
plugin_obj.version,
),
)
self._pluggy_pm.register(plugin_obj)
else:
LOG.warning(f"Plugin {plugin_obj.__class__.__name__} is disabled")
except Exception as ex: except Exception as ex:
LOG.error(f"Couldn't load plugin '{plugin_name}'") LOG.error(f"Couldn't load plugin '{plugin_name}'")
LOG.exception(ex) LOG.exception(ex)
@ -447,7 +459,6 @@ class PluginManager:
"""Create the plugin manager and register plugins.""" """Create the plugin manager and register plugins."""
LOG.info("Loading APRSD Plugins") LOG.info("Loading APRSD Plugins")
self._init()
# Help plugin is always enabled. # Help plugin is always enabled.
_help = HelpPlugin() _help = HelpPlugin()
self._pluggy_pm.register(_help) self._pluggy_pm.register(_help)
@ -465,7 +476,7 @@ class PluginManager:
LOG.info("Completed Plugin Loading.") LOG.info("Completed Plugin Loading.")
def run(self, packet: packets.core.MessagePacket): def run(self, packet: packets.core.MessagePacket):
"""Execute all the pluguns run method.""" """Execute all the plugins run method."""
with self.lock: with self.lock:
return self._pluggy_pm.hook.filter(packet=packet) return self._pluggy_pm.hook.filter(packet=packet)