This patch extends the save/restore profile function so that it saves the 
contents of pgpRules.xml too. When restoring a saved profile all existing 
per-recipient rules are _deleted_ and replaced with the ones in the saved 
profile.

---
 ui/content/pref-enigmail.js | 70 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 69 insertions(+), 1 deletion(-)

diff --git a/ui/content/pref-enigmail.js b/ui/content/pref-enigmail.js
index 03eb502..ff55f11 100644
--- a/ui/content/pref-enigmail.js
+++ b/ui/content/pref-enigmail.js
@@ -305,6 +305,7 @@ function backupPrefs() {
     var prefObj = {};
     var nsIPB = Components.interfaces.nsIPrefBranch;
 
+    // user preference
     for (var p in allPrefs) {
       var name = allPrefs[p];
 
@@ -328,6 +329,9 @@ function backupPrefs() {
       }
     }
 
+    // per-recipient rules (aka pgpRules.xml)
+    prefObj.rules = rulesToObject();
+
     // serialize everything to UTF-8 encoded JSON.
     var strm = Components.classes["@mozilla.org/network/file-output-stream;1"]
                          
.createInstance(Components.interfaces.nsIFileOutputStream);
@@ -345,6 +349,66 @@ function backupPrefs() {
   }
 }
 
+// Converts the contents of pgpRules.xml into a list of objects.
+function rulesToObject() {
+  var enigmailSvc = GetEnigmailSvc();
+  if (!enigmailSvc)
+    return [];
+
+  var rulesListObj={};
+  if (enigmailSvc.getRulesData(rulesListObj)) {
+    var rulesList=rulesListObj.value;
+    var ret = [];
+
+    if (rulesList.firstChild.nodeName=="parsererror") {
+      EnigAlert("Invalid pgprules.xml file:\n"+ 
rulesList.firstChild.textContent);
+      return [];
+    }
+
+    EnigmailLog.DEBUG("pref-enigmail.js: rulesToObject: keys loaded\n");
+
+    var node=rulesList.firstChild.firstChild;
+    while (node) {
+      if (node.tagName=="pgpRule") {
+        var userObj={
+              email: node.getAttribute("email"),
+              keyId: node.getAttribute("keyId"),
+              sign: node.getAttribute("sign"),
+              encrypt: node.getAttribute("encrypt"),
+              pgpMime: node.getAttribute("pgpMime"),
+              negate: "0"
+            };
+        if (node.getAttribute("negateRule")) {
+          userObj.negate=node.getAttribute("negateRule");
+        }
+
+        ret.push(userObj);
+      }
+      node = node.nextSibling;
+    }
+
+    return ret;
+  } else {
+    return [];
+  }
+}
+
+// _replaces_ the current pgpRules.xml with the rules given in lst
+function objectToRules(lst) {
+  var enigmailSvc = GetEnigmailSvc();
+  if (!enigmailSvc) {
+    Enigmail.ERROR("pref-enigmail.js: objectToRules failed to aquire Enigmail 
Service.\n");
+    return;
+  }
+
+  enigmailSvc.clearRules();
+
+  for (var i in lst) {
+    var obj = lst[i];
+    enigmailSvc.addRule(true, obj.email, obj.keyId, obj.sign, obj.encrypt, 
obj.pgpMime, obj.negate);
+  }
+}
+
 // Loads settings saved with backupPrefs() into Enigmail.
 function restorePrefs() {
   EnigmailLog.DEBUG("pref-enigmail.js: restorePrefs\n");
@@ -367,7 +431,11 @@ function restorePrefs() {
 
       // Set all options recorded in the JSON file.
       for (var name in prefObj) {
-        EnigSetPref(name,prefObj[name]);
+        if (name == "rules") {
+          objectToRules(prefObj[name]);
+        } else {
+          EnigSetPref(name,prefObj[name]);
+        }
       }
     } catch (ex) {
       EnigError(EnigGetString("cantReadBackupFile"));
-- 
2.3.6

_______________________________________________
enigmail-users mailing list
[email protected]
To unsubscribe or make changes to your subscription click here:
https://admin.hostpoint.ch/mailman/listinfo/enigmail-users_enigmail.net

Reply via email to