3 commits - pykolab/cli pykolab/imap
Jeroen van Meeuwen
vanmeeuwen at kolabsys.com
Sun Aug 12 13:10:54 CEST 2012
pykolab/cli/cmd_add_user_subscription.py | 96 ++++++++++++++++++++++++++++
pykolab/cli/cmd_list_user_subscriptions.py | 91 ++++++++++++++++++++++++++
pykolab/cli/cmd_remove_user_subscription.py | 96 ++++++++++++++++++++++++++++
pykolab/imap/__init__.py | 15 ----
4 files changed, 283 insertions(+), 15 deletions(-)
New commits:
commit fcf3ac7d6871e22b075b648fd51430792dd0d3a6
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date: Sun Aug 12 12:09:43 2012 +0100
Remove the self.connect() calls, as these routines are not aware of the state we wish to be in
diff --git a/pykolab/imap/__init__.py b/pykolab/imap/__init__.py
index f0cb9be..2a8ea66 100644
--- a/pykolab/imap/__init__.py
+++ b/pykolab/imap/__init__.py
@@ -538,15 +538,12 @@ class IMAP(object):
log.warning(_("Moving INBOX folder %s won't succeed as target folder %s already exists") % (old_name,new_name))
def user_mailbox_server(self, mailbox):
- self.connect(domain=self.domain)
return self.imap.find_mailfolder_server(mailbox)
def has_folder(self, folder):
"""
Check if the environment has a folder named folder.
"""
- self.connect(domain=self.domain)
-
folders = self.imap.lm(folder)
log.debug(_("Looking for folder '%s', we found folders: %r") % (folder,folders), level=8)
# Greater then one, this folder may have subfolders.
@@ -596,8 +593,6 @@ class IMAP(object):
""" Blah functions """
def move_user_folders(self, users=[], domain=None):
- self.connect(domain=domain)
-
for user in users:
if type(user) == dict:
if user.has_key('old_mail'):
@@ -627,9 +622,6 @@ class IMAP(object):
Sets the quota in IMAP using the authentication and authorization
database 'quota' attribute for the users listed in parameter 'users'
"""
-
- self.connect(domain=primary_domain)
-
if conf.has_option(primary_domain, 'quota_attribute'):
_quota_attr = conf.get(primary_domain, 'quota_attribute')
else:
@@ -707,8 +699,6 @@ class IMAP(object):
self.imap._setquota(folder, quota)
def set_user_mailhost(self, users=[], primary_domain=None, secondary_domain=[], folders=[]):
- self.connect(domain=primary_domain)
-
if conf.has_option(primary_domain, 'mailserver_attribute'):
_mailserver_attr = conf.get(primary_domain, 'mailserver_attribute')
else:
@@ -756,7 +746,6 @@ class IMAP(object):
auth.set_user_attribute(primary_domain, user, _mailserver_attr, _current_mailserver)
def parse_mailfolder(self, mailfolder):
- self.connect()
return self.imap.parse_mailfolder(mailfolder)
def expunge_user_folders(self, inbox_folders=None):
@@ -777,8 +766,6 @@ class IMAP(object):
primary_domain, secondary_domains
"""
- self.connect()
-
if inbox_folders == None:
inbox_folders = []
@@ -835,8 +822,6 @@ class IMAP(object):
List the INBOX folders in the IMAP backend. Returns a list of unique
base folder names.
"""
- self.connect(domain=primary_domain)
-
_folders = self.imap.lm("user/%")
# TODO: Replace the .* below with a regex representing acceptable DNS
# domain names.
commit 866cf4cc78523b6c15f4e3b5f2ddcc6e8780859e
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date: Sun Aug 12 12:09:24 2012 +0100
- Add commands to add and remove user's subscriptions to/from folders
diff --git a/pykolab/cli/cmd_add_user_subscription.py b/pykolab/cli/cmd_add_user_subscription.py
new file mode 100644
index 0000000..38dc2d7
--- /dev/null
+++ b/pykolab/cli/cmd_add_user_subscription.py
@@ -0,0 +1,96 @@
+# -*- coding: utf-8 -*-
+# Copyright 2010-2012 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.imap import IMAP
+from pykolab.translate import _
+from pykolab import utils
+
+log = pykolab.getLogger('pykolab.cli')
+conf = pykolab.getConf()
+
+def __init__():
+ commands.register('add_user_subscription', execute, description=description())
+
+def description():
+ return _("Subscribe a user to a folder.")
+
+def execute(*args, **kw):
+ folder_pattern = "*"
+
+ try:
+ user = conf.cli_args.pop(0)
+ try:
+ folder_pattern = conf.cli_args.pop(0)
+ except IndexError, errmsg:
+ folder_pattern = utils.ask_question(_("Folder pattern"))
+
+ except IndexError, errmsg:
+ user = utils.ask_question(_("User ID"))
+ folder_pattern = utils.ask_question(_("Folder pattern"))
+
+ if len(user.split('@')) > 1:
+ domain = user.split('@')[1]
+ else:
+ domain = conf.get('kolab', 'primary_domain')
+
+ imap = IMAP()
+ imap.connect(domain=domain, login=False)
+
+ backend = conf.get(domain, 'imap_backend')
+ if backend == None:
+ backend = conf.get('kolab', 'imap_backend')
+
+ admin_login = conf.get(backend, 'admin_login')
+ admin_password = conf.get(backend, 'admin_password')
+
+ imap.login_plain(admin_login, admin_password, user)
+
+ if not imap.has_folder(folder_pattern):
+ print >> sys.stderr, \
+ _("Cannot subscribe user to folder %r:") % (folder_pattern), \
+ _("No such folder")
+ sys.exit(1)
+
+ _folders = imap.lm(folder_pattern)
+ _subscribed_folders = imap.lsub(folder_pattern)
+ subscribed_folders = []
+
+ for _folder in _folders:
+ if not _folder in _subscribed_folders:
+ imap.subscribe(_folder)
+ subscribed_folders.append(_folder)
+
+ if len(subscribed_folders) > 0:
+ print _("Successfully subscribed user %s to the following folders:") % (
+ user
+ )
+
+ print "\n".join(subscribed_folders)
+ else:
+ print >> sys.stderr, _("User %s not subscribed to any folders.") % (
+ user
+ )
+
+ sys.exit(1)
\ No newline at end of file
diff --git a/pykolab/cli/cmd_remove_user_subscription.py b/pykolab/cli/cmd_remove_user_subscription.py
new file mode 100644
index 0000000..79e9d09
--- /dev/null
+++ b/pykolab/cli/cmd_remove_user_subscription.py
@@ -0,0 +1,96 @@
+# -*- coding: utf-8 -*-
+# Copyright 2010-2012 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.imap import IMAP
+from pykolab.translate import _
+from pykolab import utils
+
+log = pykolab.getLogger('pykolab.cli')
+conf = pykolab.getConf()
+
+def __init__():
+ commands.register('remove_user_subscription', execute, description=description())
+
+def description():
+ return _("Unsubscribe a user from a folder.")
+
+def execute(*args, **kw):
+ folder_pattern = "*"
+
+ try:
+ user = conf.cli_args.pop(0)
+ try:
+ folder_pattern = conf.cli_args.pop(0)
+ except IndexError, errmsg:
+ folder_pattern = utils.ask_question(_("Folder pattern"))
+
+ except IndexError, errmsg:
+ user = utils.ask_question(_("User ID"))
+ folder_pattern = utils.ask_question(_("Folder pattern"))
+
+ if len(user.split('@')) > 1:
+ domain = user.split('@')[1]
+ else:
+ domain = conf.get('kolab', 'primary_domain')
+
+ imap = IMAP()
+ imap.connect(domain=domain, login=False)
+
+ backend = conf.get(domain, 'imap_backend')
+ if backend == None:
+ backend = conf.get('kolab', 'imap_backend')
+
+ admin_login = conf.get(backend, 'admin_login')
+ admin_password = conf.get(backend, 'admin_password')
+
+ imap.login_plain(admin_login, admin_password, user)
+
+ if not imap.has_folder(folder_pattern):
+ print >> sys.stderr, \
+ _("Cannot subscribe user to folder %r:") % (folder_pattern), \
+ _("No such folder")
+ sys.exit(1)
+
+ _folders = imap.lm(folder_pattern)
+ _subscribed_folders = imap.lsub(folder_pattern)
+ unsubscribed_folders = []
+
+ for _folder in _folders:
+ if _folder in _subscribed_folders:
+ imap.unsubscribe(_folder)
+ unsubscribed_folders.append(_folder)
+
+ if len(unsubscribed_folders) > 0:
+ print _("Successfully unsubscribed user %s from the following folders:") % (
+ user
+ )
+
+ print "\n".join(unsubscribed_folders)
+ else:
+ print >> sys.stderr, _("User %s not be unsubscribed from any folders.") % (
+ user
+ )
+
+ sys.exit(1)
\ No newline at end of file
commit 803935a7e108d3720566a2f141985fea936dc274
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date: Sun Aug 12 11:46:59 2012 +0100
- Add command to list a user's folder subscriptions. Usage:
$ kolab list-user-subscriptions <user> [<mailbox-pattern>]
Examples:
$ kolab list-user-subscriptions john.doe at example.org
$ kolab list-user-subscriptions \
john.doe at example.org shared/lists/example.org/memo
Note that a proxyauth login is executed, and mailbox patterns can
therefore not be suffixed with the usual '@domain' realm idenfifier.
diff --git a/pykolab/cli/cmd_list_user_subscriptions.py b/pykolab/cli/cmd_list_user_subscriptions.py
new file mode 100644
index 0000000..a25e8d6
--- /dev/null
+++ b/pykolab/cli/cmd_list_user_subscriptions.py
@@ -0,0 +1,91 @@
+# -*- coding: utf-8 -*-
+# Copyright 2010-2012 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 commands
+
+import pykolab
+
+from pykolab.imap import IMAP
+from pykolab.translate import _
+from pykolab import utils
+
+log = pykolab.getLogger('pykolab.cli')
+conf = pykolab.getConf()
+
+def __init__():
+ commands.register('list_user_subscriptions', execute, description=description())
+
+def cli_options(*args, **kw):
+ my_option_group = conf.add_cli_parser_option_group(_("CLI Options"))
+ my_option_group.add_option( '--unsubscribed',
+ dest = "unsubscribed",
+ action = "store_true",
+ default = False,
+ help = _("List unsubscribed folders"))
+
+def description():
+ return _("List the folders a user is subscribed to.")
+
+def execute(*args, **kw):
+ folder_pattern = "*"
+
+ try:
+ user = conf.cli_args.pop(0)
+ try:
+ folder_pattern = conf.cli_args.pop(0)
+ except IndexError, errmsg:
+ pass
+
+ except IndexError, errmsg:
+ user = utils.ask_question(_("User ID"))
+
+ if len(user.split('@')) > 1:
+ domain = user.split('@')[1]
+ else:
+ domain = conf.get('kolab', 'primary_domain')
+
+ imap = IMAP()
+ imap.connect(domain=domain, login=False)
+
+ backend = conf.get(domain, 'imap_backend')
+ if backend == None:
+ backend = conf.get('kolab', 'imap_backend')
+
+ admin_login = conf.get(backend, 'admin_login')
+ admin_password = conf.get(backend, 'admin_password')
+
+ imap.login_plain(admin_login, admin_password, user)
+
+ subscribed_folders = imap.lsub(folder_pattern)
+
+ if conf.unsubscribed:
+ unsubscribed_folders = []
+ all_folders = imap.lm(folder_pattern)
+
+ for folder in all_folders:
+ if not folder in subscribed_folders:
+ unsubscribed_folders.append(folder)
+
+ if len(unsubscribed_folders) > 0:
+ print "\n".join(unsubscribed_folders)
+ else:
+ print _("No unsubscribed folders for user %s") % (user)
+
+ else:
+ print "\n".join(subscribed_folders)
More information about the commits
mailing list