/
pushbullet_notify.py
124 lines (115 loc) · 4.69 KB
/
pushbullet_notify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: utf-8 -*-
# Author: Guillermo Castro <github AT codegeek DOT info>
# IRC: CodeGeek AT freenode
#
# This plugin sends push notifications to pushbullet when your away.
# Requires Weechat 0.3.5
# Copyright [2020] [Guillermo Castro]
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# script variables
SCRIPT_NAME = "pushbullet_notify"
SCRIPT_AUTHOR = "Guillermo Castro <github AT codegeek DOT info>"
SCRIPT_VERSION = "0.1"
SCRIPT_LICENSE = "Apache2.0"
SCRIPT_DESCRIPTION = "Weechat push notification to Pushbullet"
PUSHBULLET_URL = "https://api.pushbullet.com/v2/pushes"
# Validate we are running inside of weechat
import_ok = True
try:
import weechat
except ImportError:
print("This script must run under WeeChat.")
import_ok = False
try:
import requests
except ImportError as message:
print("Missing package(s) for %s: %s" % (SCRIPT_NAME, message))
import_ok = False
# script settings
settings = {
"pushbullet_token" : (
"",
"Pushbullet access token (required)"),
"send_highlights" : (
"on",
"send highlighted messages"),
"send_priv_msg" : (
"on"
"send private messages"),
"nick_separator" : (
": ",
"separator to use for messages"),
"ignore_nicks" : (
"*status",
"Nicks to ignore private message from (comma separated)")
}
def message_cb(data, buffer, date, tags, displayed, highlight, prefix, message):
sendstatus = None
# Only send notification when away.
if weechat.buffer_get_string(buffer, 'localvar_away'):
token = weechat.config_get_plugin('pushbullet_token')
if (weechat.buffer_get_string(buffer, "localvar_type") == "private" and weechat.config_get_plugin('send_priv_msg') == "on"):
ignorenicks = weechat.config_get_plugin('ignore_nicks')
if ignorenicks and is_nick_in_list(ignorenicks, prefix):
pass
else:
# title: Private message from <localvar_server>@<prefix>
# body: <message>
title = "Private message from %s@%s" % (weechat.buffer_get_string(buffer, "localvar_server"), prefix)
sendstatus = send_notification(token, title, message)
elif (int(highlight) and weechat.buffer_get_string(buffer, "localvar_type") == "channel" and weechat.config_get_plugin('send_highlights') == "on"):
# title: Message from <localvar_server>@<localvar_channel>
# body: <prefix>: <message>
title = "Message from %s@%s" % (weechat.buffer_get_string(buffer, "localvar_server"), weechat.buffer_get_string(buffer, "localvar_channel"))
body = prefix + weechat.config_get_plugin('nick_separator') + message
sendstatus = send_notification(token, title, body)
# Print status to buffer if push failed
if sendstatus:
weechat.prnt("", sendstatus)
return weechat.WEECHAT_RC_OK
def send_notification(token, title, body):
status = None
if token:
response = requests.post(PUSHBULLET_URL, headers = {
"Access-Token": token
}, json={
"type": "note",
"title": title,
"body": body
})
if not response:
status = "Invalid response: {0}".format(response.reason)
else:
status = "Pushbullet token hasn't been set. Message ignored."
return status
def is_nick_in_list(list, nick):
for i in range(len(list)):
if list[i] == nick:
return True
return False
def script_main():
if not weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESCRIPTION, "", ""):
return
version = weechat.info_get('version_number', '') or 0
for option, value in settings.items():
if not weechat.config_is_set_plugin(option):
weechat.config_set_plugin(option, value[0])
if int(version) >= 0x00030500:
weechat.config_set_desc_plugin(
option, '%s (default: "%s")' % (value[1], value[0]))
weechat.hook_print("", "irc_privmsg", "", 1, "message_cb", "")
if __name__ == "__main__" and import_ok:
script_main()
weechat.prnt("", "Script %s loaded. Make sure config option 'pushbullet_token' is set." % SCRIPT_NAME)