commit 306d315d1d032bb350cd63419ba5b357b99c0e7e
parent 6eecd275c3b8be4fa84fc46c7420cf86756d7565
Author: Jaromil <jaromil@dyne.org>
Date:   Thu,  5 Feb 2015 11:02:02 +0000
extras: make roundcube-majordomo create sieve filters even when addressbook is empty
Diffstat:
1 file changed, 31 insertions(+), 17 deletions(-)
diff --git a/extras/roundcube-majordomo/roundomo b/extras/roundcube-majordomo/roundomo
@@ -1,6 +1,6 @@
 #!/usr/bin/env zsh
 #
-# Copyright (C) 2014 Denis Roio <jaromil@dyne.org>
+# Copyright (C) 2014-2015 Denis Roio <jaromil@dyne.org>
 #
 # This source  code is free  software; you can redistribute  it and/or
 # modify it under the terms of  the GNU Public License as published by
@@ -16,7 +16,7 @@
 # this source code; if not, write to:
 # Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-REVISION="Oct/2014"
+REVISION="Feb/2015"
 
 QUIET=0
 DEBUG=0
@@ -51,8 +51,11 @@ myquery() {
 
 
 whitelist() {
+    # check args
     [[ "$1" == "" ]] && { error "no user specified to whitelist()"; return 1 }    
     user="$1"
+
+    # retrieve userid in db
     check_user="select user_id from users where username='$user';"
     user_id=`print $check_user | myquery`
     
@@ -60,16 +63,18 @@ whitelist() {
     
     notice "Roundomo taking care of: $user (id: $user_id)"
 
+    # retrieve contacts in db
     list_contacts="select email from contacts where user_id='$user_id';"
     contacts=(`print $list_contacts | myquery`)
     contacts_count=${#contacts}
     act "contacts found in addressbook: ${contacts_count}"
     [[ $contacts_count == 0 ]] && return 1
     
+    # hash contacts to avoid duplicate computation
     contacts_hash="`print ${=contacts}|sha1sum|awk '{print $1}'`"
 
-    # if --force is used then refresh whitelist for all account even
-    # if no new contacts have been detected
+    # if --force is used then force refresh of whitelist even is hash
+    # is the same (meaning there are no new contacts)
     option_is_set -f || { 
         [[ -r ".$user" ]] && {
 	        contacts_hash_prev=`cat ".$user"`
@@ -82,24 +87,33 @@ whitelist() {
     
     cat <<EOF > ".$user.sieve"
 require ["fileinto","mailbox","variables"];
+EOF
+
+    # whitelist only if contacts are present: 
+    # makes sure priv is activated even with empty addressbook
+    [[ $contacts_count = 0 ]] || {
+        cat <<EOF >> ".$user.sieve"
 if header :contains "From" [
 EOF
-    for i in $contacts; do
-	    contacts_count=$(( $contacts_count - 1 ))
-        # a single entry may contain more comma separated addresses
-        ii=(${(s:,:)i}) 
-        for c in $ii; do
-	        print -n "\"$c\"" >> ".$user.sieve"
-	        [[ $contacts_count -gt 0 ]] && {
-	            print -n "," >> ".$user.sieve" }
+        for i in $contacts; do
+	        contacts_count=$(( $contacts_count - 1 ))
+            # a single entry may contain more comma separated addresses
+            ii=(${(s:,:)i}) 
+            for c in $ii; do
+	            print -n "\"$c\"" >> ".$user.sieve"
+	            [[ $contacts_count -gt 0 ]] && {
+	                print -n "," >> ".$user.sieve" }
+            done
+	        print >> ".$user.sieve"
         done
-	    print >> ".$user.sieve"
-    done
-    cat <<EOF >> ".$user.sieve"
+        cat <<EOF >> ".$user.sieve"
 ]
 { fileinto "INBOX"; stop; }
-
-# spam
+EOF
+    }
+    
+    # static rules: spam, priv, lists and social networks
+    cat <<EOF >> ".$user.sieve"
 if header :is "X-Spam-Flag" "YES" {
     fileinto :create "zz.spam"; stop;
 }