pykolab/cli

Jeroen van Meeuwen vanmeeuwen at kolabsys.com
Mon Oct 21 13:19:32 CEST 2013


 pykolab/cli/cmd_add_alias.py |  129 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)

New commits:
commit 82703985d2b3a1f0fcc1f54eb0347bb68c4ee296
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Mon Oct 21 13:19:09 2013 +0200

    Add a command to add an alias to an existing user entry

diff --git a/pykolab/cli/cmd_add_alias.py b/pykolab/cli/cmd_add_alias.py
new file mode 100644
index 0000000..c090997
--- /dev/null
+++ b/pykolab/cli/cmd_add_alias.py
@@ -0,0 +1,129 @@
+# -*- coding: utf-8 -*-
+# Copyright 2010-2013 Kolab Systems AG (http://www.kolabsys.com)
+#
+# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
+#
+# 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; version 3 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 Library General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# 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.translate import _
+
+log = pykolab.getLogger('pykolab.cli')
+conf = pykolab.getConf()
+
+def __init__():
+    commands.register('add_alias', execute, description="Add alias.")
+
+def execute(*args, **kw):
+
+    try:
+        primary_rcpt_address = conf.cli_args.pop(0)
+        try:
+            secondary_rcpt_address = conf.cli_args.pop(0)
+        except:
+            print >> sys.stderr, _("Specify the (new) alias address")
+            sys.exit(1)
+    except:
+        print >> sys.stderr, _("Specify the existing recipient address")
+        sys.exit(1)
+
+    if len(primary_rcpt_address.split('@')) > 1:
+        primary_rcpt_domain = primary_rcpt_address.split('@')[-1]
+    else:
+        primary_rcpt_domain = conf.get('kolab', 'primary_domain')
+
+    auth = Auth(domain=primary_rcpt_domain)
+
+    domains = auth.list_domains()
+
+    #print domains
+
+    if len(secondary_rcpt_address.split('@')) > 1:
+        secondary_rcpt_domain = secondary_rcpt_address.split('@')[-1]
+    else:
+        secondary_rcpt_domain = conf.get('kolab', 'primary_domain')
+
+    # Check if either is in fact a domain
+    if not primary_rcpt_domain.lower() in domains.keys():
+        print >> sys.stderr, _("Domain %r is not a local domain") % (primary_rcpt_domain)
+        sys.exit(1)
+
+    if not secondary_rcpt_domain.lower() in domains.keys():
+        print >> sys.stderr, _("Domain %r is not a local domain") % (secondary_rcpt_domain)
+        sys.exit(1)
+
+    if not primary_rcpt_domain == secondary_rcpt_domain:
+        if not domains[primary_rcpt_domain] == domains[secondary_rcpt_domain]:
+            print >> sys.stderr, _("Primary and secondary domain do not have the same parent domain")
+            sys.exit(1)
+
+    primary_recipient_dn = auth.find_recipient(primary_rcpt_address)
+
+    if primary_recipient_dn == [] or len(primary_recipient_dn) == 0:
+        print >> sys.stderr, _("No such recipient %r") % (primary_rcpt_address)
+        sys.exit(1)
+
+    secondary_recipient_dn = auth.find_recipient(secondary_rcpt_address)
+
+    if not secondary_recipient_dn == [] and not len(secondary_recipient_dn) == 0:
+        print >> sys.stderr, _("Recipient for alias %r already exists") % (secondary_rcpt_address)
+        sys.exit(1)
+
+    rcpt_attrs = conf.get_list('ldap', 'mail_attributes')
+
+    primary_rcpt_attr = rcpt_attrs[0]
+
+    if len(rcpt_attrs) >= 2:
+        secondary_rcpt_attr = rcpt_attrs[1]
+    else:
+        print >> sys.stderr, _("Environment is not configured for " + \
+            "users to hold secondary mail attributes")
+
+        sys.exit(1)
+
+    primary_recipient = auth.get_entry_attributes(primary_rcpt_domain, primary_recipient_dn, rcpt_attrs)
+
+    if not primary_recipient.has_key(primary_rcpt_attr):
+        print >> sys.stderr, _("Recipient %r is not the primary recipient for address %r") % (primary_recipient, primary_rcpt_address)
+        sys.exit(1)
+
+    if not primary_recipient.has_key(secondary_rcpt_attr):
+        auth.set_entry_attributes(primary_rcpt_domain, primary_recipient_dn, {secondary_rcpt_attr: [ secondary_rcpt_address ] })
+    else:
+        if isinstance(primary_recipient[secondary_rcpt_attr], basestring):
+            new_secondary_rcpt_attrs = [
+                    primary_recipient[secondary_rcpt_attr],
+                    secondary_rcpt_address
+                ]
+
+        else:
+            new_secondary_rcpt_attrs = \
+                    primary_recipient[secondary_rcpt_attr] + \
+                    [ secondary_rcpt_address ]
+
+        auth.set_entry_attributes(
+                primary_rcpt_domain,
+                primary_recipient_dn,
+                {
+                        secondary_rcpt_attr: new_secondary_rcpt_attrs
+                    }
+            )
+




More information about the commits mailing list