Esempio n. 1
0
File: IRC.py Progetto: 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()
Esempio n. 2
0
def parseServer(data):
    mList = []
    splitData = string.split(data,"\n")

    for i in splitData:
        try:
            #TODO:REMOVE?? THIS IS KIND OF OBSOLETE, since the 'NOTICE AUTH' is handled by the NOTICE Resp
            if "NOTICE AUTH" in i:
                m = serverMsg()
                #First method of parsing, NOTICE AUTH way(command has two :)       
                
                msg=re.search("(:)[^:]*$",i).group(0)[1:]
                m.msg = msg #The message(the last : and the string after, and the [1:] to delete the :) *** Looking up your hostname...
        
                firstPart = i.replace(msg,"") #The server "next.SpotChat.org" and the code "NOTICE AUTH" ([1:] get's rid of the : )
                sFirstPart=string.split(firstPart) #Split the "server"(next.spotchat.org) and the "code"(NOTICE AUTH)
                m.server=sFirstPart[0]
                m.code = sFirstPart[1]

                mList.append(m)
            else:        
                m = serverMsg()
                #Second method of parsing, used if the command doesn't have a : when the message starts(only has : at the beggining) 
                #Well it's actually used when there is no NOTICE AUTH in the message                

                splitI = string.split(i) #Split the command with " "
                if len(splitI) > 0:
                    hSplitI=0 #The position in the splitI where the message starts(-1)
                    try:
                        m.server = splitI[0] #The server, next.spotchat.org
                        m.code = splitI[1] #The code, 001
                        m.nick = splitI[2] #The nick, usually your own nick, Nyx
                        try:
                            m.channel = splitI[3]
                            hSplitI=3
                        except:
                            hSplitI=2

                        if m.code in PongStuff.numericCode():
                            hSplitI=2
                    #If there is an exception in the above code, try this without getting the server info
                    except:
                        traceback.print_exc()
                        m.code = splitI[0]
                        m.nick = splitI[1]
                        hSplitI=1
                    #---------------------------------------------------Get the MSG
                    count=0
                    for i in splitI:
                        #Add all the parts of the message, to the m.msg
                        if count > hSplitI:
                            if m.msg != "":
                                m.msg = m.msg + " " + i
                            else:
                                if i[1:] == ":":
                                    m.msg = i[1:]
                                else:
                                    m.msg = i
                        count=count+1
                    #--------------------------------------------------Get the MSG END


                    #import re
                    #pDebug(data[1:])
                    #reMatch = re.search(":.+",data[1:])
                    #try:
                        #m.msg = reMatch.group(0)[1:]
                    #except:
                        #pDebug("\033[1;40m\033[1;33mNo match for the message\033[1;m\033[1;m")                
                    mList.append(m)
        except:
            #traceback.print_exc()
            pDebug("Error in parseServer. " + i)


    return mList