Ejemplo n.º 1
0
class Client(LineReceiver):
    def parsemsg(self, s):
        if not s:
            raise BadMessage

        sender = ""

        if s.find(":") == 0:
            sender, s = s.split(" ", 1)
            sender = sender[1:]

        if s.find(" :") != -1:
            s, trailing = s.split(" :", 1)
            args = s.split()
            args.append(trailing)
        else:
            args = s.split()

        command = args.pop(0).upper()

        return sender, command, args

    def __init__(self, state):
        self.state = state
        self.isadmin = False
        self.api = API(self)

    def lineReceived(self, line):
        try:
            sender, command, args = self.parsemsg(line)
        except BadMessage:
            return

        if hasattr(self.api, "on_%s" % command.lower()):
            getattr(self.api, "on_%s" % command.lower())(sender, args)

        try:
            self.factory.firehook(command, sender, args)
        except Exception:
            traceback.print_exc(5)

    def sendLine(self, line):
        # Assure that the string isn't unicode.
        return LineReceiver.sendLine(self, str(line))

    def connectionMade(self):
        self.factory.resetDelay()
        self.api.protocol(1)
        self.api.login(self.state["config"]["general"]["nick"], self.state["config"]["general"]["password"])
        self.factory.firehook("CONNECTED")

    def connectionLost(self, reason=connectionDone):
        self.factory.firehook("DISCONNECTED", reason)
Ejemplo n.º 2
0
 def __init__(self, state):
     self.state = state
     self.isadmin = False
     self.api = API(self)
Ejemplo n.º 3
0
 def __init__(self, state):
     self.api = API(self, state)
     self.state = state
Ejemplo n.º 4
0
class ConnectingClient(basic.LineReceiver):
    def __init__(self, state):
        self.api = API(self, state)
        self.state = state

    def parsemsg(self, s):
        if not s:
            raise BadMessage

        if s.find(' :') != -1:
            s, trailing = s.split(' :', 1)
            args = s.split()
            args.append(trailing)
        else:
            args = s.split()

        command = args.pop(0).upper()

        return command, args

    _ping_deferred = None

    def _idle_ping(self):
        self._ping_deferred = None
        self._reconnect_deferred = reactor.callLater(
            self.factory.pong_timeout, self._timeout_reconnect)

        self.api.ping()

    def _timeout_reconnect(self):
        self.factory.firehook("TIMEDOUT", self)
        self.transport.loseConnection()

    def on_pong(self, args):
        if self._reconnect_deferred:
            self._reconnect_deferred.cancel()
            self._reconnect_deferred = None
            
            self._ping_deferred = reactor.callLater(
                self.factory.ping_interval, self._idle_ping)
    
    def connectionMade(self):
        self.factory.firehook("CONNECTED", self)
        #self._ping_deferred = reactor.callLater(self.factory.ping_interval, self._idle_ping)

    def connectionLost(self, reason):
        self.factory.firehook("DISCONNECTED", self, reason)

        if self.api.welcomed:
            self.api.leave()

            self.state["users"][self.api.user.nick].delConnection(self)
            
    def lineReceived(self, line):
        if self._ping_deferred is not None:
            self._ping_deferred.reset(self.factory.ping_interval)
            
        try:
            command, args = self.parsemsg(line)
        except BadMessage:
            self.api.error(constants.BADMSG) # Bad or empty message
            return
        try:
            if self.api.welcomed:
                self.parseMessage(command, args)
            else:
                self.api.auth(command, args)
        except Exception:
            traceback.print_exc(5)

    def parseMessage(self, command, args):
        handled = False

        self.factory.firehook("MESSAGE", self, command, args)

        for thing in [self, self.api]:
            if hasattr(thing, "on_%s"%command):
                try:
                    handled = True
                    getattr(thing, "on_%s"%command)(args)
                except Exception, e:
                    traceback.print_exc(5)
                    
        if not handled:
            self.api.error(constants.BADMSG)