forked from mdugas/mumo-grouptag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grouptag.py
212 lines (168 loc) · 7.31 KB
/
grouptag.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python
# -*- coding: utf-8
# Copyright (C) 2015-2016 Mathieu Dugas <mad2k6@nabb.ca>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# - Neither the name of the Mumble Developers nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# `AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# grouptag.py
#
# Module for moving/muting/deafening idle players after
# a certain amount of time and unmuting/undeafening them
# once they become active again
#
from mumo_module import (commaSeperatedIntegers,
commaSeperatedStrings,
MumoModule)
from threading import Timer
import re
class grouptag(MumoModule):
default_config = {'grouptag':(
('interval', int, 10),
('servers', commaSeperatedIntegers, []),
),
lambda x: re.match('(all)|(server_\d+)', x):(
('groupmap', commaSeperatedStrings, ['admin:admin']),
),
}
def __init__(self, name, manager, configuration=None):
MumoModule.__init__(self, name, manager, configuration)
self.murmur = manager.getMurmurModule()
self.watchdog = None
def connected(self):
self.affectedusers = {} # {serverid:set(sessionids,...)}
manager = self.manager()
log = self.log()
log.debug("Register for Meta & Server callbacks")
cfg = self.cfg()
servers = cfg.grouptag.servers
if not servers:
servers = manager.SERVERS_ALL
manager.subscribeServerCallbacks(self, servers)
manager.subscribeMetaCallbacks(self, servers)
if not self.watchdog:
self.watchdog = Timer(cfg.grouptag.interval, self.setTag)
self.watchdog.start()
def disconnected(self):
self.affectedusers = {}
if self.watchdog:
self.watchdog.stop()
self.watchdog = None
def isuseringroup(self, server, user, group_to_check):
'''Checks if userid is member of the excluded_for_afk group in the root channel'''
try:
scfg = getattr(self.cfg(), 'server_%d' % int(server.id()))
except AttributeError:
scfg = self.cfg().all
ACL = server.getACL(0)
userid = user.userid
for group in ACL[1]:
if userid in group.members and group.name == group_to_check:
return True
return False
def setTag(self):
cfg = self.cfg()
try:
meta = self.manager().getMeta()
if not cfg.grouptag.servers:
servers = meta.getBootedServers()
else:
servers = [meta.getServer(server) for server in cfg.grouptag.servers]
for server in servers:
if not server: continue
if server:
for user in server.getUsers().itervalues():
self.updateTags(server, user)
finally:
# Renew the timer
self.watchdog.cancel()
self.watchdog = Timer(cfg.grouptag.interval, self.setTag)
self.watchdog.start()
def updateTags(self, server, user):
log = self.log()
sid = server.id()
try:
scfg = getattr(self.cfg(), 'server_%d' % sid)
except AttributeError:
scfg = self.cfg().all
try:
index = self.affectedusers[sid]
except KeyError:
self.affectedusers[sid] = set()
index = self.affectedusers[sid]
# Remember values so we can see changes later
tags = []
# grab all the appropriate tags for the user and update the tagset.
for i in range(len(scfg.groupmap) - 1, -1, -1):
try:
map = scfg.groupmap[i]
except IndexError:
log.warning("Incomplete configuration for stage %d of server %i, ignored", i, server.id())
continue
group = map.split(':')[0]
tag = map.split(':')[1]
if self.isuseringroup(server, user, group) and tag not in tags:
log.info('Adding tag %s to user %s on serverid %i', tag, user.name, server.id())
tags.append(tag)
userstate = server.getState(int(user.session))
if any(x.split(':')[1] in user.name for x in scfg.groupmap):
prefix = [x for x in scfg.groupmap if x.split(':')[1] in user.name][0].split(':')[1]
prefix = user.name.find(prefix)+len(prefix)
original_username = user.name[prefix:]
else:
original_username = user.name
if len(tags) > 0:
userstate.name='%s%s' % (''.join(tags), original_username)
else:
userstate.name='%s' % (original_username)
server.setState(userstate)
log.info('Setting tags %s to user %s on server %d', tags, original_username, server.id())
#
#--- Server callback functions
#
def userDisconnected(self, server, state, context=None):
try:
index = self.affectedusers[server.id()]
if state.session in index:
index.remove(state.session)
except KeyError:
pass
def userConnected(self, server, state, context=None):
self.setTag();
def userStateChanged(self, server, state, context=None): pass
def userTextMessage(self, server, user, message, current=None): pass
def channelCreated(self, server, state, context=None): pass
def channelRemoved(self, server, state, context=None): pass
def channelStateChanged(self, server, state, context=None): pass
#
#--- Meta callback functions
#
def started(self, server, context = None):
sid = server.id()
self.affectedusers[sid] = set()
self.log().debug('Handling server %d', sid)
def stopped(self, server, context = None):
sid = server.id()
self.affectedusers[sid] = set()
self.log().debug('Server %d gone', sid)