mirror of
https://github.com/gryf/weechat-highlightxmpp.git
synced 2025-12-18 20:10:23 +01:00
Merge pull request #2 from Mic92/master
switch to better maintained sleekxmpp
This commit is contained in:
2
README
2
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.
|
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.
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
def send_xmpp(data, signal, message, trial=1):
|
||||||
# connected if not connected
|
jid = w.config_get_plugin('jid')
|
||||||
# & if we were disconnected, try to connect again
|
jid_to = w.config_get_plugin('to')
|
||||||
if client and client.isConnected():
|
if not jid_to:
|
||||||
return True
|
jid_to = jid
|
||||||
w.prnt('', "XMPP: Connecting")
|
|
||||||
jid_name = w.config_get_plugin('jid')
|
|
||||||
password = w.config_get_plugin('password')
|
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):
|
xmpp = SendMsgBot(jid, password, jid_to, message)
|
||||||
global client
|
if not xmpp.connect():
|
||||||
|
w.prnt('', "Unable to connect to XMPP server.")
|
||||||
# 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)
|
|
||||||
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):
|
||||||
|
|||||||
Reference in New Issue
Block a user