Ejemplo n.º 1
0
def blockingCallFromThread(reactor, f, *a, **kw):
    """
    Run a function in the reactor from a thread, and wait for the result
    synchronously, i.e. until the callback chain returned by the function
    get a result.

    @param reactor: The L{IReactorThreads} provider which will be used to
        schedule the function call.
    @param f: the callable to run in the reactor thread
    @type f: any callable.
    @param a: the arguments to pass to C{f}.
    @param kw: the keyword arguments to pass to C{f}.

    @return: the result of the callback chain.
    @raise: any error raised during the callback chain.
    """
    queue = Queue.Queue()
    def _callFromThread():
        result = defer.maybeDeferred(f, *a, **kw)
        result.addBoth(queue.put)
    reactor.callFromThread(_callFromThread)
    result = queue.get()
    if isinstance(result, failure.Failure):
        result.raiseException()
    return result
Ejemplo n.º 2
0
    def protocol_output(self, message, req=None):
        """This is how we send shit to AOL"""
        try:
            # so, utf16 doubles the size of the FLAP packets, which
            # really limits our max message size.  if none of the ordinals
            # are outside the 7bit ascii range, convert to ascii bytes
            if not [ch for ch in message if ord(ch) > 127]:
                message = message.encode('us-ascii')


            # ooookay, let's just strip out all html tags
            message = stripHTML(message)

            #print 'trying to output: %r' % message
            # escape stuff so it's not all like LOL HTML
            # print 'before esc: %r' % message
            # message = oscar.html(message)
            # print 'after esc: %r' % message
            # message = message.replace('"', "'")
            # print 'after repl: %r' % message
            # color output if requested
            #if req.colorize:
            #    style = random.choice(self.colorlib._rainbow_map.keys())
            #    message = self.colorlib.rainbow(message, style=style)

            # AIM reacts indignantly to overlong messages, so we need to
            # wrap.  try not to break up html tags injected by colorlib.
            if not hasattr(req, 'chat'):
                req.chat = None
            if not hasattr(req, 'aim'):
                req.aim = self.oscar_connection

            if req.chat:
                width = 2048
                func = req.chat.sendMessage
            else:
                width = 2545  # longer than chatrooms, who knows...
                func = req.aim.sendMessage

            # unicode stuff takes two bytes due to shitty utf-16
            if isinstance(message, unicode):
                width = int(width / 2) - 1

            # make room for the div tag that sets a black background
            #if req.colorize:
            #    width -= 43

            # XXX this regex does really really bad things with lots of color
            for line in self.xmlwrap(message, width):

                #if req.colorize:
                #    line = self.black_fmt % line

                args = [line]
                if not req.chat:
                    if not req.nick:
                        req.nick = req.sendto
                    args.insert(0, req.nick)
                reactor.callFromThread(func, *args)

                # don't spam ourselves off the server
                sleep(1)

        except Exception, error:
            log.exception(error)