Add an XML tab (/xml_tab) to view incoming/outgoing stanzas

Fixes #2074
This commit is contained in:
mathieui 2011-11-29 00:00:47 +01:00 committed by Florent Le Coz
parent a1575237fd
commit 4d7a11e238
3 changed files with 117 additions and 0 deletions

View file

@ -41,6 +41,7 @@ class Connection(sleekxmpp.ClientXMPP):
jid = '%s/%s' % (config.get('server', 'anon.louiz.org'), resource)
password = None
sleekxmpp.ClientXMPP.__init__(self, jid, password, ssl=True)
self.core = None
self.auto_reconnect = False
self.auto_authorize = None
self.register_plugin('xep_0030')
@ -75,3 +76,15 @@ class Connection(sleekxmpp.ClientXMPP):
return False
self.process(threaded=True)
return True
def send_raw(self, data, now=False, reconnect=None):
"""
Overrides XMLStream.send_raw, with an event added
"""
if self.core:
self.core.outgoing_stanza(data)
sleekxmpp.ClientXMPP.send_raw(self, data, now, reconnect)
class MatchAll(sleekxmpp.xmlstream.matcher.base.MatcherBase):
def match(self, xml):
return True

View file

@ -27,6 +27,7 @@ import collections
from sleekxmpp.xmlstream.stanzabase import JID
from sleekxmpp.xmlstream.stanzabase import StanzaBase
from sleekxmpp.xmlstream.handler import Callback
log = logging.getLogger(__name__)
@ -91,6 +92,7 @@ class Core(object):
self.running = True
self.events = events.EventHandler()
self.xmpp = singleton.Singleton(connection.Connection)
self.xmpp.core = self
self.remote_fifo = None
# a unique buffer used to store global informations
# that are displayed in almost all tabs, in an
@ -98,6 +100,7 @@ class Core(object):
self.information_buffer = TextBuffer()
self.information_win_size = config.get('info_win_height', 2, 'var')
self.information_win = windows.TextWin(300)
self.xml_buffer = TextBuffer()
self.tab_win = windows.GlobalInfoBar()
self.information_buffer.add_window(self.information_win)
self.tabs = []
@ -142,6 +145,7 @@ class Core(object):
'decline': (self.command_decline, _("Usage: /decline <room> [reason]\nDecline: Decline the invitation to room with or without reason."), self.completion_decline),
'bookmarks': (self.command_bookmarks, _("Usage: /bookmarks\nBookmarks: Show the current bookmarks."), None),
'remove_bookmark': (self.command_remove_bookmark, _("Usage: /remove_bookmark [jid]\nRemove Bookmark: Remove the specified bookmark, or the bookmark on the current tab, if any."), self.completion_remove_bookmark),
'xml_tab': (self.command_xml_tab, _("Usage: /xml_tab\nXML Tab: Open an XML tab."), None),
}
self.key_func = {
@ -188,6 +192,7 @@ class Core(object):
self.xmpp.add_event_handler("chatstate_paused", self.on_chatstate_paused)
self.xmpp.add_event_handler("chatstate_gone", self.on_chatstate_gone)
self.xmpp.add_event_handler("chatstate_inactive", self.on_chatstate_inactive)
self.xmpp.register_handler(Callback('ALL THE STANZAS', connection.MatchAll(None), self.incoming_stanza))
self.timed_events = set()
@ -228,6 +233,23 @@ class Core(object):
self.information_win.resize(self.information_win_size, tabs.Tab.width,
tabs.Tab.height - 1 - self.information_win_size - tabs.Tab.tab_win_height(), 0)
def outgoing_stanza(self, stanza):
self.add_message_to_text_buffer(self.xml_buffer, '\x191}<--\x19o %s' % stanza)
if isinstance(self.current_tab(), tabs.XMLTab):
self.current_tab().refresh()
self.doupdate()
def incoming_stanza(self, stanza):
self.add_message_to_text_buffer(self.xml_buffer, '\x192}-->\x19o %s' % stanza)
if isinstance(self.current_tab(), tabs.XMLTab):
self.current_tab().refresh()
self.doupdate()
def command_xml_tab(self, arg):
"""/xml_tab"""
tab = tabs.XMLTab()
self.add_tab(tab, True)
def resize_global_info_bar(self):
"""
Resize the GlobalInfoBar only once at each resize

View file

@ -2399,6 +2399,88 @@ class MucListTab(Tab):
def on_scroll_down(self):
self.listview.scroll_down()
class XMLTab(Tab):
def __init__(self):
Tab.__init__(self)
self.state = 'normal'
self.text_win = windows.TextWin()
self.core.xml_buffer.add_window(self.text_win)
self.default_help_message = windows.HelpText("/ to enter a command")
self.commands['close'] = (self.close, _("Usage: /close\nClose: Just close this tab."), None)
self.input = self.default_help_message
self.key_func['^T'] = self.close
self.key_func['^I'] = self.completion
self.key_func["KEY_DOWN"] = self.on_scroll_down
self.key_func["KEY_UP"] = self.on_scroll_up
self.key_func["/"] = self.on_slash
self.resize()
def on_slash(self):
"""
'/' is pressed, activate the input
"""
curses.curs_set(1)
self.input = windows.CommandInput("", self.reset_help_message, self.execute_slash_command)
self.input.resize(1, self.width, self.height-1, 0)
self.input.do_command("/") # we add the slash
def reset_help_message(self, _=None):
if self.core.current_tab() is self:
curses.curs_set(0)
self.input = self.default_help_message
self.input.refresh()
self.core.doupdate()
return True
def on_scroll_up(self):
self.text_win.scroll_up(self.text_win.height-1)
def on_scroll_down(self):
self.text_win.scroll_down(self.text_win.height-1)
def execute_slash_command(self, txt):
if txt.startswith('/'):
self.input.key_enter()
self.execute_command(txt)
return self.reset_help_message()
def completion(self):
if isinstance(self.input, windows.CommandInput):
self.complete_commands(self.input)
def on_input(self, key, raw):
res = self.input.do_command(key, raw=raw)
if res:
return True
if not raw and key in self.key_func:
return self.key_func[key]()
def close(self, arg=None):
self.core.close_tab()
def resize(self):
if not self.visible:
return
self.need_resize = False
self.text_win.resize(self.height-2, self.width, 0, 0)
self.input.resize(1, self.width, self.height-1, 0)
def refresh(self):
if self.need_resize:
self.resize()
log.debug(' TAB Refresh: %s',self.__class__.__name__)
self.text_win.refresh()
self.refresh_tab_win()
self.input.refresh()
def on_lose_focus(self):
self.state = 'normal'
def on_gain_focus(self):
self.state = 'current'
curses.curs_set(0)
class SimpleTextTab(Tab):
"""
A very simple tab, with just a text displaying some