Compare commits

1 Commits

Author SHA1 Message Date
16f60ee26d Use commandline tool for sending XMPP messages.
Loading sleekxmpp library with current Weechat version, causes to
highlightxmpp to fail. Probable reason is that sleekxmpp heavily uses
threading, while Weechat docs[1] clearly states, that threads shouldn't
be used.

[1] https://weechat.org/files/doc/stable/weechat_scripting.en.html#weechat_architecture
2021-01-02 19:39:34 +01:00
2 changed files with 78 additions and 75 deletions

5
README
View File

@@ -5,6 +5,11 @@ a detatched terminal where you're not always looking at your IRC window.
Requires SleekXMPP (`pip install sleekxmpp`) Requires SleekXMPP (`pip install sleekxmpp`)
Changelog: Changelog:
* 0.6:
- switch from sleekxmpp to commandline utility sendxmpp
(https://github.com/lhost/sendxmpp) or go-sendxmpp
(https://salsa.debian.org/mdosch/go-sendxmpp).
* 0.5: * 0.5:
- switch to sleekxmpp as xmpp library (http://github.com/fritzy/SleekXMPP) - switch to sleekxmpp as xmpp library (http://github.com/fritzy/SleekXMPP)

View File

@@ -1,5 +1,6 @@
# HighlightXMPP 0.5 for IRC. Requires WeeChat >= 0.3.0, # HighlightXMPP 0.6 for IRC. Requires WeeChat >= 0.3.0,
# Python >= 2.6, and sleekxmpp. # Python >= 2.6, and sendxmpp (https://github.com/lhost/sendxmpp) or
# equivalent.
# Repo: https://github.com/jpeddicord/weechat-highlightxmpp # Repo: https://github.com/jpeddicord/weechat-highlightxmpp
# #
# Copyright (c) 2009-2015 Jacob Peddicord <jacob@peddicord.net> # Copyright (c) 2009-2015 Jacob Peddicord <jacob@peddicord.net>
@@ -19,80 +20,77 @@
# #
####### #######
# #
# You must configure this plugin before using: # You must configure sendxmpp before using this plugin, so you'll be able to
# send message from commandline using format:
# #
# JID messages are sent from: # echo "message" | sendxmpp someid@jabber.org
# /set plugins.var.python.highlightxmpp.jid someid@jabber.org
# alternatively, to use a specific resource:
# /set plugins.var.python.highlightxmpp.jid someid@jabber.org/resource
# #
# Password for the above JID: # also you will need to provide JID to send messages:
# /set plugins.var.python.highlightxmpp.password abcdef
# #
# 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
#
# finally, you might want to change the sendxmpp binary to some other
# implementation (like https://salsa.debian.org/mdosch/go-sendxmpp) and/or
# provide some additional parameters, i.e.:
#
# /set plugins.var.python.highlightxmpp.command "/path/to/go-sendxmpp -t"
#
# You can set debug option to "on" if you experience issues regarding sending
# messages via sendxmpp:
#
# /set plugins.var.python.highlightxmpp.debug off
#
import subprocess
import sys import weechat
import weechat as w
import sleekxmpp
if sys.version_info < (3, 0):
from sleekxmpp.util.misc_ops import setdefaultencoding
setdefaultencoding('utf8')
info = ( SETTINGS = {'command': 'sendxmpp',
'highlightxmpp', 'to': '',
'debug': 'off'}
INFO = ('highlightxmpp',
'Jacob Peddicord <jacob@peddicord.net>', 'Jacob Peddicord <jacob@peddicord.net>',
'0.5', '0.6',
'GPL3', 'GPL3',
"Relay highlighted & private IRC messages over XMPP (Jabber)", "Relay highlighted & private IRC messages over XMPP (Jabber)",
'', '',
'' '')
)
settings = {
'jid': '',
'password': '',
'to': '',
}
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 send_xmpp(data, signal, message, trial=1): def send_xmpp(data, signal, message):
jid = w.config_get_plugin('jid') """Send XMPP message using external commandline tool."""
jid_to = w.config_get_plugin('to') jid = weechat.config_get_plugin('to')
if not jid_to: command = weechat.config_get_plugin('command').split(' ')
jid_to = jid debug = weechat.config_get_plugin('debug') == 'on'
password = w.config_get_plugin('password')
xmpp = SendMsgBot(jid, password, jid_to, message) if not jid:
if not xmpp.connect(): weechat.prnt('', 'You need to provide destination JID to use this '
w.prnt('', "Unable to connect to XMPP server.") 'plugin.')
return w.WEECHAT_RC_OK return weechat.WEECHAT_RC_OK
xmpp.process(block=True)
return w.WEECHAT_RC_OK command.append(jid)
pipe = subprocess.Popen(command, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
message = message.encode()
except UnicodeDecodeError:
pass
_, stderr = pipe.communicate(input=message)
if pipe.returncode != 0 and debug:
try:
stderr = stderr.decode()
except UnicodeDecodeError:
pass
weechat.prnt('', 'Error sending message to %s:\n%s' % (jid, stderr))
return weechat.WEECHAT_RC_OK
# register with weechat if weechat.register(*INFO):
if w.register(*info): for setting in SETTINGS:
# add our settings if not weechat.config_is_set_plugin(setting):
for setting in settings: weechat.config_set_plugin(setting, SETTINGS[setting])
if not w.config_is_set_plugin(setting):
w.config_set_plugin(setting, settings[setting]) weechat.hook_signal('weechat_highlight', 'send_xmpp', '')
# and finally our hooks weechat.hook_signal('weechat_pv', 'send_xmpp', '')
w.hook_signal('weechat_highlight', 'send_xmpp', '')
w.hook_signal('weechat_pv', 'send_xmpp', '')