chatServer/modules/history.py

59 lines
1.8 KiB
Python
Raw Normal View History

2021-10-28 06:17:07 +00:00
global historyMaxMessages
historyMaxMessages = 10000
global addToHistory
def addToHistory(user,id,cmd):
if historyMaxMessages == 0: return
messageHistory = dbGetTable(p(accountPath,user,"history.db"),"HISTORY")
if messageHistory == False: messageHistory = [[],[]]
hlength = len(messageHistory[0])
while hlength >= historyMaxMessages:
del messageHistory[0][0]
del messageHistory[1][0]
hlength -= 1
break
messageHistory[0].append(id)
messageHistory[1].append(cmd)
dbSetTable(p(accountPath,user,"history.db"),"HISTORY",messageHistory)
global getHistoryUntil
def getHistoryUntil(user,id):
messageHistory = dbGetTable(p(accountPath,user,"history.db"),"HISTORY")
if messageHistory == False: messageHistory = [[],[]]
id = int(id)
hlength = len(messageHistory[0])
index = hlength - 1
while index > 0:
mId = int(messageHistory[0][index])
if mId < id: break
yield messageHistory[1][index]
index -= 1
def f(self,cmd,*args):
if len(args) != 1:
return ["error","nonfatal","syntax","Correct syntax: " +cmd+ ",<message id>"]
fetchTill = 0
try:
fetchTill = int(args[0])
except:
return ["error","nonfatal","syntax","The message ID has to be an integer"]
with connectionsLock:
with fileLock:
user = connections[self.cid]["user"]
lastMessage = 0
if "lastMessage" in connections[self.cid]:
lastMessage = int(connections[self.cid]["lastMessage"])
if lastMessage < fetchTill:
connections[self.cid]["lastMessage"] = str(fetchTill)
msgList = []
for msg in getHistoryUntil(user,fetchTill):
msgList.append(commandlineToList(msg))
msgList.reverse()
for msg in msgList:
connections[self.cid]["threadOut"].queue.put(listToCommandline("history" + msg))
commands["getHistory"] = f