Compare commits

3 Commits

Author SHA1 Message Date
a8c3eeda14 Add some options to surpass the output. 2023-11-07 19:28:01 +01:00
Jacob Peddicord
0e78873a71 Merge pull request #6 from gryf/sendxmpp
Use commandline tool for sending XMPP messages.
2021-01-23 16:19:57 -08:00
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 89 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,98 +1,107 @@
# 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>
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or # the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
####### #######
# #
# 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 os
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 = (
'highlightxmpp',
'Jacob Peddicord <jacob@peddicord.net>',
'0.5',
'GPL3',
"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): SETTINGS = {'command': 'sendxmpp',
jid = w.config_get_plugin('jid') 'to': '',
jid_to = w.config_get_plugin('to') 'debug': 'off'}
if not jid_to: INFO = ('highlightxmpp',
jid_to = jid 'Jacob Peddicord <jacob@peddicord.net>',
password = w.config_get_plugin('password') '0.6',
'GPL3',
xmpp = SendMsgBot(jid, password, jid_to, message) "Relay highlighted & private IRC messages over XMPP (Jabber)",
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 def send_xmpp(data, signal, message):
if w.register(*info): """Send XMPP message using external commandline tool."""
# add our settings jid = weechat.config_get_plugin('to')
for setting in settings: command = weechat.config_get_plugin('command').split(' ')
if not w.config_is_set_plugin(setting): debug = weechat.config_get_plugin('debug') == 'on'
w.config_set_plugin(setting, settings[setting])
# and finally our hooks if os.path.exists(os.path.expanduser("~/.dont_send_xmpp")):
w.hook_signal('weechat_highlight', 'send_xmpp', '') return weechat.WEECHAT_RC_OK
w.hook_signal('weechat_pv', 'send_xmpp', '')
if not jid:
weechat.prnt('', 'You need to provide destination JID to use this '
'plugin.')
return weechat.WEECHAT_RC_OK
command.append(jid)
try:
pipe = subprocess.Popen(command, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except FileNotFoundError as ex:
if debug:
weechat.prnt('', 'Command %s cannot be find.' % ex.filename)
return weechat.WEECHAT_RC_OK
try:
message = message.encode()
except UnicodeDecodeError:
pass
# don't throw your output on the screen.
res = pipe.communicate(input=message)
if pipe.returncode != 0 and debug:
try:
stderr = res[1].decode()
except UnicodeDecodeError:
stderr = res[1]
weechat.prnt('', 'Error sending message to %s:\n%s' % (jid, stderr))
return weechat.WEECHAT_RC_OK
if weechat.register(*INFO):
for setting in SETTINGS:
if not weechat.config_is_set_plugin(setting):
weechat.config_set_plugin(setting, SETTINGS[setting])
weechat.hook_signal('weechat_highlight', 'send_xmpp', '')
weechat.hook_signal('weechat_pv', 'send_xmpp', '')