use Network Event Dispatcher in connection_handler to handle HttpAuth events.

This commit is contained in:
Yann Leboulanger 2010-07-20 10:35:38 +02:00
parent 796213c9cb
commit 01d486ec60
4 changed files with 79 additions and 50 deletions

View File

@ -41,6 +41,7 @@ from calendar import timegm
import datetime
import common.xmpp
import common.caps_cache as capscache
from common import helpers
from common import gajim
@ -50,8 +51,10 @@ from common.pubsub import ConnectionPubSub
from common.pep import ConnectionPEP
from common.protocol.caps import ConnectionCaps
from common.protocol.bytestream import ConnectionSocks5Bytestream
import common.caps_cache as capscache
from common import ged
from common import nec
from common.nec import NetworkEvent
from plugins import GajimPlugin
if gajim.HAVE_FARSIGHT:
from common.jingle import ConnectionJingle
else:
@ -1008,6 +1011,9 @@ ConnectionCaps, ConnectionHandlersBase, ConnectionJingle):
self.gmail_last_tid = None
self.gmail_last_time = None
gajim.ged.register_event_handler('http-auth-received', ged.CORE,
self._nec_http_auth_received)
def build_http_auth_answer(self, iq_obj, answer):
if not self.connection or self.connected < 2:
return
@ -1022,17 +1028,17 @@ ConnectionCaps, ConnectionHandlersBase, ConnectionJingle):
common.xmpp.protocol.ERR_NOT_AUTHORIZED)
self.connection.send(err)
def _nec_http_auth_received(self, obj):
if obj.conn.name != self.name:
return
if obj.opt in ('yes', 'no'):
obj.conn.build_http_auth_answer(obj.iq_obj, obj.opt)
return True
def _HttpAuthCB(self, con, iq_obj):
log.debug('HttpAuthCB')
opt = gajim.config.get_per('accounts', self.name, 'http_auth')
if opt in ('yes', 'no'):
self.build_http_auth_answer(iq_obj, opt)
else:
id_ = iq_obj.getTagAttr('confirm', 'id')
method = iq_obj.getTagAttr('confirm', 'method')
url = iq_obj.getTagAttr('confirm', 'url')
msg = iq_obj.getTagData('body') # In case it's a message with a body
self.dispatch('HTTP_AUTH', (method, url, id_, iq_obj, msg))
gajim.nec.push_incoming_event(HttpAuthReceivedEvent(None, conn=self,
iq_obj=iq_obj))
raise common.xmpp.NodeProcessed
def _ErrorCB(self, con, iq_obj):
@ -2459,3 +2465,21 @@ ConnectionCaps, ConnectionHandlersBase, ConnectionJingle):
con.RegisterHandler('presence', self._StanzaArrivedCB)
con.RegisterHandler('message', self._StanzaArrivedCB)
con.RegisterHandler('unknown', self._StreamCB, 'urn:ietf:params:xml:ns:xmpp-streams', xmlns='http://etherx.jabber.org/streams')
class HttpAuthReceivedEvent(nec.NetworkIncomingEvent):
name = 'http-auth-received'
base_network_events = []
def generate(self):
if not self.conn:
self.conn = self.base_event.conn
if not self.iq_obj:
self.iq_obj = self.base_event.xmpp_iq
self.opt = gajim.config.get_per('accounts', self.conn.name, 'http_auth')
self.iq_id = self.iq_obj.getTagAttr('confirm', 'id')
self.method = self.iq_obj.getTagAttr('confirm', 'method')
self.url = self.iq_obj.getTagAttr('confirm', 'url')
# In case it's a message with a body
self.msg = self.iq_obj.getTagData('body')
return True

View File

@ -30,6 +30,9 @@ log = logging.getLogger('gajim.common.ged')
PRECORE = 30
CORE = 40
POSTCORE = 50
GUI1 = 60
GUI2 = 70
POSTGUI = 80
class GlobalEventsDispatcher(object):
@ -61,4 +64,5 @@ class GlobalEventsDispatcher(object):
log.debug('%s\nArgs: %s'%(event_name, str(args)))
if event_name in self.handlers:
for priority, handler in self.handlers[event_name]:
handler(*args, **kwargs)
if handler(*args, **kwargs):
return

View File

