I will use this Thread as a collection of various Scripts and Hacks for the Community.
For even more you could have a look in my public github grommunio repository
And if you are IRC-User, i'm also idling in #grommunio on https://www.oftc.net
Suggestions are always welcome.
The thread is closed to keep it easy to read.
Just start a new thread with a reference to whatever.
Completion
apt-get|zypper install bash-completion python3-argcomplete
eval "$(register-python-argcomplete grommunio-admin --complete)"
# OR
apt-get|zypper install bash-completion grommunio-admin-api-bash-completion
zypper in ShellCheck
#OR
apt-get install shellcheck
grom_users
- Return List of all Users [ optional Filter on Domain ]
grom_doms() { grommunio-admin domain list |awk '{print $2}' ; }
grom_users() {
local doms
if [[ "$#" -ge 1 ]]; then
doms=$(grep "^${1}" <<< "$(grom_doms)")
else
doms=$(grom_doms)
fi
for dom in $doms; do \
grommunio-admin user query --format json-structured | \
jq -r '.[]|select(.username|endswith("'"${dom}"'"))|.username'
done
}
EXMDB - man
exmdb_list_folders
exmdb_list_folders() {
local U SHORT RE HEXIT
case $1 in
"$(grep "^$1" <<< $(grom_users))")
U="${1}"
shift;;
*)return 1;;
esac
for arg in "$@"
do
case "$arg" in
-s) SHORT=1;;
-r) RE="-r";;
-h) HEXIT=1;;
*);;
esac
done
grommunio-admin exmdb "${U}" folder list --format csv $RE |sed '1d' |awk -F, '{print $1";"$3}' | while read LINE; do
if [ ${HEXIT+x} ] ; then LINE=$(echo $LINE|awk -F";" '{printf "0x%x;%s\n", $1, $2}') ; fi
if [ ${SHORT+x} ] ; then LINE=$(echo $LINE|cut -d";" -f1) ; fi
echo "${LINE}"
done
}
exmdb_get_permissions
_exmdb_folders_long(){ exmdb_list_folders $1 -r -h ; }
_exmdb_folders_short(){ exmdb_list_folders $1 -r -h -s ; }
#GetFolderPermissions
exmdb_get_permissions() {
local folders owner short
_grom_users="${_grom_users:=$(grom_users)}"
local owner folders user folder
if [[ "$_grom_users" =~ $1 ]]; then
owner="${1}"
folders=${folders:="$(_exmdb_folders_long $owner)"}
short=${short:="$(_exmdb_folders_short $owner)"}
for name in $short; do
echo $(awk "/^$name;/" <<< $folders)
grommunio-admin exmdb "${owner}" folder permissions "${name}" ${user}
done
fi
}
complete -F _comp_grom_users exmdb_get_permissions
exmdb_set_stdFolders - Overwrite $_exmdb_std_folders
Example for MH that shouldn't have Full $Perms
# Default Folders
# 0x9;Oberste Ebene des Informationsspeichers
# 0xa;Gesendete Elemente
# 0xb;Gelöschte Elemente
# 0xc;Postausgang
# 0xd;Posteingang
# 0xe;Entwürfe
# 0xf;Kalender
# 0x10;Journal
# 0x11;Notizen
# 0x12;Aufgaben
_exmdb_std_folders="$(echo 0x{10..12} 0x{a..f})
exmdb_set_stdFolders() {
local owner user acl acls
if [[ "$#" -ge 3 ]]; then
owner="${1}"
user="${2}"
shift 2
acls="${@}"
grommunio-admin exmdb "${owner}" folder grant 0x9 "${user}" foldervisible
for folder in ${_exmdb_std_folders}; do
#hmmmm ... worked different $someday
for acl in $acls; do
grommunio-admin exmdb "${owner}" folder grant "${folder}" "${user}" "${acl}"
done
done
fi
}
# completion
ACL="all create createsubfolder deleteany deleteowned editany editowned foldercontact folderowner foldervisible freebusydetailed freebusysimple readany sendas storeowner"
_comp_exmdb_set_stdFolders() {
_grom_users="${_grom_users:=$(grom_users)}"
local cur
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
case $COMP_CWORD in
1)
COMPREPLY=( $(compgen -W "${_grom_users}" -- ${cur}) )
;;
2)
COMPREPLY=( $(compgen -W "${_grom_users}" -- ${cur}) )
;;
*)
COMPREPLY=( $(compgen -W "\$ACL ${ACL}" -- ${cur}) )
;;
esac
return 0
}
complete -F _comp_exmdb_set_stdFolders exmdb_set_stdFolders
Cleaning
_grom_query_trashbin_count() {
local user mdir sel
_grom_users="${_grom_users:=$(grom_users)}"
if [[ "$#" -eq 0 ]]; then
for user in $_grom_users; do
printf '%s;%s\n' "$user" "$(_grom_query_trashbin_count "${user}")"
done
fi
if [[ "$#" -eq 1 ]]; then
if [[ "$_grom_users" =~ "$1" ]]; then
user="$1"
mdir="$(_grom_query_maildir ${user})/exmdb/exchange.sqlite3"
sel="select count(message_id) from messages where parent_fid=0xb;"
echo $(sqlite3 ${mdir} "${sel}")
fi
fi
}
_grom_query_trashbin_ids() {
local user mdir sel
if [[ "$#" -eq 1 ]]; then
_grom_users="${_grom_users:=$(grom_users)}"
if [[ "$_grom_users" =~ "$1" ]]; then
mdir="$(_grom_query_maildir $1)/exmdb/exchange.sqlite3"
sel="select message_id from messages where parent_fid=0xb;"
printf '%s ' "$(sqlite3 ${mdir} "${sel}")"
fi
fi
}
_grom_empty_trashbin() {
local user msgid
_grom_users="${_grom_users:=$(grom_users)}"
if [[ "$#" -eq 0 ]]; then
for user in $_grom_users; do
_grom_empty_trashbin "${user}"
done
fi
if [[ "$#" -eq 1 ]]; then
if [[ "$_grom_users" =~ "$1" ]]; then
user="$1"
logger -t grom_empty_trashbin \
"$user: $(_grom_query_trashbin_count "$user") messages to remove"
for msgid in $(_grom_query_trashbin_ids ${user}); do
_grom_empty_trashbin ${user} ${msgid}
done
fi
fi
if [[ "$#" -eq 2 ]]; then
_grom_users="${_grom_users:=$(grom_users)}"
if [[ "$_grom_users" =~ "$1" ]]; then
user="$1"
msgid="$2"
(
printf '%s: %s\n' "${user}" "${msgid}"
/usr/libexec/gromox/delmsg -u "${user}" -f 0xb "${msgid}"
) | logger -t _grom_empty_trashbin
fi
fi
}
_grom_query_maildir(){
grommunio-admin user query --filter username="${1}" maildir
}
# gromox-2.10+
grom_cleanup() {
local maildir
for user in $(grom_users); do
yellow "User: ${user}"
maildir="$(_grom_query_maildir $user)"
printf 'purge-datafiles: %s\n' "${maildir}"
gromox-mbop -d "${maildir}" purge-datafiles
printf 'vacuum: %s\n' "${maildir}"
gromox-mbop -d "${maildir}" vacuum
done
echo -e "$MESSAGE"
}
# gromox-2.9-
grom_cleanup_old() {
local maildir
MESSAGE="\n\tTo really cleanup use: "$(blue "grom_cleanup")" "$(red "clean")"\n"
SWITCH="-n";
if [[ "$1" = "clean" ]]; then SWITCH="";MESSAGE=""; fi
for user in $(grom_users); do
yellow "User: ${user}:"
maildir="$(_grom_query_maildir $user)"
/usr/libexec/gromox/cleaner "${SWITCH}" -d "${maildir}" |grep --color=always -e "^" -e "[^ ]*[0-9](M|G)B"
printf 'vacuum: %s\n' "${maildir}" ; /usr/sbin/gromox-mbop vacuum "${maildir}"
done
echo -e "$MESSAGE"
}
Simple Bash-Completion
# completion
_comp_grom_users() {
_grom_users="${_grom_users:=$(grom_users)}"
local cur
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
case $COMP_CWORD in
1)
COMPREPLY=( $(compgen -W "${_grom_users}" -- ${cur}) )
compopt +o nospace
;;
esac
return 0
}
complete -F _comp_grom_users _grom_query_trashbin_count _grom_query_maildir _grom_query_trashbin_ids _grom_empty_trashbin