switch to better maintained sleekxmpp

This commit is contained in:
Jörg Thalheim
2015-10-11 08:38:59 +02:00
parent ccdd39151c
commit 9dc1c1c01f
2 changed files with 34 additions and 55 deletions

2
README
View File

@@ -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. a detatched terminal where you're not always looking at your IRC window.
Changelog: Changelog:
* 0.5:
- switch to sleekxmpp as xmpp library (http://github.com/fritzy/SleekXMPP)
* 0.4: * 0.4:
- Actually fixed random XML appearing in messages. - Actually fixed random XML appearing in messages.

View File

@@ -31,19 +31,18 @@
# JID messages are sent *to* (if not set, defaults to the same jid as above): # JID messages are sent *to* (if not set, defaults to the same jid as above):
# /set plugins.var.python.highlightxmpp.to myid@jabber.org # /set plugins.var.python.highlightxmpp.to myid@jabber.org
from time import sleep import sys
import warnings
import weechat as w import weechat as w
import sleekxmpp
# the XMPP module currently has a lot of deprecations if sys.version_info < (3, 0):
with warnings.catch_warnings(): from sleekxmpp.util.misc_ops import setdefaultencoding
warnings.simplefilter("ignore") setdefaultencoding('utf8')
import xmpp
info = ( info = (
'highlightxmpp', 'highlightxmpp',
'Jacob Peddicord <jpeddicord@ubuntu.com>', 'Jacob Peddicord <jpeddicord@ubuntu.com>',
'0.3', '0.5',
'GPL3', 'GPL3',
"Relay highlighted & private IRC messages over XMPP (Jabber)", "Relay highlighted & private IRC messages over XMPP (Jabber)",
'', '',
@@ -56,58 +55,36 @@ settings = {
'to': '', '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')
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): def send_xmpp(data, signal, message, trial=1):
global client jid = w.config_get_plugin('jid')
# 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') jid_to = w.config_get_plugin('to')
# send to self if no target set
if not jid_to: if not jid_to:
jid_to = w.config_get_plugin('jid') jid_to = jid
password = w.config_get_plugin('password')
# send the message xmpp = SendMsgBot(jid, password, jid_to, message)
msg = xmpp.protocol.Message(jid_to, msgtxt, typ='chat') if not xmpp.connect():
try: w.prnt('', "Unable to connect to XMPP server.")
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)
return w.WEECHAT_RC_OK return w.WEECHAT_RC_OK
xmpp.process(block=True)
return w.WEECHAT_RC_OK
# register with weechat # register with weechat
if w.register(*info): if w.register(*info):