summaryrefslogtreecommitdiffstats
path: root/notify.py
Side-by-side diff
Diffstat (limited to 'notify.py') (more/less context) (ignore whitespace changes)
-rw-r--r--notify.py151
1 files changed, 67 insertions, 84 deletions
diff --git a/notify.py b/notify.py
index c8b91e9..5193aaf 100644
--- a/notify.py
+++ b/notify.py
@@ -1,116 +1,99 @@
-# Author: lavaramano <lavaramano AT gmail DOT com>
-# Improved by: BaSh - <bash.lnx AT gmail DOT com>
-# Ported to Weechat 0.3.0 by: Sharn - <sharntehnub AT gmail DOT com)
-# This Plugin Calls the libnotify bindings via python when somebody says your nickname, sends you a query, etc.
-# To make it work, you may need to download: python-notify (and libnotify - libgtk)
-# Requires Weechat 0.3.0
-# Released under GNU GPL v2
-#
-# 2010-09-22, Raghavendra Prabhu
-# Added some stuff.
-# 2010-02-20, Aron Griffis <[email protected]>
-# version 0.0.5: Add nick_separator, don't call show_notification twice on
-# privmsg, fix spelling s/nofify/notify/, use nick as "summary" for privmsg
-# notification, fit in 80 columns, tweak vim modeline.
-# 2010-01-24, David Rubin <[email protected]>
-# version 0.0.4.2 Fixed issue with self notifications when used with out "smart_notification"
-# 2010-01-19, Didier Roche <[email protected]>
-# version 0.0.4.1: add private message sender name
-# 2010-01-19, Didier Roche <[email protected]>
-# version 0.0.4: add smart notification:
-# be notified only if you're not in the current channel/pv window (off by default)
-# 2009-06-16, kba <[email protected]>:
-# version 0.0.3: added config options for icon and urgency
-# 2009-05-02, FlashCode <[email protected]>:
-# version 0.0.2.1: sync with last API changes
-
-import weechat, pynotify, string
-weechat.register("notify", "lavaramano", "0.0.5", "GPL", "notify: A real time notification system for weechat", "", "")
+
+import weechat
+import os, datetime, subprocess, re, sys, shlex
+weechat.register("notify", "lavaramano", "0.0.5", "GPL",
+"notify: A real time notification system for weechat", "", "")
# script options
settings = {
- "show_hilights" : "on",
- "show_priv_msg" : "on",
- "nick_separator" : ": ",
- "icon" : "/usr/share/pixmaps/weechat.xpm",
- "path" : "~/logs/me.log",
- "urgency" : "normal",
- "smart_notification" : "off",
+ "show_hilights" : "on",
+ "show_priv_msg" : "on",
+ "nick_separator" : ": ",
+ "icon" : "/usr/share/pixmaps/weechat.xpm",
+ "path" : "~/logs/me.log",
+ "urgency" : "normal",
+ "smart_notification" : "off",
}
-urgencies = {
- "low" : pynotify.URGENCY_LOW,
- "critical" : pynotify.URGENCY_CRITICAL,
- "normal" : pynotify.URGENCY_NORMAL,
-}
# Init everything
-for option, default_value in settings.items():
+for option, default_value in settings.items():
if weechat.config_get_plugin(option) == "":
- weechat.config_set_plugin(option, default_value)
+ weechat.config_set_plugin(option, default_value)
# Hook privmsg/hilights
-weechat.hook_print("", "irc_privmsg", "", 1, "notify_show", "")
+weechat.hook_print("", "irc_privmsg", "", 1, "notify_show", "")
# Functions
-def notify_show(data, bufferp, uber_empty, tagsn, isdisplayed,
- ishilight, prefix, message):
+def notify_show(data, bufferp, uber_empty, tagsn, isdisplayed,
+ ishilight, prefix, message):
"""Sends highlighted message to be printed on notification"""
try:
- if (weechat.config_get_plugin('smart_notification') == "on" and
- bufferp == weechat.current_buffer() and focussed()):
- pass
+# if (weechat.config_get_plugin('smart_notification') == "on" and
+# bufferp == weechat.current_buffer()):
+ #bufferp == weechat.current_buffer() and focussed()):
+# pass
- elif (weechat.buffer_get_string(bufferp, "localvar_type") == "private" and
- weechat.config_get_plugin('show_priv_msg') == "on"):
- show_notification(prefix, message)
+ if (weechat.buffer_get_string(bufferp, "localvar_type") == "private"
+ and weechat.config_get_plugin('show_priv_msg') == "on"):
+ show_notification(prefix, message)
elif (ishilight == "1" and
weechat.config_get_plugin('show_hilights') == "on"):
- buffer = (weechat.buffer_get_string(bufferp, "short_name") or
- weechat.buffer_get_string(bufferp, "name"))
- show_notification(buffer, prefix +
+ obuffer = (weechat.buffer_get_string(bufferp, "short_name") or
+ weechat.buffer_get_string(bufferp, "name"))
+ show_notification(obuffer, prefix +
weechat.config_get_plugin('nick_separator') + message)
- except ex:
- writetofile(weechat.config_get_plugin('path'),prefix,message,str(ex))
+ except:
+ write_to_file(weechat.config_get_plugin('path'), prefix,
+ 'FAILED:'+message, sys.exc_info()[0])
return weechat.WEECHAT_RC_OK
-def show_notification(chan,message):
- print('\a')
- pynotify.init("wee-notifier")
- writetofile(weechat.config_get_plugin('path'),chan,message)
- wn = pynotify.Notification(chan, message, weechat.config_get_plugin('icon'))
- wn.set_urgency(urgencies[weechat.config_get_plugin('urgency')] or
- pynotify.URGENCY_NORMAL)
- wn.show()
-
-def writetofile(path,chan,message,exception=""):
- import os
- import datetime
+def show_notification(chan, message):
+ """
+ To show notifications with custom notify-send.
+ """
+ chan = "Weechat: "+chan
+ message = "~ "+message+" ~"
+ command=shlex.split("notify-send " + chan + message)
+ try:
+ subprocess.Popen(command)
+ except Exception:
+ pass
+ finally:
+ write_to_file(weechat.config_get_plugin('path'), chan, message)
+
+
+def write_to_file(path, chan, message, exception=""):
+ """
+ Logging the notifications/exceptions to file.
+ """
fullpath = os.path.expanduser(path)
- file = open(fullpath,"a")
- file.writelines(str(datetime.datetime.now().ctime()))
+ xfile = open(fullpath, "a")
+ xfile.writelines(str(datetime.datetime.now().ctime()))
if exception:
- file.writelines("\n\n --------> "+exception+"<------------\n")
- file.writelines(" \n\nWeechat priv:==============>\n")
- file.writelines(str(chan+" : "+message+"\n"))
- file.writelines("<==============\n\n")
- file.close()
+ xfile.writelines("\n\n EXCEPTION: --------> "+str(exception)+"<------------\n")
+ xfile.writelines(" \n\nWeechat priv:==============>\n")
+ xfile.writelines(str(chan+" : "+message+"\n"))
+ xfile.writelines("<==============\n\n")
+ xfile.close()
def focussed():
- import subprocess
- import re
+ """
+ To determine whether notification be sent based on tmux window and current workspace.
+ """
+
# tmux display-message -p and xdotool getactivewindow getwindowpid
- x,y=subprocess.Popen("\
- [[ `xdotool getactivewindow getwindowpid` == `pidof urxvtd` ]];\
- echo -n $?",shell=True,stdout=subprocess.PIPE).communicate()
- if(x == 0):
- current,discard = subprocess.Popen("tmux display-message -p",shell=True\
- ,stdout=subprocess.PIPE).communicate()
- if re.match(".*weechat.*",current):
+ same_workspace, _ = subprocess.Popen("\
+ [[ $(xdotool getactivewindow getwindowpid 2>/dev/null) == $(pidof urxvtd 2>/dev/null) ]];\
+ echo -n $?", shell=True, stdout=subprocess.PIPE).communicate()
+ if(same_workspace == 0):
+ current, _ = subprocess.Popen("tmux display-message -p",
+ shell=True, stdout=subprocess.PIPE).communicate()
+ if re.match(".*weechat.*", current):
return True
return False