From 2ab225fc0eb2a5541dcdbaf39ff67bd1ffaec39f Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 15 Feb 2022 12:45:37 +0100 Subject: [PATCH] Removing Sieve script errors in migrating sieve --- migrate-sieve | 92 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 37 deletions(-) diff --git a/migrate-sieve b/migrate-sieve index ceaf0d0..ee2e4f9 100755 --- a/migrate-sieve +++ b/migrate-sieve @@ -9,7 +9,7 @@ QUIET='n' SIMULATE='n' FORCE="n" -VERSION="4.1" +VERSION="5.1" # console colors: RED="" @@ -521,6 +521,59 @@ get_all_sieve_dns() { } +#------------------------------------------------ +create_sieve() { + + local sieve_file="$1" + local result="$2" + local sieve_orig= + local requires= + local sieve="" + + if [[ -f "${sieve_file}" ]] ; then + if [[ "${FORCE}" == "y" ]] ; then + info "Sieve file '${CYAN}${sieve_file}${NORMAL}' wil be overriden." + else + info "Sieve file '${CYAN}${sieve_file}${NORMAL}' is already existing." + return + fi + fi + + sieve_orig=$( echo "${result}" | grep -i '^mailSieveRuleSource:' ) + if echo "${sieve_orig}" | grep -iq '^mailSieveRuleSource: ' ; then + sieve_orig=$( echo "${sieve_orig}" | sed -e 's/^mailSieveRuleSource: *//i' ) + else + sieve_orig=$( echo "${sieve_orig}" | sed -e 's/^mailSieveRuleSource:: *//i' | base64 -d ) + fi + + requires= + + requires=$( echo "${sieve_orig}" | grep '^[ ]*require ' | sed -e 's/^[ ]*//' ) + requires=$( echo "${requires}" | sed -e 's/ \("[^"]*"\)/ \[\1\]/g' | sort -u ) + if [[ -n "${requires}" ]] ; then + sieve="${requires}\n\n" + fi + sieve+=$( echo "${sieve_orig}" | \ + grep -v '^[ ]*require ' | \ + awk 'NF {p=1} p' | \ + sed -e 's/ *\(Comments\)/\1/gi' -e 's/ *\(Keywords\)/\1/gi' ) + + if [[ "${SIMULATE}" == "y" ]] ; then + info "Sieve script:" + if [[ $( echo -e "${sieve}" | wc -l ) -gt 8 ]] ; then + echo -e "${sieve}" | head -n 7 + echo "..." + else + echo -e "${sieve}" + fi + else + echo -e "${sieve}" > "${sieve_file}" + fi + CHMOD 0600 "${sieve_file}" + CHOWN "${VMAIL_USER}:${VMAIL_GROUP}" "${sieve_file}" + +} + #------------------------------------------------ migrate_entry() { @@ -562,7 +615,6 @@ migrate_entry() { local sieve_dir="${mailbox_dir}/sieve" local sieve_file="${sieve_dir}/${SIEVE_FILE_BASE}" local dovecot_link_target="sieve/${SIEVE_FILE_BASE}" - local sieve_info= local color_param='--color=always' if [[ "${HAS_COLORS}" == "n" ]] ; then @@ -574,41 +626,7 @@ migrate_entry() { ensure_vmail_dir "${mailbox_dir}" ensure_vmail_dir "${sieve_dir}" - local create_sieve="n" - - if [[ -f "${sieve_file}" ]] ; then - if [[ "${FORCE}" == "y" ]] ; then - info "Sieve file '${CYAN}${sieve_file}${NORMAL}' wil be overriden." - create_sieve="y" - else - info "Sieve file '${CYAN}${sieve_file}${NORMAL}' is already existing." - fi - else - create_sieve="y" - fi - - if [[ "${create_sieve}" == "y" ]] ; then - sieve_info=$( echo "${result}" | grep -i '^mailSieveRuleSource:' ) - if echo "${sieve_info}" | grep -iq '^mailSieveRuleSource: ' ; then - sieve_info=$( echo "${sieve_info}" | sed -e 's/^mailSieveRuleSource: *//i' ) - else - sieve_info=$( echo "${sieve_info}" | sed -e 's/^mailSieveRuleSource:: *//i' | base64 -d ) - fi - - if [[ "${SIMULATE}" == "y" ]] ; then - info "Sieve script:" - if [[ $( echo "${sieve_info}" | wc -l ) -gt 6 ]] ; then - echo "${sieve_info}" | head -n 5 - echo "..." - else - echo "${sieve_info}" - fi - else - echo "${sieve_info}" > "${sieve_file}" - fi - CHMOD 0600 "${sieve_file}" - CHOWN "${VMAIL_USER}:${VMAIL_GROUP}" "${sieve_file}" - fi + create_sieve "${sieve_file}" "${result}" if [[ -e "${sieve_file}" && "${QUIET}" != "y" ]] ; then ls -l "${color_param}" "${sieve_file}" -- 2.39.5