def loopfunc(): while True: #Get message from server here message = "" while message == "": message = sharedMethods.getSocketResponse(ssock, timeout=-1, buffersize=1) #Process message processMsgRecvd(message, ssock, nickname)
def server_thread(self, clientSock, clientAddr): sent_ping, is_notice = False, False self.log("Received connection from:", str(clientAddr), level='info') try: connection = IRCConnection(sock=clientSock, ID=self.next_id) registration_messages = 0 #Used to prevent bad clients from spamming registration messages. while registration_messages <= 4: #This allows for one mistake while registering try: text = sharedMethods.getSocketResponse(clientSock, timeout=self.listen_timeout, buffersize=1) #ACII stuff except socket.timeout: #This handles the, "send a ping after no data goes through a connection" part if sent_ping: break #Then this connection is no longer live connection.send_message(IRCMessage("PING :" + self.host, server=self)) sent_ping = True continue sent_ping = False message = IRCMessage(text, server=self) #Parse the message from the client is_notice = (message.command == 'NOTICE') #If it is notice, don't send any responses if not connection.isComplete(): registration_messages += 1 #Then we are still in the registration process if message.source == None: message.source = connection.name #The source of a message is, by default, the name of the connection from whence it came try: if message.command not in IRCServer.message_handlers: #If the command does not exist, inform the client raise IRCException(message.command, message=421) IRCServer.message_handlers[message.command](self, connection, message) #Process the command except IRCException as e: self.log(e, level='error') if not is_notice and (type(e.message) == int or e.should_forward): #Only forward errors to the client if the command was not notice and the error code is numeric) self.send_error(e, connection) if e.message in disconnection_errors: break if message.command == 'QUIT': break #If the user sent quit, then quit if not connection.isComplete(): connection.send_message(IRCMessage("ERROR")) except IRCException as e: self.log(e, level='error') if not is_notice and (type(e.message) == int or e.should_forward): #Then this is an error code and should be sent to the client self.send_error(e, connection) except IOError as e: self.log(e, level='error') finally: self.deregister_user(connection) #The finally ensures that the client is /always/ properly disconnected, regardless of how the connection was closed return True