This commit is contained in:
sidya82 2015-04-06 14:55:45 +02:00
parent e140cf3f97
commit 86f33b67b9

View File

@ -20,56 +20,56 @@ def handleConnection(connection, client_address):
def handleRequest(connection, data): def handleRequest(connection, data):
try: #try:
arrayData = data.split(" ") arrayData = data.split(" ")
if usersConnected[connection][1] is not None: if usersConnected[connection][1] is not None:
if (not arrayData[0][0] == "/"): if (not arrayData[0][0] == "/"):
connection.sendall("SUCC_MESSAGE_SENDED".encode()) connection.sendall("SUCC_MESSAGE_SENDED".encode())
broadcastMsg(connection, "NEW_MSG {} {} ".format(usersConnected[connection][1], data)) broadcastMsg(connection, "NEW_MSG {} {} ".format(usersConnected[connection][1], data))
return return
else:
if arrayData[0] == "/name":
changeName(connection, arrayData[1])
return
if arrayData[0] == "/userlist":
userListActive(connection)
return
if arrayData[0] == "/userlistaway":
userListAway(connection)
return
if arrayData[0] == "/askpm":
askPrivateMsg(connection, arrayData[1])
return
if arrayData[0] == "/acceptpm":
acceptPrivateMsg(connection, arrayData[1])
return
if arrayData[0] == "/rejectpm":
rejectPrivateMsg(connection, arrayData[1])
return
if arrayData[0] == "/pm":
privateMsg(connection, arrayData[1], arrayData[2:])
return
if arrayData[0] == "/enable":
enableUser(connection)
return
if arrayData[0] == "/disable":
disableUser(connection)
return
if arrayData[0] == "/quit":
connection.shutdown(socket.SHUT_RD)
return
connection.sendall("ERR_COMMAND_NOT_FOUND".encode())
else: else:
if arrayData[0] == "/newname": if arrayData[0] == "/name":
newName(connection, arrayData[1]) changeName(connection, arrayData[1])
return
if arrayData[0] == "/userlist":
userListActive(connection)
return
if arrayData[0] == "/userlistaway":
userListAway(connection)
return
if arrayData[0] == "/askpm":
askPrivateMsg(connection, arrayData[1])
return
if arrayData[0] == "/acceptpm":
acceptPrivateMsg(connection, arrayData[1])
return
if arrayData[0] == "/rejectpm":
rejectPrivateMsg(connection, arrayData[1])
return
if arrayData[0] == "/pm":
privateMsg(connection, arrayData[1], arrayData[2:])
return
if arrayData[0] == "/enable":
enableUser(connection)
return
if arrayData[0] == "/disable":
disableUser(connection)
return return
if arrayData[0] == "/quit": if arrayData[0] == "/quit":
connection.shutdown(socket.SHUT_RD) connection.shutdown(socket.SHUT_RD)
return return
connection.sendall("ERR_NO_NICKNAME".encode()) connection.sendall("ERR_COMMAND_NOT_FOUND".encode())
except Exception as e : else:
if arrayData[0] == "/newname":
newName(connection, arrayData[1])
return
if arrayData[0] == "/quit":
connection.shutdown(socket.SHUT_RD)
return
connection.sendall("ERR_NO_NICKNAME".encode())
"""except Exception as e :
log.printL("Handle request fail : {}".format(str(e)), Log.lvl.FAIL) log.printL("Handle request fail : {}".format(str(e)), Log.lvl.FAIL)
connection.sendall("ERR_INTERNAL_SERVER_ERROR".encode()) connection.sendall("ERR_INTERNAL_SERVER_ERROR".encode())"""
def broadcastMsg(connection, message): def broadcastMsg(connection, message):
@ -128,18 +128,18 @@ def askPrivateMsg(connection, pseudo):
connection.sendall("ALREADY_ASKED".encode()) connection.sendall("ALREADY_ASKED".encode())
else: else:
askPM.append(pm) askPM.append(pm)
log.printL("askPm []".format(askPM), Log.lvl.DEBUG) log.printL("askPm {}".format(askPM), Log.lvl.DEBUG)
c.sendall("ASKING_FOR_PM {}".format(usersConnected[connection][1]).encode()) c.sendall("ASKING_FOR_PM {}".format(usersConnected[connection][1]).encode())
connection.sendall("SUCC_INVITED".encode()) connection.sendall("SUCC_INVITED".encode())
def acceptPrivateMsg(connection, pseudo): def acceptPrivateMsg(connection, pseudo):
log.printL("askPm []".format(askPM), Log.lvl.DEBUG) log.printL("askPm {}".format(askPM), Log.lvl.DEBUG)
c = getConnectionByPseudo(pseudo) c = getConnectionByPseudo(pseudo)
if c is None: if c is None:
connection.sendall("ERR_USER_NOT_FOUND".encode()) connection.sendall("ERR_USER_NOT_FOUND".encode())
else: else:
pm = (connection, c) pm = (c, connection)
if pm not in askPM: if pm not in askPM:
connection.sendall("ERR_USER_HAS_NOT_ASK".encode()) connection.sendall("ERR_USER_HAS_NOT_ASK".encode())
else: else:
@ -153,7 +153,7 @@ def rejectPrivateMsg(connection, pseudo):
if c is None: if c is None:
connection.sendall("ERR_USER_NOT_FOUND".encode()) connection.sendall("ERR_USER_NOT_FOUND".encode())
else: else:
pm = (connection, c) pm = (c, connection)
if pm not in askPM: if pm not in askPM:
connection.sendall("ERR_USER_HAS_NOT_ASK".encode()) connection.sendall("ERR_USER_HAS_NOT_ASK".encode())
else: else:
@ -174,6 +174,50 @@ def privateMsg(connection, pseudo, msg):
connection.sendall("SUCC_PM_SENDED".encode()) connection.sendall("SUCC_PM_SENDED".encode())
def askFile(connection, pseudo, file):
c = getConnectionByPseudo(pseudo)
if c is None:
connection.sendall("ERR_USER_NOT_FOUND".encode())
else:
f = (connection, c, file)
if f in askFT:
connection.sendall("ERR_ALREADY_ONE".encode())
else:
askFT.append(f)
log.printL("askPm {}".format(askPM), Log.lvl.DEBUG)
c.sendall("HAS_ASKED_FILE {} {}".format(pseudo, file))
connection.sendall("SUCC_ASKED_FILE".encode())
def acceptFile(connection, pseudo, file, ip, port):
log.printL("askFT {}".format(askPM), Log.lvl.DEBUG)
c = getConnectionByPseudo(pseudo)
if c is None:
connection.sendall("ERR_USER_NOT_FOUND".encode())
else:
f = (c, connection, file)
if f not in askFT:
connection.sendall("ERR_USER_HAS_NOT_ASK".encode())
else:
askFT.remove(f)
connection.sendall("SUCC_FILE_ACCEPTED".encode())
c.sendall("CAN_SEND_FILE {} {} {} {}".format(file,pseudo,ip,port).encode())
def rejectFile(connection, pseudo, file):
c = getConnectionByPseudo(pseudo)
if c is None:
connection.sendall("ERR_USER_NOT_FOUND".encode())
else:
f = (c, connection, file)
if f not in askFT:
connection.sendall("ERR_USER_HAS_NOT_ASK".encode())
else:
askPM.remove(f)
connection.sendall("SUCC_FILE_REFUSED".encode())
def enableUser(connection): def enableUser(connection):
if usersConnected[connection][2] == False: if usersConnected[connection][2] == False:
usersConnected[connection][2] = True usersConnected[connection][2] = True
@ -216,10 +260,11 @@ def main():
# Global vars # Global vars
global usersConnected, log, sock global usersConnected, log, sock
global askPM, validatePM global askPM, validatePM
global askFT, validateFT global askFT
usersConnected = {} usersConnected = {}
askPM = [] askPM = []
validatePM = [] validatePM = []
askFT = []
config = configparser.ConfigParser() config = configparser.ConfigParser()
if not os.path.isfile("dncserver.conf"): if not os.path.isfile("dncserver.conf"):
config['NETWORK'] = {'port': '2222'} config['NETWORK'] = {'port': '2222'}