Script: snotify.py

Play sound when messages are printed in buffers.
Author: Mrokii — Version: 0.1.4 — License: GPL-3.0-or-later
For WeeChat ≥ 0.3.0.
Tags: notify, py2, py3
Added: 2010-10-23 — Updated: 2022-01-25

Download GitHub Repository

  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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Copyright (c) 2010 by Stephan Huebner <s.huebnerfun01@gmx.org>
#
# Intended use:
#
# play a soundfile when a message comes in (for choosable buffers)
#
# TODO: implement playing a soundfile when a highlight occurs
# TODO: (possibly) show the number of unread private messages and
#       highlights in status bar
# TODO: Find out, why sometimes "w.hook_process" doesn't seem to
#       start (or at least no sound is being played)
# TODO: (possibly) Change app-choosing to some predefined options (like
#       mplayer, vlc or ogg123, etc...)
# TODO: Make the whole script more Python-like (if I find out how...)
# TODO: Make the script not beep on message from yourself
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# History:
#  2010-10-17: version 0.1: initial release
#  2010-10-17: version 0.1.1:
#     * change: If a buffer + sound is added and psound is empty, then
#               psound is set to given sound
#     * fix: bugs in "on"- and "off"-functions
#     * fix: "on"- and "off"-statusses were not used
#  2010-11-15: Version 0.1.3
#     * change: No sound is played anymore on messages from yourself.
#     * fix: a new sound for an entry already in the list wasn't set.
#     * change: Replaced "weechat.hook_process" with Pythons' "subprocess.Popen"
#  2022-01-25: Version 0.1.4
#     * fix: replace tabs by spaces for indentation,
#            make script compatible with Python 3

import subprocess as s
from os.path import expanduser

SCR_NAME    = "snotify"
SCR_AUTHOR  = "Stephan Huebner <shuebnerfun01@gmx.org>"
SCR_VERSION = "0.1.4"
SCR_LICENSE = "GPL3"
SCR_DESC    = "Play a soundfile for messages in choosable channels or queries"
SCR_COMMAND = "snotify"

import_ok = True

bfList = []
muted = False

try:
    import weechat as w
except:
    print("Script must be run under weechat. http://www.weechat.org")
    import_ok = False

settings = {
    "player" : "mplayer", # Application used to play a soundfile
    "psound" : "", # Sound that should be played on private messages
    "hsound" : "", # Sound that should be played on highlights
    "buffers" : ""
}

def errMsg(myMsg):
    alert("ERR: " + myMsg)
    return

def fn_hook_process(data, command, rc, stdout, stderr):
    alert(stderr)
    return w.WEECHAT_RC_OK

def fn_privmsg(data, bufferp, time, tags, display, is_hilight, prefix, msg):
    global bfList
    global settings
    servername = (w.buffer_get_string(bufferp, "name").split("."))[0]
    ownNick = w.info_get("irc_nick", servername)
    mySound = ""
    if not muted and prefix != ownNick:
        if settings["player"] == "":
            errMsg("'Player' isn't set!")
        else:
            for lEntry in bfList:
                if lEntry["buffer"] == w.buffer_get_string(bufferp, "name"):
                    # we found a buffer of that name
                    if lEntry["status"] == "on":
                        if lEntry["sound"] == "":
                            if settings["psound"] == "":
                                errMsg("No sound defined! Please set either the " +
                                       "regular 'psound'-option or the 'sound'-" +
                                       "option for this buffer.")
                            else:
                                mySound = settings["psound"]
                        else:
                            mySound = lEntry["sound"]
                        s.Popen([settings["player"], expanduser(mySound)],
                                stderr=s.STDOUT, stdout=s.PIPE)
                    break
    return w.WEECHAT_RC_OK

