Ejemplo n.º 1
0
Archivo: IRC.py Proyecto: dom96/nyx
    def ping_pong(self):

        ERROR = "" #ERROR :Closing link: (dom96@host) [Quit: MSG]

        msg = ""
        while self.connected:
            try:
                data = self.cSocket.recv(1024)

                msg += data
                if msg !="":
                    #If the message ends with \n (Carriage Return) then that means that the command is a full command
                    #If not then that means that the last line of msg is a uncompletely received command,
                    #Which means i have to wait, for the rest of the command before parsing.
                    if msg.endswith("\r\n"):
                        pDebug("Raw received data from server:\n \033[1;32m" + msg + " \033[1;m")
                        #Loop through each message, and parse it
                        for i in msg.split("\r\n"):
                        
                            #If a PING is received, reply with a PONG
                            if i.startswith("PING"):
                                pingMsg = i.replace("\n","").replace("\r","")
                                pDebug("Received PING( \033[1;32m" + pingMsg + "\033[1;m )")
                                #Reply with a PONG command, to keep the connection alive.
                                self.cSocket.send("PONG :" + pingMsg.split(":")[1] + "\r\n") 
                                pDebug("Replied to Ping with: \033[1;34m" + "PONG :" + pingMsg.split(":")[1] + "\\r\\n\033[1;m")
                                msg=""
                            #If a ERROR message is received
                            elif i.startswith("ERROR"):
                                pDebug(ERROR)
                                if ERROR != "RECONNECT":
                                    ERROR = i
                                else:
                                    ERROR = ""
                                import IRC
                                for event in IRC.eventFunctions:
                                    if event.eventName == "onServerDisconnect" and event.cServer == self:
                                        gobject.idle_add(event.aFunc, self, otherStuff, i)
                            #If anything else is received
                            else:
                                #Makes responses like 'NOTICE AUTH'(Without the :Server in front) work
                                if (i.startswith(":") == False):
                                    i = ":" + self.cAddress.cAddress + " " + i
                            
                                #!--MOTD STUFF--!#
                                PongStuff.motdStuff(self,i,otherStuff)
                                #!--PRIVMSG STUFF START--!#
                                PongStuff.privmsgResp(self,i,otherStuff)
                                #!--PART MSG--!#
                                PongStuff.partResp(self,i,otherStuff)
                                #!--JOIN MSG--!#
                                PongStuff.joinResp(self,i,otherStuff)
                                #!--QUIT MSG--!#
                                PongStuff.quitResp(self,i,otherStuff)
                                #!--USERS STUFF--!#
                                PongStuff.userStuff(self,i)
                                #!--NOTICE MSG--!#
                                PongStuff.noticeResp(self,i,otherStuff)
                                #!--KICK MSG--!#
                                PongStuff.kickResp(self,i,otherStuff)
                                #!--NICK MSG--!#
                                PongStuff.nickResp(self,i,otherStuff)
                                #!--SERVER MSG--!#
                                PongStuff.servResp(self,i,otherStuff)
                                #!--MODE MSG--!#
                                PongStuff.modeResp(self,i,otherStuff)
                                #!--PING MSG--!#
                                PongStuff.pongResp(self,i)
                                #!--TOPIC MSG--!#
                                PongStuff.topicStuff(self,i,otherStuff)
                                #!--Channel Mode Change MSG--#!
                                PongStuff.channelModeStuff(self,i,otherStuff)
                                #!--KILL MSG--#! - #If the message is
                                #KILL then it makes ERROR = "RECONNECT"
                                ERROR = PongStuff.killResp(self, i, ERROR, otherStuff)
                                #Reset the msg after parsing
                                msg=""
                else:
                    if data=="":
                        pDebug("\033[1;31mServer closed connection\033[1;m")
                        self.connected=False
                        import IRC
                        for event in IRC.eventFunctions:
                            if event.eventName == "onServerDisconnect" and event.cServer == self:
                                gobject.idle_add(event.aFunc, self, otherStuff)
                        self.cSocket.close()
                        if ERROR == "":
                            self.cycle_address()
                        else:
                            ERROR = ""


            except Exception as err:
                pDebug("\033[1;40m\033[1;33m" + str(err) + "\033[1;m\033[1;m")
                traceback.print_exc()