2019-08-22 18:33:10 +08:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# coding=UTF-8
|
|
|
|
|
2019-08-24 17:22:23 +08:00
|
|
|
import os, time, json
|
|
|
|
from .core.ttypes import OpType
|
2019-08-24 14:43:16 +08:00
|
|
|
|
2019-08-22 18:33:10 +08:00
|
|
|
class Yuuki_Data:
|
|
|
|
def __init__(self):
|
2019-08-24 17:22:23 +08:00
|
|
|
|
|
|
|
# Data
|
|
|
|
|
|
|
|
self.Data = {}
|
|
|
|
|
|
|
|
self.DataType = {
|
|
|
|
"Global":{
|
|
|
|
"SecurityService":False,
|
|
|
|
},
|
|
|
|
"Group": {},
|
|
|
|
"BlackList":[]
|
|
|
|
}
|
|
|
|
|
|
|
|
self.GroupType = {
|
|
|
|
"ID":"",
|
|
|
|
"SEGroup":None,
|
|
|
|
"Ext_Admin":[]
|
|
|
|
}
|
|
|
|
|
|
|
|
self.SEGrouptype = {
|
|
|
|
OpType.NOTIFIED_UPDATE_GROUP:False,
|
|
|
|
OpType.NOTIFIED_INVITE_INTO_GROUP:False,
|
|
|
|
OpType.NOTIFIED_ACCEPT_GROUP_INVITATION:False,
|
|
|
|
OpType.KICKOUT_FROM_GROUP:False,
|
|
|
|
OpType.NOTIFIED_KICKOUT_FROM_GROUP:False
|
|
|
|
}
|
|
|
|
|
|
|
|
self.DataPath = "data/"
|
|
|
|
self.DataName = "{}.json"
|
|
|
|
|
|
|
|
if not os.path.isdir(self.DataPath):
|
|
|
|
os.mkdir(self.DataPath)
|
|
|
|
|
|
|
|
for Type in self.DataType:
|
|
|
|
name = self.DataPath + self.DataName.format(Type)
|
|
|
|
if not os.path.isfile(name):
|
|
|
|
with open(name, "w") as f:
|
|
|
|
f.write("")
|
2019-08-24 20:40:17 +08:00
|
|
|
Type = 0
|
2019-08-24 17:22:23 +08:00
|
|
|
else:
|
|
|
|
with open(name, "r") as f:
|
2019-08-24 20:31:58 +08:00
|
|
|
try:
|
|
|
|
json.loads(f.read())
|
|
|
|
Type = 0
|
|
|
|
except ValueError:
|
|
|
|
Type = 1
|
|
|
|
assert Type == 0, "{}\nJson Test Error".format(name)
|
2019-08-24 17:22:23 +08:00
|
|
|
|
|
|
|
# Data Initialize
|
|
|
|
|
|
|
|
for Type in self.DataType:
|
|
|
|
name = self.DataPath + self.DataName.format(Type)
|
|
|
|
with open(name, "r+") as f:
|
2019-08-24 20:40:17 +08:00
|
|
|
text = f.read()
|
|
|
|
if text != "":
|
|
|
|
self.Data[Type] = json.loads(text)
|
2019-08-24 17:22:23 +08:00
|
|
|
else:
|
|
|
|
self.Data[Type] = self.DataType[Type]
|
|
|
|
f.write(json.dumps(self.Data[Type]))
|
|
|
|
|
|
|
|
# Log
|
|
|
|
|
2019-08-24 14:43:16 +08:00
|
|
|
self.LogType = {
|
|
|
|
"JoinGroup":"<li>%s: %s(%s) -> Inviter: %s</li>"
|
|
|
|
}
|
|
|
|
|
|
|
|
self.LogPath = "logs/"
|
|
|
|
self.LogName = "{}.html"
|
|
|
|
|
|
|
|
self.initHeader = "<title>{} - SYB</title>" \
|
|
|
|
"<meta charset='utf-8' />"
|
|
|
|
|
2019-08-24 15:05:00 +08:00
|
|
|
if not os.path.isdir(self.LogPath):
|
|
|
|
os.mkdir(self.LogPath)
|
|
|
|
|
2019-08-24 14:43:16 +08:00
|
|
|
for Type in self.LogType:
|
|
|
|
name = self.LogPath + self.LogName.format(Type)
|
2019-08-24 15:02:26 +08:00
|
|
|
if not os.path.isfile(name):
|
2019-08-24 14:43:16 +08:00
|
|
|
with open(name, "w") as f:
|
|
|
|
f.write(self.initHeader.format(Type))
|
|
|
|
|
2019-08-24 17:22:23 +08:00
|
|
|
def file(self, Type, Mode):
|
2019-08-24 14:43:16 +08:00
|
|
|
return open(self.LogPath + self.LogName.format(Type), Mode)
|
|
|
|
|
2019-08-24 17:22:23 +08:00
|
|
|
def updateData(self, Type, Input, Data):
|
|
|
|
with self.file(Type, "r") as f:
|
|
|
|
Data_ = json.loads(f.read())
|
|
|
|
if type(self.DataType[Type]) == list:
|
|
|
|
if Input:
|
|
|
|
Data_.append(Data)
|
|
|
|
else:
|
|
|
|
Data_.remove(Data)
|
|
|
|
elif type(self.DataType[Type]) == dict:
|
|
|
|
Data_[Type][Input] = Data
|
|
|
|
with self.file(Type, "w") as f:
|
|
|
|
f.write(json.dumps(Data_))
|
|
|
|
self.Data[Type] = Data_
|
|
|
|
|
2019-08-24 14:43:16 +08:00
|
|
|
def updateLog(self, Type, Data):
|
2019-08-24 17:22:23 +08:00
|
|
|
with self.file(Type, "a") as f:
|
2019-08-24 14:43:16 +08:00
|
|
|
f.write(self.LogType[Type] % Data)
|
|
|
|
|
|
|
|
def getTime(self, format="%b %d %Y %H:%M:%S %Z"):
|
|
|
|
Time = time.localtime(time.time())
|
|
|
|
return time.strftime(format, Time)
|
2019-08-24 17:22:23 +08:00
|
|
|
|
|
|
|
def getData(self, Type):
|
|
|
|
return self.Data[Type]
|
|
|
|
|
|
|
|
def getDataGroupID(self, DataGroup):
|
|
|
|
return DataGroup["ID"]
|
|
|
|
|
|
|
|
def getGroup(self, GroupID):
|
|
|
|
Groups = self.getData("Group")
|
|
|
|
if len(Groups) > 0:
|
|
|
|
GroupIDs = [self.getDataGroupID(Group) for Group in Groups]
|
|
|
|
if GroupID in GroupIDs:
|
|
|
|
return Groups[GroupID]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def getSEGroup(self, GroupID):
|
|
|
|
Group = self.getGroup(GroupID)
|
|
|
|
if Group:
|
|
|
|
return Group["SEGroup"]
|
|
|
|
else:
|
|
|
|
return None
|