weechat-gotify/gotify.py

179 lines
5.3 KiB
Python
Raw Normal View History

2017-02-10 18:01:01 +00:00
# -*- coding: utf-8 -*-
2018-11-16 12:40:03 +00:00
# https://github.com/flocke/weechat-gotify
2017-02-10 18:01:01 +00:00
try:
import weechat
except ImportError:
from sys import exit
print('This script has to run under WeeChat (https://weechat.org/).')
exit(1)
2018-11-16 12:40:03 +00:00
import requests
2017-02-10 18:01:01 +00:00
2018-11-16 12:40:03 +00:00
SCRIPT_NAME = 'gotify'
SCRIPT_AUTHOR = 'flocke'
SCRIPT_VERSION = '0.1.0'
2017-02-10 18:01:01 +00:00
SCRIPT_LICENSE = 'MIT'
2018-11-16 12:40:03 +00:00
SCRIPT_DESC = 'Send highlights and mentions through Gotify'
2017-02-10 18:01:01 +00:00
SETTINGS = {
'host': (
'',
2018-11-16 12:40:03 +00:00
'host for the gotify api'),
'token': (
'',
'app token for the gotify api'),
'priority': (
'2',
'priority of the message'),
2017-02-10 18:01:01 +00:00
'separator': (
': ',
'separator between nick and message in notifications'),
2018-11-16 14:27:04 +00:00
'timeout': (
'5',
'timeout for the message sending in seconds'),
2017-02-10 18:01:01 +00:00
'notify_on_highlight': (
'on',
'push notifications for highlights in buffers (on/off)'),
'notify_on_privmsg': (
'on',
'push notifications for private messages (on/off)'),
'notify_when': (
'always',
'when to push notifications (away/detached/always/never)'),
'ignore_buffers': (
'',
'comma-separated list of buffers to ignore'),
'ignore_nicks': (
'',
'comma-separated list of users to not push notifications from'),
}
def send_message(title, message):
2018-11-16 12:40:03 +00:00
token = weechat.config_get_plugin('token')
2018-11-16 14:27:04 +00:00
host = weechat.config_get_plugin('host')
if token != '' and host != '':
timeout = float(weechat.config_get_plugin('timeout'))
priority = int(weechat.config_get_plugin('priority'))
api = host.rstrip('/') + '/message?token=' + token
2017-02-10 18:01:01 +00:00
2018-11-16 12:40:03 +00:00
data = {
"message": message,
"title": title,
2018-11-16 14:27:04 +00:00
"priority": priority
2018-11-16 12:40:03 +00:00
}
2017-02-10 18:01:01 +00:00
2018-11-16 14:27:04 +00:00
try:
resp = requests.post(api, json=data, timeout=timeout)
except requests.exceptions.Timeout:
weechat.prnt("", "{0:s}failed to send notification, the request to the Gotify API timed out".format(weechat.prefix("error")))
2017-02-10 18:01:01 +00:00
def get_sender(tags, prefix):
# attempt to find sender from tags
# nicks are always prefixed with 'nick_'
for tag in tags:
if tag.startswith('nick_'):
return tag[5:]
# fallback method to find sender from prefix
# nicks in prefixes are prefixed with optional modes (e.g @ for operators)
# so we have to strip away those first, if they exist
if prefix.startswith(('~', '&', '@', '%', '+', '-', ' ')):
return prefix[1:]
return prefix
def get_buffer_names(buffer):
buffer_names = []
buffer_names.append(weechat.buffer_get_string(buffer, 'short_name'))
buffer_names.append(weechat.buffer_get_string(buffer, 'name'))
return buffer_names
def should_send(buffer, tags, nick, highlighted):
if not nick:
# a nick is required to form a correct message, bail
return False
if highlighted:
if weechat.config_get_plugin('notify_on_highlight') != 'on':
# notifying on highlights is disabled, bail
return False
elif weechat.buffer_get_string(buffer, 'localvar_type') == 'private':
if weechat.config_get_plugin('notify_on_privmsg') != 'on':
# notifying on private messages is disabled, bail
return False
else:
# not a highlight or private message, bail
return False
notify_when = weechat.config_get_plugin('notify_when')
if notify_when == 'never':
# user has opted to not be notified, bail
return False
elif notify_when == 'away':
# user has opted to only be notified when away
infolist_args = (
weechat.buffer_get_string(buffer, 'localvar_channel'),
weechat.buffer_get_string(buffer, 'localvar_server'),
weechat.buffer_get_string(buffer, 'localvar_nick')
)
if not None in infolist_args:
infolist = weechat.infolist_get('irc_nick', '', ','.join(infolist_args))
if infolist:
away_status = weechat.infolist_integer(infolist, 'away')
if not away_status:
# user is not away, bail
return False
elif notify_when == 'detached':
# user has opted to only be notified when detached (relays)
num_relays = weechat.info_get('relay_client_count', 'connected')
if num_relays != '0':
# some relay(s) connected, bail
2017-02-10 18:01:01 +00:00
return False
if nick == weechat.buffer_get_string(buffer, 'localvar_nick'):
# the sender was the current user, bail
return False
if nick in weechat.config_get_plugin('ignore_nicks').split(','):
# the sender was on the ignore list, bail
return False
for buffer_name in get_buffer_names(buffer):
if buffer_name in weechat.config_get_plugin('ignore_buffers').split(','):
# the buffer was on the ignore list, bail
return False
return True
def message_callback(data, buffer, date, tags, displayed, highlight, prefix, message):
nick = get_sender(tags, prefix)
if should_send(buffer, tags, nick, int(highlight)):
message = '%s%s%s' % (nick, weechat.config_get_plugin('separator'), message)
if int(highlight):
buffer_names = get_buffer_names(buffer)
send_message(buffer_names[0] or buffer_names[1], message)
else:
send_message('Private Message', message)
return weechat.WEECHAT_RC_OK
# register plugin
weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, '', '')
# grab all messages in any buffer
weechat.hook_print('', '', '', 1, 'message_callback', '')
# register configuration defaults
for option, value in SETTINGS.items():
if not weechat.config_is_set_plugin(option):
weechat.config_set_plugin(option, value[0])
weechat.config_set_desc_plugin(option, '%s (default: "%s")' % (value[1], value[0]))