From 370e5a9e1a2ce21dfb5a1c8c4663e7358b56ca2f Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@freedombone.net>
Date: Thu, 29 Nov 2018 10:32:29 +0000
Subject: [PATCH] Tidying

---
 src/freedombone-app-xmpp | 84 +++++++++++++++++++++++++++-------------
 1 file changed, 57 insertions(+), 27 deletions(-)

diff --git a/src/freedombone-app-xmpp b/src/freedombone-app-xmpp
index d0632ef04..0dead3cf5 100755
--- a/src/freedombone-app-xmpp
+++ b/src/freedombone-app-xmpp
@@ -676,6 +676,57 @@ function upgrade_xmppsend {
 
 }
 
+function prosody_remove_module_from_config {
+    remove_prosody_module_name="$1"
+    prosody_config_file="$2"
+
+    if [ ! "${remove_prosody_module_name}" ]; then
+        return
+    fi
+
+    if [ ! -f "${prosody_config_file}" ]; then
+        echo "Config file ${prosody_config_file} not found when removing module $remove_prosody_module_name"
+        return
+    fi
+
+    if grep -q "\"${remove_prosody_module_name}\"" "${prosody_config_file}"; then
+        sed -i "/\"${remove_prosody_module_name}\"/d" "${prosody_config_file}"
+        xmpp_restart=1
+    fi
+}
+
+function prosody_remove_module {
+    remove_prosody_module_name="$1"
+    prosody_remove_module_from_config "${remove_prosody_module_name}" /etc/prosody/prosody.cfg.lua
+    prosody_remove_module_from_config "${remove_prosody_module_name}" /etc/prosody/conf.avail/xmpp.cfg.lua
+}
+
+function prosody_add_module_to_config {
+    new_prosody_module_name="$1"
+    prosody_config_file="$2"
+
+    if [ ! "${new_prosody_module_name}" ]; then
+        return
+    fi
+
+    if [ ! -f "${prosody_config_file}" ]; then
+        echo "Config file ${prosody_config_file} not found when adding module $new_prosody_module_name"
+        return
+    fi
+
+    if ! grep -q "\"${new_prosody_module_name}\"" "$prosody_config_file"; then
+        sed -i "/\"pep\"/a \"${new_prosody_module_name}\";" "$prosody_config_file"
+        sed -i "s|\"${new_prosody_module_name}\"|        \"${new_prosody_module_name}\"|g" "$prosody_config_file"
+        xmpp_restart=1
+    fi
+}
+
+function prosody_add_module {
+    new_prosody_module_name="$1"
+    prosody_add_module_to_config "${new_prosody_module_name}" /etc/prosody/prosody.cfg.lua
+    prosody_add_module_to_config "${new_prosody_module_name}" /etc/prosody/conf.avail/xmpp.cfg.lua
+}
+
 function upgrade_xmpp {
     xmpp_restart=
 
@@ -698,42 +749,21 @@ function upgrade_xmpp {
     xmpp_server_blacklist /etc/prosody/prosody.cfg.lua
 
     # add word filter to muc
-    if ! grep -q '"filter_words"' /etc/prosody/prosody.cfg.lua; then
-        sed -i '/"muc_limits"/a "filter_words";' /etc/prosody/prosody.cfg.lua
-        sed -i 's|"filter_words"|        "filter_words"|g' /etc/prosody/prosody.cfg.lua
-        xmpp_restart=1
-    fi
+    prosody_add_module "filter_words"
     if ! grep -q 'filter_words =' /etc/prosody/prosody.cfg.lua; then
         echo 'filter_words = {}' >> /etc/prosody/prosody.cfg.lua
         xmpp_restart=1
     fi
 
-    # add battery saving
-    if grep -q '"csi_battery_saver"' /etc/prosody/prosody.cfg.lua; then
-        sed -i '/"csi"/a "csi_battery_saver"; -- Extra CSI battery saving' /etc/prosody/prosody.cfg.lua
-        sed -i 's|"csi_battery_saver"|        "csi_battery_saver"|g' /etc/prosody/prosody.cfg.lua
-        xmpp_restart=1
-    fi
+    # extra battery saving
+    prosody_add_module "csi_battery_saver"
 
     # remove muc logging
-    if grep -q '"muc_log"' /etc/prosody/prosody.cfg.lua; then
-        sed -i '/"muc_log"/d' /etc/prosody/prosody.cfg.lua
-        xmpp_restart=1
-    fi
-    if grep -q '"muc_log_http"' /etc/prosody/prosody.cfg.lua; then
-        sed -i '/"muc_log_http"/d' /etc/prosody/prosody.cfg.lua
-        xmpp_restart=1
-    fi
+    prosody_remove_module "muc_log"
+    prosody_remove_module "muc_log_http"
 
     # remove any broadcast settings
-    if grep -q '"broadcast"' /etc/prosody/prosody.cfg.lua; then
-        sed -i '/broadcast/d' /etc/prosody/prosody.cfg.lua
-        xmpp_restart=1
-    fi
-    if grep -q '"broadcast"' /etc/prosody/conf.avail/xmpp.cfg.lua; then
-        sed -i '/broadcast/d' /etc/prosody/conf.avail/xmpp.cfg.lua
-        xmpp_restart=1
-    fi
+    prosody_remove_module "broadcast"
 
     if grep -q "/etc/ssl/certs/xmpp.dhparam" /etc/prosody/prosody.cfg.lua; then
         cp /etc/ssl/certs/xmpp.dhparam /etc/prosody/xmpp.dhparam
-- 
GitLab