weechatrn.py (2673B)
1 import weechat 2 import json 3 4 weechat.register("WeechatRN", "mhoran", "1.0", "MIT", 5 "WeechatRN push notification plugin", "", "") 6 7 # Plugin options 8 # /set plugins.var.python.weechatrn.push_token 9 script_options = { 10 "push_token": "" 11 } 12 13 for option, default_value in script_options.items(): 14 if weechat.config_is_set_plugin(option): 15 script_options[option] = weechat.config_get_plugin(option) 16 else: 17 weechat.config_set_plugin(option, default_value) 18 19 # Register a custom command so the relay can set the token if the relay is 20 # configured to blacklist certain commands (like /set). 21 def weechatrn_cb(data, buffer, args): 22 weechat.config_set_plugin("push_token", args) 23 return weechat.WEECHAT_RC_OK 24 25 hook = weechat.hook_command("weechatrn", "", "", "", "", "weechatrn_cb", "") 26 27 # Reset in-memory push token on config change. 28 def config_cb(data, option, value): 29 if option == "plugins.var.python.weechatrn.push_token": 30 script_options["push_token"] = value 31 return weechat.WEECHAT_RC_OK 32 33 weechat.hook_config("plugins.var.python.weechatrn.*", "config_cb", "") 34 35 # Only notify for PMs or highlights if message is not tagged with notify_none 36 # (ignores messages from ourselves). 37 def priv_msg_cb(data, buffer, date, tags, displayed, highlight, prefix, 38 message): 39 if "notify_none" in tags.split(","): 40 return weechat.WEECHAT_RC_OK 41 42 body = u"<%s> %s" % (prefix, message) 43 is_pm = weechat.buffer_get_string(buffer, "localvar_type") == "private" 44 if is_pm: 45 send_push(title="Private message from %s" % prefix, body=body) 46 elif int(highlight) and weechat.current_buffer() != buffer: 47 buffer_name = (weechat.buffer_get_string(buffer, "short_name") or 48 weechat.buffer_get_string(buffer, "name")) 49 send_push(title="Highlight in %s" % buffer_name, body=body) 50 51 return weechat.WEECHAT_RC_OK 52 53 weechat.hook_print("", "irc_privmsg", "", 1, "priv_msg_cb", "") 54 55 # Send push notification to Expo server. Message JSON encoded in the format: 56 # { "to": "EXPO_PUSH_TOKEN", 57 # "title": "Notification title", 58 # "body": "Notification body" } 59 def process_expo_cb(data, command, return_code, out, err): 60 return weechat.WEECHAT_RC_OK 61 62 def send_push(title, body): 63 push_token = script_options["push_token"] 64 if push_token == "": 65 return 66 67 post_body = { "to": push_token, "title": title, "body": body } 68 options = { 69 "httpheader": "Content-Type: application/json", 70 "postfields": json.dumps(post_body) } 71 weechat.hook_process_hashtable( 72 "url:https://exp.host/--/api/v2/push/send", 73 options, 20000, "process_expo_cb", "")