# This weechat plugin sends OS X notifications for weechat messages
#
# Install terminal-notifier, no other configuration needed.
#
# History:
#
# Version 1.0.0: initial release
# Version 1.0.1: fix escape characters which broke terminal-notifier
# Version 1.0.2: set the nick as the title of the notification
# Version 1.0.3: throttle notification calls


import datetime
import distutils.spawn
import functools
import os
import os.path
import pipes
import weechat


# https://gist.github.com/ChrisTM/5834503
class throttle(object):
    """
    Decorator that prevents a function from being called more than once every
    time period.

    To create a function that cannot be called more than once a minute:

        @throttle(minutes=1)
        def my_fun():
            pass
    """
    def __init__(self, seconds=0, minutes=0, hours=0):
        self.throttle_period = datetime.timedelta(
            seconds=seconds, minutes=minutes, hours=hours
        )
        self.time_of_last_call = datetime.datetime.min

    def __call__(self, fn):
        @functools.wraps(fn)
        def wrapper(*args, **kwargs):
            now = datetime.datetime.now()
            time_since_last_call = now - self.time_of_last_call
            if time_since_last_call > self.throttle_period:
                self.time_of_last_call = now
                return fn(*args, **kwargs)
        return wrapper


def needs_escape(string):
    return string[0] in '[-("'


@throttle(seconds=1)
def notify(data, signal, signal_data):
    separated = signal_data.split("\t")
    try:
        name = separated[0]
    except IndexError:
        name = "WeeChat"

    message = "\t".join(separated[1:])
    if needs_escape(message):
        message = "\\%s" % message

    command = ("terminal-notifier -message %s -title %s -sound Hero"
               % (pipes.quote(message), pipes.quote(name)))
    exit_code = os.system(command)
    if exit_code == 0:
        return weechat.WEECHAT_RC_ERROR
    else:
        return weechat.WEECHAT_RC_OK


def main():
    if distutils.spawn.find_executable("terminal-notifier") is None:
        return weechat.WEECHAT_RC_ERROR

    if not weechat.register("terminal_notifier", "Keith Smiley", "1.0.3", "MIT",
                            "Get OS X notifications for messages", "", ""):
        return weechat.WEECHAT_RC_ERROR

    weechat.hook_signal("weechat_pv", "notify", "")
    weechat.hook_signal("weechat_highlight", "notify", "")

    return weechat.WEECHAT_RC_OK

if __name__ == "__main__":
    main()

@@ -352,102 +352,115 @@
 lls%0A
-%0A%0Aimport datetime%0Aimport distutils.spawn%0Aimport functools%0Aimport os%0Aimport os.path%0Aimport pipe
+# Version 1.1.0: switch to applescript for notifications%0A%0Aimport datetime%0Aimport functools%0Aimport subproces
 s%0Aim
@@ -1401,67 +1401,8 @@
 r%0A%0A%0A
-def needs_escape(string):%0A    return string%5B0%5D in '%5B-(%22'%0A%0A%0A
 @thr
@@ -1510,27 +1510,28 @@
 ry:%0A        
-nam
+titl
 e = separate
@@ -1566,19 +1566,20 @@
         
-nam
+titl
 e = %22Wee
@@ -1632,31 +1632,82 @@
     
-if needs_escape
+notification = %22%22%22display notification %22%25s%22 with title %22%25s%22 %22%22%22 %25 
 (message
 ):%0A 
@@ -1702,18 +1702,17 @@
 (message
-):
+,
 %0A       
@@ -1716,163 +1716,270 @@
     
-message = %22%5C%5C%25s%22 %25 message%0A%0A    command = (%22terminal-notifier -message %25s -title %25s -sound Hero%22%0A               %25 (pipes.quote(message), pipes.quote(name))
+                                                               title)%0A    process = subprocess.Popen(%22osascript -%22.split(), stdin=subprocess.PIPE,%0A                               stdout=subprocess.PIPE, stderr=subprocess.PIPE)%0A    process.communicate(notification
 )%0A  
@@ -1996,25 +1996,21 @@
 e = 
-os.system(command
+process.wait(
 )%0A  
@@ -2064,21 +2064,18 @@
 CHAT_RC_
-ERROR
+OK
 %0A    els
@@ -2111,18 +2111,21 @@
 CHAT_RC_
-OK
+ERROR
 %0A%0A%0Adef m
@@ -2135,118 +2135,8 @@
 ():%0A
-    if distutils.spawn.find_executable(%22terminal-notifier%22) is None:%0A        return weechat.WEECHAT_RC_ERROR%0A%0A
     
@@ -2199,19 +2199,19 @@
 ey%22, %221.
-0.3
+1.0
 %22, %22MIT%22

Switch to applescript for notifications