From c2e4d3911783d31198cd1f1a5f0e987bf6d7fc9c Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Sat, 12 Aug 2023 12:17:53 +1000 Subject: [PATCH] Events, CurrentScan, pluginsState, ENABLE_PLUGINS work --- pialert/logger.py | 10 ++++++---- pialert/plugin.py | 13 +++++++++---- pialert/publishers/apprise.py | 35 ++++++++++++++++++++++------------- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/pialert/logger.py b/pialert/logger.py index 36f0ee64..eb7c6ee8 100755 --- a/pialert/logger.py +++ b/pialert/logger.py @@ -74,10 +74,12 @@ def print_log (pText): # textchars = bytearray({7,8,9,10,12,13,27} | set(range(0x20, 0x100)) - {0x7f}) # is_binary_string = lambda bytes: bool(bytes.translate(None, textchars)) -def append_file_binary (pPath, input): - file = open (pPath, 'ab') - file.write (input) - file.close() +def append_file_binary(file_path, input_data): + with open(file_path, 'ab') as file: + if isinstance(input_data, str): + input_data = input_data.encode('utf-8') # Encode string as bytes + file.write(input_data) + #------------------------------------------------------------------------------- diff --git a/pialert/plugin.py b/pialert/plugin.py index b85d7dd5..5b9b4280 100755 --- a/pialert/plugin.py +++ b/pialert/plugin.py @@ -20,7 +20,10 @@ class plugins_state: self.processScan = processScan #------------------------------------------------------------------------------- -def run_plugin_scripts(db, runType, pluginsState = plugins_state()): +def run_plugin_scripts(db, runType, pluginsState = None): + + if pluginsState == None: + pluginsState = plugins_state() # Header updateState(db,"Run: Plugins") @@ -604,13 +607,15 @@ def process_plugin_events(db, plugin, pluginsState): sql.executemany(q, sqlParams) db.commitDB() + + # perform scan if mapped to CurrentScan table + if dbTable == 'CurrentScan': + pluginsState.processScan = True db.commitDB() - # perform scan if mapped to CurrentScan table - if dbTable == 'CurrentScan': - pluginsState.processScan = True + return pluginsState diff --git a/pialert/publishers/apprise.py b/pialert/publishers/apprise.py index 9d065a3f..b497147b 100755 --- a/pialert/publishers/apprise.py +++ b/pialert/publishers/apprise.py @@ -14,29 +14,38 @@ def check_config(): return True #------------------------------------------------------------------------------- -def send (msg: noti_struc): +def send(msg: noti_struc): html = msg.html text = msg.text - #Define Apprise compatible payload (https://github.com/caronc/apprise-api#stateless-solution) + # Define Apprise compatible payload (https://github.com/caronc/apprise-api#stateless-solution) payload = html if conf.APPRISE_PAYLOAD == 'text': payload = text - _json_payload={ - "urls": conf.APPRISE_URL, - "title": "Pi.Alert Notifications", - "format": conf.APPRISE_PAYLOAD, - "body": payload + _json_payload = { + "urls": conf.APPRISE_URL, + "title": "Pi.Alert Notifications", + "format": conf.APPRISE_PAYLOAD, + "body": payload } try: - # try runnning a subprocess - p = subprocess.Popen(["curl","-i","-X", "POST" ,"-H", "Content-Type:application/json" ,"-d", json.dumps(_json_payload), conf.APPRISE_HOST], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - stdout, stderr = p.communicate() - # write stdout and stderr into .log files for debugging if needed - logResult (stdout, stderr) # TO-DO should be changed to mylog + # Construct the curl command with input from the JSON payload + curl_command = [ + "curl", "-i", "-X", "POST", "-H", "Content-Type:application/json", + conf.APPRISE_HOST + ] + + # Run the curl command with the JSON payload as input + completed_process = subprocess.run( + curl_command, input=json.dumps(_json_payload), + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True + ) + + # Log the stdout and stderr + logResult(completed_process.stdout, completed_process.stderr) # TO-DO should be changed to mylog except subprocess.CalledProcessError as e: - # An error occured, handle it + # An error occurred, handle it mylog('none', [e.output]) \ No newline at end of file