Exemplo n.º 1
0
    def send(self, txt):
        """ send text to irc server. """

        if not txt:
            return

        if self.stopped:
            return

        try:
            self.outputlock.acquire()
            now = time.time()
            timetosleep = 4 - (now - self.lastoutput)
            if timetosleep > 0 and not self.nolimiter:
                rlog(0, self.name, 'flood protect')
                time.sleep(timetosleep)
            txt = toenc(strippedtxt(txt))
            txt = txt.rstrip()
            self._raw(txt)
            try:
                self.outputlock.release()
            except:
                pass
            self.lastoutput = time.time()
        except Exception, ex:
            try:
                self.outputlock.release()
            except:
                pass
            if not self.blocking and 'broken pipe' in str(ex).lower():
                rlog(11, self.name, 'broken pipe error .. ignoring')
            else:
                rlog(11, self.name, 'send error: %s' % str(ex))
                self.reconnect()
                return
Exemplo n.º 2
0
    def send(self, txt):

        """ send text to irc server. """

        if not txt:
            return

        if self.stopped:
            return

        try:
            self.outputlock.acquire()
            now = time.time()
            timetosleep = 4 - (now - self.lastoutput)
            if timetosleep > 0 and not self.nolimiter:
                rlog(0, self.name, 'flood protect')
                time.sleep(timetosleep)
            txt = toenc(strippedtxt(txt))
            txt = txt.rstrip()
            self._raw(txt)
            try:
                self.outputlock.release()
            except:
                pass
            self.lastoutput = time.time()
        except Exception, ex:
            try:
                self.outputlock.release()
            except:
                pass
            if not self.blocking and 'broken pipe' in str(ex).lower():
                rlog(11, self.name, 'broken pipe error .. ignoring')
            else:
                rlog(11, self.name, 'send error: %s' % str(ex))
                self.reconnect()
                return
Exemplo n.º 3
0
    def _readloop(self):
        """ loop on the socketfile. """

        self.stopreadloop = 0
        self.stopped = 0
        doreconnect = 0
        timeout = 1
        rlog(5, self.name, 'starting readloop')
        prevtxt = ""

        while not self.stopped and not self.stopreadloop:

            try:
                time.sleep(0.01)
                if self.ssl:
                    intxt = inputmorhps.do(self.sock.read()).split('\n')
                else:
                    intxt = inputmorphs.do(self.fsock.readline()).split('\n')
                # if intxt == "" the other side has disconnected
                if self.stopreadloop or self.stopped:
                    doreconnect = 0
                    break
                if not intxt or not intxt[0]:
                    doreconnect = 1
                    break
                if prevtxt:
                    intxt[0] = prevtxt + intxt[0]
                    prevtxt = ""
                if intxt[-1] != '':
                    prevtxt = intxt[-1]
                    intxt = intxt[:-1]
                for r in intxt:
                    r = r.rstrip()
                    rr = fromenc(r, self.encoding)
                    if not rr:
                        continue
                    res = strippedtxt(rr)
                    res = rr
                    rlog(2, self.name, res)
                    # parse txt read into an ircevent
                    try:
                        ievent = Ircevent().parse(self, res)
                    except Exception, ex:
                        handle_exception()
                        continue
                    # call handle_ievent
                    if ievent:
                        self.handle_ievent(ievent)
                    timeout = 1

            except UnicodeError:
                handle_exception()
                continue

            except socket.timeout:
                # timeout occured .. first time send ping .. reconnect if
                # second timeout follows
                if self.stopped:
                    break
                timeout += 1
                if timeout > 2:
                    doreconnect = 1
                    rlog(10, self.name, 'no pong received')
                    break
                rlog(1, self.name, "socket timeout")
                pingsend = self.ping()
                if not pingsend:
                    doreconnect = 1
                    break
                continue

            except socket.sslerror, ex:
                # timeout occured .. first time send ping .. reconnect if
                # second timeout follows
                if self.stopped or self.stopreadloop:
                    break
                if not 'timed out' in str(ex):
                    handle_exception()
                    doreconnect = 1
                    break
                timeout += 1
                if timeout > 2:
                    doreconnect = 1
                    rlog(10, self.name, 'no pong received')
                    break
                rlog(1, self.name, "socket timeout")
                pingsend = self.ping()
                if not pingsend:
                    doreconnect = 1
                    break
                continue

            except IOError, ex:
                if 'temporarily' in str(ex):
                    continue
Exemplo n.º 4
0
     except:
         errno = 0
         errstr = str(ex)
     if errno == 35 or errno == 11:
         continue
     else:
         raise
 except Exception, ex:
     # other exception occured .. close connection
     handle_exception()
     rlog(10, self.name, 'closing dcc with ' + nick)
     partyline.del_party(nick)
     return
 try:
     # see if user provided channel
     res = strippedtxt(res.strip())
     chan = checkchan(self, res)
     if chan != None:
         (channel, res) = chan
     else:
         channel = nick
     # create ircevent
     ievent = Ircevent()
     ievent.nick = nick
     ievent.userhost = userhost
     ievent.channel = channel
     ievent.origtxt = res
     ievent.txt = res
     ievent.cmnd = 'DCC'
     ievent.bot = self
     ievent.sock = sock
