203 lines
7.3 KiB
Python
203 lines
7.3 KiB
Python
import os
|
|
import random
|
|
import string
|
|
import ast
|
|
import copy
|
|
import time
|
|
|
|
if not os.path.exists("PM server/acc's"):
|
|
os.mkdir("PM server")
|
|
file = open("PM server/acc's", 'w')
|
|
file.write(str({}))
|
|
file.close()
|
|
file = open("PM server/conversations", 'w')
|
|
file.write(str({}))
|
|
file.close()
|
|
file = open("PM server/acc's", 'r')
|
|
accounts = file.read()
|
|
accounts = ast.literal_eval(accounts)
|
|
file.close()
|
|
file = open("PM server/conversations", 'r')
|
|
conversations = file.read()
|
|
conversations = ast.literal_eval(conversations)
|
|
file.close()
|
|
file = open("PM server/bots", 'r')
|
|
bots = file.read()
|
|
bots = ast.literal_eval(bots)
|
|
file.close()
|
|
|
|
currentsessionids = {}
|
|
sessiontime = {}
|
|
|
|
def updatefiles():
|
|
global accounts
|
|
global conversations
|
|
global bots
|
|
while True:
|
|
try:
|
|
file = open("PM server/acc's", 'r')
|
|
accounts = file.read()
|
|
accounts = ast.literal_eval(accounts)
|
|
file.close()
|
|
file = open("PM server/conversations", 'r')
|
|
conversations = file.read()
|
|
conversations = ast.literal_eval(conversations)
|
|
file.close()
|
|
file = open("PM server/bots", 'r')
|
|
bots = file.read()
|
|
bots = ast.literal_eval(bots)
|
|
file.close()
|
|
break
|
|
except:
|
|
pass
|
|
|
|
updatefiles()
|
|
|
|
def saveaccounts(target):
|
|
file = open("PM server/acc's", 'w')
|
|
file.write(str(target))
|
|
file.close()
|
|
|
|
def saveconversations(target):
|
|
file = open("PM server/conversations", 'w')
|
|
file.write(str(target))
|
|
file.close()
|
|
|
|
def savebots(target):
|
|
file = open("PM server/bots", 'w')
|
|
file.write(str(target))
|
|
file.close()
|
|
|
|
def GET(args, ressources=None):
|
|
global accounts
|
|
global conversations
|
|
global bots
|
|
global currentsessionids
|
|
global sessiontime
|
|
function = args["targetfunction"]
|
|
if function == "getsessionid":
|
|
name = args["name"]
|
|
password = args["pass"]
|
|
for i in currentsessionids.copy():
|
|
if currentsessionids[i] == name:
|
|
del currentsessionids[i]
|
|
del sessiontime[i]
|
|
if name in accounts:
|
|
if accounts[name]["pass"] == password:
|
|
temp = " "
|
|
for i in range(1,20):
|
|
temp = temp + random.choice(list(string.ascii_letters+string.digits))
|
|
currentsessionids[temp] = name
|
|
sessiontime[temp] = time.time()
|
|
return temp
|
|
else:
|
|
return "wrong password"
|
|
else:
|
|
return "account not found"
|
|
elif function == "register":
|
|
name = args["name"]
|
|
password = args["pass"]
|
|
if not name in accounts:
|
|
accounts[name] = {"pass": password}
|
|
saveaccounts(accounts)
|
|
temp = " "
|
|
for i in range(1,20):
|
|
temp = temp + random.choice(list(string.ascii_letters+string.digits))
|
|
currentsessionids[temp] = name
|
|
sessiontime[temp] = time.time()
|
|
return temp
|
|
else:
|
|
return "name already used!"
|
|
elif args["targetfunction"] == "update":
|
|
updatefiles()
|
|
sessionid = args["sessionid"]
|
|
if sessionid in currentsessionids:
|
|
lasttime = sessiontime[sessionid]
|
|
temp = time.time() - lasttime
|
|
if not temp > 60:
|
|
sessiontime[sessionid] = time.time()
|
|
name = currentsessionids[sessionid]
|
|
temp = []
|
|
for i in conversations:
|
|
if name in i.split("<splitedaccountname>"):
|
|
temp.append({"speakers": i, "conversation": copy.deepcopy(conversations[i])})
|
|
return temp
|
|
else:
|
|
currentsessionids.pop(sessionid)
|
|
sessiontime.pop(sessionid)
|
|
return "sessionid ran out"
|
|
elif args["targetfunction"] == "botgetconversations":
|
|
updatefiles()
|
|
if args["botname"] in bots:
|
|
if bots[args["botname"]]["pass"] == args["pass"]:
|
|
for i in bots[args["botname"]]["conversations"]:
|
|
bots[args["botname"]]["conversations"][i] = conversations[i]
|
|
savebots(bots)
|
|
print(bots[args["botname"]]["conversations"])
|
|
return bots[args["botname"]]["conversations"]
|
|
else:
|
|
return "wrong password"
|
|
else:
|
|
return "bot not found"
|
|
elif args["targetfunction"] == "botsend":
|
|
updatefiles
|
|
if args["botname"] in bots:
|
|
if bots[args["botname"]]["pass"] == args["pass"]:
|
|
message = args["message"]
|
|
conversation = args["conversation"]
|
|
if conversation in conversations:
|
|
if conversation in bots[args["botname"]]["conversations"]:
|
|
conversations[conversation] = conversations[conversation] + "message from bot " + message + "\n"
|
|
saveconversations(conversations)
|
|
return "sucess"
|
|
else:
|
|
return "no permission to send in this conversation"
|
|
else:
|
|
return "conversation not found"
|
|
else:
|
|
return "wrong password"
|
|
else:
|
|
return "bot not found"
|
|
elif args["targetfunction"] == "createbot":
|
|
updatefiles()
|
|
if not args["botname"] in bots:
|
|
bots[args["botname"]] = {}
|
|
bots[args["botname"]]["pass"] = args["pass"]
|
|
bots[args["botname"]]["conversations"] = {}
|
|
savebots(bots)
|
|
return "sucess"
|
|
else:
|
|
return "bot already exists"
|
|
|
|
def PATCH(args, ressources=None):
|
|
global accounts
|
|
global conversations
|
|
global currentsessionids
|
|
global sessiontime
|
|
if args["targetfunction"] == "send":
|
|
updatefiles()
|
|
sessionid = args["sessionid"]
|
|
message = args["message"]
|
|
name = currentsessionids[sessionid]
|
|
conversation = args["conversation"]
|
|
if conversation in conversations:
|
|
if name in conversation:
|
|
if message.split(" ")[0] != "!addbot":
|
|
conversations[conversation] = conversations[conversation] + name + ":" + message + "\n"
|
|
else:
|
|
message = message.split(" ")
|
|
if message[1] in bots:
|
|
bots[message[1]]["conversations"][conversation] = conversations[conversation]
|
|
conversations[conversation] = conversations[conversation] + "bot '{}' added!".format(message[1]) + "\n"
|
|
savebots(bots)
|
|
else:
|
|
conversations[conversation] = conversations[conversation] + "bot not found" + "\n"
|
|
saveconversations(conversations)
|
|
elif args["targetfunction"] == "newconv":
|
|
sessionid = args["sessionid"]
|
|
name = currentsessionids[sessionid]
|
|
target = args["target"]
|
|
if target in accounts:
|
|
conversations[name+"<splitedaccountname>"+target] = ""
|
|
saveconversations(conversations)
|