-
Notifications
You must be signed in to change notification settings - Fork 3
/
MsgBox.py
279 lines (229 loc) · 7.31 KB
/
MsgBox.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
#!/usr/bin/env python
from Util import Util
import User
from MsgHead import MsgHead
from Log import Log
import Config
import struct
import time
class MsgBox:
name = ''
fAll = None
fIn = None
fContent = None
def __init__(self, username):
self.name = username
def LockGeneral(self, file, write):
if (write):
return Util.WLockFile(file)
else:
return Util.RLockFile(file)
def LockAll(self, write):
if (self.fAll == None):
if (not self.OpenAll(write)):
return False
return self.LockGeneral(self.fAll, write)
def LockIn(self, write):
if (self.fIn == None):
if (not self.OpenIn(write)):
return False
return self.LockGeneral(self.fIn, write)
def LockContent(self, write):
if (self.fContent == None):
if (not self.OpenContent(write)):
return False
return self.LockGeneral(self.fContent, write)
def UnlockGeneral(self, file):
if (file != None):
return Util.UnlockFile(file)
else:
return True # maybe not ok!
def UnlockAll(self):
return self.UnlockGeneral(self.fAll)
def UnlockIn(self):
return self.UnlockGeneral(self.fIn)
def UnlockContent(self):
return self.UnlockGeneral(self.fContent)
def OpenGeneral(self, file, write):
path = User.User.OwnFile(self.name, file)
if (write):
try:
return open(path, 'r+b')
except IOError:
try:
return open(path, 'w+b')
except:
return None
except:
return None
else:
try:
return open(path, 'rb')
except:
return None
def OpenIn(self, write):
self.fIn = self.OpenGeneral('msgindex2', write)
return (self.fIn != None)
def OpenAll(self, write):
self.fAll = self.OpenGeneral('msgindex', write)
return (self.fAll != None)
def OpenContent(self, write):
self.fContent = self.OpenGeneral('msgcontent', write)
return (self.fContent != None)
def CloseIn(self):
self.fIn.close()
def CloseAll(self):
self.fAll.close()
def CloseContent(self):
self.fContent.close()
def SizeIn(self):
return Util.SizeGeneral(self.fIn)
def SizeAll(self):
return Util.SizeGeneral(self.fAll)
def SizeContent(self):
return Util.SizeGeneral(self.fContent)
def GetUnreadCount(self):
if (not self.OpenIn(write = False)):
return -1
if (not self.LockIn(write = False)):
self.CloseIn()
return -1
size = self.SizeIn()
count = (size - 4) / MsgHead.size
if (size <= 0):
ret = 0
else:
ret = struct.unpack('=i', self.fIn.read(4))[0]
if (ret >= count):
ret = 0
else:
ret = count - ret
self.UnlockIn()
self.CloseIn()
return ret
def GetUnreadMsg(self):
if (not self.OpenIn(write = True)):
return -1
if (not self.LockIn(write = True)):
self.CloseIn()
return -1
size = self.SizeIn()
count = (size - 4) / MsgHead.size
if (size <= 0):
ret = -1
else:
ret = struct.unpack('=i', self.fIn.read(4))[0]
if (ret >= count):
ret = -1
else:
index = ret + 1
self.fIn.seek(0)
self.fIn.write(struct.pack('=i', index))
self.UnlockIn()
self.CloseIn()
return ret
def LoadMsgHead(self, index, all):
if (all):
if (not self.OpenAll(write = False)):
return None
else:
if (not self.OpenIn(write = False)):
return None
if (all):
file = self.fAll
else:
file = self.fIn
if (not self.LockGeneral(file, write = False)):
self.CloseIn()
return None
size = Util.SizeGeneral(file)
count = (size - 4) / MsgHead.size
if (index < 0 or index >= count):
self.UnlockGeneral(file)
file.close()
return None
file.seek(index * MsgHead.size + 4)
ret = MsgHead(file.read(MsgHead.size))
self.UnlockGeneral(file)
file.close()
return ret
def SaveMsgText(self, msghead, msg):
msg = msg.encode('gbk', 'ignore')
if (not self.OpenAll(write = True)):
return -1;
if (not self.OpenContent(write = True)):
self.CloseAll();
return -1;
if (not self.LockAll(write = True)):
self.CloseAll();
self.CloseContent();
return -1;
content_size = self.SizeContent();
idx_size = self.SizeAll();
count = 0;
i = 0;
if (idx_size <= 0):
self.fAll.write('\0\0\0\0');
else:
count = (idx_size - 4) / MsgHead.size;
self.fAll.seek(count * MsgHead.size + 4);
msglen = len(msg) + 1;
if (msglen >= Config.MAX_MSG_SIZE):
msglen = Config.MAX_MSG_SIZE - 1;
msghead.pos = content_size;
msghead.len = msglen;
self.fAll.write(msghead.pack());
self.fContent.seek(content_size);
if (msglen != len(msg) + 1):
self.fContent.write(msg[:msglen]);
else:
self.fContent.write(msg);
self.fContent.write('\0');
self.CloseContent();
self.UnlockAll();
self.CloseAll();
if (not msghead.sent):
if (not self.OpenIn(write = True)):
return -1;
if (not self.LockIn(write = True)):
self.CloseIn();
return -1;
inbox_size = self.SizeIn();
inbox_count = 0;
if (inbox_size <= 0):
self.fIn.write('\0\0\0\0');
else:
inbox_count = (inbox_size - 4) / MsgHead.size;
self.fIn.seek(inbox_count * MsgHead.size + 4);
self.fIn.write(msghead.pack());
self.UnlockIn();
self.CloseIn();
return 0;
def LoadMsgText(self, msghead):
if (not self.OpenContent(write = False)):
return None
self.fContent.seek(msghead.pos);
msglen = msghead.len;
if (msglen >= Config.MAX_MSG_SIZE):
msglen = Config.MAX_MSG_SIZE - 1;
msg = self.fContent.read(msglen);
self.CloseContent();
return Util.gbkDec(Util.CString(msg))
def GetMsgCount(self, all):
path = ""
if (all):
path = User.User.OwnFile(self.name, "msgindex")
else:
path = User.User.OwnFile(self.name, "msgindex2")
size = Util.GetSize(path)
if (size <= 0): return 0
return (size - 4) / MsgHead.size
def ClearMsg(self):
files = ['msgindex', 'msgindex2', 'msgcontent'];
for file in files:
try:
path = User.User.OwnFile(self.name, file)
os.remove(path)
except:
Log.warn("remove %s for user %s failed" % (file, self.name))
return 0