@ -41,7 +41,9 @@ class NetworkEventsController(object):
def register_incoming_event(self, event_class):
for base_event_name in event_class.base_network_events:
self.incoming_events_generators.setdefault(base_event_name, []).append(event_class)
event_list = self.incoming_events_generators.setdefault(base_event_name, [])
if not event_class in event_list:
event_list.append(event_class)
def unregister_incoming_event(self, event_class):
for base_event_name in event_class.base_network_events:
@ -55,8 +57,9 @@ class NetworkEventsController(object):
pass
def push_incoming_event(self, event_object):
if self._generate_events_based_on_incoming_event(event_object):
gajim.ged.raise_event(event_object.name, event_object)
if event_object.generate():
if self._generate_events_based_on_incoming_event(event_object):
gajim.ged.raise_event(event_object.name, event_object)
def push_outgoing_event(self, event_object):
pass
@ -93,25 +96,7 @@ class NetworkEvent(object):
def init(self):
pass
def _set_kwargs_as_attributes(self, **kwargs):
for k, v in kwargs.iteritems():
setattr(self, k, v)
def __str__(self):
return '<NetworkEvent object> Attributes: %s'%(pformat(self.__dict__))
def __repr__(self):
return '<NetworkEvent object> Attributes: %s'%(pformat(self.__dict__))
class NetworkIncomingEvent(NetworkEvent):
base_network_events = []
'''
Names of base network events that new event is going to be generated on.
'''
def init(self):
pass
def generate(self):
'''
Generates new event (sets it's attributes) based on event object.
@ -125,10 +110,25 @@ class NetworkIncomingEvent(NetworkEvent):
:return: True if generated event should be dispatched, False otherwise.
'''
pass
return True
def _set_kwargs_as_attributes(self, **kwargs):
for k, v in kwargs.iteritems():
setattr(self, k, v)
def __str__(self):
return '<NetworkEvent object> Attributes: %s'%(pformat(self.__dict__))
def __repr__(self):
return '<NetworkEvent object> Attributes: %s'%(pformat(self.__dict__))
class NetworkIncomingEvent(NetworkEvent):
base_network_events = []
'''
Names of base network events that new event is going to be generated on.
'''
class NetworkOutgoingEvent(NetworkEvent):
def init(self):
pass
pass

View File

@ -147,23 +147,24 @@ class Interface:
self.instances['change_nick_dialog'] = dialogs.ChangeNickDialog(
account, room_jid, title, prompt)
def handle_event_http_auth(self, account, data):
def handle_event_http_auth(self, obj):
#('HTTP_AUTH', account, (method, url, transaction_id, iq_obj, msg))
def response(account, iq_obj, answer):
gajim.connections[account].build_http_auth_answer(iq_obj, answer)
def response(account, answer):
obj.conn.build_http_auth_answer(obj.iq_obj, answer)
def on_yes(is_checked, account, iq_obj):
response(account, iq_obj, 'yes')
def on_yes(is_checked, obj):
response(obj, 'yes')
account = obj.conn.name
sec_msg = _('Do you accept this request?')
if gajim.get_number_of_connected_accounts() > 1:
sec_msg = _('Do you accept this request on account %s?') % account
if data[4]:
sec_msg = data[4] + '\n' + sec_msg
if obj.msg:
sec_msg = obj.msg + '\n' + sec_msg
dialog = dialogs.YesNoDialog(_('HTTP (%(method)s) Authorization for '
'%(url)s (id: %(id)s)') % {'method': data[0], 'url': data[1],
'id': data[2]}, sec_msg, on_response_yes=(on_yes, account, data[3]),
on_response_no=(response, account, data[3], 'no'))
'%(url)s (id: %(id)s)') % {'method': obj.method, 'url': obj.url,
'id': obj.iq_id}, sec_msg, on_response_yes=(on_yes, obj),
on_response_no=(response, obj, 'no'))
def handle_event_error_answer(self, account, array):
#('ERROR_ANSWER', account, (id, jid_from, errmsg, errcode))
@ -2140,7 +2141,6 @@ class Interface:
'FILE_SEND_ERROR': [self.handle_event_file_send_error],
'STANZA_ARRIVED': [self.handle_event_stanza_arrived],
'STANZA_SENT': [self.handle_event_stanza_sent],
'HTTP_AUTH': [self.handle_event_http_auth],
'VCARD_PUBLISHED': [self.handle_event_vcard_published],
'VCARD_NOT_PUBLISHED': [self.handle_event_vcard_not_published],
'ASK_NEW_NICK': [self.handle_event_ask_new_nick],
@ -2181,7 +2181,8 @@ class Interface:
'JINGLE_DISCONNECTED': [self.handle_event_jingle_disconnected],
'JINGLE_ERROR': [self.handle_event_jingle_error],
'PEP_RECEIVED': [self.handle_event_pep_received],
'CAPS_RECEIVED': [self.handle_event_caps_received]
'CAPS_RECEIVED': [self.handle_event_caps_received],
'http-auth-received': [self.handle_event_http_auth],
}
def register_core_handlers(self):
@ -2192,7 +2193,7 @@ class Interface:
"""
for event_name, event_handlers in self.handlers.iteritems():
for event_handler in event_handlers:
gajim.ged.register_event_handler(event_name, ged.CORE,
gajim.ged.register_event_handler(event_name, ged.GUI1,
event_handler)
################################################################################