Changeset View
Changeset View
Standalone View
Standalone View
pykolab/cli/cmd_remove_mailaddress.py
Show All 11 Lines | |||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | # GNU General Public License for more details. | ||||
# You should have received a copy of the GNU General Public License | # You should have received a copy of the GNU General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
# | # | ||||
from __future__ import print_function | |||||
import sys | import sys | ||||
import commands | import commands | ||||
import pykolab | import pykolab | ||||
from pykolab.auth import Auth | from pykolab.auth import Auth | ||||
from pykolab import utils | from pykolab import utils | ||||
from pykolab.translate import _ | from pykolab.translate import _ | ||||
log = pykolab.getLogger('pykolab.cli') | log = pykolab.getLogger('pykolab.cli') | ||||
conf = pykolab.getConf() | conf = pykolab.getConf() | ||||
def __init__(): | def __init__(): | ||||
commands.register('remove_mail', execute, description=description()) | commands.register('remove_mail', execute, description=description()) | ||||
def description(): | def description(): | ||||
return """Remove a recipient's mail address.""" | return """Remove a recipient's mail address.""" | ||||
def execute(*args, **kw): | def execute(*args, **kw): | ||||
try: | try: | ||||
email_address = conf.cli_args.pop(0) | email_address = conf.cli_args.pop(0) | ||||
except IndexError, errmsg: | except IndexError: | ||||
email_address = utils.ask_question("Email address to remove") | email_address = utils.ask_question("Email address to remove") | ||||
# Get the domain from the email address | # Get the domain from the email address | ||||
if len(email_address.split('@')) > 1: | if len(email_address.split('@')) > 1: | ||||
domain = email_address.split('@')[1] | domain = email_address.split('@')[1] | ||||
else: | else: | ||||
log.error(_("Invalid or unqualified email address.")) | log.error(_("Invalid or unqualified email address.")) | ||||
sys.exit(1) | sys.exit(1) | ||||
Show All 16 Lines | def execute(*args, **kw): | ||||
if isinstance(recipients, basestring): | if isinstance(recipients, basestring): | ||||
recipient = recipients | recipient = recipients | ||||
# Only a single recipient found, remove the address | # Only a single recipient found, remove the address | ||||
attributes = auth.get_entry_attributes(domain, recipient, mail_attributes) | attributes = auth.get_entry_attributes(domain, recipient, mail_attributes) | ||||
# See which attribute holds the value we're trying to remove | # See which attribute holds the value we're trying to remove | ||||
for attribute in attributes.keys(): | for attribute in attributes: | ||||
if isinstance(attributes[attribute], list): | if isinstance(attributes[attribute], list): | ||||
if email_address in attributes[attribute]: | if email_address in attributes[attribute]: | ||||
attributes[attribute].pop(attributes[attribute].index(email_address)) | attributes[attribute].pop(attributes[attribute].index(email_address)) | ||||
replace_attributes = { | replace_attributes = { | ||||
attribute: attributes[attribute] | attribute: attributes[attribute] | ||||
} | } | ||||
auth.set_entry_attributes(domain, recipient, replace_attributes) | auth.set_entry_attributes(domain, recipient, replace_attributes) | ||||
else: | else: | ||||
if email_address == attributes[attribute]: | if email_address == attributes[attribute]: | ||||
auth.set_entry_attributes(domain, recipient, {attribute: None}) | auth.set_entry_attributes(domain, recipient, {attribute: None}) | ||||
pass | pass | ||||
else: | else: | ||||
print >> sys.stderr, _("Found the following recipients:") | print(_("Found the following recipients:"), file=sys.stderr) | ||||
for recipient in recipients: | for recipient in recipients: | ||||
print recipient | print(recipient) |