This version deals gracefully with situations where Calendar or Meeting Notifications folder have already been mounted by assistant.
Code:
# ! /bin/sh
#
# Script to perform Calendar delegation between accounts
# Author: Elliot Wilen
#
# Usage ./zmdelegate source_account delegate_account
#
#
# Name of notifications folder
notifications="Meeting Notifications"
# Name of filter used on boss's account
filtername="Meeting Notifications"
# variable to look at response to interactive prompt
response=
# Note: could do all checking and then output list of all problems
# instead of exiting after first detected problem
#
# Verify exactly two arguments
# (Could use shell Shift command to allow multiple delegation)
if test $# -ne 2
then
echo "Must supply exactly two arguments."
exit 1
fi
# Assign variables
boss_account=$1
assistant_account=$2
# Verify <boss_account> and <assistant_account> exist on the server
if ! zmmailbox -z -m $boss_account gms 1>/dev/null 2>/dev/null
then
echo "Boss account doesn't exist. Exiting."
exit 1
fi
if ! zmmailbox -z -m $assistant_account gms 1>/dev/null 2>/dev/null
then
echo "Assistant account doesn't exist. Exiting."
exit 1
fi
# Check if Meeting Notifications folder already exists; if it does, then post warning and ask if want to continue.
# Otherwise Create Meeting Notifications folder
if zmmailbox -z -m $boss_account gf "$notifications" 1>/dev/null 2>/dev/null
then
while [ "$response" != "y" -a "$response" != "n" ]
do
echo -e 'Folder "\c'
echo -e "$notifications\c"
echo -e '" already exists, use it to store/share notification messages? (y/n) \c'
read response
done
if [ "$response" = n ]
then
echo "Command cancelled."
exit 1
fi
else
echo -e 'Creating folder "\c'
echo -e "$notifications\c"
echo -e '".'
zmmailbox -z -m $boss_account cf -V message "/$notifications" 1>/dev/null 2>/dev/null
fi
# Share Meeting Notifications folder and Calendar to assistant with Manager rights.
echo "Sharing Meeting Notifications folder."
zmmailbox -z -m $boss_account mfg "/$notifications" account $assistant_account rwidx
echo "Sharing Calendar."
zmmailbox -z -m $boss_account mfg "/Calendar" account $assistant_account rwidx
# I have to use account names in names of mountpoints because I don't see
# a way to discover display names via CLI. Could prompt for user-defined sharepoint
# names.
# Check if Meeting Notifications is already mounted. (Edge case: it could be mounted but in the Trash.)
if zmmailbox -z -m $assistant_account gaf | grep \($boss_account\:`zmmailbox -z -m $boss_account gaf | awk '/\/Meeting\ Notifications$/ {print $1}'`\) 1>/dev/null
then
echo "Meeting Notifications is already mounted in assistant account...proceeding..."
else
# Check for name conflict.
if zmmailbox -z -m $assistant_account gf "/$boss_account $notifications" 1>/dev/null 2>/dev/null
then
echo -e 'Naming conflict--delete or rename folder "\c'
echo -e "$boss_account $notifications\c"
echo -e '" on assistant account.'
exit 1
else
# Accept mail folder share by assistant (only for web interface; Outlook must be done at the workstation)
echo "Mounting Meeting Notifications folder on assistant account. Right-click in Zimbra Web Client to change name."
zmmailbox -z -m $assistant_account cm -F# "/$boss_account $notifications" $boss_account "/$notifications" 1>/dev/null
fi
fi
# Check if Calendar is already mounted.
if zmmailbox -z -m $assistant_account gaf | grep \($boss_account\:`zmmailbox -z -m $boss_account gaf | awk '/\/Calendar$/ {print $1}'`\) 1>/dev/null
then
echo "Calendar is already mounted in boss account...proceeding..."
else
# Check for name conflict.
if zmmailbox -z -m $assistant_account gf "/$boss_account Calendar" 1>/dev/null 2>/dev/null
then
echo -e 'Naming conflict--delete or rename calendar "\c'
echo -e "$boss_account Calendar\c"
echo -e '" on assistant account.'
exit 1
else
# Accept Calendar share by assistant (only for web interface; Outlook must be done at the workstation)
echo "Mounting Calendar folder on assistant account. Right-click in Zimbra Web Client to change name."
zmmailbox -z -m $assistant_account cm -F# "/$boss_account Calendar" $boss_account "/Calendar" 1>/dev/null
fi
fi
echo "(In Outlook, use File>Open>Other User's Mailbox to access.)"
# Verify that filter rule with same name doesn't already exist
if zmmailbox -z -m $boss_account gfrl | grep ^\"Meeting\ Notifications\" 1>/dev/null
then
echo -e 'Naming conflict--delete or rename filter "\c'
echo -e "$filtername\c"
echo -e '" on boss account.'
exit 1
fi
# Create filter to file meeting messages into folder
echo "Creating filter in boss account."
zmmailbox -z -m $boss_account afrl -f "$filtername" body contains "Content-Type: text/calendar" body contains "method=" body contains "METHOD:" body contains "BEGIN:VEVENT" body contains "BEGIN:VCALENDAR" fileinto "/$notifications" stop
echo "Delegation complete!"