mirror of
https://github.com/gryf/weechat-highlightxmpp.git
synced 2026-02-08 06:15:46 +01:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0e78873a71 | ||
| 16f60ee26d | |||
|
|
6c7c37f0c3 | ||
|
|
4278e196c3 | ||
|
|
9dc1c1c01f | ||
|
|
ccdd39151c | ||
|
|
5ed160cdba |
13
README
13
README
@@ -2,7 +2,20 @@ When loaded, this plugin will send you a message over XMPP (Jabber) when a
|
||||
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.
|
||||
|
||||
Requires SleekXMPP (`pip install sleekxmpp`)
|
||||
|
||||
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:
|
||||
- switch to sleekxmpp as xmpp library (http://github.com/fritzy/SleekXMPP)
|
||||
|
||||
* 0.4:
|
||||
- Actually fixed random XML appearing in messages.
|
||||
|
||||
* 0.3:
|
||||
- Fixed message type to 'chat'; should fix problems with offline send
|
||||
and XML embedded in the message on some receivers
|
||||
|
||||
162
highlightxmpp.py
162
highlightxmpp.py
@@ -1,120 +1,96 @@
|
||||
# HighlightXMPP 0.3 for IRC. Requires WeeChat >= 0.3.0 and Python >= 2.6.
|
||||
# HighlightXMPP 0.6 for IRC. Requires WeeChat >= 0.3.0,
|
||||
# Python >= 2.6, and sendxmpp (https://github.com/lhost/sendxmpp) or
|
||||
# equivalent.
|
||||
# Repo: https://github.com/jpeddicord/weechat-highlightxmpp
|
||||
#
|
||||
# Copyright (c) 2009-2011 Jacob Peddicord <jpeddicord@ubuntu.com>
|
||||
#
|
||||
#
|
||||
# Copyright (c) 2009-2015 Jacob Peddicord <jacob@peddicord.net>
|
||||
#
|
||||
# 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
|
||||
# the Free Software Foundation; either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# 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:
|
||||
# /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
|
||||
# echo "message" | sendxmpp someid@jabber.org
|
||||
#
|
||||
# Password for the above JID:
|
||||
# /set plugins.var.python.highlightxmpp.password abcdef
|
||||
# also you will need to provide JID to send messages:
|
||||
#
|
||||
# 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
|
||||
|
||||
from time import sleep
|
||||
import warnings
|
||||
import weechat as w
|
||||
import weechat
|
||||
|
||||
# the XMPP module currently has a lot of deprecations
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore")
|
||||
import xmpp
|
||||
|
||||
info = (
|
||||
'highlightxmpp',
|
||||
'Jacob Peddicord <jpeddicord@ubuntu.com>',
|
||||
'0.3',
|
||||
'GPL3',
|
||||
"Relay highlighted & private IRC messages over XMPP (Jabber)",
|
||||
'',
|
||||
''
|
||||
)
|
||||
SETTINGS = {'command': 'sendxmpp',
|
||||
'to': '',
|
||||
'debug': 'off'}
|
||||
INFO = ('highlightxmpp',
|
||||
'Jacob Peddicord <jacob@peddicord.net>',
|
||||
'0.6',
|
||||
'GPL3',
|
||||
"Relay highlighted & private IRC messages over XMPP (Jabber)",
|
||||
'',
|
||||
'')
|
||||
|
||||
settings = {
|
||||
'jid': '',
|
||||
'password': '',
|
||||
'to': '',
|
||||
}
|
||||
|
||||
client = None
|
||||
def send_xmpp(data, signal, message):
|
||||
"""Send XMPP message using external commandline tool."""
|
||||
jid = weechat.config_get_plugin('to')
|
||||
command = weechat.config_get_plugin('command').split(' ')
|
||||
debug = weechat.config_get_plugin('debug') == 'on'
|
||||
|
||||
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')
|
||||
if not jid:
|
||||
weechat.prnt('', 'You need to provide destination JID to use this '
|
||||
'plugin.')
|
||||
return weechat.WEECHAT_RC_OK
|
||||
|
||||
command.append(jid)
|
||||
|
||||
pipe = subprocess.Popen(command, stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
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
|
||||
message = message.encode()
|
||||
except UnicodeDecodeError:
|
||||
pass
|
||||
|
||||
def send_xmpp(data, signal, msg, 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, msg, typ='chat')
|
||||
_, stderr = pipe.communicate(input=message)
|
||||
if pipe.returncode != 0 and debug:
|
||||
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, msg, trial + 1)
|
||||
return w.WEECHAT_RC_OK
|
||||
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 w.register(*info):
|
||||
# add our settings
|
||||
for setting in settings:
|
||||
if not w.config_is_set_plugin(setting):
|
||||
w.config_set_plugin(setting, settings[setting])
|
||||
# and finally our hooks
|
||||
w.hook_signal('weechat_highlight', 'send_xmpp', '')
|
||||
w.hook_signal('weechat_pv', 'send_xmpp', '')
|
||||
|
||||
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', '')
|
||||
|
||||
Reference in New Issue
Block a user