SHARE
TWEET

Untitled

a guest Mar 3rd, 2015 223 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Modified to send notifications over ssh
  2.  
  3. #  Project: lnotify
  4. #  Description: A libnotify script for weechat. Uses
  5. #  subprocess.call to execute notify-send with arguments.
  6. #  Author: kevr <kevr@nixcode.us>
  7. #  License: GPL3
  8. #
  9. # 0.1.2
  10. # added option to display weechat's icon by tomboy64
  11. #
  12. # 0.1.3
  13. # changed the way that icon to WeeChat notification is specified.
  14. # (No absolute path is needed)
  15. # /usr/bin/notify-send isn't needed anymore.
  16. # (pynotify is handling notifications now)
  17. # changed the way that lnotify works. When using gnome 3, every new
  18. # notification was creating a new notification instance. The way that
  19. # it is now, all WeeChat notifications are in a group (that have the
  20. # WeeChat icon and have WeeChat name).
  21. # Got report that it has better look for KDE users too.
  22. #
  23. # 0.1.4
  24. # change hook_print callback argument type of displayed/highlight
  25. # (WeeChat >= 1.0)
  26. #
  27. # 0.2.0
  28. # - changed entire system to hook_process_hashtable calls to notify-send
  29. # - also changed the configuration option names and methods
  30. # Note: If you want pynotify, refer to the 'notify.py' weechat script
  31. import weechat as weechat
  32. import subprocess
  33. import os
  34.  
  35. lnotify_name = "lnotify"
  36. lnotify_version = "0.2.0"
  37. lnotify_license = "GPL3"
  38.  
  39. # convenient table checking for bools
  40. true = { "on": True, "off": False }
  41.  
  42. # declare this here, will be global config() object
  43. # but is initialized in __main__
  44. cfg = None
  45.  
  46. class config(object):
  47.     def __init__(self):
  48.         # default options for lnotify
  49.         self.opts = {
  50.             "highlight": "on",
  51.             "query": "on",
  52.             "notify_away": "off",
  53.             "icon": "weechat",
  54.         }
  55.  
  56.         self.init_config()
  57.         self.check_config()
  58.  
  59.     def init_config(self):
  60.         for opt, value in self.opts.items():
  61.             temp = weechat.config_get_plugin(opt)
  62.             if not len(temp):
  63.                 weechat.config_set_plugin(opt, value)
  64.  
  65.     def check_config(self):
  66.         for opt in self.opts:
  67.             self.opts[opt] = weechat.config_get_plugin(opt)
  68.  
  69.     def __getitem__(self, key):
  70.         return self.opts[key]
  71.  
  72. def printc(msg):
  73.     weechat.prnt("", msg)
  74.  
  75. def handle_msg(data, pbuffer, date, tags, displayed, highlight, prefix, message):
  76.     highlight = bool(highlight) and cfg["highlight"]
  77.     query = true[cfg["query"]]
  78.     notify_away = true[cfg["notify_away"]]
  79.     buffer_type = weechat.buffer_get_string(pbuffer, "localvar_type")
  80.     away = weechat.buffer_get_string(pbuffer, "localvar_away")
  81.  
  82.     if away and not notify_away:
  83.         return weechat.WEECHAT_RC_OK
  84.  
  85.     buffer_name = weechat.buffer_get_string(pbuffer, "short_name")
  86.  
  87.     if buffer_type == "private" and query:
  88.         notify_user(buffer_name, message)
  89.     elif buffer_type == "channel" and highlight:
  90.         notify_user("{} @ {}".format(prefix, buffer_name), message)
  91.  
  92.     return weechat.WEECHAT_RC_OK
  93.  
  94. def process_cb(data, command, return_code, out, err):
  95.     if return_code == weechat.WEECHAT_HOOK_PROCESS_ERROR:
  96.         weechat.prnt("", "Error with command '%s'" % command)
  97.     elif return_code != 0:
  98.         weechat.prnt("", "return_code = %d" % return_code)
  99.         weechat.prnt("", "notify-send has an error")
  100.     return weechat.WEECHAT_RC_OK
  101.  
  102. def notify_user(origin, message):
  103.     FNULL = open(os.devnull, 'w')
  104.     subprocess.Popen(['ssh', 'altair', 'DISPLAY=:0', 'notify-send', '"'+origin+'"', '"'+message+'"'], stdout=FNULL, stderr=FNULL)
  105.     return weechat.WEECHAT_RC_OK
  106.  
  107. # execute initializations in order
  108. if __name__ == "__main__":
  109.     weechat.register(lnotify_name, "kevr", lnotify_version, lnotify_license,
  110.         "{} - A libnotify script for weechat".format(lnotify_name), "", "")
  111.  
  112.     cfg = config()
  113.     print_hook = weechat.hook_print("", "", "", 1, "handle_msg", "")
RAW Paste Data
Want to get better at Python?
Learn to code Python in 2017
Top