Greetings,
I am trying to figure out how to script the deletion of a list of known contacts from a specific mailbox. I'm using a base installation of Ubuntu Server 10.04.3 LTS and would like to use whatever tools that come with the server (rather than installing extra packages)
For example, I have a mailbox called "target@mydomain.com" that has a list of addresses in the default "Contacts" folder. I want to go through those contacts and delete any that match a list of email addresses to delete.
Delete List = "admin@mydomain.com;antivirus@mydomain.com;ham@myd omain.com;joedirt@mydomain.com"
This command will generate a text file with all contacts for the user, including the "ID" which is necessary for the delete command:
Code:
/opt/zimbra/bin/zmmailbox -z -m target@mydomain.com getAllContacts > /temp/contacts.txt
Which produces a file similar to:
Code:
Id: 300
Folder: /Contacts
Date: 10/28/11 09:16
Revision: 200
Attrs:
createTimeStamp: 20111026165603Z
lastName: admin
email: admin@mydomain.com
zimbraId: 26a2c068-2ad9-42e3-9f63-5f2f44c57dfc
objectClass: amavisAccount
fullName: admin
fileAs: 8:admin
firstName: admin
modifyTimeStamp: 20111026165627Z
Id: 301
Folder: /Contacts
Date: 10/28/11 09:16
Revision: 201
Attrs:
createTimeStamp: 20111026165603Z
lastName: antivirus
email: antivirus@mydomain.com
zimbraId: 26a2c068-2ad9-42e3-9f63-5f2f44c57dfc
objectClass: amavisAccount
fullName: antivirus antivirus
fileAs: 8:antivirus
firstName: antivirus
modifyTimeStamp: 20111026165627Z
Id: 302
Folder: /Contacts
Date: 10/28/11 09:16
Revision: 202
Attrs:
createTimeStamp: 20111026165603Z
lastName: ham
email: ham@mydomain.com
zimbraId: 26a2c068-2ad9-42e3-9f63-5f2f44c57dfc
objectClass: amavisAccount
fullName: ham ham
fileAs: 8:ham
firstName: ham
modifyTimeStamp: 20111026165627Z
Id: 303
Folder: /Contacts
Date: 10/28/11 09:16
Revision: 203
Attrs:
createTimeStamp: 20111026165603Z
lastName: dirt
email: joedirt@mydomain.com
zimbraId: 26a2c068-2ad9-42e3-9f63-5f2f44c57dfc
objectClass: amavisAccount
fullName: joe dirt
fileAs: 8:joe dirt
firstName: joe
modifyTimeStamp: 20111026165627Z
Id: 304
Folder: /Contacts
Date: 10/28/11 09:16
Revision: 204
Attrs:
createTimeStamp: 20111026165603Z
lastName: presley
email: epresley@mydomain.com
zimbraId: 26a2c068-2ad9-42e3-9f63-5f2f44c57dfc
objectClass: amavisAccount
fullName: elvis presley
fileAs: 8:elvis presley
firstName: elvis
modifyTimeStamp: 20111026165627Z
Once I obtain the IDs, I can then issue a command similar to the below:
Code:
/opt/zimbra/bin/zmmailbox -z -m target@mydomain.com deleteContact 300,301,302,303
I have a feeling the solution is the use of awk, sed or perl but I have not found an example that I can use. Any pointers in the right direction would help appreciated.
NOTE: I have already seen
this topic but the solution provided used ruby and it is not installed on this server.
Thanks,
LHammonds
================================================== ===================
EDIT: I think I may have it figured out.
I did a quick script doing this:
Code:
#!/bin/bash
TXTFILE="/temp/contacts.txt"
while read tagid tagval; do
echo "tag = ${tagid}, value = ${tagval}"
done < ${TXTFILE} Seems that I can include some logic to test for "Id:" and then see if the next value for "email:" is one of the items to delete and record the last value for the last "Id:" found.
I'll post the solution when I have it working.
EDIT: Crap! All this wasted time trying to find a solution and I had it right the 1st time. I was incorrectly trying to use and uppercase E in "email" since the I was uppercase for Id. Dang it!!!
Code:
#!/bin/bash
TXTFILE="/temp/contacts.txt"
EXCEPTIONS="admin@mydomain.com;antivirus@mydomain.com;ham@mydomain.com;joedirt@mydomain.com"
while read tagid tagval; do
if [ "${tagid}" = "Id:" ]; then
CURRID=${tagval}
fi
if [ "${tagid}" = "email:" ]; then
# Determine if this is an email address that we are looking for.
if `echo ${EXCEPTIONS} | grep -q "${tagval}"`
then
echo "Found! ${CURRID} = ${tagval}"
fi
fi
done < ${TXTFILE}