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 notify_user(origin, message):
88 notify_cmd = [
89 "notify-send",
90 f"{origin}: {message}"
91 ]
92 try:
93 subprocess.check_call(
94 notify_cmd,
95 stderr=subprocess.STDOUT,
96 stdout=subprocess.DEVNULL,
97 )
98 except Exception as e:
99 weechat.prnt("", e)
100 return weechat.WEECHAT_HOOK_PROCESS_ERROR
101
102 return weechat.WEECHAT_RC_OK
103
104# execute initializations in order
105if __name__ == "__main__":
106 weechat.register(xnotify_name, "icy", xnotify_version, xnotify_license,
107 "{} - xnotify plugin for weechat".format(xnotify_name), "", "")
108
109 cfg = config()
110 print_hook = weechat.hook_print("", "", "", 1, "handle_msg", "")
111