From 9dc1c1c01fc488997dfc06d2985deee681b6bfce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Sun, 11 Oct 2015 08:38:59 +0200 Subject: [PATCH] switch to better maintained sleekxmpp --- README | 2 ++ highlightxmpp.py | 87 ++++++++++++++++++------------------------------ 2 files changed, 34 insertions(+), 55 deletions(-) diff --git a/README b/README index 0f9986c..19b075a 100644 --- a/README +++ b/README @@ -3,6 +3,8 @@ highlighted message or a private message is received. Great for running under a detatched terminal where you're not always looking at your IRC window. Changelog: + * 0.5: + - switch to sleekxmpp as xmpp library (http://github.com/fritzy/SleekXMPP) * 0.4: - Actually fixed random XML appearing in messages. diff --git a/highlightxmpp.py b/highlightxmpp.py index 40dff7c..891dc07 100644 --- a/highlightxmpp.py +++ b/highlightxmpp.py @@ -31,19 +31,18 @@ # JID messages are sent *to* (if not set, defaults to the same jid as above): # /set plugins.var.python.highlightxmpp.to myid@jabber.org -from time import sleep -import warnings +import sys import weechat as w +import sleekxmpp -# the XMPP module currently has a lot of deprecations -with warnings.catch_warnings(): - warnings.simplefilter("ignore") - import xmpp +if sys.version_info < (3, 0): + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') info = ( 'highlightxmpp', 'Jacob Peddicord ', - '0.3', + '0.5', 'GPL3', "Relay highlighted & private IRC messages over XMPP (Jabber)", '', @@ -56,58 +55,36 @@ settings = { 'to': '', } -client = None +class SendMsgBot(sleekxmpp.ClientXMPP): + def __init__(self, jid, password, recipient, message): + sleekxmpp.ClientXMPP.__init__(self, jid, password) + self.jid = jid + self.recipient = recipient + self.msg = message + self.add_event_handler("session_start", self.start, threaded=True) + def start(self, event): + self.send_presence() + self.get_roster() + self.send_message(mto=self.recipient, + mbody=self.msg, + mtype='chat') + self.disconnect(wait=True) -def connect_xmpp(): - global client - # connected if not connected - # & if we were disconnected, try to connect again - if client and client.isConnected(): - return True - w.prnt('', "XMPP: Connecting") - jid_name = w.config_get_plugin('jid') + +def send_xmpp(data, signal, message, trial=1): + jid = w.config_get_plugin('jid') + jid_to = w.config_get_plugin('to') + if not jid_to: + jid_to = jid password = w.config_get_plugin('password') - try: - jid = xmpp.protocol.JID(jid_name) - client = xmpp.Client(jid.getDomain(), debug=[]) - client.connect() - client.auth(jid.getNode(), password) - except: - w.prnt('', "XMPP: Could not connect or authenticate to server.") - client = None - return False - return True -def send_xmpp(data, signal, msgtxt, trial=1): - global client - - # ignore XMPP's deprecation warnings - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - - # connect to xmpp if we need to - if not connect_xmpp(): - return w.WEECHAT_RC_OK - jid_to = w.config_get_plugin('to') - - # send to self if no target set - if not jid_to: - jid_to = w.config_get_plugin('jid') - - # send the message - msg = xmpp.protocol.Message(jid_to, msgtxt, typ='chat') - try: - client.send(msg) - except IOError: - # every now and then the connection will still exist but a send will - # fail. catch that here and try to reconnect. isConnected() should - # start to realize that once this exception is triggered. - if trial > 3: - w.prnt('', "XMPP: Could not send to server.") - else: - sleep(0.5) - send_xmpp(data, signal, msgtxt, trial + 1) + xmpp = SendMsgBot(jid, password, jid_to, message) + if not xmpp.connect(): + w.prnt('', "Unable to connect to XMPP server.") return w.WEECHAT_RC_OK + xmpp.process(block=True) + return w.WEECHAT_RC_OK + # register with weechat if w.register(*info):