Readme requierement
This commit is contained in:
parent
0a3489ab05
commit
a2a2fa3e4d
@ -2,122 +2,129 @@ import os, socket, threading, sys, configparser, re
|
|||||||
from serveur import Log
|
from serveur import Log
|
||||||
|
|
||||||
|
|
||||||
def handleConnection(connection, client_address) :
|
def handleConnection(connection, client_address):
|
||||||
try:
|
try:
|
||||||
log.printL("Connection from IP -> {}".format(client_address), Log.lvl.INFO)
|
log.printL("Connection from IP -> {}".format(client_address), Log.lvl.INFO)
|
||||||
#userListActive(connection)
|
|
||||||
#userListAway(connection)
|
|
||||||
while True:
|
while True:
|
||||||
data = connection.recv(4096)
|
data = connection.recv(4096)
|
||||||
if data:
|
if data:
|
||||||
log.printL("Request from IP -> {}"
|
log.printL("Request from IP -> {}"
|
||||||
" {}".format(client_address,data.decode()), Log.lvl.INFO)
|
" {}".format(client_address, data.decode()), Log.lvl.INFO)
|
||||||
threading.Thread(target=handleRequest, args=(connection, data.decode())).start()
|
threading.Thread(target=handleRequest, args=(connection, data.decode())).start()
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
except Exception as e :
|
except Exception as e:
|
||||||
log.printL(str(e), Log.lvl.FAIL)
|
log.printL("Handle connection fail : ".format(str(e)), Log.lvl.FAIL)
|
||||||
finally:
|
finally:
|
||||||
quit(connection)
|
quit(connection)
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
else :
|
|
||||||
if arrayData[0] == "/name" :
|
|
||||||
changeName(connection, arrayData[1])
|
|
||||||
return
|
return
|
||||||
if arrayData[0] == "/askpm" :
|
else:
|
||||||
askPrivateMsg(connection,arrayData[1])
|
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:
|
||||||
|
if arrayData[0] == "/newname":
|
||||||
|
newName(connection, arrayData[1])
|
||||||
return
|
return
|
||||||
if arrayData[0] == "/acceptpm" :
|
if arrayData[0] == "/quit":
|
||||||
acceptPrivateMsg(connection,arrayData[1])
|
connection.shutdown(socket.SHUT_RD)
|
||||||
return
|
return
|
||||||
if arrayData[0] == "/rejectpm" :
|
connection.sendall("ERR_NO_NICKNAME".encode())
|
||||||
rejectPrivateMsg(connection,arrayData[1])
|
except Exception as e :
|
||||||
return
|
log.printL("Handle request fail : {}".format(str(e)), Log.lvl.FAIL)
|
||||||
if arrayData[0] == "/pm" :
|
connection.sendall("ERR_INTERNAL_SERVER_ERROR".encode())
|
||||||
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" :
|
|
||||||
quit(connection)
|
|
||||||
return
|
|
||||||
connection.sendall("ERR_COMMAND_NOT_FOUND".encode())
|
|
||||||
else:
|
|
||||||
if arrayData[0] == "/newname" :
|
|
||||||
newName(connection, arrayData[1])
|
|
||||||
return
|
|
||||||
if arrayData[0] == "/quit" :
|
|
||||||
quit(connection)
|
|
||||||
return
|
|
||||||
connection.sendall("ERR_NO_NICKNAME".encode())
|
|
||||||
"""except Exception as e :
|
|
||||||
log.printL(str(e), Log.lvl.FAIL)"""
|
|
||||||
|
|
||||||
|
|
||||||
def broadcastMsg(connection,message):
|
def broadcastMsg(connection, message):
|
||||||
log.printL("User Connected : {}".format(usersConnected), Log.lvl.DEBUG)
|
log.printL("User Connected : {}".format(usersConnected), Log.lvl.DEBUG)
|
||||||
for con, value in usersConnected.items() :
|
for con, value in usersConnected.items():
|
||||||
if usersConnected[con][1] is not None and con != connection and connection[con][2] == True:
|
if value[1] is not None and con != connection and value[2] == True:
|
||||||
try:
|
try:
|
||||||
con.sendall(message.encode())
|
con.sendall(message.encode())
|
||||||
except Exception as e :
|
except Exception as e:
|
||||||
log.printL(str(e), Log.lvl.FAIL)
|
log.printL(str(e), Log.lvl.FAIL)
|
||||||
|
|
||||||
|
|
||||||
def userListActive(connection):
|
def userListActive(connection):
|
||||||
l = "USERLIST "
|
l = "USERLIST "
|
||||||
for con, value in usersConnected.items() :
|
for con, value in usersConnected.items():
|
||||||
if value[2] :
|
if value[1] is not None and value[2]:
|
||||||
l += value[1] + " "
|
l += value[1] + " "
|
||||||
connection.sendall(l[:-1].encode())
|
connection.sendall(l[:-1].encode())
|
||||||
|
|
||||||
|
|
||||||
def userListAway(connection):
|
def userListAway(connection):
|
||||||
l = "USERAWAY "
|
l = "USERAWAY "
|
||||||
for con,value in usersConnected.items() :
|
for con, value in usersConnected.items():
|
||||||
if value[2]:
|
if value[1] is not None and not value[2]:
|
||||||
l += value[1] + " "
|
l += value[1] + " "
|
||||||
connection.sendall(l[:-1].encode())
|
connection.sendall(l[:-1].encode())
|
||||||
|
|
||||||
|
|
||||||
def changeName(connection, pseudo):
|
def changeName(connection, pseudo):
|
||||||
if not re.match("^\w{3,15}$",pseudo) :
|
if not re.match("^\w{3,15}$", pseudo):
|
||||||
connection.sendall("ERR_INVALID_NICKNAME".encode())
|
connection.sendall("ERR_INVALID_NICKNAME".encode())
|
||||||
else:
|
else:
|
||||||
broadcastMsg(connection,"NAME_CHANGED {} {}".format(usersConnected[connection][1], pseudo))
|
broadcastMsg(connection, "NAME_CHANGED {} {}".format(usersConnected[connection][1], pseudo))
|
||||||
connection.sendall("SUCC_VALID_NICKNAME".encode())
|
connection.sendall("SUCC_VALID_NICKNAME".encode())
|
||||||
usersConnected[connection][1] = pseudo
|
usersConnected[connection][1] = pseudo
|
||||||
|
|
||||||
|
|
||||||
def newName(connection, pseudo):
|
def newName(connection, pseudo):
|
||||||
if not re.match("^\w{3,15}$",pseudo) :
|
if not re.match("^\w{3,15}$", pseudo):
|
||||||
connection.sendall("ERR_INVALID_NICKNAME".encode())
|
connection.sendall("ERR_INVALID_NICKNAME".encode())
|
||||||
else:
|
else:
|
||||||
broadcastMsg(connection, "HAS_JOIN {} ".format(pseudo))
|
broadcastMsg(connection, "HAS_JOIN {} ".format(pseudo))
|
||||||
connection.sendall("SUCC_CHANNEL_JOINED".encode())
|
connection.sendall("SUCC_CHANNEL_JOINED".encode())
|
||||||
usersConnected[connection][1] = pseudo
|
usersConnected[connection][1] = pseudo
|
||||||
|
userListActive(connection)
|
||||||
|
userListAway(connection)
|
||||||
|
|
||||||
|
|
||||||
def askPrivateMsg(connection,pseudo):
|
def askPrivateMsg(connection, pseudo):
|
||||||
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 = (connection, c)
|
||||||
if pm in askPM :
|
if pm in askPM:
|
||||||
connection.sendall("ALREADY_ASKED".encode())
|
connection.sendall("ALREADY_ASKED".encode())
|
||||||
else:
|
else:
|
||||||
askPM.append(pm)
|
askPM.append(pm)
|
||||||
@ -127,11 +134,11 @@ def askPrivateMsg(connection,pseudo):
|
|||||||
|
|
||||||
def acceptPrivateMsg(connection, pseudo):
|
def acceptPrivateMsg(connection, pseudo):
|
||||||
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 = (connection, c)
|
||||||
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:
|
||||||
askPM.remove(pm)
|
askPM.remove(pm)
|
||||||
@ -141,11 +148,11 @@ def acceptPrivateMsg(connection, pseudo):
|
|||||||
|
|
||||||
def rejectPrivateMsg(connection, pseudo):
|
def rejectPrivateMsg(connection, pseudo):
|
||||||
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 = (connection, c)
|
||||||
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:
|
||||||
askPM.remove(pm)
|
askPM.remove(pm)
|
||||||
@ -154,19 +161,19 @@ def rejectPrivateMsg(connection, pseudo):
|
|||||||
|
|
||||||
def privateMsg(connection, pseudo, msg):
|
def privateMsg(connection, pseudo, msg):
|
||||||
c = getConnectionByPseudo(pseudo)
|
c = getConnectionByPseudo(pseudo)
|
||||||
if c is None :
|
if c is None:
|
||||||
connection.sendall("ERR_DEST_NOT_FOUND".encode())
|
connection.sendall("ERR_DEST_NOT_FOUND".encode())
|
||||||
else:
|
else:
|
||||||
pm = (connection,c)
|
pm = (connection, c)
|
||||||
if sorted(pm) not in sorted(validatePM) :
|
if sorted(pm) not in sorted(validatePM):
|
||||||
connection.sendall("ERR_NOT_ACCEPTED".encode())
|
connection.sendall("ERR_NOT_ACCEPTED".encode())
|
||||||
else:
|
else:
|
||||||
c.sendall("NEW_PM {} {}".format(pseudo,msg).encode())
|
c.sendall("NEW_PM {} {}".format(pseudo, msg).encode())
|
||||||
connection.sendall("SUCC_PM_SENDED".encode())
|
connection.sendall("SUCC_PM_SENDED".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
|
||||||
connection.sendall("SUCC_ENABLED".encode())
|
connection.sendall("SUCC_ENABLED".encode())
|
||||||
else:
|
else:
|
||||||
@ -174,44 +181,47 @@ def enableUser(connection):
|
|||||||
|
|
||||||
|
|
||||||
def disableUser(connection):
|
def disableUser(connection):
|
||||||
if usersConnected[connection][2] == True :
|
if usersConnected[connection][2] == True:
|
||||||
usersConnected[connection][2] = False
|
usersConnected[connection][2] = False
|
||||||
connection.sendall("SUCC_DISABLED".encode())
|
connection.sendall("SUCC_DISABLED".encode())
|
||||||
else:
|
else:
|
||||||
connection.sendall("ERR_NOT_ENABLED".encode())
|
connection.sendall("ERR_NOT_ENABLED".encode())
|
||||||
|
|
||||||
|
|
||||||
def quit(connection) :
|
def quit(connection):
|
||||||
connection.sendall("SUCCESSFUL_LOGOUT".encode())
|
try:
|
||||||
|
connection.sendall("SUCCESSFUL_LOGOUT".encode())
|
||||||
|
except OSError: # Client close the socket in this side not properly
|
||||||
|
log.printL("Client IP -> {} close connection not properly"
|
||||||
|
"".format(usersConnected[connection][0]), Log.lvl.WARNING)
|
||||||
connection.close()
|
connection.close()
|
||||||
log.printL("Disconnection from IP -> {}".format(usersConnected[connection][0]), Log.lvl.INFO)
|
log.printL("Disconnection from IP -> {}".format(usersConnected[connection][0]), Log.lvl.INFO)
|
||||||
pseudo = usersConnected[connection][1]
|
pseudo = usersConnected[connection][1]
|
||||||
usersConnected.pop(connection)
|
usersConnected.pop(connection)
|
||||||
broadcastMsg(connection,"HAS_LEFT {}".format(pseudo))
|
broadcastMsg(connection, "HAS_LEFT {}".format(pseudo))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def getConnectionByPseudo(pseudo):
|
def getConnectionByPseudo(pseudo):
|
||||||
for con, value in usersConnected.items() :
|
for con, value in usersConnected.items():
|
||||||
if value[1] == pseudo :
|
if value[1] == pseudo:
|
||||||
return con
|
return con
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
#Global vars
|
# Global vars
|
||||||
global usersConnected, log, sock
|
global usersConnected, log, sock
|
||||||
usersConnected = {}
|
|
||||||
global askPM, validatePM
|
global askPM, validatePM
|
||||||
global askFT, validateFT
|
global askFT, validateFT
|
||||||
|
usersConnected = {}
|
||||||
askPM = []
|
askPM = []
|
||||||
validatePM = []
|
validatePM = []
|
||||||
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'}
|
||||||
config['LOG'] = {'logDirectory': 'log'}
|
config['LOG'] = {'logDirectory': 'log'}
|
||||||
with open('dncserver.conf', 'w') as configfile:
|
with open('dncserver.conf', 'w') as configfile:
|
||||||
config.write(configfile)
|
config.write(configfile)
|
||||||
config.read("dncserver.conf")
|
config.read("dncserver.conf")
|
||||||
log = Log.Log(config["LOG"]["logdirectory"])
|
log = Log.Log(config["LOG"]["logdirectory"])
|
||||||
log.printL("Configuration Log", Log.lvl.INFO)
|
log.printL("Configuration Log", Log.lvl.INFO)
|
||||||
@ -224,27 +234,22 @@ def main():
|
|||||||
sock.listen(5)
|
sock.listen(5)
|
||||||
log.printL("Server Listen on port {}".format(config["NETWORK"]["port"]), Log.lvl.INFO)
|
log.printL("Server Listen on port {}".format(config["NETWORK"]["port"]), Log.lvl.INFO)
|
||||||
|
|
||||||
|
try:
|
||||||
try :
|
while True:
|
||||||
while True :
|
|
||||||
#Connection client
|
#Connection client
|
||||||
connection, client_address = sock.accept()
|
connection, client_address = sock.accept()
|
||||||
usersConnected[connection] = [client_address,None,True] # ip pseudo status
|
usersConnected[connection] = [client_address, None, True] # ip pseudo status
|
||||||
threading.Thread(target=handleConnection,args=(connection,client_address)).start()
|
threading.Thread(target=handleConnection, args=(connection, client_address)).start()
|
||||||
except KeyboardInterrupt :
|
except KeyboardInterrupt:
|
||||||
# Disable to received more requests on socket
|
# Disable to received more requests on socket
|
||||||
for con, value in usersConnected.items():
|
for con, value in usersConnected.items():
|
||||||
con.shutdown(socket.SHUT_RD)
|
con.shutdown(socket.SHUT_RD)
|
||||||
finally :
|
finally:
|
||||||
#Wait for threads finish
|
#Wait for threads finish
|
||||||
log.printL("Wait for threads ending", Log.lvl.INFO)
|
log.printL("Wait for threads ending", Log.lvl.INFO)
|
||||||
for t in threading.enumerate():
|
for t in threading.enumerate():
|
||||||
if t!= threading.main_thread():
|
if t != threading.main_thread():
|
||||||
t.join()
|
t.join()
|
||||||
#Close all clients sockets
|
|
||||||
for con, value in usersConnected.items():
|
|
||||||
quit(con)
|
|
||||||
#Close the socket server
|
|
||||||
sock.close()
|
sock.close()
|
||||||
log.printL("Server shutdown", Log.lvl.INFO)
|
log.printL("Server shutdown", Log.lvl.INFO)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
Reference in New Issue
Block a user