-
Notifications
You must be signed in to change notification settings - Fork 81
/
users.py
199 lines (152 loc) · 6.86 KB
/
users.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
# -*- coding: utf-8 -*-
from constants import *
from threading import Lock
import soundqueue
import messages
class Users(dict):
"""Object that stores and update all connected users"""
def __init__(self, mumble_object, callbacks):
self.mumble_object = mumble_object
self.callbacks = callbacks
self.myself = None # user object of the pymumble thread itself
self.myself_session = None # session number of the pymumble thread itself
self.lock = Lock()
def update(self, message):
"""Update a user informations, based in an incoming message"""
self.lock.acquire()
if message.session not in self:
self[message.session] = User(self.mumble_object, message)
self.callbacks(PYMUMBLE_CLBK_USERCREATED, self[message.session])
if message.session == self.myself_session:
self.myself == self[message.session]
else:
actions = self[message.session].update(message)
self.callbacks(PYMUMBLE_CLBK_USERUPDATED, self[message.session], actions)
self.lock.release()
def remove(self, message):
"""Remove a user object based on server info"""
self.lock.acquire()
if message.session in self:
user = self[message.session]
del self[message.session]
self.callbacks(PYMUMBLE_CLBK_USERREMOVED, user, message)
self.lock.release()
def set_myself(self, session):
"""Set the "myself" user"""
self.myself_session = session
if session in self:
self.myself = self[session]
def count(self):
"""Return the count of connected users"""
return len(self)
class User(dict):
"""Object that store one user"""
def __init__(self, mumble_object, message):
self.mumble_object = mumble_object
self["session"] = message.session
self.update(message)
self.sound = soundqueue.SoundQueue(self.mumble_object) # will hold this user incoming audio
def update(self, message):
"""Update user state, based on an incoming message"""
actions = dict()
if message.HasField("actor"):
actions["actor"] = message.actor
for (field, value) in message.ListFields():
if field.name in ("session", "actor", "comment", "texture"):
continue
actions.update(self.update_field(field.name, value))
if message.HasField("comment_hash"):
if message.HasField("comment"):
self.mumble_object.blobs[message.comment_hash] = message.comment
else:
self.mumble_object.blobs.get_user_comment(message.comment_hash)
if message.HasField("texture_hash"):
if message.HasField("texture"):
self.mumble_object.blobs[message.texture_hash] = message.texture
else:
self.mumble_object.blobs.get_user_texture(message.texture_hash)
return(actions) # return a dict, useful for the callback functions
def update_field(self, name, field):
"""Update one state value for a user"""
actions = dict()
if name not in self or self[name] != field:
self[name] = field
actions[name] = field
return(actions)
def get_property(self, property):
if property in self:
return(self[property])
else:
return None
def mute(self):
"""Mute a user"""
params = {"session": self["session"]}
if self["session"] == self.mumble_object.users.myself_session:
params["self_mute"] = True
else:
params["mute"] = True
cmd = messages.ModUserState(self.mumble_object.users.myself_session, params)
self.mumble_object.execute_command(cmd)
def unmute(self):
"""Unmute a user"""
params = {"session": self["session"]}
if self["session"] == self.mumble_object.users.myself_session:
params["self_mute"] = False
else:
params["mute"] = False
cmd = messages.ModUserState(self.mumble_object.users.myself_session, params)
self.mumble_object.execute_command(cmd)
def deafen(self):
"""Deafen a user"""
params = {"session": self["session"]}
if self["session"] == self.mumble_object.users.myself_session:
params["self_deaf"] = True
else:
params["deaf"] = True
cmd = messages.ModUserState(self.mumble_object.users.myself_session, params)
self.mumble_object.execute_command(cmd)
def undeafen(self):
"""Undeafen a user"""
params = {"session": self["session"]}
if self["session"] == self.mumble_object.users.myself_session:
params["self_deaf"] = False
else:
params["deaf"] = False
cmd = messages.ModUserState(self.mumble_object.users.myself_session, params)
self.mumble_object.execute_command(cmd)
def suppress(self):
"""Disable a user"""
params = {"session": self["session"],
"suppress": True}
cmd = messages.ModUserState(self.mumble_object.users.myself_session, params)
self.mumble_object.execute_command(cmd)
def unsuppress(self):
"""Enable a user"""
params = {"session": self["session"],
"suppress": False}
cmd = messages.ModUserState(self.mumble_object.users.myself_session, params)
self.mumble_object.execute_command(cmd)
def recording(self):
"""Set the user as recording"""
params = {"session": self["session"],
"recording": True}
cmd = messages.ModUserState(self.mumble_object.users.myself_session, params)
self.mumble_object.execute_command(cmd)
def unrecording(self):
"""Set the user as not recording"""
params = {"session": self["session"],
"recording": False}
cmd = messages.ModUserState(self.mumble_object.users.myself_session, params)
self.mumble_object.execute_command(cmd)
def comment(self, comment):
"""Set the user comment"""
params = {"session": self["session"],
"comment": comment}
cmd = messages.ModUserState(self.mumble_object.users.myself_session, params)
self.mumble_object.execute_command(cmd)
def texture(self, texture):
"""Set the user texture"""
params = {"session": self["session"],
"texture": texture}
cmd = messages.ModUserState(self.mumble_object.users.myself_session, params)
self.mumble_object.execute_command(cmd)