def fn_command(data, buffer, args):
    global bfList
    global muted
    args = args.split()
    myStatus = "on"
    mySound = ""
    myBuffer = ""
    if len(args)>0:
        try: # set a valid buffer and soundfile
            myArg = args[1]
            myBuffer = w.buffer_search("irc", myArg)
            if myBuffer == "":
                # no buffer with that name, so so assume it's the soundfile
                mySound = myArg
                myBuffer = w.current_buffer()
            else: # we have a buffer, now look for a soundfile
                try:
                    mySound = args[2]
                except:
                    mySound = ""
        except: # no further arguments, so set valid buffer + sound
            myBuffer = w.current_buffer()
            mySound = settings["psound"]
        myBuffer = w.buffer_get_string(myBuffer, "name")
        if args[0] == "test":
            if settings["psound"] == "":
                errMsg("No sound defined! Please set 'psound'-option!")
            else:
                s.Popen([settings["player"], expanduser(mySound)],
                        stderr=s.STDOUT, stdout=s.PIPE)
        elif args[0] == "list":
            for lEntry in bfList:
                alert("BUFFER: " + lEntry["buffer"] + " | " +
                    "STATUS: " + lEntry["status"] + " | " +
                    "SOUND:  " + lEntry["sound"])
            if len(bfList) == 0:
                errMsg("No buffers configured!")
        elif args[0] == "muteall":
            muted = True
            alert("All buffers are muted")
        elif args[0] == "demuteall":
            muted = False
            alert("Sounds will be played")
        elif args[0] == "add":
            for listIndex in range(len(bfList)):
                if bfList[listIndex]["buffer"] == myBuffer:
                    bfList.pop(listIndex)
                    break
            bfList += [{"buffer":myBuffer,"status":myStatus,"sound":mySound}]
            if settings["psound"] == "":
                settings["psound"] = mySound
                w.config_set_plugin("psound", mySound)
            w.config_set_plugin("buffers", fn_createBufferString())
        elif args[0] == "remove":
            for listIndex in range(len(bfList)):
                if bfList[listIndex]["buffer"] == myBuffer:
                    bfList.pop(listIndex)
                    break
            w.config_set_plugin("buffers", fn_createBufferString())
        elif args[0] == "on":
            for listIndex in range(len(bfList)):
                if bfList[listIndex]["buffer"] == myBuffer:
                    bfList[listIndex]["status"] = "on"
                    w.config_set_plugin("buffers", fn_createBufferString())
                    break
        elif args[0] == "off":
            for lIndex in range(len(bfList)):
                if bfList[lIndex]["buffer"] == myBuffer:
                    bfList[lIndex]["status"] = "off"
                    w.config_set_plugin("buffers", fn_createBufferString())
                    break
    return w.WEECHAT_RC_OK

def fn_createBufferString():
    global bfList
    myString = ""
    for lEntry in bfList:
        myString += "{'" + lEntry["buffer"] + "','" + lEntry["status"]
        myString += "','" + lEntry["sound"] + "'}"
    return myString

def alert(myString):
    w.prnt("", myString)
    return

def fn_configchange(data, option, value):
    global settings
    fields = option.split(".")
    myOption = fields[-1]
    try:
        settings[myOption] = value
        #alert("Option {0} has been changed to {1}".format(myOption,
        #            settings[myOption]))
    except KeyError:
        errMsg("There is no option named %s" %myOption)
    return w.WEECHAT_RC_OK

if __name__ == "__main__" and import_ok:
    if w.register(SCR_NAME, SCR_AUTHOR, SCR_VERSION, SCR_LICENSE,
                        SCR_DESC, "", ""):
        # synchronize weechat- and scriptsettings
        for option, default_value in settings.items():
            if not w.config_is_set_plugin(option):
                w.config_set_plugin(option, default_value)
            else:
                settings[option] = w.config_get_plugin(option)
                if option == "buffers":    # need to set buffers seperately
                    myBuffers = settings[option][2:-2]
                    try:
                        myBuffers = myBuffers.split("'}{'")
                        for tmp in myBuffers:
                            myBuffer, myStatus, mySound = tmp.split("','")
                            bfList += [{"buffer":myBuffer,"status":myStatus,
                                        "sound":mySound}]
                    except:
                        myBuffers = ""
        w.hook_print("", "", "", 1, "fn_privmsg", "") # catch prvmsg
        w.hook_config("plugins.var.python." + SCR_NAME + ".*",
                            "fn_configchange", "") # catch configchanges
        w.hook_command( # help-text
            SCR_COMMAND, SCR_DESC,
            "[test] | [add] [buffer] [sound[] | [remove] buffer | " +
            "[on] [buffer] | [off] [buffer] | [muteall] | " +
            "[demuteall] | [list]",
            "Attention:" +
"""
* in places where you can enter a buffername, its best to use the 'Tab'-key
  to cycle through available buffers. If no buffer is chosen at all or the
  buffer doesn't exist, the current buffer will be used.
* You are free to choose whatever player you want (by entering its' name. I
  urge you to *not* enter any command that could be dangerous, as the
  command will be executed just as if it were entered on the commandline.
  The suggestion for common players would be 'mplayer', 'vlc' or 'ogg123';
  without the quotes of course).

Available options are:
- player:  The application used to play a soundfile
- psound:  The soundfile that should be played
- buffers: The saved buffers (it's best to not edit that setting directly.
  Rather choose one of the above mentioned commands).

Commands:
- test:      Test soundplaying with current settings
- add:       add/edit a buffer. If no sound is chosen, the standard on is being
             used (if one is set)
- remove:    Remove a buffer.
- off:       Turn off sound for buffer
- on:        Turn on sound for buffer
- muteall:   Turn off sounds for all buffers
- demuteall: Turn on sounds for all buffers that were activated before
- list:      List all saved buffers""",
            "|| add %(buffers_names)"
            "|| remove %(buffers_names)"
            "|| on %(buffers_names)"
            "|| off %(buffers_names)"
            "mute"
            "muteall", "fn_command", ""
            )