Changeset View
Changeset View
Standalone View
Standalone View
ucs/kolab_sieve.py
Show All 15 Lines | |||||
# 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/>. | ||||
# | # | ||||
# workaround for PEP 366 | # workaround for PEP 366 | ||||
__package__ = '' | __package__ = '' | ||||
import listener | import listener | ||||
Lint: PEP8 E402: module level import not at top of file | |||||
import logging | import logging | ||||
Lint: PEP8 E402 module level import not at top of file Lint: PEP8 E402: module level import not at top of file | |||||
import os | import os | ||||
Lint: PEP8 E402 module level import not at top of file Lint: PEP8 E402: module level import not at top of file | |||||
import sys | import sys | ||||
Lint: PEP8 E402 module level import not at top of file Lint: PEP8 E402: module level import not at top of file | |||||
import univention_baseconfig | import univention_baseconfig | ||||
Lint: PEP8 E402 module level import not at top of file Lint: PEP8 E402: module level import not at top of file | |||||
import univention.debug as ulog | import univention.debug as ulog | ||||
Lint: PEP8 E402 module level import not at top of file Lint: PEP8 E402: module level import not at top of file | |||||
sys.path = [ | sys.path = [ | ||||
os.path.abspath( | os.path.abspath( | ||||
os.path.join( | os.path.join( | ||||
os.path.dirname( | os.path.dirname( | ||||
os.path.realpath(os.path.abspath(__file__)) | os.path.realpath(os.path.abspath(__file__)) | ||||
), | ), | ||||
'..' | '..' | ||||
) | ) | ||||
) ] + sys.path | )] + sys.path | ||||
sys.stderr = open('/dev/null', 'a') | sys.stderr = open('/dev/null', 'a') | ||||
name = 'kolab_sieve' | name = 'kolab_sieve' | ||||
description = "Sieve Script Management for Kolab Groupware on UCS" | description = "Sieve Script Management for Kolab Groupware on UCS" | ||||
# The filter has to be composed to make sure only Kolab Groupware | # The filter has to be composed to make sure only Kolab Groupware | ||||
# related objects are passed along to this listener module. | # related objects are passed along to this listener module. | ||||
filter = '(objectClass=kolabInetOrgPerson)' | filter = '(objectClass=kolabInetOrgPerson)' | ||||
#attributes = [ '*' ] | #attributes = [ '*' ] | ||||
Lint: PEP8 E265 block comment should start with '# ' Lint: PEP8 E265: block comment should start with '# ' | |||||
import pykolab | import pykolab | ||||
Lint: PEP8 E402 module level import not at top of file Lint: PEP8 E402: module level import not at top of file | |||||
from pykolab import constants | from pykolab import constants | ||||
Lint: PEP8 E402 module level import not at top of file Lint: PEP8 E402: module level import not at top of file | |||||
from pykolab import utils | from pykolab import utils | ||||
Lint: PEP8 E402 module level import not at top of file Lint: PEP8 E402: module level import not at top of file | |||||
log = pykolab.getLogger('pykolab.listener') | log = pykolab.getLogger('pykolab.listener') | ||||
log.remove_stdout_handler() | log.remove_stdout_handler() | ||||
log.setLevel(logging.DEBUG) | log.setLevel(logging.DEBUG) | ||||
log.debuglevel = 9 | log.debuglevel = 9 | ||||
conf = pykolab.getConf() | conf = pykolab.getConf() | ||||
conf.finalize_conf(fatal=False) | conf.finalize_conf(fatal=False) | ||||
conf.debuglevel = 9 | conf.debuglevel = 9 | ||||
from pykolab.auth import Auth | from pykolab.auth import Auth | ||||
Lint: PEP8 E402 module level import not at top of file Lint: PEP8 E402: module level import not at top of file | |||||
def handler(*args, **kw): | def handler(*args, **kw): | ||||
auth = Auth() | auth = Auth() | ||||
auth.connect() | auth.connect() | ||||
if len(args) == 4: | if len(args) == 4: | ||||
# moddn, not relevant for Sieve Script Management | # moddn, not relevant for Sieve Script Management | ||||
pass | pass | ||||
elif len(args) == 3: | elif len(args) == 3: | ||||
dn = args[0] | dn = args[0] | ||||
new = utils.normalize(args[1]) | new = utils.normalize(args[1]) | ||||
old = utils.normalize(args[2]) | old = utils.normalize(args[2]) | ||||
if isinstance(old, dict) and len(old.keys()) > 0: | if isinstance(old, dict) and len(old.keys()) > 0: | ||||
# Either the entry changed or was deleted | # Either the entry changed or was deleted | ||||
if isinstance(new, dict) and len(new.keys()) > 0: | if isinstance(new, dict) and len(new.keys()) > 0: | ||||
# The entry was modified. | # The entry was modified. | ||||
result_attr = conf.get('cyrus-sasl', 'result_attribute') | result_attr = conf.get('cyrus-sasl', 'result_attribute') | ||||
if not new.has_key(result_attr): | if result_attr not in new: | ||||
log.error( | log.error( | ||||
"Entry %r does not have attribute %r" % ( | "Entry %r does not have attribute %r" % ( | ||||
dn, | dn, | ||||
result_attr | result_attr | ||||
) | ) | ||||
) | ) | ||||
return | return | ||||
# See if the mailserver_attribute exists | # See if the mailserver_attribute exists | ||||
mailserver_attribute = conf.get('ldap', 'mailserver_attribute').lower() | mailserver_attribute = conf.get('ldap', 'mailserver_attribute').lower() | ||||
Lint: PEP8 E501 line too long (87 > 79 characters) Lint: PEP8 E501: line too long (87 > 79 characters) | |||||
if mailserver_attribute == None: | if mailserver_attribute is None: | ||||
log.error("Mail server attribute is not set") | log.error("Mail server attribute is not set") | ||||
# TODO: Perhaps, query for IMAP servers. If there is only one, | # TODO: Perhaps, query for IMAP servers. If there is only one, | ||||
Lint: PEP8 E501 line too long (82 > 79 characters) Lint: PEP8 E501: line too long (82 > 79 characters) | |||||
# we know what to do. | # we know what to do. | ||||
return | return | ||||
if new.has_key(mailserver_attribute): | if mailserver_attribute in new: | ||||
if not new[mailserver_attribute] == constants.fqdn: | if not new[mailserver_attribute] == constants.fqdn: | ||||
log.info( | log.info( | ||||
"The mail server for user %r is set, and it is not me (%r)" % ( | "The mail server for user %r is set, and it is not me (%r)" % ( | ||||
Lint: PEP8 E501 line too long (95 > 79 characters) Lint: PEP8 E501: line too long (95 > 79 characters) | |||||
dn, | dn, | ||||
new[mailserver_attribute] | new[mailserver_attribute] | ||||
) | ) | ||||
) | ) | ||||
return | return | ||||
else: | else: | ||||
log.error("Entry %r does not have a mail server set" % (dn)) | log.error("Entry %r does not have a mail server set" % (dn)) | ||||
Lint: PEP8 E501 line too long (80 > 79 characters) Lint: PEP8 E501: line too long (80 > 79 characters) | |||||
return | return | ||||
conf.plugins.exec_hook( | conf.plugins.exec_hook( | ||||
'sieve_mgmt_refresh', | 'sieve_mgmt_refresh', | ||||
kw = { | kw={'user': new[result_attr]} | ||||
'user': new[result_attr] | |||||
} | |||||
) | ) | ||||
else: | else: | ||||
# The entry was deleted. This is irrelevant for | # The entry was deleted. This is irrelevant for | ||||
# Sieve Script Management | # Sieve Script Management | ||||
return | return | ||||
elif isinstance(new, dict) and len(new.keys()) > 0: | elif isinstance(new, dict) and len(new.keys()) > 0: | ||||
# Old is not a dict (or empty), so the entry is just created | # Old is not a dict (or empty), so the entry is just created | ||||
# See if the mailserver_attribute exists | # See if the mailserver_attribute exists | ||||
mailserver_attribute = conf.get('ldap', 'mailserver_attribute').lower() | mailserver_attribute = conf.get('ldap', 'mailserver_attribute').lower() | ||||
Lint: PEP8 E501 line too long (83 > 79 characters) Lint: PEP8 E501: line too long (83 > 79 characters) | |||||
result_attr = conf.get('cyrus-sasl', 'result_attribute').lower() | result_attr = conf.get('cyrus-sasl', 'result_attribute').lower() | ||||
if mailserver_attribute == None: | if mailserver_attribute is None: | ||||
log.error("Mail server attribute is not set") | log.error("Mail server attribute is not set") | ||||
# TODO: Perhaps, query for IMAP servers. If there is only one, | # TODO: Perhaps, query for IMAP servers. If there is only one, | ||||
# we know what to do. | # we know what to do. | ||||
return | return | ||||
if new.has_key(mailserver_attribute): | if mailserver_attribute in new: | ||||
if not new[mailserver_attribute] == constants.fqdn: | if not new[mailserver_attribute] == constants.fqdn: | ||||
log.info("The mail server for user %r is set, and it is not me (%r)" % (dn, new[mailserver_attribute])) | log.info("The mail server for user %r is set, and it is not me (%r)" % (dn, new[mailserver_attribute])) | ||||
Lint: PEP8 E501 line too long (123 > 79 characters) Lint: PEP8 E501: line too long (123 > 79 characters) | |||||
return | return | ||||
conf.plugins.exec_hook( | conf.plugins.exec_hook( | ||||
'sieve_mgmt_refresh', | 'sieve_mgmt_refresh', | ||||
kw = { | kw={'user': new[result_attr]} | ||||
'user': new[result_attr] | |||||
} | |||||
) | ) | ||||
else: | else: | ||||
log.info("entry %r changed, but no new or old attributes" % (dn)) | log.info("entry %r changed, but no new or old attributes" % (dn)) | ||||
module level import not at top of file