'Manually trigger a custom event.
:param name: The name of the event to trigger.
:param data: Data that will be passed to each event handler.
Defaults to an empty dictionary, but is usually
a stanza object.
:param direct: Runs the event directly if True, skipping the
event queue. All event handlers will run in the
same thread.'

def event(self, name, data=None, direct=False):

    if (not data):
        data = {}
    log.debug((u'Event    triggered:    ' + name))
    handlers = self.__event_handlers.get(name, [])
    for handler in handlers:
        out_data = (copy.copy(data) if (len(handlers) > 1) else data)
        old_exception = getattr(data, u'exception', None)
        if direct:
            try:
                handler[0](out_data)
            except Exception as e:
                error_msg = u'Error    processing    event    handler:    %s'
                log.exception(error_msg, str(handler[0]))
                if old_exception:
                    old_exception(e)
                else:
                    self.exception(e)
        else:
            self.event_queue.put((u'event', handler, out_data))
        if handler[2]:
            with self.__event_handlers_lock:
                try:
                    h_index = self.__event_handlers[name].index(handler)
                    self.__event_handlers[name].pop(h_index)
                except:
                    pass
