forked from it25gmbh/plucs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plucs.py
executable file
·120 lines (102 loc) · 3.77 KB
/
plucs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -*- coding: utf-8 -*-
#
# PLUCS (XMPP integration for UCS)
"""PLUCS listener module."""
#
# Copyright 2013-2014 it25 GmbH
#
# http://www.it25.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.
# pylint: disable-msg=C0103,W0704
__package__ = '' # workaround for PEP 366
import listener
import univention.debug as ud
import univention.config_registry
# import subprocess
import os
name = 'plucs'
description = 'Reconfigures eJabberd service'
hostname = listener.baseConfig['hostname']
filter = '(&' + \
'(objectClass=univentionXMPPHost)' + \
'(cn=%s))' % hostname
attributes = ['xmppDomains','univentionService']
# set to true when handler really got a change
changed = False
def initialize():
"""Initialize the module once on first start or after clean."""
ud.debug(ud.LISTENER, ud.INFO, 'plucs: initialized')
def handler(dn, new, old):
"""Handle changes to 'dn'."""
global changed
ucr = univention.config_registry.ConfigRegistry()
ud.debug(ud.LISTENER, ud.INFO, "plucs: DN '%s' changed" % dn)
ud.debug(ud.LISTENER, ud.INFO, "plucs: old = '%s'" % old.get('xmppDomains'))
ud.debug(ud.LISTENER, ud.INFO, "plucs: new = '%s'" % new.get('xmppDomains'))
# It should be possible to switch off an XMPP host by removing the
# XMPP service entry, without losing its xmppDomains.
hasService = 'XMPP' in new.get('univentionService')
ud.debug(ud.LISTENER, ud.INFO, "plucs: service XMPP = %s" % hasService)
# Old LDAP value doesn't matter.
# We have to compare with the old value currently stored in UCR.
oldval = ucr.get('xmpp/domains')
msg = ''
if not hasService:
msg = 'service XMPP not assigned'
newval = ''
elif new.get('xmppDomains') is None:
msg = 'no XMPP domains set'
newval = ''
else:
newval = ' '.join(new.get('xmppDomains'))
if newval == '':
msg = 'list of XMPP domains is empty'
if msg != '':
ud.debug(ud.LISTENER, ud.INFO, "plucs: %s" % msg)
if oldval != newval:
changed = True
listener.setuid(0)
ud.debug(ud.LISTENER, ud.INFO, "plucs: setting xmpp domains to '%s'" % newval)
try:
univention.config_registry.handler_set(['xmpp/domains=%s' % newval])
finally:
listener.unsetuid()
def clean():
"""Handle request to clean-up the module."""
return
def postrun():
ud.debug(ud.LISTENER, ud.INFO, "postrun: plucs running")
global changed
if not changed:
ud.debug(ud.LISTENER, ud.INFO, "plucs: nothing changed, not restarting daemon.")
return
changed = False
ucr = univention.config_registry.ConfigRegistry()
ucr.load()
if ucr.is_true("plucs/autostart", False):
if ucr.is_true('plucs/restart/listener', False):
ud.debug(ud.LISTENER, ud.INFO, 'PLUCS: Restarting server')
try:
listener.run('/usr/sbin/invoke-rc.d', ['invoke-rc.d', 'plucs', 'restart'], uid=0)
except Exception, e:
ud.debug(ud.ADMIN, ud.WARN, 'The restart of the PLUCS server failed: %s' % str(e))
else:
ud.debug(ud.ADMIN, ud.INFO, 'PLUCS: the automatic restart of the PLUCS server by the listener is disabled. Set plucs/restart/listener to true to enable this option.')
else:
ud.debug(ud.LISTENER, ud.INFO, 'plucs: autostart disabled in config_registry - not started.')