-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimap_fix_missing_folders
executable file
·111 lines (96 loc) · 4.57 KB
/
imap_fix_missing_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
#!/usr/bin/env bash
################################################################################
##
## Fix missing mailbox sub-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"
usage() {
echo "Usage: ${SCRIPT_NAME} [options]"
echo "Fix missing mailbox sub-folders"
echo ""
echo -e "Options:"
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?:" opt; do
case "$opt" in
h|\?)
if [ ! -z $OPTARG ] ; then
echo "${SCRIPT_NAME}: invalid option -- $OPTARG" >&2
fi
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
echo "Scanning user mailboxes for sub-folders without parent ..."
userprefix="${IMAP_NAMESPACE_USER}${IMAP_HIERARCHY_SEP}"
subfolders=$(${KOLAB_CMD}list-mailboxes "$userprefix*${IMAP_HIERARCHY_SEP}*${IMAP_HIERARCHY_SEP}*%")
last_parent_mbxname=""
while read mailbox <&3 ; do
mbxname=$(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')
username=$(echo "$mbxname" | sed -n -E 's/^([^\'"$IMAP_HIERARCHY_SEP"']+)[\'"$IMAP_HIERARCHY_SEP"'](.*)$/\1/p')
parent_mbxname=$(echo "$mbxname" | sed -n -E 's/^(.*)[\'"$IMAP_HIERARCHY_SEP"'](.*?)$/\1/p')
if [ "$last_parent_mbxname" == "$parent_mbxname" ] ; then
continue
fi
last_parent_mbxname="$parent_mbxname"
while [ -n "$parent_mbxname" ] && [ "$parent_mbxname" != "$username" ] ; do
if [ $(${KOLAB_CMD}list-mailboxes "$userprefix$parent_mbxname$domain" | head -c1 | wc -c) -eq 0 ] ; then
echo "Creating missing folder $parent_mbxname for user $username$domain ..."
${KOLAB_CMD}create-mailbox "$userprefix$parent_mbxname$domain"
fi
parent_mbxname=$(echo "$parent_mbxname" | sed -n -E 's/^(.*)[\'"$IMAP_HIERARCHY_SEP"'](.*?)$/\1/p')
done
done 3<<< "$subfolders"
echo "Scanning shared mailboxes for sub-folders without parent ..."
mbxprefix="${IMAP_NAMESPACE_SHARED}${IMAP_HIERARCHY_SEP}"
subfolders=$(${KOLAB_CMD}list-mailboxes "$mbxprefix*${IMAP_HIERARCHY_SEP}*${IMAP_HIERARCHY_SEP}*%")
last_parent_mbxname=""
while read mailbox <&3 ; do
mbxname=$(echo "$mailbox" | sed -n -E 's/^'"$IMAP_NAMESPACE_SHARED"'[\'"$IMAP_HIERARCHY_SEP"'](.*?)(@[^@]*)$/\1/p')
domain=$(echo "$mailbox" | sed -n -E 's/^'"$IMAP_NAMESPACE_SHARED"'[\'"$IMAP_HIERARCHY_SEP"'](.*?)(@[^@]*)$/\2/p')
username=$(echo "$mbxname" | sed -n -E 's/^([^\'"$IMAP_HIERARCHY_SEP"']+)[\'"$IMAP_HIERARCHY_SEP"'](.*)$/\1/p')
parent_mbxname=$(echo "$mbxname" | sed -n -E 's/^(.*)[\'"$IMAP_HIERARCHY_SEP"'](.*?)$/\1/p')
if [ "$last_parent_mbxname" == "$parent_mbxname" ] ; then
continue
fi
last_parent_mbxname="$parent_mbxname"
parent_mbx="$mailbox"
while [ -n "$parent_mbxname" ] && [ "$parent_mbxname" != "$username" ] ; do
if [ $(${KOLAB_CMD}list-mailboxes "$mbxprefix$parent_mbxname$domain" | head -c1 | wc -c) -eq 0 ] ; then
echo "Creating missing folder $parent_mbxname for shared mailbox $username$domain ..."
${KOLAB_CMD}create-mailbox "$mbxprefix$parent_mbxname$domain"
fi
parent_mbxname=$(echo "$parent_mbxname" | sed -n -E 's/^(.*)[\'"$IMAP_HIERARCHY_SEP"'](.*?)$/\1/p')
done
done 3<<< "$subfolders"
echo "Done."