This commit is contained in:
sidya82 2015-04-02 17:45:24 +02:00
parent 39009a1531
commit f031b6af67

View File

@ -1,16 +1,12 @@
import argparse import os, socket, threading, sys, configparser, re
import os
import socket
import threading
import sys
import configparser
import 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:
@ -28,6 +24,7 @@ def handleRequest(connection, data):
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())
broadcastMsg( "NEW_MSG {} {} ".format(usersConnected[connection][1], data)) broadcastMsg( "NEW_MSG {} {} ".format(usersConnected[connection][1], data))
return return
else : else :
@ -55,7 +52,7 @@ def handleRequest(connection, data):
if arrayData[0] == "/quit" : if arrayData[0] == "/quit" :
quit(connection) quit(connection)
return return
connection.send("ERR_COMMAND_NOT_FOUND".encode()) connection.sendall("ERR_COMMAND_NOT_FOUND".encode())
else: else:
if arrayData[0] == "/newname" : if arrayData[0] == "/newname" :
newName(connection, arrayData[1]) newName(connection, arrayData[1])
@ -63,122 +60,126 @@ def handleRequest(connection, data):
if arrayData[0] == "/quit" : if arrayData[0] == "/quit" :
quit(connection) quit(connection)
return return
connection.send("ERR_NO_NICKNAME".encode()) connection.sendall("ERR_NO_NICKNAME".encode())
"""except Exception as e : """except Exception as e :
log.printL(str(e), Log.lvl.FAIL)""" log.printL(str(e), Log.lvl.FAIL)"""
def broadcastMsg(message): def broadcastMsg(connection,message):
for con, value in usersConnected.items() : for con, value in usersConnected.items() :
if usersConnected[con][1] is not None : if usersConnected[con][1] is not None or con != connection:
try: try:
con.send(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 : for con, value in usersConnected.items() :
if value[2] == True : if value[2] == True :
l += value[1] + " " l += value[1] + " "
connection.send(l[:-1].encode()) connection.sendall(l[:-1].encode())
def userListAway(connection): def userListAway(connection):
l = "USERAWAY " l = "USERAWAY "
for con,value in usersConnected : for con,value in usersConnected.items() :
if value[2] == False : if value[2] == False :
l += value[1] + " " l += value[1] + " "
connection.send(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.send("ERR_INVALID_NICKNAME".encode()) connection.sendall("ERR_INVALID_NICKNAME".encode())
else: else:
broadcastMsg("NAME_CHANGED {} {}".format(usersConnected[connection][1], pseudo)) broadcastMsg(connection,"NAME_CHANGED {} {}".format(usersConnected[connection][1], pseudo))
connection.sendall("SUCC_VALID_NICKNAME".encode())
usersConnected[connection][1] = pseudo usersConnected[connection][1] = pseudo
def newName(connection, pseudo): def newName(connection, pseudo):
broadcastMsg("HAS_JOIN {} ".format(pseudo)) if not re.match("^\w{3,15}$",pseudo) :
connection.send("SUCC_VALID_NICKNAME".encode()) connection.sendall("ERR_INVALID_NICKNAME".encode())
else:
broadcastMsg(connection, "HAS_JOIN {} ".format(pseudo))
connection.sendall("SUCC_CHANNEL_JOINED".encode())
usersConnected[connection][1] = pseudo usersConnected[connection][1] = pseudo
def askPrivateMsg(connection,pseudo): def askPrivateMsg(connection,pseudo):
c = getConnectionByPseudo(pseudo) c = getConnectionByPseudo(pseudo)
if c is None : if c is None :
connection.send("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.send("ALREADY_ASKED".encode()) connection.sendall("ALREADY_ASKED".encode())
else: else:
askPM.append(pm) askPM.append(pm)
c.send("ASKING_FOR_PM {}".format(pseudo).encode()) c.sendall("ASKING_FOR_PM {}".format(pseudo).encode())
connection.send("SUCC_INVITED".encode()) connection.sendall("SUCC_INVITED".encode())
def acceptPrivateMsg(connection, pseudo): def acceptPrivateMsg(connection, pseudo):
c = getConnectionByPseudo(pseudo) c = getConnectionByPseudo(pseudo)
if c is None : if c is None :
connection.send("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.send("ERR_USER_HAS_NOT_ASK".encode()) connection.sendall("ERR_USER_HAS_NOT_ASK".encode())
else: else:
askPM.remove(pm) askPM.remove(pm)
validatePM.append(pm) validatePM.append(pm)
connection.send("SUCC_PRIVATE_DISCUSSION_ACCEPTED".encode()) connection.sendall("SUCC_PRIVATE_DISCUSSION_ACCEPTED".encode())
def rejectPrivateMsg(connection, pseudo): def rejectPrivateMsg(connection, pseudo):
c = getConnectionByPseudo(pseudo) c = getConnectionByPseudo(pseudo)
if c is None : if c is None :
connection.send("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.send("ERR_USER_HAS_NOT_ASK".encode()) connection.sendall("ERR_USER_HAS_NOT_ASK".encode())
else: else:
askPM.remove(pm) askPM.remove(pm)
connection.send("SUCC_PRIVATE_DISCUSSION_REFUSED".encode()) connection.sendall("SUCC_PRIVATE_DISCUSSION_REFUSED".encode())
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.send("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.send("ERR_NOT_ACCEPTED".encode()) connection.sendall("ERR_NOT_ACCEPTED".encode())
else: else:
c.send("NEW_PM {} {}".format(pseudo,msg).encode()) c.sendall("NEW_PM {} {}".format(pseudo,msg).encode())
connection.send("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.send("SUCC_ENABLED".encode()) connection.sendall("SUCC_ENABLED".encode())
else: else:
connection.send("ERR_NOT_DISABLED".encode()) connection.sendall("ERR_NOT_DISABLED".encode())
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.send("SUCC_DISABLED".encode()) connection.sendall("SUCC_DISABLED".encode())
else: else:
connection.send("ERR_NOT_ENABLED".encode()) connection.sendall("ERR_NOT_ENABLED".encode())
def quit(connection) : def quit(connection) :
connection.send("SUCCESSFUL_LOGOUT".encode()) connection.sendall("SUCCESSFUL_LOGOUT".encode())
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)
usersConnected.pop(connection) usersConnected.pop(connection)