-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_spam_folders
executable file
·129 lines (112 loc) · 4.48 KB
/
create_spam_folders
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env bash
################################################################################
##
## Create IMAP spam sorting folders
##
## Copyright (C) 2015-2024 Michael Roland <[email protected]>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <https://www.gnu.org/licenses/>.
##
################################################################################
SCRIPT_NAME=$(basename $0)
SCRIPT_PATH=$(readlink -f "$(dirname $0)")
. "$SCRIPT_PATH/kolab_config_loader"
spam_drop="${IMAP_FOLDER_SPAM_DEFAULT}"
salearn_spam="${IMAP_FOLDER_SA_LEARN_SPAM_DEFAULT}"
salearn_ham="${IMAP_FOLDER_SA_LEARN_HAM_DEFAULT}"
usage() {
echo "Usage: ${SCRIPT_NAME} [options]"
echo "Create IMAP spam sorting folders (if missing)"
echo ""
echo -e "Options:"
echo -e "\t-D <folder> Incoming Spam folder (Spam drop)"
echo -e "\t (default: $spam_drop)"
echo -e "\t-S <folder> Spam sorting folder (for sa-learn-cyrus)"
echo -e "\t (default: $salearn_spam)"
echo -e "\t-H <folder> NoSpam (ham) sorting folder (for sa-learn-cyrus)"
echo -e "\t (default: $salearn_ham)"
echo -e "\t-h Show this message"
echo ""
echo "Copyright (c) 2015-2024 Michael Roland <[email protected]>"
echo "License GPLv3+: GNU GPL version 3 or later <https://www.gnu.org/licenses/>"
echo ""
echo "This is free software: you can redistribute and/or modify it under the"
echo "terms of the GPLv3+. There is NO WARRANTY; not even the implied warranty"
echo "of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
echo ""
}
while getopts ":h?D:S:H:" opt; do
case "$opt" in
h|\?)
if [ ! -z $OPTARG ] ; then
echo "${SCRIPT_NAME}: invalid option -- $OPTARG" >&2
fi
usage
exit 1
;;
D)
spam_drop=$OPTARG
;;
S)
salearn_spam=$OPTARG
;;
H)
salearn_ham=$OPTARG
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
if [ ! -z "$salearn_spam" ] ; then
if [ ! -z "$spam_drop" ] ; then
salearn_spam="$spam_drop${IMAP_HIERARCHY_SEP}$salearn_spam"
fi
fi
if [ ! -z "$salearn_ham" ] ; then
if [ ! -z "$spam_drop" ] ; then
salearn_ham="$spam_drop${IMAP_HIERARCHY_SEP}$salearn_ham"
fi
fi
echo "Scanning mailboxes for missing spam folders ..."
userprefix="${IMAP_NAMESPACE_USER}${IMAP_HIERARCHY_SEP}"
for mailbox in $(${KOLAB_CMD}list-mailboxes "$userprefix%") ; do
username=$(echo "$mailbox" | sed -n -E 's/^'"$IMAP_NAMESPACE_USER"'[\'"$IMAP_HIERARCHY_SEP"'](.*?)(@[^@]*)$/\1/p')
domain=$(echo "$mailbox" | sed -n -E 's/^'"$IMAP_NAMESPACE_USER"'[\'"$IMAP_HIERARCHY_SEP"'](.*?)(@[^@]*)$/\2/p')
if [ $(${KOLAB_CMD}user-info "$username$domain" | head -c1 | wc -c) -gt 0 ] ; then
# user exists, try to create folders
for folder in \
"$spam_drop" \
"$salearn_spam" \
"$salearn_ham" \
; do
if [ ! -z "$folder" ] ; then
foldermbx="$userprefix$username${IMAP_HIERARCHY_SEP}$folder$domain"
if [ $(${KOLAB_CMD}list-mailboxes "$foldermbx" | head -c1 | wc -c) -eq 0 ] ; then
echo "Creating missing folder '$folder' for user '$username$domain'"
${KOLAB_CMD}create-mailbox "$foldermbx"
${KOLAB_CMD}add-user-subscription "$username$domain" "$folder"
fi
foldertype="${IMAP_FOLDERTYPE_MAIL}"
if [ "$folder" == "$spam_drop" ] ; then
foldertype="${IMAP_FOLDERTYPE_JUNKMAIL}"
fi
if ! ${KOLAB_CMD}list-mailbox-metadata "$foldermbx" 2>/dev/null | grep -q -E '^\s*'"${IMAP_ATTRIBUTE_FOLDERTYPE}"'\s+' ; then
echo "Setting missing folder-type attribute '$foldertype' for folder '$folder' for user '$username$domain'"
${KOLAB_CMD}set-mailbox-metadata "$foldermbx" "${IMAP_ATTRIBUTE_FOLDERTYPE}" "$foldertype"
fi
fi
done
fi
done
echo "Done."