From e9b9a1edb920ebe601acebf117e32228f1872cea Mon Sep 17 00:00:00 2001
From: Simone Margaritelli <evilsocket@gmail.com>
Date: Sat, 5 Oct 2019 13:25:34 +0200
Subject: [PATCH] new: new auto-update plugin

---
 sdcard/rootfs/root/pwnagotchi/config.yml      |  3 +
 .../pwnagotchi/plugins/default/auto-backup.py |  6 +-
 .../pwnagotchi/plugins/default/auto-update.py | 59 +++++++++++++++++++
 3 files changed, 65 insertions(+), 3 deletions(-)
 create mode 100644 sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/auto-update.py

diff --git a/sdcard/rootfs/root/pwnagotchi/config.yml b/sdcard/rootfs/root/pwnagotchi/config.yml
index 24bcdb7..7a2fa4f 100644
--- a/sdcard/rootfs/root/pwnagotchi/config.yml
+++ b/sdcard/rootfs/root/pwnagotchi/config.yml
@@ -6,6 +6,9 @@ main:
     custom_plugins:
     # which plugins to load and enable
     plugins:
+      auto-update:
+        enabled: false
+        interval: 1 # every day
       auto-backup:
         enabled: false
         interval: 1 # every day
diff --git a/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/auto-backup.py b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/auto-backup.py
index 1e4acb9..5bc186d 100644
--- a/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/auto-backup.py
+++ b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/auto-backup.py
@@ -10,7 +10,7 @@ import subprocess
 from datetime import datetime
 
 OPTIONS = dict()
-LAST_BACKUP = None
+LAST_UPDATE = None
 READY = False
 
 def on_loaded():
@@ -18,7 +18,7 @@ def on_loaded():
     Gets called when the plugin gets loaded
     """
     global READY
-    global LAST_BACKUP
+    global LAST_UPDATE
 
     if 'files' not in OPTIONS or ('files' in OPTIONS and OPTIONS['files'] is None):
         logging.error("AUTO-BACKUP: No files to backup.")
@@ -42,7 +42,7 @@ def on_internet_available(display, config, log):
     """
     Called in manual mode when there's internet connectivity
     """
-    global LAST_BACKUP
+    global LAST_UPDATE
 
     if READY:
         if LAST_BACKUP is not None:
diff --git a/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/auto-update.py b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/auto-update.py
new file mode 100644
index 0000000..ca2d6fc
--- /dev/null
+++ b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/auto-update.py
@@ -0,0 +1,59 @@
+__author__ = 'evilsocket@gmail.com'
+__version__ = '1.0.0'
+__name__ = 'auto-update'
+__license__ = 'GPL3'
+__description__ = 'This plugin performs an "apt update && apt upgrade" when internet is availaible.'
+
+import os
+import logging
+import subprocess
+from datetime import datetime
+
+OPTIONS = dict()
+LAST_UPDATE = None
+READY = False
+STATUS_FILE = '/root/.auto-update'
+
+
+def on_loaded():
+    global READY
+    global LAST_UPDATE
+
+    if 'interval' not in OPTIONS or ('interval' in OPTIONS and OPTIONS['interval'] is None):
+        logging.error("AUTO-UPDATE: Interval is not set.")
+        return
+
+    if os.path.exists(STATUS_FILE):
+        LAST_UPDATE = datetime.fromtimestamp(os.path.getmtime(STATUS_FILE))
+
+    READY = True
+
+
+def on_internet_available(display, config, log):
+    global LAST_UPDATE
+
+    if READY:
+        if LAST_UPDATE is not None:
+            if (datetime.now() - LAST_UPDATE).days < OPTIONS['interval']:
+                return
+
+        try:
+            logging.info("AUTO-UPDATE: updating packages index ...")
+
+            update = subprocess.Popen('apt update -y', shell=True, stdin=None,
+                                      stdout=open("/dev/null", "w"), stderr=None, executable="/bin/bash")
+            update.wait()
+
+            logging.info("AUTO-UPDATE: updating packages ...")
+
+            upgrade = subprocess.Popen('apt upgrade -y', shell=True, stdin=None,
+                                       stdout=open("/dev/null", "w"), stderr=None, executable="/bin/bash")
+            upgrade.wait()
+
+            logging.info("AUTO-UPDATE: complete.")
+
+            LAST_UPDATE = datetime.now()
+            with open(STATUS_FILE, 'w') as f:
+                f.write('success')
+        except Exception as e:
+            logging.exception("AUTO-UPDATE ERROR")