-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUserInfo.py
102 lines (82 loc) · 3 KB
/
UserInfo.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
import struct
import Config
from Util import Util
ALL_PAGER = 0x1
FRIEND_PAGER = 0x2
ALLMSG_PAGER = 0x4
FRIENDMSG_PAGER = 0x8
CHECK_MAIL = 0x1
CHECK_MSG = 0x2
class UserInfo:
_fields = ['active', 'uid', 'pid', 'invisible', 'sockactive', 'sockaddr', 'destuid', 'mode', 'pager', 'in_chat', ['chatid', 1], ['from', 1], 'logintime', 'fill', 'freshtime', 'utmpkey', 'mailbox_prop', ['userid', 1], ['realname', 1], ['username', 1], 'friendsnum', ['friends_uid', 2, '=%di' % Config.MAXFRIENDS ], 'currentboard', 'mailcheck']
_parser = struct.Struct('=iiiiiiiiii16s%dsi36siiI20s20s40si%dsiI' % (Config.IPLEN + 4, Config.MAXFRIENDS * 4))
ACTIVE_POS = 0
UID_POS = ACTIVE_POS + 4
PID_POS = UID_POS + 4
INVISIBLE_POS = PID_POS + 4
SOCKACTIVE_POS = INVISIBLE_POS + 4
SOCKADDR_POS = SOCKACTIVE_POS + 4
DESTUID_POS = SOCKADDR_POS + 4
MODE_POS = DESTUID_POS + 4
PAGER_POS = MODE_POS + 4
INCHAT_POS = MODE_POS + 4
USERID_POS = 0x80
USERID_SIZE = 20
#struct user_info
size = _parser.size
def __repr__(self):
return "Idx: %d Active: %s UID: %d User: %s" % (self._index, self.active, self.uid, self.userid)
def load(self):
if (self._index < 0):
raise Exception("Cannot load without index!")
from Utmp import Utmp
self.unpack(Utmp.utmpshm.read(UserInfo.size, self._index * UserInfo.size))
def save(self):
if (self._index < 0):
raise Exception("Cannot save without index!")
from Utmp import Utmp
Utmp.utmpshm.write(self.pack(), self._index * UserInfo.size)
def GetInt(self, offset):
from Utmp import Utmp
return struct.unpack('=i', Utmp.utmpshm.read(4, self._index * UserInfo.size + offset))[0]
def GetMode(self):
return self.GetInt(UserInfo.MODE_POS)
def unpack(self, str):
Util.Unpack(self, UserInfo._parser.unpack(str))
def pack(self):
try:
result = UserInfo._parser.pack(*Util.Pack(self))
except Exception as e:
print Util.Pack(self)
raise e
return result
def __init__(self, idx = 0):
self._index = idx - 1
if (idx != 0):
self.load()
else:
Util.InitStruct(self)
self.friends_nick = []
def GetIndex(self):
return self._index + 1
def SetIndex(self, index):
self._index = index - 1
def HasFriend(self, uid):
if (self.friendsnum <= 0):
return False, None
for i in range(self.friendsnum):
if (self.friends_uid[i] == uid):
return True, i
return False, None
def GetNick(self, i):
if (len(self.friends_nick) >= i):
return None
return self.friends_nick[i]
def AcceptMsg(self, friend = False):
if (friend):
return self.pager & FRIENDMSG_PAGER
else:
return self.pager & ALLMSG_PAGER
def SetMailCheck(self):
self.mailcheck &= ~CHECK_MAIL
self.save()