|
| 1 | +# MIT License |
| 2 | +# |
| 3 | +# Copyright (c) Emma Eilefsen Glenna <[email protected]> (https://eilefsen.net) |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 9 | +# of the Software, and to permit persons to whom the Software is furnished to do so, |
| 10 | +# subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all copies |
| 13 | +# or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 16 | +# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 17 | +# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 18 | +# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 19 | +# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | +# USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | +# |
| 22 | +# ATTRIBUTIONS: |
| 23 | +# |
| 24 | +# This script was made by modifying an older script referenced below: |
| 25 | +# |
| 26 | +# notifications_center (https://github.com/sindresorhus/weechat-notification-center) |
| 27 | +# Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com) |
| 28 | +# included under the MIT license (https://opensource.org/license/mit/) |
| 29 | + |
| 30 | +import datetime |
| 31 | +import weechat |
| 32 | + |
| 33 | + |
| 34 | +SCRIPT_NAME = "kitty_notifications" |
| 35 | +SCRIPT_AUTHOR = "Emma Eilefsen Glenna <[email protected]>" |
| 36 | +SCRIPT_VERSION = "1.0.0" |
| 37 | +SCRIPT_LICENSE = "MIT" |
| 38 | +SCRIPT_DESC = "Pass highlights and private messages as OS notifications via the Kitty terminal (OSC 99)" |
| 39 | + |
| 40 | +weechat.register( |
| 41 | + SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", "" |
| 42 | +) |
| 43 | + |
| 44 | +WEECHAT_VERSION = weechat.info_get("version_number", "") or 0 |
| 45 | +DEFAULT_OPTIONS = { |
| 46 | + "show_highlights": "on", |
| 47 | + "show_private_message": "on", |
| 48 | + "show_message_text": "on", |
| 49 | + "ignore_old_messages": "off", |
| 50 | + "ignore_current_buffer_messages": "off", |
| 51 | + "channels": "", |
| 52 | + "tags": "", |
| 53 | +} |
| 54 | + |
| 55 | +for key, val in DEFAULT_OPTIONS.items(): |
| 56 | + if not weechat.config_is_set_plugin(key): |
| 57 | + weechat.config_set_plugin(key, val) |
| 58 | + |
| 59 | +weechat.hook_print( |
| 60 | + "", "irc_privmsg," + weechat.config_get_plugin("tags"), "", 1, "notify", "" |
| 61 | +) |
| 62 | + |
| 63 | + |
| 64 | +def notify( |
| 65 | + data: str, |
| 66 | + buffer: str, |
| 67 | + date: str, |
| 68 | + tags: str, |
| 69 | + displayed: int, |
| 70 | + highlight: int, |
| 71 | + prefix: str, |
| 72 | + message: str, |
| 73 | +) -> int: |
| 74 | + # Ignore if it's yourself |
| 75 | + own_nick = weechat.buffer_get_string(buffer, "localvar_nick") |
| 76 | + if prefix == own_nick or prefix == ("@%s" % own_nick): |
| 77 | + return weechat.WEECHAT_RC_OK |
| 78 | + |
| 79 | + # Ignore messages from the current buffer |
| 80 | + if ( |
| 81 | + weechat.config_get_plugin("ignore_current_buffer_messages") == "on" |
| 82 | + and buffer == weechat.current_buffer() |
| 83 | + ): |
| 84 | + return weechat.WEECHAT_RC_OK |
| 85 | + |
| 86 | + # Ignore messages older than the configured threshold (such as ZNC logs) if enabled |
| 87 | + if weechat.config_get_plugin("ignore_old_messages") == "on": |
| 88 | + message_time = datetime.datetime.fromtimestamp(int(date)) |
| 89 | + now_time = datetime.datetime.now() |
| 90 | + |
| 91 | + # Ignore if the message is greater than 5 seconds old |
| 92 | + if (now_time - message_time).seconds > 5: |
| 93 | + return weechat.WEECHAT_RC_OK |
| 94 | + |
| 95 | + channel_allow_list = [] |
| 96 | + if weechat.config_get_plugin("channels") != "": |
| 97 | + channel_allow_list = weechat.config_get_plugin("channels").split(",") |
| 98 | + channel = weechat.buffer_get_string(buffer, "localvar_channel") |
| 99 | + |
| 100 | + if channel in channel_allow_list: |
| 101 | + if weechat.config_get_plugin("show_message_text") == "on": |
| 102 | + print_osc99( |
| 103 | + f"{prefix} {channel}", |
| 104 | + message, |
| 105 | + ) |
| 106 | + else: |
| 107 | + print_osc99( |
| 108 | + "Channel Activity", |
| 109 | + f"In {channel} by {prefix}", |
| 110 | + ) |
| 111 | + elif weechat.config_get_plugin("show_highlights") == "on" and int(highlight): |
| 112 | + if weechat.config_get_plugin("show_message_text") == "on": |
| 113 | + print_osc99( |
| 114 | + f"{prefix} {channel}", |
| 115 | + message, |
| 116 | + ) |
| 117 | + else: |
| 118 | + print_osc99( |
| 119 | + "Highlighted Message", |
| 120 | + f"In {channel} by {prefix}", |
| 121 | + ) |
| 122 | + elif ( |
| 123 | + weechat.config_get_plugin("show_private_message") == "on" |
| 124 | + and "irc_privmsg" in tags |
| 125 | + and "notify_private" in tags |
| 126 | + ): |
| 127 | + if weechat.config_get_plugin("show_message_text") == "on": |
| 128 | + print_osc99( |
| 129 | + f"{prefix} [private]", |
| 130 | + message, |
| 131 | + ) |
| 132 | + else: |
| 133 | + print_osc99( |
| 134 | + "Private Message", |
| 135 | + f"From {prefix}", |
| 136 | + ) |
| 137 | + return weechat.WEECHAT_RC_OK |
| 138 | + |
| 139 | + |
| 140 | +def print_osc99( |
| 141 | + title: str, |
| 142 | + body: str, |
| 143 | +) -> None: |
| 144 | + with open("/dev/tty", "w") as tty: |
| 145 | + tty.write(f"\x1b]99;i=1:d=0:p=title;{title}\x1b\\") |
| 146 | + tty.write(f"\x1b]99;i=1:d=1:p=body;{body}\x1b\\") |
0 commit comments