forked from lilydjwg/xmpptalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.py
287 lines (243 loc) · 8.2 KB
/
user.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#
# (C) Copyright 2012 lilydjwg <[email protected]>
#
# This file is part of xmpptalk.
#
# xmpptalk 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, either version 3 of the License, or
# (at your option) any later version.
#
# xmpptalk 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with xmpptalk. If not, see <http://www.gnu.org/licenses/>.
#
import logging
from functools import lru_cache
import datetime
import pymongo.errors
import models
import config
from greenlets import Welcome
from misc import *
logger = logging.getLogger(__name__)
class UserMixin:
# _cached_jid: the corresponding jid cached in _cached_user
_cached_jid = _cached_user = None
_cached_gp = None # Group or dict object
current_jid = current_user = None
@property
def current_user(self):
if self._cached_jid == self.current_jid:
return self._cached_user
if self.current_jid is None:
return
plainjid = str(self.current_jid.bare())
user = models.connection.User.one({'jid': plainjid})
# not in database
if user is None:
user = self.db_add_user(plainjid)
Welcome(self.current_jid, self, use_roster_nick=True)
self._cached_jid = self.current_jid
self._cached_user = user
return user
def handle_userjoin_before(self):
# TODO do block check here
# may invoke twice
return True
def db_add_user(self, plainjid):
'''
add new user to database, return the added user; if alreadly exists, query
and return it
'''
u = models.connection.User()
u.jid = plainjid
if plainjid == config.root:
u.flag = PERM_USER | PERM_GPADMIN | PERM_SYSADMIN
try:
u.save()
except pymongo.errors.DuplicateKeyError:
return False
return u
def set_user_nick(self, *args, **kwargs):
'''set sender's nick in database
return the old `User` document, raise ValueError if duplicate
use `increase` tells if this is an auto action so that the counter should
not be increased
This will reset the nick cache.
'''
try:
return self._set_user_nick(*args, **kwargs)['nick']
except TypeError: #None
pass
def set_self_nick(self, nick):
'''set sender's nick in database
return the old nick or None
This will reset the nick cache.
'''
jid = str(self.current_jid.bare())
user = self._set_user_nick(jid, nick)
return user['nick']
def _set_user_nick(self, plainjid, nick, increase=True):
'''set a user's nick in database
return the old `User` document, raise ValueError if duplicate
`increase` tells if this is an auto action so that the counter should not
be increased
This will reset the nick cache.
'''
if getattr(config, "nick_change_interval", None):
if self.current_user.nick_changes and \
self.now - self.current_user.nick_lastchange < config.nick_change_interval:
d = seconds2time(config.nick_change_interval.total_seconds())
raise Forbidden(_("you can't change your nick too often; only once in %s is allowed.") % d)
models.validate_nick(nick)
if self.nick_exists(nick):
raise ValueError(_('duplicate nick name: %s') % nick)
self.user_get_nick.cache_clear()
update = {
'$set': {
'nick': nick,
'nick_lastchange': self.now,
}
}
if increase:
update['$inc'] = {'nick_changes': 1}
ret = models.connection.User.collection.find_and_modify(
{'jid': plainjid}, update
)
self.current_user.reload()
return ret
@lru_cache()
def user_get_nick(self, plainjid):
'''get a user's nick
The result is cached so if any of the users's nicks change, call `cache_clear()`.
Fallback to `self.get_name` if not found in database'''
u = models.connection.User.one({'jid': plainjid}, ['nick'])
nick = u.nick if u else None
if nick is None:
#fallback
nick = self.get_name(plainjid)
return nick
def nick_exists(self, nick):
return models.connection.User.find_one({'nick': nick}, {}) is not None
def get_user_by_nick(self, nick):
'''returns a `User` object
nick should not be `None` or an arbitrary one will be returned'''
return models.connection.User.find_one({'nick': nick})
def get_user_by_jid(self, jid):
return models.connection.User.one({'jid': jid})
def user_reset_stop(self):
models.connection.User.collection.update(
{'jid': self.current_user.jid}, {'$set': {
'stop_until': self.now,
}}
)
#FIXME: if self.current_user has been deleted
self.current_user.reload()
self.user_update_presence(self.current_user)
def user_reset_mute(self, user):
models.connection.User.collection.update(
{'jid': user.jid}, {'$set': {
'mute_until': self.now,
}}
)
self.user_update_presence(self.current_user)
def user_update_msglog(self, msg):
'''Note: This won't reload `self.current_user`'''
models.connection.User.collection.update(
{'jid': self.current_user.jid}, {'$inc': {
'msg_chars': len(msg),
'msg_count': 1,
}}
)
def user_update_presence(self, user):
if isinstance(user, str):
user = self.get_user_by_jid(user)
if not user:
return
prefix = ''
sec1 = (user.mute_until - self.now).total_seconds()
if sec1 > 0:
t = (user.mute_until + config.timezoneoffset).strftime(dateformat)
prefix += _('(muted until %s) ') % t
sec2 = (user.stop_until - self.now).total_seconds()
if sec2 > 0:
t = (user.stop_until + config.timezoneoffset).strftime(dateformat)
prefix += _('(stopped until %s) ') % t
try:
seconds = min(sec for sec in (sec1, sec2) if sec > 0)
except ValueError:
seconds = 0
logger.debug('%s: %r seconds to go; sec1 = %r, sec2 = %r',
user.jid, seconds, sec1, sec2)
self.xmpp_setstatus(
prefix + self.group_status,
to_jid=user.jid,
)
if seconds > 0.1:
self.update_on_setstatus.add(user.jid)
# XXX: Too many handlers?
self.delayed_call(seconds, self.user_update_presence, user.jid)
else:
try:
self.update_on_setstatus.remove(user.jid)
except KeyError:
pass
def user_disappeared(self, plainjid):
models.connection.User.collection.update(
{'jid': plainjid}, {'$set': {
'last_seen': self.now,
}}
)
def user_delete(self, user):
user.delete()
self.unsubscribe(user.jid)
def handle_userjoin(self, action=None):
'''add the user to database and say Welcome'''
# TODO: 根据 action 区别处理
plainjid = str(self.current_jid.bare())
self._cached_jid = None
u = self.db_add_user(plainjid)
if u is False:
logger.warn('%s already in database', plainjid)
else:
Welcome(self.current_jid, self)
logger.info('%s joined', plainjid)
def handle_userleave(self, action=None):
'''user has left, delete the user from database'''
# TODO: 根据 action 区别处理
self.current_user.delete()
self._cached_jid = None
logger.info('%s left', self.current_jid)
@property
def group_status(self):
gp = self._cached_gp or models.connection.Group.one()
if gp is None:
return ''
else:
return gp.get('status', None)
@group_status.setter
def group_status(self, value):
# external change takes effect here
self._cached_gp = models.connection.Group.collection.find_and_modify(
None, {'$set': {'status': value}}, new=True
)
for jid in self.update_on_setstatus.copy():
self.user_update_presence(jid)
@property
def welcome(self):
gp = self._cached_gp or models.connection.Group.one()
if gp is None:
return DEFAULT_WELOME
else:
return gp.get('welcome', DEFAULT_WELOME)
@welcome.setter
def welcome(self, value):
# external change takes effect here
self._cached_gp = models.connection.Group.collection.find_and_modify(
None, {'$set': {'welcome': value}}, new=True
)