Skip to content

Instantly share code, notes, and snippets.

@cstewart90
Last active February 4, 2017 00:59
Show Gist options
  • Save cstewart90/92591aa0c29e17ece7eae64d79045f68 to your computer and use it in GitHub Desktop.
Save cstewart90/92591aa0c29e17ece7eae64d79045f68 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# Copyright (C) 2017 Christopher Stewart <[email protected]>
# 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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Derived from: irssinotifier
# Author: Caspar Clemens Mierau <[email protected]>
# Homepage: https://github.com/leitmedium/weechat-irssinotifier
import weechat, urllib, urllib2
weechat.register("joinnotififer",
"kanzo",
"0.1",
"GPL3",
"joinnotifier: Send push notifications to a Join device.",
"",
"")
settings = {
"api_key": "Join API Key.",
"device_id": "Join deviceId.",
"only_away": "Only send notifications when set as away.",
"ignore_buffers": "Comma separated list of buffers to ignore.",
"ignore_servers": "Comma separated list of servers to ignore.",
"ignore_nicks": "Comma separated list of nicks to ignore.",
}
required_settings = ["api_key", "device_id"]
for option, help_text in settings.items():
if not weechat.config_is_set_plugin(option):
weechat.config_set_plugin(option, "")
if option in required_settings and weechat.config_get_plugin(option) == "":
weechat.prnt("", weechat.prefix("error") + "irssinotifier: Please set option: %s" % option)
weechat.prnt("", "joinnotifier: /set plugins.var.python.joinnotifier.%s STRING" % option)
weechat.config_set_desc_plugin(option, help_text)
# Hook privmsg/hilights
weechat.hook_print("", "notify_message", "", 1, "notify_show", "")
weechat.hook_print("", "notify_private", "", 1, "notify_show", "")
# Functions
def notify_show(data, bufferp, uber_empty, tagsn, isdisplayed,
ishilight, prefix, message):
# irc PMs are caught by notify_private, but we need notify_message to
# capture hilights in channels.
if 'notify_message' in tagsn and not ishilight:
return weechat.WEECHAT_RC_OK
# are we away?
away = weechat.buffer_get_string(bufferp,"localvar_away")
if (away == "" and weechat.config_get_plugin("only_away") == "on"):
return weechat.WEECHAT_RC_OK
# get local nick for buffer
mynick = weechat.buffer_get_string(bufferp,"localvar_nick")
# get buffer info
name = weechat.buffer_get_string(bufferp,"name")
server = weechat.buffer_get_string(bufferp, "localvar_server")
channel = weechat.buffer_get_string(bufferp, "localvar_channel")
# ignore buffers on ignorelists
if not (server in weechat.config_get_plugin("ignore_servers").split(",") or
name in weechat.config_get_plugin("ignore_buffers").split(",") or
prefix in weechat.config_get_plugin("ignore_nicks").split(",")):
# only notify if the message was not sent by myself
if (weechat.buffer_get_string(bufferp, "localvar_type") == "private") and (prefix!=mynick):
send_notification(channel, prefix, message)
elif int(ishilight):
buffer = (weechat.buffer_get_string(bufferp, "short_name") or name)
send_notification(buffer, prefix, message)
return weechat.WEECHAT_RC_OK
def send_notification(chan, nick, message):
text = "[{}] <{}> {}".format(chan, nick, message)
API_KEY = weechat.config_get_plugin("api_key")
DEVICE_ID = weechat.config_get_plugin("device_id")
if API_KEY and DEVICE_ID:
params = {"apikey": API_KEY, "deviceId": DEVICE_ID, "text": text, "title": "WeeChat"}
params = urllib.urlencode(params)
url = "url:https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?" + params
weechat.hook_process(url, 30 * 1000, "cb_send_notification", "")
return weechat.WEECHAT_RC_OK
def cb_send_notification(data, command, rc, out, err):
return weechat.WEECHAT_RC_OK
# vim: autoindent expandtab smarttab shiftwidth=4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment