This repository has been archived on 2021-09-15. You can view files and clone it, but cannot push or open issues or pull requests.
L3DNC/serveur/Server.py

333 lines
12 KiB
Python
Raw Normal View History

2015-04-02 17:45:24 +02:00
import os, socket, threading, sys, configparser, re
2015-03-30 20:54:29 +02:00
from serveur import Log
2015-04-03 12:09:00 +02:00
def handleConnection(connection, client_address):
2015-04-03 11:13:58 +02:00
try:
log.printL("Connection from IP -> {}".format(client_address), Log.lvl.INFO)
while True:
data = connection.recv(4096)
if data:
log.printL("Request from IP -> {}"
2015-04-03 12:09:00 +02:00
" {}".format(client_address, data.decode()), Log.lvl.INFO)
2015-04-03 11:13:58 +02:00
threading.Thread(target=handleRequest, args=(connection, data.decode())).start()
else:
break
2015-04-03 12:09:00 +02:00
except Exception as e:
log.printL("Handle connection fail : ".format(str(e)), Log.lvl.FAIL)
2015-04-03 11:13:58 +02:00
finally:
2015-04-03 11:14:41 +02:00
quit(connection)
2015-03-30 20:54:29 +02:00
def handleRequest(connection, data):
2015-04-06 14:55:45 +02:00
#try:
arrayData = data.split(" ")
if usersConnected[connection][1] is not None:
if (not arrayData[0][0] == "/"):
connection.sendall("SUCC_MESSAGE_SENDED".encode())
broadcastMsg(connection, "NEW_MSG {} {} ".format(usersConnected[connection][1], data))
return
2015-04-03 12:09:00 +02:00
else:
2015-04-06 14:55:45 +02:00
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":
2015-04-08 10:08:59 +02:00
privateMsg(connection, arrayData[1], " ".join(arrayData[2:]))
2015-04-06 14:55:45 +02:00
return
2015-04-08 19:16:40 +02:00
if arrayData[0] == "/pmfile":
askFile(connection,arrayData[1],arrayData[2])
return
if arrayData[0] == "/acceptfile":
acceptFile(connection, arrayData[1], arrayData[2],arrayData[3])
return
if arrayData[0] == "/rejectfile":
rejectFile(connection, arrayData[1], " ".join(arrayData[2:]))
return
2015-04-06 14:55:45 +02:00
if arrayData[0] == "/enable":
enableUser(connection)
return
if arrayData[0] == "/disable":
disableUser(connection)
2015-04-01 08:37:05 +02:00
return
2015-04-03 12:09:00 +02:00
if arrayData[0] == "/quit":
connection.shutdown(socket.SHUT_RD)
2015-04-02 16:53:31 +02:00
return
2015-04-06 14:55:45 +02:00
connection.sendall("ERR_COMMAND_NOT_FOUND".encode())
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())
2015-04-08 19:16:40 +02:00
"""except IndexError:
log.printL("Parameter missing in the request", Log.lvl.WARNING)
connection.sendall("ERR_PARAMETER_MISSING".encode())
except Exception as e :
2015-04-03 12:09:00 +02:00
log.printL("Handle request fail : {}".format(str(e)), Log.lvl.FAIL)
2015-04-06 14:55:45 +02:00
connection.sendall("ERR_INTERNAL_SERVER_ERROR".encode())"""
2015-04-03 12:09:00 +02:00
def broadcastMsg(connection, message):
2015-04-03 11:19:47 +02:00
log.printL("User Connected : {}".format(usersConnected), Log.lvl.DEBUG)
2015-04-03 12:09:00 +02:00
for con, value in usersConnected.items():
if value[1] is not None and con != connection and value[2] == True:
2015-04-02 16:53:31 +02:00
try:
2015-04-02 17:45:24 +02:00
con.sendall(message.encode())
2015-04-03 12:09:00 +02:00
except Exception as e:
2015-04-02 16:53:31 +02:00
log.printL(str(e), Log.lvl.FAIL)
2015-03-31 11:53:24 +02:00
def userListActive(connection):
l = "USERLIST "
2015-04-03 12:09:00 +02:00
for con, value in usersConnected.items():
if value[1] is not None and value[2]:
2015-03-31 11:53:24 +02:00
l += value[1] + " "
2015-04-02 17:45:24 +02:00
connection.sendall(l[:-1].encode())
2015-03-31 11:53:24 +02:00
def userListAway(connection):
l = "USERAWAY "
2015-04-03 12:09:00 +02:00
for con, value in usersConnected.items():
if value[1] is not None and not value[2]:
2015-03-31 11:53:24 +02:00
l += value[1] + " "
2015-04-02 17:45:24 +02:00
connection.sendall(l[:-1].encode())
2015-03-30 20:54:29 +02:00
2015-03-31 11:53:24 +02:00
def changeName(connection, pseudo):
2015-04-03 12:09:00 +02:00
if not re.match("^\w{3,15}$", pseudo):
2015-04-02 17:45:24 +02:00
connection.sendall("ERR_INVALID_NICKNAME".encode())
2015-04-08 19:16:40 +02:00
elif getConnectionByPseudo(pseudo) is not None:
connection.sendall("ERR_NICKNAME_ALREADY_USED")
2015-04-02 16:53:31 +02:00
else:
2015-04-03 12:09:00 +02:00
broadcastMsg(connection, "NAME_CHANGED {} {}".format(usersConnected[connection][1], pseudo))
2015-04-02 17:45:24 +02:00
connection.sendall("SUCC_VALID_NICKNAME".encode())
2015-04-02 16:53:31 +02:00
usersConnected[connection][1] = pseudo
2015-03-30 20:54:29 +02:00
2015-04-02 13:47:02 +02:00
def newName(connection, pseudo):
2015-04-03 12:09:00 +02:00
if not re.match("^\w{3,15}$", pseudo):
2015-04-02 17:45:24 +02:00
connection.sendall("ERR_INVALID_NICKNAME".encode())
2015-04-08 19:16:40 +02:00
elif getConnectionByPseudo(pseudo) is not None:
connection.sendall("ERR_NICKNAME_ALREADY_USED".encode())
2015-04-02 17:45:24 +02:00
else:
broadcastMsg(connection, "HAS_JOIN {} ".format(pseudo))
connection.sendall("SUCC_CHANNEL_JOINED".encode())
usersConnected[connection][1] = pseudo
2015-04-03 12:09:00 +02:00
userListActive(connection)
userListAway(connection)
2015-04-02 13:47:02 +02:00
2015-04-03 12:09:00 +02:00
def askPrivateMsg(connection, pseudo):
2015-03-31 11:53:24 +02:00
c = getConnectionByPseudo(pseudo)
2015-04-03 12:09:00 +02:00
if c is None:
2015-04-02 17:45:24 +02:00
connection.sendall("ERR_USER_NOT_FOUND".encode())
2015-03-31 11:53:24 +02:00
else:
2015-04-03 12:09:00 +02:00
pm = (connection, c)
if pm in askPM:
2015-04-02 17:45:24 +02:00
connection.sendall("ALREADY_ASKED".encode())
2015-03-31 11:53:24 +02:00
else:
askPM.append(pm)
2015-04-06 14:55:45 +02:00
log.printL("askPm {}".format(askPM), Log.lvl.DEBUG)
2015-04-04 17:13:11 +02:00
c.sendall("ASKING_FOR_PM {}".format(usersConnected[connection][1]).encode())
2015-04-02 17:45:24 +02:00
connection.sendall("SUCC_INVITED".encode())
2015-03-31 11:53:24 +02:00
def acceptPrivateMsg(connection, pseudo):
2015-04-06 14:55:45 +02:00
log.printL("askPm {}".format(askPM), Log.lvl.DEBUG)
2015-03-31 11:53:24 +02:00
c = getConnectionByPseudo(pseudo)
2015-04-03 12:09:00 +02:00
if c is None:
2015-04-02 17:45:24 +02:00
connection.sendall("ERR_USER_NOT_FOUND".encode())
2015-03-31 11:53:24 +02:00
else:
2015-04-06 14:55:45 +02:00
pm = (c, connection)
2015-04-03 12:09:00 +02:00
if pm not in askPM:
2015-04-02 17:45:24 +02:00
connection.sendall("ERR_USER_HAS_NOT_ASK".encode())
2015-03-31 11:53:24 +02:00
else:
askPM.remove(pm)
validatePM.append(pm)
2015-04-02 17:45:24 +02:00
connection.sendall("SUCC_PRIVATE_DISCUSSION_ACCEPTED".encode())
2015-04-08 19:16:40 +02:00
c.sendall("SUCC_PRIVATE_DISCUSSION_OK {}".format(usersConnected[connection][1]).encode())
2015-03-31 11:53:24 +02:00
def rejectPrivateMsg(connection, pseudo):
c = getConnectionByPseudo(pseudo)
2015-04-03 12:09:00 +02:00
if c is None:
2015-04-02 17:45:24 +02:00
connection.sendall("ERR_USER_NOT_FOUND".encode())
2015-03-31 11:53:24 +02:00
else:
2015-04-06 14:55:45 +02:00
pm = (c, connection)
2015-04-08 10:08:59 +02:00
pmr = (connection, c)
2015-04-03 12:09:00 +02:00
if pm not in askPM:
2015-04-08 10:08:59 +02:00
if pm in validatePM :
validatePM.remove(pm)
connection.sendall("SUCC_PRIVATE_DISCUSSION_REFUSED".encode())
2015-04-08 19:16:40 +02:00
c.sendall("SUCC_PRIVATE_DISCUSSION_REJECTED {}".format(usersConnected[connection][1]).encode())
elif pmr in validatePM :
validatePM.remove(pmr)
connection.sendall("SUCC_PRIVATE_DISCUSSION_REFUSED".encode())
c.sendall("SUCC_PRIVATE_DISCUSSION_REJECTED {}".format(usersConnected[connection][1]).encode())
2015-04-08 10:08:59 +02:00
else :
2015-04-08 19:16:40 +02:00
connection.sendall("ERR_USER_HAS_NOT_ASK".encode())
2015-03-31 11:53:24 +02:00
else:
askPM.remove(pm)
2015-04-02 17:45:24 +02:00
connection.sendall("SUCC_PRIVATE_DISCUSSION_REFUSED".encode())
2015-04-08 19:16:40 +02:00
c.sendall("SUCC_PRIVATE_DISCUSSION_REJECTED {}".format(usersConnected[connection][1]).encode())
2015-03-31 11:53:24 +02:00
def privateMsg(connection, pseudo, msg):
c = getConnectionByPseudo(pseudo)
2015-04-03 12:09:00 +02:00
if c is None:
2015-04-02 17:45:24 +02:00
connection.sendall("ERR_DEST_NOT_FOUND".encode())
2015-03-31 11:53:24 +02:00
else:
2015-04-03 12:09:00 +02:00
pm = (connection, c)
2015-04-07 13:47:19 +02:00
pmr = (c,connection)
2015-04-07 13:48:21 +02:00
if pm not in validatePM and pmr not in validatePM:
2015-04-02 17:45:24 +02:00
connection.sendall("ERR_NOT_ACCEPTED".encode())
2015-03-31 11:53:24 +02:00
else:
2015-04-03 12:09:00 +02:00
c.sendall("NEW_PM {} {}".format(pseudo, msg).encode())
2015-04-02 17:45:24 +02:00
connection.sendall("SUCC_PM_SENDED".encode())
2015-03-31 11:53:24 +02:00
2015-04-06 14:55:45 +02:00
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)
2015-04-08 19:16:40 +02:00
log.printL("askFT {}".format(askFT), Log.lvl.DEBUG)
c.sendall("HAS_ASKED_FILE {} {}".format(usersConnected[connection][1], file).encode())
2015-04-06 14:55:45 +02:00
connection.sendall("SUCC_ASKED_FILE".encode())
2015-04-08 19:16:40 +02:00
def acceptFile(connection, pseudo, file, port):
log.printL("askFT {}".format(askFT), Log.lvl.DEBUG)
2015-04-06 14:55:45 +02:00
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)
2015-04-08 19:16:40 +02:00
connection.sendall("SUCC_FILE_ACCEPTED {}".format(usersConnected[c][0][0]).encode())
c.sendall("CAN_SEND_FILE {} {} {} {}".format(file,pseudo,usersConnected[connection][0][0], port).encode())
2015-04-06 14:55:45 +02:00
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())
2015-04-01 08:37:05 +02:00
def enableUser(connection):
2015-04-03 12:09:00 +02:00
if usersConnected[connection][2] == False:
2015-04-01 08:37:05 +02:00
usersConnected[connection][2] = True
2015-04-02 17:45:24 +02:00
connection.sendall("SUCC_ENABLED".encode())
2015-04-04 17:13:11 +02:00
broadcastMsg(connection,"IS_NOW_ENABLE {}".format(usersConnected[connection][1]))
2015-04-01 08:37:05 +02:00
else:
2015-04-02 17:45:24 +02:00
connection.sendall("ERR_NOT_DISABLED".encode())
2015-04-01 08:37:05 +02:00
def disableUser(connection):
2015-04-03 12:09:00 +02:00
if usersConnected[connection][2] == True:
2015-04-01 08:37:05 +02:00
usersConnected[connection][2] = False
2015-04-02 17:45:24 +02:00
connection.sendall("SUCC_DISABLED".encode())
2015-04-04 17:13:11 +02:00
broadcastMsg(connection,"IS_NOW_DISABLE {}".format(usersConnected[connection][1]))
2015-04-01 08:37:05 +02:00
else:
2015-04-02 17:45:24 +02:00
connection.sendall("ERR_NOT_ENABLED".encode())
2015-04-01 08:37:05 +02:00
2015-04-03 12:09:00 +02:00
def quit(connection):
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)
2015-03-30 20:54:29 +02:00
connection.close()
log.printL("Disconnection from IP -> {}".format(usersConnected[connection][0]), Log.lvl.INFO)
2015-04-03 11:13:58 +02:00
pseudo = usersConnected[connection][1]
2015-03-31 13:25:34 +02:00
usersConnected.pop(connection)
2015-04-03 12:09:00 +02:00
broadcastMsg(connection, "HAS_LEFT {}".format(pseudo))
2015-03-30 20:54:29 +02:00
2015-03-31 11:53:24 +02:00
def getConnectionByPseudo(pseudo):
2015-04-03 12:09:00 +02:00
for con, value in usersConnected.items():
if value[1] == pseudo:
2015-03-31 11:53:24 +02:00
return con
return None
2015-03-30 20:54:29 +02:00
def main():
2015-04-03 12:09:00 +02:00
# Global vars
2015-03-31 11:53:24 +02:00
global usersConnected, log, sock
global askPM, validatePM
2015-04-06 14:55:45 +02:00
global askFT
2015-04-03 12:09:00 +02:00
usersConnected = {}
2015-03-31 11:53:24 +02:00
askPM = []
validatePM = []
2015-04-06 14:55:45 +02:00
askFT = []
2015-04-02 16:53:31 +02:00
config = configparser.ConfigParser()
2015-04-03 12:09:00 +02:00
if not os.path.isfile("dncserver.conf"):
2015-04-02 16:53:31 +02:00
config['NETWORK'] = {'port': '2222'}
config['LOG'] = {'logDirectory': 'log'}
with open('dncserver.conf', 'w') as configfile:
2015-04-03 12:09:00 +02:00
config.write(configfile)
2015-04-02 16:53:31 +02:00
config.read("dncserver.conf")
log = Log.Log(config["LOG"]["logdirectory"])
2015-04-02 17:00:20 +02:00
log.printL("Configuration Log", Log.lvl.INFO)
log.printL("Server start", Log.lvl.INFO)
2015-04-02 16:53:31 +02:00
2015-03-30 20:54:29 +02:00
#Init socket serv
sock = socket.socket()
2015-04-02 16:53:31 +02:00
sock.bind(("", int(config["NETWORK"]["port"])))
2015-03-30 20:54:29 +02:00
sock.listen(5)
2015-04-02 16:53:31 +02:00
log.printL("Server Listen on port {}".format(config["NETWORK"]["port"]), Log.lvl.INFO)
2015-03-30 20:54:29 +02:00
2015-04-03 12:09:00 +02:00
try:
while True:
2015-03-30 20:54:29 +02:00
#Connection client
connection, client_address = sock.accept()
2015-04-03 12:09:00 +02:00
usersConnected[connection] = [client_address, None, True] # ip pseudo status
threading.Thread(target=handleConnection, args=(connection, client_address)).start()
except KeyboardInterrupt:
2015-03-30 20:54:29 +02:00
# Disable to received more requests on socket
for con, value in usersConnected.items():
con.shutdown(socket.SHUT_RD)
2015-04-03 12:09:00 +02:00
finally:
2015-03-30 20:54:29 +02:00
#Wait for threads finish
log.printL("Wait for threads ending", Log.lvl.INFO)
for t in threading.enumerate():
2015-04-03 12:09:00 +02:00
if t != threading.main_thread():
2015-03-30 20:54:29 +02:00
t.join()
sock.close()
log.printL("Server shutdown", Log.lvl.INFO)
sys.exit(0)