From 3261710bf8d5b3dc8d038016825af39fa1cdbf98 Mon Sep 17 00:00:00 2001 From: Hemna Date: Sun, 20 Dec 2020 12:14:51 -0500 Subject: [PATCH] Fixed send-message with email command and others This patch fixes a minor issue with the new send-message command You now should use nargs to send the email command because it includes a - as the start. click assumed that any - looks ike an argument. So call aprsd with aprsd send-command -- -wb sendmap This patch also adds -h as a help option for aprsd to make it simpler to type. This patch adds the VersionPlugin so you can remotely request the version of aprsd that's running. --- aprsd/client.py | 8 +++++--- aprsd/main.py | 15 +++++++++++---- aprsd/messaging.py | 7 +++++-- aprsd/plugin.py | 18 ++++++++++++++++++ requirements.txt | 1 + 5 files changed, 40 insertions(+), 9 deletions(-) diff --git a/aprsd/client.py b/aprsd/client.py index cba9dd8..b3613dd 100644 --- a/aprsd/client.py +++ b/aprsd/client.py @@ -41,6 +41,7 @@ class Client(object): host = self.config["aprs"].get("host", "rotate.aprs.net") port = self.config["aprs"].get("port", 14580) connected = False + backoff = 1 while not connected: try: LOG.info("Creating aprslib client") @@ -49,10 +50,11 @@ class Client(object): aprs_client.logger = LOG aprs_client.connect() connected = True + backoff = 1 except Exception as e: - LOG.error("Unable to connect to APRS-IS server.\n") - print(str(e)) - time.sleep(5) + LOG.error("Unable to connect to APRS-IS server. '{}' ".format(e)) + time.sleep(backoff) + backoff = backoff * 2 continue LOG.debug("Logging in to APRS-IS with user '%s'" % user) return aprs_client diff --git a/aprsd/main.py b/aprsd/main.py index e5f8256..7a32edf 100644 --- a/aprsd/main.py +++ b/aprsd/main.py @@ -23,6 +23,7 @@ # python included libs import logging import os +import random import signal import sys import time @@ -50,6 +51,8 @@ LOG_LEVELS = { "DEBUG": logging.DEBUG, } +CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) + # localization, please edit: # HOST = "noam.aprs2.net" # north america tier2 servers round robin # USER = "KM6XXX-9" # callsign of this aprs client with SSID @@ -85,7 +88,7 @@ Default type: auto ) -@click.group(help=cmd_help) +@click.group(help=cmd_help, context_settings=CONTEXT_SETTINGS) @click.version_option() def main(): pass @@ -271,7 +274,7 @@ def sample_config(): help="the APRS-IS password for APRS_LOGIN", ) @click.argument("tocallsign") -@click.argument("command", default="location") +@click.argument("command", nargs=-1) def send_message( loglevel, quiet, config_file, aprs_login, aprs_password, tocallsign, command ): @@ -294,16 +297,20 @@ def send_message( setup_logging(config, loglevel, quiet) LOG.info("APRSD Started version: {}".format(aprsd.__version__)) + message_number = random.randint(1, 90) + if type(command) is tuple: + command = " ".join(command) + LOG.info("Sending Command '{}'".format(command)) def rx_packet(packet): - LOG.debug("Got packet back {}".format(packet)) + # LOG.debug("Got packet back {}".format(packet)) messaging.log_packet(packet) resp = packet.get("response", None) if resp == "ack": sys.exit(0) cl = client.Client(config) - messaging.send_message_direct(tocallsign, command) + messaging.send_message_direct(tocallsign, command, message_number) try: # This will register a packet consumer with aprslib diff --git a/aprsd/messaging.py b/aprsd/messaging.py index 9877c91..de46953 100644 --- a/aprsd/messaging.py +++ b/aprsd/messaging.py @@ -200,10 +200,13 @@ def log_message( LOG.info("\n".join(log_list)) -def send_message_direct(tocall, message): +def send_message_direct(tocall, message, message_number=None): """Send a message without a separate thread.""" cl = client.get_client() - this_message_number = 1 + if not message_number: + this_message_number = 1 + else: + this_message_number = message_number fromcall = CONFIG["aprs"]["login"] line = "{}>APRS::{}:{}{{{}\n".format( fromcall, diff --git a/aprsd/plugin.py b/aprsd/plugin.py index 6b88b7c..7c8c07e 100644 --- a/aprsd/plugin.py +++ b/aprsd/plugin.py @@ -15,6 +15,7 @@ import requests import six from thesmuggler import smuggle +import aprsd from aprsd import email, messaging from aprsd.fuzzyclock import fuzzy @@ -31,6 +32,7 @@ CORE_PLUGINS = [ "aprsd.plugin.PingPlugin", "aprsd.plugin.TimePlugin", "aprsd.plugin.WeatherPlugin", + "aprsd.plugin.VersionPlugin", ] @@ -480,3 +482,19 @@ class EmailPlugin(APRSDPluginBase): # messaging.send_message(fromcall, "Bad email address") return reply + + +class VersionPlugin(APRSDPluginBase): + """Version of APRSD Plugin.""" + + version = "1.0" + command_regex = "^[vV]" + command_name = "version" + + # message_number:time combos so we don't resend the same email in + # five mins {int:int} + email_sent_dict = {} + + def command(self, fromcall, message, ack): + LOG.info("Version COMMAND") + return "APRSD version '{}'".format(aprsd.__version__) diff --git a/requirements.txt b/requirements.txt index ba02e11..99304d9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -19,6 +19,7 @@ pbr==5.5.1 # via -r requirements.in pluggy==0.13.1 # via -r requirements.in py3-validate-email==0.2.12 # via -r requirements.in pyyaml==5.3.1 # via -r requirements.in +requests==2.25.1 # via -r requirements.in shellingham==1.3.2 # via click-completion six==1.15.0 # via -r requirements.in, click-completion, imapclient thesmuggler==1.0.1 # via -r requirements.in