Exemplo n.º 5
0
     except:
         errno = 0
         errstr = str(ex)
     if errno == 35 or errno == 11:
         continue
     else:
         raise
 except Exception, ex:
     # other exception occured .. close connection
     handle_exception()
     rlog(10, self.name, 'closing dcc with ' + nick)
     partyline.del_party(nick)
     return
 try:
     # see if user provided channel
     res = strippedtxt(res.strip())
     chan = checkchan(self, res)
     if chan != None:
         (channel, res) = chan
     else:
         channel = nick
     # create ircevent
     ievent = Ircevent()
     ievent.nick = nick
     ievent.userhost = userhost
     ievent.channel = channel
     ievent.origtxt = res
     ievent.txt = res
     ievent.cmnd = 'DCC'
     ievent.bot = self
     ievent.sock = sock
Exemplo n.º 6
0
    def _readloop(self):

        """ loop on the socketfile. """

        self.stopreadloop = 0
        self.stopped = 0
        doreconnect = 0
        timeout = 1
        rlog(5, self.name, 'starting readloop')
        prevtxt = ""

        while not self.stopped and not self.stopreadloop:

            try:
                time.sleep(0.01)
                if self.ssl:
                    intxt = inputmorhps.do(self.sock.read()).split('\n')
                else:
                    intxt = inputmorphs.do(self.fsock.readline()).split('\n')
                # if intxt == "" the other side has disconnected
                if self.stopreadloop or self.stopped:
                    doreconnect = 0
                    break
                if not intxt or not intxt[0]:
                    doreconnect = 1
                    break
                if prevtxt:
                    intxt[0] = prevtxt + intxt[0]
                    prevtxt = ""
                if intxt[-1] != '':
                    prevtxt = intxt[-1]
                    intxt = intxt[:-1]
                for r in intxt:
                    r = r.rstrip()
                    rr = fromenc(r, self.encoding)
                    if not rr:
                        continue
                    res = strippedtxt(rr)
                    res = rr
                    rlog(2, self.name, res)
                    # parse txt read into an ircevent
                    try:
                        ievent = Ircevent().parse(self, res)
                    except Exception, ex:
                        handle_exception()
                        continue
                    # call handle_ievent 
                    if ievent:
                        self.handle_ievent(ievent)
                    timeout = 1

            except UnicodeError:
                handle_exception()
                continue

            except socket.timeout:
                # timeout occured .. first time send ping .. reconnect if
                # second timeout follows
                if self.stopped:
                    break
                timeout += 1
                if timeout > 2:
                    doreconnect = 1
                    rlog(10, self.name, 'no pong received')
                    break
                rlog(1, self.name, "socket timeout")
                pingsend = self.ping()
                if not pingsend:
                    doreconnect = 1
                    break
                continue

            except socket.sslerror, ex:
                # timeout occured .. first time send ping .. reconnect if
                # second timeout follows
                if self.stopped or self.stopreadloop:
                    break
                if not 'timed out' in str(ex):
                    handle_exception()
                    doreconnect = 1
                    break
                timeout += 1
                if timeout > 2:
                    doreconnect = 1
                    rlog(10, self.name, 'no pong received')
                    break
                rlog(1, self.name, "socket timeout")
                pingsend = self.ping()
                if not pingsend:
                    doreconnect = 1
                    break
                continue

            except IOError, ex:
                if 'temporarily' in str(ex):
                    continue
Exemplo n.º 7
0
        rlog(5, 'tcp', 'shutting down main loop')

    def _handle(self, input, addr):
        if cfg['tcpseed']:
            data = ""
            for i in range(len(input)/16):
                try:
                    data += crypt.decrypt(input[i*16:i*16+16])
                except Exception, ex:
                    rlog(10, 'tcp', "can't decrypt: %s" % str(ex))
                    data = input
                    break
        else:
            data = input
        if cfg['tcpstrip']:
            data = strippedtxt(data)
        # check if tcp is enabled and source ip is in tcpallow list
        if cfg['tcp'] and (addr[0] in cfg['tcpallow'] or \
_inmask(addr[0])):
            # get printto and passwd data
            header = re.search('(\S+) (\S+) (.*)', data)
            if header:
                # check password
                if header.group(1) == cfg['tcppassword']:
                    printto = header.group(2)    # is the nick/channel
                    # check if printto is in allowednicks
                    if not printto in cfg['tcpallowednicks']:
                        rlog(10, 'tcp', "tcp denied %s" % printto )
                        return
                    rlog(0, 'tcp', str(addr[0]) +  " tcp allowed")
                    text = header.group(3)    # is the text