2014-04-03 19:21:01 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import json
|
|
|
|
import re
|
2014-04-07 21:58:23 +00:00
|
|
|
import urllib
|
2014-04-03 19:21:01 +00:00
|
|
|
|
|
|
|
import weechat as w
|
|
|
|
|
2014-04-08 20:41:07 +00:00
|
|
|
w.register('weebullet', 'Lefty', '0.2.0', 'BSD', 'weebullet pushes notifications from IRC to Pushbullet.', '', '')
|
2014-04-03 19:21:01 +00:00
|
|
|
|
|
|
|
w.hook_print("", "irc_privmsg", "", 1, "priv_msg_cb", "")
|
2014-04-03 20:51:11 +00:00
|
|
|
w.hook_command(
|
|
|
|
"send_push_note", #command
|
|
|
|
"send a push note", # description
|
|
|
|
"[message]" # arguments description,
|
|
|
|
"", #argument
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"cmd_send_push_note", "")
|
2014-04-03 19:21:01 +00:00
|
|
|
|
2014-04-07 21:58:23 +00:00
|
|
|
configs = {
|
|
|
|
# "timeout": 20000,
|
2014-04-03 20:41:39 +00:00
|
|
|
"api_key": ""
|
|
|
|
}
|
|
|
|
|
2014-04-07 21:58:23 +00:00
|
|
|
for option, default_value in configs.items():
|
2014-04-03 20:41:39 +00:00
|
|
|
if w.config_get_plugin(option) == "":
|
|
|
|
w.prnt("", w.prefix("error") + "pushbullet: Please set option: %s" % option)
|
2014-04-07 21:58:23 +00:00
|
|
|
if type(default_value) == "str":
|
|
|
|
w.prnt("", "pushbullet: /set plugins.var.python.weebullet.%s STRING" % option)
|
|
|
|
elif type(default_value) == "int":
|
|
|
|
w.prnt("", "pushbullet: /set plugins.var.python.weebullet.%s INT" % option)
|
|
|
|
else:
|
|
|
|
w.prnt("", "pushbullet: /set plugins.var.python.weebullet.%s VALUE" % option)
|
2014-04-03 19:21:01 +00:00
|
|
|
|
|
|
|
|
2014-04-08 03:40:03 +00:00
|
|
|
def process_pushbullet_cb(data, url, status, response, err):
|
|
|
|
body = None
|
|
|
|
headers = {}
|
|
|
|
lines = response.rstrip().splitlines()
|
|
|
|
status_code = int(lines.pop(0).split()[1])
|
|
|
|
for line in lines:
|
|
|
|
if body == "":
|
|
|
|
body += line
|
|
|
|
continue
|
|
|
|
header_line = line.split(":", 2)
|
|
|
|
if len(header_line) != 2:
|
|
|
|
body = ""
|
|
|
|
continue
|
|
|
|
headers[header_line[0].strip()] = header_line[1].strip()
|
|
|
|
|
|
|
|
|
2014-04-07 21:58:23 +00:00
|
|
|
# response is the string of http body
|
2014-04-08 03:40:03 +00:00
|
|
|
if status == w.WEECHAT_HOOK_PROCESS_ERROR:
|
2014-04-07 21:58:23 +00:00
|
|
|
w.prnt("", "[weebullet] Error sending to pushbullet: %s - %s" % (status, url))
|
|
|
|
return w.WEECHAT_RC_ERROR
|
2014-04-03 19:21:01 +00:00
|
|
|
|
2014-04-08 03:42:40 +00:00
|
|
|
if status_code is 401 or status_code is 403:
|
|
|
|
w.prnt("", "[weebullet] Invalid API Token: %s" % (w.config_get_plugin("api_key")))
|
|
|
|
return w.WEECHAT_RC_ERROR
|
2014-04-08 03:40:03 +00:00
|
|
|
if status_code is not 200:
|
|
|
|
w.prnt("", "[weebullet] Error sending to pushbullet: %s - %s - %s" % (url, status_code, body))
|
|
|
|
return w.WEECHAT_RC_ERROR
|
|
|
|
|
2014-04-07 21:58:23 +00:00
|
|
|
return w.WEECHAT_RC_OK
|
2014-04-03 19:21:01 +00:00
|
|
|
|
2014-04-07 21:58:23 +00:00
|
|
|
def send_push(title, body):
|
|
|
|
apikey = w.config_get_plugin("api_key")
|
|
|
|
apiurl = "https://%s:@api.pushbullet.com/api/pushes" % (apikey)
|
|
|
|
timeout = 20000 # FIXME - actually use config
|
|
|
|
payload = urllib.urlencode({'type': 'note', 'title': title, 'body': body})
|
2014-04-08 03:40:03 +00:00
|
|
|
w.hook_process_hashtable("url:" + apiurl, { "postfields": payload, "header":"1" }, timeout, "process_pushbullet_cb", "")
|
2014-04-03 19:21:01 +00:00
|
|
|
|
2014-04-03 20:51:11 +00:00
|
|
|
def cmd_send_push_note(data, buffer, args):
|
|
|
|
send_push(
|
|
|
|
title="Manual Notification from weechat",
|
|
|
|
body=args
|
|
|
|
)
|
|
|
|
return w.WEECHAT_RC_OK
|
2014-04-03 19:21:01 +00:00
|
|
|
|
|
|
|
def priv_msg_cb(data, bufferp, uber_empty, tagsn, isdisplayed,
|
|
|
|
ishilight, prefix, message):
|
|
|
|
"""Sends highlighted message to be printed on notification"""
|
|
|
|
|
|
|
|
am_away = w.buffer_get_string(bufferp, 'localvar_away')
|
|
|
|
|
|
|
|
if not am_away:
|
2014-04-08 20:46:23 +00:00
|
|
|
# TODO: make debug a configurable
|
|
|
|
#w.prnt("", "[weebullet] Not away, skipping notification")
|
2014-04-03 19:21:01 +00:00
|
|
|
return w.WEECHAT_RC_OK
|
|
|
|
|
|
|
|
notif_body = u"<%s> %s" % (
|
|
|
|
prefix.decode('utf-8'),
|
|
|
|
message.decode('utf-8')
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check that it's in a "/q" buffer and that I'm not the one writing the msg
|
|
|
|
is_pm = w.buffer_get_string(bufferp, "localvar_type") == "private"
|
|
|
|
is_notify_private = re.search(r'(^|,)notify_private(,|$)', tagsn) is not None
|
|
|
|
# PM (query)
|
|
|
|
if (is_pm and is_notify_private):
|
|
|
|
send_push(
|
|
|
|
title="Privmsg from %s" % prefix.decode('utf-8'),
|
|
|
|
body=notif_body
|
|
|
|
)
|
|
|
|
|
|
|
|
# Highlight (your nick is quoted)
|
|
|
|
elif (ishilight == "1"):
|
|
|
|
bufname = (w.buffer_get_string(bufferp, "short_name") or
|
|
|
|
w.buffer_get_string(bufferp, "name"))
|
|
|
|
send_push(
|
|
|
|
title="Highlight in %s" % bufname.decode('utf-8'),
|
|
|
|
body=notif_body
|
|
|
|
)
|
|
|
|
|
|
|
|
return w.WEECHAT_RC_OK
|