commit d307b30f81a161403424f28dbf039dbef767adf7
parent fddb6efdd40dfb3039c5195ebf8d766bbbda48a5
Author: Jaromil <jaromil@dyne.org>
Date:   Fri, 26 Dec 2014 16:33:10 +0100
convert old addressbook format to new one, backward compatibility check
Diffstat:
2 files changed, 67 insertions(+), 1 deletion(-)
diff --git a/src/jaro b/src/jaro
@@ -701,7 +701,11 @@ main()
     import)  import_addressbook ${PARAM} ;;
     "export")
             case "$PARAM" in
-                abook) export_abook ;;
+                abook)
+                    notice "Exporting old addressbook to new format"
+                    list=whitelist; export_abook
+                    list=blacklist; export_abook
+                    ;;
                 vcard) export_vcard ;;
                 *) export_vcard ;;
             esac
diff --git a/src/zlibs/addressbook b/src/zlibs/addressbook
@@ -32,6 +32,15 @@ create_addressbook() {
         return 1
     }
 
+    [[ -r "$MAILDIRS"/Addressbook ]] && {
+        notice "Updating the old addressbook to a new format"
+        _list=$list
+        list=blacklist; export_abook
+        list=whitelist; export_abook
+        list=$_list
+        mv "$MAILDIRS"/Addressbook "$MAILDIRS"/Addressbook.old 
+    }
+
     touch "$ab"
 
     # make sure is private
@@ -354,6 +363,59 @@ BEGIN { newcard=0; c=0; name=""; email=""; }
     notice "Done importing addresses"
 }
 
+# export old addressbook format to abook
+export_abook() {
+
+    [[ -r $MAILDIRS/Addressbook ]] || {
+        notice "Old addressbook format not found"
+        act "there is nothing to convert to abook"
+        return 1
+    }
+
+    lock $MAILDIRS/Addressbook
+
+    out=$MAILDIRS/$list.abook
+    act "Exporting $list to abook format: $out"
+    rm -f $out
+
+    func "launching SELECT email,name sqlite3 query"
+    addresses=`cat <<EOF | ${SQL} -column -header -batch $MAILDIRS/Addressbook \
+    | grep -v '^email'
+.width 40 100
+.mode list
+.separator '|'
+SELECT email, name FROM $list;
+EOF`
+
+    unlock $MAILDIRS/Addressbook
+    func "converting database into abook format"
+    cat <<EOF > $out
+# abook addressbook file
+
+[format]
+program=JaroMail
+version=$VERSION
+
+EOF
+    c=0
+    for a in ${(f)addresses}; do
+        _email="${(Q)a[(ws:|:)1]}"
+        # remove from name all what is an email between brackets
+        # crop (trim) all beginning and ending whitespaces from name
+        _name=`print ${(Q)a[(ws:|:)2]} | trim`
+        { test "${_email}" != "" } && {
+            cat <<EOF >> $out
+[${c}]
+name=${_name}
+email=${_email}
+
+EOF
+            c=$(( $c + 1 ))
+        }
+    done
+
+}
+
 # export addressbook to vcard
 export_vcard() {
     abook --convert --informat abook \