comparison sat/plugins/plugin_misc_xmllog.py @ 2691:1ecceac3df96

plugin XEP-0198: Stream Management implementation: - hooks can now be set in stream onElement and send methods - xmllog refactored to use new hooks - client.isConnected now uses transport.connected method - fixed reconnection, SàT will now try to reconnect indefinitely until it success, unresolvable failure happen (e.g. invalid certificate), or explicit disconnection is requested (or a plugin change this behaviour) - new triggers: "stream_hooks", "disconnecting", "disconnected", and "xml_init" (replace "XML Initialized")
author Goffi <goffi@goffi.org>
date Sun, 18 Nov 2018 15:49:46 +0100
parents 56f94936df1e
children ab2696e34d29
comparison
equal deleted inserted replaced
2690:56bfe1b79204 2691:1ecceac3df96
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from sat.core.log import getLogger 22 from sat.core.log import getLogger
23 23
24 log = getLogger(__name__) 24 log = getLogger(__name__)
25 from twisted.words.xish import domish 25 from twisted.words.xish import domish
26 from twisted.words.xish import xmlstream 26 from functools import partial
27 27
28 PLUGIN_INFO = { 28 PLUGIN_INFO = {
29 C.PI_NAME: "Raw XML log Plugin", 29 C.PI_NAME: "Raw XML log Plugin",
30 C.PI_IMPORT_NAME: "XmlLog", 30 C.PI_IMPORT_NAME: "XmlLog",
31 C.PI_TYPE: "Misc", 31 C.PI_TYPE: "Misc",
33 C.PI_DEPENDENCIES: [], 33 C.PI_DEPENDENCIES: [],
34 C.PI_MAIN: "XmlLog", 34 C.PI_MAIN: "XmlLog",
35 C.PI_HANDLER: "no", 35 C.PI_HANDLER: "no",
36 C.PI_DESCRIPTION: _(u"""Send raw XML logs to bridge"""), 36 C.PI_DESCRIPTION: _(u"""Send raw XML logs to bridge"""),
37 } 37 }
38
39 host = None
40
41
42 def send(self, obj):
43 global host
44 if isinstance(obj, basestring):
45 log = unicode(obj)
46 elif isinstance(obj, domish.Element):
47 log = obj.toXml()
48 else:
49 log.error(_(u"INTERNAL ERROR: Unmanaged XML type"))
50 host.bridge.xmlLog("OUT", log, self._profile)
51 return self._original_send(obj)
52
53
54 def onElement(self, element):
55 global host
56 host.bridge.xmlLog("IN", element.toXml(), self._profile)
57 return self._original_onElement(element)
58 38
59 39
60 class XmlLog(object): 40 class XmlLog(object):
61 41
62 params = """ 42 params = """
69 </params> 49 </params>
70 """ % { 50 """ % {
71 "label_xmllog": _("Activate XML log") 51 "label_xmllog": _("Activate XML log")
72 } 52 }
73 53
74 def __init__(self, host_): 54 def __init__(self, host):
75 log.info(_("Plugin XML Log initialization")) 55 log.info(_("Plugin XML Log initialization"))
76 global host 56 self.host = host
77 host = host_
78
79 # parameters
80 host.memory.updateParams(self.params) 57 host.memory.updateParams(self.params)
81
82 # bridge
83 host.bridge.addSignal( 58 host.bridge.addSignal(
84 "xmlLog", ".plugin", signature="sss" 59 "xmlLog", ".plugin", signature="sss"
85 ) # args: direction("IN" or "OUT"), xml_data, profile 60 ) # args: direction("IN" or "OUT"), xml_data, profile
86 61
87 self.do_log = host.memory.getParamA("Xml log", "Debug") 62 host.trigger.add("stream_hooks", self.addHooks)
63
64 def addHooks(self, client, receive_hooks, send_hooks):
65 self.do_log = self.host.memory.getParamA("Xml log", "Debug")
88 if self.do_log: 66 if self.do_log:
89 XmlStream = xmlstream.XmlStream 67 receive_hooks.append(partial(self.onReceive, client=client))
90 XmlStream._original_send = XmlStream.send 68 send_hooks.append(partial(self.onSend, client=client))
91 XmlStream._original_onElement = XmlStream.onElement
92 XmlStream.send = send
93 XmlStream.onElement = onElement
94 XmlStream._profile = ""
95 host.trigger.add("XML Initialized", self.setProfile)
96 log.info(_(u"XML log activated")) 69 log.info(_(u"XML log activated"))
70 return True
97 71
98 def setProfile(self, xmlstream, profile): 72 def onReceive(self, element, client):
99 xmlstream._profile = profile 73 self.host.bridge.xmlLog("IN", element.toXml(), client.profile)
100 return True 74
75 def onSend(self, obj, client):
76 if isinstance(obj, basestring):
77 log = unicode(obj)
78 elif isinstance(obj, domish.Element):
79 log = obj.toXml()
80 else:
81 log.error(_(u"INTERNAL ERROR: Unmanaged XML type"))
82 self.host.bridge.xmlLog("OUT", log, client.profile)