summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2012-05-13 17:30:48 +0100
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2012-05-30 14:50:55 +0200
commitfb6003ce967d0205909c6ae04dba99a1cd82d628 (patch)
tree14d69da50ed6c5cf417fb345915669ea422f1b2a
parent803459c61a2c854e712cd54a5c20a3f213d3f3df (diff)
downloadpykolab-fb6003ce967d0205909c6ae04dba99a1cd82d628.tar.gz
Update cmd_remove_mailaddress
-rw-r--r--pykolab/cli/cmd_remove_mailaddress.py71
1 files changed, 54 insertions, 17 deletions
diff --git a/pykolab/cli/cmd_remove_mailaddress.py b/pykolab/cli/cmd_remove_mailaddress.py
index d6eb43b..19c4756 100644
--- a/pykolab/cli/cmd_remove_mailaddress.py
+++ b/pykolab/cli/cmd_remove_mailaddress.py
@@ -17,10 +17,14 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
+import sys
+
import commands
import pykolab
+from pykolab.auth import Auth
+from pykolab import utils
from pykolab.translate import _
log = pykolab.getLogger('pykolab.cli')
@@ -30,27 +34,60 @@ def __init__():
commands.register('remove_mail', execute, description=description())
def description():
- return """Remove a user's mail address."""
+ return """Remove a recipient's mail address."""
def execute(*args, **kw):
- uid = conf.cli_args.pop(0)
- email_address = conf.cli_args.pop(0)
+ try:
+ email_address = conf.cli_args.pop(0)
+ except IndexError, errmsg:
+ email_address = utils.ask_question("Email address to remove")
+
+ # Get the domain from the email address
+ if len(email_address.split('@')) > 1:
+ domain = email_address.split('@')[1]
+ else:
+ log.error(_("Invalid or unqualified email address."))
+ sys.exit(1)
+
+ auth = Auth()
+ auth.connect(domain=domain)
+ recipients = auth.find_recipient(email_address)
+
+ if len(recipients) == 0:
+ log.error(_("No recipient found for email address %r") % (email_address))
+ sys.exit(1)
+
+ log.debug(_("Found the following recipient(s): %r") % (recipients), level=8)
+
+ mail_attributes = conf.get_list(domain, 'mail_attributes')
+ if mail_attributes == None or len(mail_attributes) < 1:
+ mail_attributes = conf.get_list(conf.get('kolab', 'auth_mechanism'), 'mail_attributes')
+
+ log.debug(_("Using the following mail attributes: %r") % (mail_attributes), level=8)
+
+ if isinstance(recipients, basestring):
+ recipient = recipients
- user = auth.find_user('uid', uid)
- user = {
- 'dn': user
- }
+ # Only a single recipient found, remove the address
+ attributes = auth.get_entry_attributes(domain, recipient, mail_attributes)
- user['mail'] = auth.get_user_attribute('klab.cc', user, 'mail')
- user['mailalternateaddress'] = auth.get_user_attribute('klab.cc', user, 'mailalternateaddress')
+ # See which attribute holds the value we're trying to remove
+ for attribute in attributes.keys():
+ if isinstance(attributes[attribute], list):
+ if email_address in attributes[attribute]:
+ attributes[attribute].pop(attributes[attribute].index(email_address))
+ replace_attributes = {
+ attribute: attributes[attribute]
+ }
- if user['mail'] == email_address:
- auth.set_user_attribute('klab.cc', user, 'mail', '')
+ auth.set_entry_attributes(domain, recipient, replace_attributes)
+ else:
+ if email_address == attributes[attribute]:
+ auth.set_entry_attributes(domain, recipient, {attribute: None})
+ pass
- if email_address in user['mailalternateaddress']:
- _user_addresses = []
- for address in user['mailalternateaddress']:
- if not address == email_address:
- _user_addresses.append(address)
+ else:
+ print >> sys.stderr, _("Found the following recipients:")
- auth.set_user_attribute('klab.cc', user, 'mailAlternateAddress', _user_addresses)
+ for recipient in recipients:
+ print recipient