all repos — dotfiles @ 2c00620ef710e80a5702621b075274fdfd3a6e2b

my *nix dotfiles

weechat/xnotify.py (view raw)

  1from __future__ import unicode_literals
  2
  3import weechat as weechat
  4import subprocess
  5from os import environ, path
  6
  7xnotify_name = "xnotify"
  8xnotify_version = "0.3.5"
  9xnotify_license = "GPL3"
 10
 11# convenient table checking for bools
 12true = { "on": True, "off": False }
 13
 14# declare this here, will be global config() object
 15# but is initialized in __main__
 16cfg = None
 17
 18class config(object):
 19    def __init__(self):
 20        # default options for xnotify
 21        self.opts = {
 22            "highlight": "on",
 23            "query": "on",
 24            "notify_away": "off",
 25            "icon": "weechat",
 26        }
 27
 28        self.init_config()
 29        self.check_config()
 30
 31    def init_config(self):
 32        for opt, value in self.opts.items():
 33            temp = weechat.config_get_plugin(opt)
 34            if not len(temp):
 35                weechat.config_set_plugin(opt, value)
 36
 37    def check_config(self):
 38        for opt in self.opts:
 39            self.opts[opt] = weechat.config_get_plugin(opt)
 40
 41    def __getitem__(self, key):
 42        return self.opts[key]
 43
 44def printc(msg):
 45    weechat.prnt("", msg)
 46
 47def handle_msg(data, pbuffer, date, tags, displayed, highlight, prefix, message):
 48    highlight = bool(highlight) and cfg["highlight"]
 49    query = true[cfg["query"]]
 50    notify_away = true[cfg["notify_away"]]
 51    buffer_type = weechat.buffer_get_string(pbuffer, "localvar_type")
 52    away = weechat.buffer_get_string(pbuffer, "localvar_away")
 53    x_focus = False
 54    window_name = ""
 55    my_nickname = "nick_" + weechat.buffer_get_string(pbuffer, "localvar_nick")
 56
 57    # Check if active window is in the ignore_windows_list and skip notification
 58    if (environ.get('DISPLAY') != None) and path.isfile("/bin/xdotool"):
 59        cmd_pid="xdotool getactivewindow getwindowpid".split()
 60        window_pid = subprocess.check_output(cmd_pid).decode("utf-8")
 61        cmd_name=("ps -ho comm -p %s"%(window_pid)).split()
 62        window_name = subprocess.check_output(cmd_name).decode("utf-8")
 63        ignore_windows_list = []
 64        if window_name in ignore_windows_list:
 65            x_focus = True
 66            return weechat.WEECHAT_RC_OK
 67
 68    if pbuffer == weechat.current_buffer() and x_focus:
 69        return weechat.WEECHAT_RC_OK
 70
 71    if away and not notify_away:
 72        return weechat.WEECHAT_RC_OK
 73
 74    if my_nickname in tags:
 75        return weechat.WEECHAT_RC_OK
 76
 77    buffer_name = weechat.buffer_get_string(pbuffer, "short_name")
 78
 79
 80    if buffer_type == "private" and query:
 81        notify_user(buffer_name, message)
 82    elif buffer_type == "channel" and highlight:
 83        notify_user("{} @ {}".format(prefix, buffer_name), message)
 84
 85    return weechat.WEECHAT_RC_OK
 86
 87def process_cb(data, command, return_code, out, err):
 88    if return_code == weechat.WEECHAT_HOOK_PROCESS_ERROR:
 89        weechat.prnt("", "Error with command '%s'" % command)
 90    elif return_code != 0:
 91        weechat.prnt("", "return_code = %d" % return_code)
 92        weechat.prnt("", "notify-send has an error")
 93    return weechat.WEECHAT_RC_OK
 94
 95def notify_user(origin, message):
 96    hook = weechat.hook_process_hashtable("notify-send",
 97        { 
 98          "arg1": "WeeChat", "arg2": "",
 99          "arg3": origin + '\t', "arg4": message,
100        },
101        20000, "process_cb", "")
102
103    return weechat.WEECHAT_RC_OK
104
105# execute initializations in order
106if __name__ == "__main__":
107    weechat.register(xnotify_name, "kevr", xnotify_version, xnotify_license,
108        "{} - A libnotify script for weechat".format(xnotify_name), "", "")
109
110    cfg = config()
111    print_hook = weechat.hook_print("", "", "", 1, "handle_msg", "")
112