File: gntpnotify.py

package info (click to toggle)
weechat-scripts 20180330-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,072 kB
  • sloc: python: 44,904; perl: 27,389; ruby: 2,101; lisp: 339; tcl: 244; sh: 8; makefile: 7
file content (109 lines) | stat: -rw-r--r-- 2,874 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# -*- coding: utf-8 -*-
# Author: Ryan Feng <odayfans at gmail dot com>
# License: GPL3
# Version: 0.2
# Changelog
# 0.2:
# * Fewer error messages

import gntp.notifier as notifier
import weechat
import time

# Logging
def log(msg):
    weechat.prnt("",msg)

# Register plugin
weechat.register("gntpnotify",
             "Ryan Feng",
             "0.2",
             "GPL3",
             "GNTP Notify: Growl notifications using python-gntp",
             "",
             "")

# Create hooks
weechat.hook_signal("*,irc_in2_PRIVMSG","private_msg","")
weechat.hook_signal("weechat_highlight","hilight_msg","")

growl = None
grow_loaded = False
last_log_time = None
log_period = 10 # Redisplay error log in 10s
error_log_max = 3  # Only show no more than 3 error messages in 10s
error_logged = 0

def connect():
    global growl, grow_loaded, log_period, error_log_max, last_log_time, error_logged
    # Create grow object
    growl = notifier.GrowlNotifier(
        applicationName = "Weechat",
        notifications = ["irc message"],
        defaultNotifications = ["irc message"],
        )
    try:
        grow_loaded = growl.register()
    except:
        grow_loaded = False
        if last_log_time is None:
            last_log_time = time.time()
        elapsed_time = time.time() - last_log_time
        if elapsed_time >= log_period:
            error_logged = 0

        if error_logged < error_log_max:
            log("Cannot create notifier object, please make sure Growl has started")
            error_logged += 1
            last_log_time = time.time()

    return grow_loaded

def show_notification(title, message):
    if not grow_loaded:
        connect()
        return
    r = growl.notify(
            noteType = "irc message",
            title = title,
            description = message,
            sticky = False,
            priority = 1
            )
    if not r:
        log("Cannot send notification")

def private_msg(data, signal, message):
    # message is a raw irc message sent from the server
    message = message[1:]

    # Get sender
    sender = message[:message.find('!')-1]

    # Get receiver
    receiver = message.split()[2]

    # Get message body
    msg = message[message.find(':')+1:]

    # Get the server which sent this message
    server = signal.partition(',')[0]

    # Ignore all PRIVMSGs send to a channel,the rest are messages send to 'me'
    if not receiver.startswith('#'):
        show_notification("%s @ #%s" % (sender,server),msg)

    return weechat.WEECHAT_RC_OK

def hilight_msg(data, signal, message):
    # TODO: Get the sender of the message and the channel where the highlight displayed
    sender,_,tmp_msg = message.partition('\t')

    _,_,msg = tmp_msg.partition(' ')

    if grow_loaded:
        show_notification("%s" % sender, msg)
    else:
        connect()
    return weechat.WEECHAT_RC_OK