Mercurial > libervia-pubsub
comparison sat_pubsub/delegation.py @ 322:54d90c73b8b5
mam: various improvments:
- put common namespaces ton const
- VAL_RSM_MAX_DEFAULT can be None if default limit is not wanted
- ItemDate now has a 'date' attribute
- MAMService is MonkeyPatched the same way as PubSubService to handle PEP
- fixed error mapping in mam module
- PEP is handled
- properly manage date in a payload independent way
- when PEP is used, send privileged messages
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 05 Jan 2016 23:13:13 +0100 |
parents | 5d7c3787672e |
children | 0a93d71f9503 |
comparison
equal
deleted
inserted
replaced
321:c7fe09894952 | 322:54d90c73b8b5 |
---|---|
24 from wokkel.subprotocols import XMPPHandler | 24 from wokkel.subprotocols import XMPPHandler |
25 from wokkel import pubsub | 25 from wokkel import pubsub |
26 from wokkel import data_form | 26 from wokkel import data_form |
27 from wokkel import disco, iwokkel | 27 from wokkel import disco, iwokkel |
28 from wokkel.iwokkel import IPubSubService | 28 from wokkel.iwokkel import IPubSubService |
29 from wokkel import mam | |
29 from twisted.python import log | 30 from twisted.python import log |
30 from twisted.words.protocols.jabber import jid, error | 31 from twisted.words.protocols.jabber import jid, error |
31 from twisted.words.protocols.jabber.xmlstream import toResponse | 32 from twisted.words.protocols.jabber.xmlstream import toResponse |
32 from twisted.words.xish import domish | 33 from twisted.words.xish import domish |
33 from zope.interface import implements | 34 from zope.interface import implements |
38 DELEGATION_FWD_XPATH = '/iq[@type="set"]/delegation[@xmlns="{}"]/forwarded[@xmlns="{}"]'.format(DELEGATION_NS, FORWARDED_NS) | 39 DELEGATION_FWD_XPATH = '/iq[@type="set"]/delegation[@xmlns="{}"]/forwarded[@xmlns="{}"]'.format(DELEGATION_NS, FORWARDED_NS) |
39 | 40 |
40 DELEGATION_MAIN_SEP = "::" | 41 DELEGATION_MAIN_SEP = "::" |
41 DELEGATION_BARE_SEP = ":bare:" | 42 DELEGATION_BARE_SEP = ":bare:" |
42 | 43 |
44 TO_HACK = ((IPubSubService, pubsub, "PubSubRequest"), | |
45 (mam.IMAMService, mam, "MAMRequest")) | |
46 | |
47 | |
43 class InvalidStanza(Exception): | 48 class InvalidStanza(Exception): |
44 pass | 49 pass |
45 | |
46 | 50 |
47 | 51 |
48 class DelegationsHandler(XMPPHandler): | 52 class DelegationsHandler(XMPPHandler): |
49 implements(iwokkel.IDisco) | 53 implements(iwokkel.IDisco) |
50 _service_hacked = False | 54 _service_hacked = False |
51 | 55 |
52 def __init__(self): | 56 def __init__(self): |
53 super(DelegationsHandler, self).__init__() | 57 super(DelegationsHandler, self).__init__() |
54 | 58 |
55 def _service_hack(self): | 59 def _service_hack(self): |
56 """Patch the PubSubService to track delegated stanzas""" | 60 """Patch the request classes of services to track delegated stanzas""" |
57 # XXX: we need to monkey patch to track origin of the stanza in PubSubRequest. | 61 # XXX: we need to monkey patch to track origin of the stanza in PubSubRequest. |
58 # As PubSubRequest from sat.tmp.wokkel.pubsub use _request_class while | 62 # As PubSubRequest from sat.tmp.wokkel.pubsub use _request_class while |
59 # original wokkel.pubsub use directly pubsub.PubSubRequest, we need to | 63 # original wokkel.pubsub use directly pubsub.PubSubRequest, we need to |
60 # check which version is used before monkeypatching | 64 # check which version is used before monkeypatching |
61 for handler in self.parent.handlers: | 65 for handler in self.parent.handlers: |
62 if IPubSubService.providedBy(handler): | 66 for service, module, default_base_cls in TO_HACK: |
63 if hasattr(handler, '_request_class'): | 67 if service.providedBy(handler): |
64 request_base_class = handler._request_class | 68 if hasattr(handler, '_request_class'): |
65 else: | 69 request_base_class = handler._request_class |
66 request_base_class = pubsub.PubSubRequest | 70 else: |
67 | 71 request_base_class = getattr(module, default_base_cls) |
68 class PubSubRequestWithDelegation(request_base_class): | 72 |
69 """A PubSubReques which put an indicator if the stanza comme from delegation""" | 73 class RequestWithDelegation(request_base_class): |
70 | 74 """A XxxRequest which put an indicator if the stanza comme from delegation""" |
71 @classmethod | 75 |
72 def fromElement(cls, element): | 76 @classmethod |
73 """Check if element comme from delegation, and set a delegated flags | 77 def fromElement(cls, element): |
74 | 78 """Check if element comme from delegation, and set a delegated flags |
75 delegated flaf is either False, or it's a jid of the delegating server | 79 |
76 the delegated flag must be set on element before use | 80 delegated flag is either False, or it's a jid of the delegating server |
77 """ | 81 the delegated flag must be set on element before use |
78 try: | 82 """ |
79 # __getattr__ is overriden in domish.Element, so we use __getattribute__ | 83 try: |
80 delegated = element.__getattribute__('delegated') | 84 # __getattr__ is overriden in domish.Element, so we use __getattribute__ |
81 except AttributeError: | 85 delegated = element.__getattribute__('delegated') |
82 delegated = False | 86 except AttributeError: |
83 instance = cls.__base__.fromElement(element) | 87 delegated = False |
84 instance.delegated = delegated | 88 instance = cls.__base__.fromElement(element) |
85 return instance | 89 instance.delegated = delegated |
86 | 90 return instance |
87 if hasattr(handler, '_request_class'): | 91 |
88 handler._request_class = PubSubRequestWithDelegation | 92 if hasattr(handler, '_request_class'): |
89 else: | 93 handler._request_class = RequestWithDelegation |
90 pubsub.PubSubRequest = PubSubRequestWithDelegation | 94 else: |
95 setattr(module, default_base_cls, RequestWithDelegation) | |
91 DelegationsHandler._service_hacked = True | 96 DelegationsHandler._service_hacked = True |
92 | 97 |
93 def connectionInitialized(self): | 98 def connectionInitialized(self): |
94 if not self._service_hacked: | 99 if not self._service_hacked: |
95 self._service_hack() | 100 self._service_hack() |