all repos — dotfiles @ 7dd3e2b8bfb50a6e39693915f5926697498a01ed

my *nix dotfiles

bin/elnotify.py (view raw)

  1from __future__ import unicode_literals
  2
  3import weechat as weechat
  4import subprocess
  5from os import environ, path
  6
  7crimnotify_name = "crimnotify"
  8crimnotify_version = "0.1"
  9crimnotify_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 lnotify
 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_name == "#crimson":
 83        notify_user("{}".format(prefix), message)
 84    elif buffer_type == "channel" and highlight:
 85        notify_user("{} @ {}".format(prefix, buffer_name), message)
 86
 87    return weechat.WEECHAT_RC_OK
 88
 89def process_cb(data, command, return_code, out, err):
 90    if return_code == weechat.WEECHAT_HOOK_PROCESS_ERROR:
 91        weechat.prnt("", "Error with command '%s'" % command)
 92    elif return_code != 0:
 93        weechat.prnt("", "return_code = %d" % return_code)
 94        weechat.prnt("", "notify-send has an error")
 95    return weechat.WEECHAT_RC_OK
 96
 97def notify_user(origin, message):
 98    hook = weechat.hook_process_hashtable("notify-send",
 99        { "arg1": "-i", "arg2": cfg["icon"],
100          "arg3": "-a", "arg4": "WeeChat",
101          "arg5": origin, "arg6": message },
102        20000, "process_cb", "")
103
104    return weechat.WEECHAT_RC_OK
105
106# execute initializations in order
107if __name__ == "__main__":
108    weechat.register(crimnotify_name, "kevr", crimnotify_version, crimnotify_license,
109        "{} - A libnotify script for weechat".format(crimnotify_name), "", "")
110
111    cfg = config()
112    print_hook = weechat.hook_print("", "", "", 1, "handle_msg", "")
113