Script: rnotify.tcl

Sends highlights to (remote) client using libnotify.
Author: Gotisch — Version: 1.4 — License: GPL-3.0-or-later
For WeeChat ≥ 0.3.0, requires: libnotify.
Tags: notify
Added: 2010-01-06 — Updated: 2015-12-31

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
# Remote Notification Script v1.4
# by Gotisch <gotisch@gmail.com>
#
# With help of this script you can make weechat create notification bubbles
# in ubuntu or any other distribution that supports libnotify.
#
# Changelog:
# 1.4
#		fixed problem with reserved characters preventing notification (see http://wiki.tcl.tk/1353) (thanks Ongy)
# 1.3
#       fixed yet more typos and a possible problem with notifications not showing when they should.
# 1.2
#       fixed small typo that prevented remote notification (thanks Jesse)
# 1.1
#       added setting: privmessage to customize notifications of messages in query
# 1.0
#       initial release
#
# How does it work?
#
# The script inside weechat will either call libnotify directly, or it will
# send the data to the "server" listening on a port which will call the
# libnotify executable and create the notification. This "remote" option
# is the main use of the script.
#
# Example 1:    Weechat runs on the local pc
#       /tcl load rnotify.tcl
#       and set the port
#       /set plugins.var.tcl.rnotify.port local
#
# Example 2:    Weechat runs on a remote pc and you login via ssh port you
#       want to use is 4321
#       sh location/of/rnotify.tcl 4321 & ssh -R 4321:localhost:4321 username@host
#       on server you start weechat (or resume screen or whatever).
#       Then inside weechat
#       /tcl load rnotify.tcl
#       and set the port
#       /set plugins.var.tcl.rnotify.port 4321
#
# General Syntax:
#       In weechat
#       /set plugins.var.tcl.rnotify.port <portnumber to send notifies to/ or local>
#       To get notifications for private messages set:
#       /set plugins.var.tcl.rnotify.privmessage [no(default)|all|inactive]
#           no - no notifications for private messages (besides on highlight)
#           all - notifications for all private messages
#           inactive - notifications only for messages that are not the currently active buffer
#       As script
#       rnotify.tcl <portnumber to listen on>
#       if no port is given it will listen on 1234.
#
# Requirements:
#       libnotify (esp. notify-send executable)
#
# Possible problems:
#       It could be other programs send data to the notify port when using remote
#       mode. This will then lead to the following: either break the script, or
#       make weird notification bubbles.
# \
exec tclsh "$0" ${1+"$@"}

if {[namespace exists ::weechat]} {
	# We have been called inside weechat
	namespace eval weechat::script::rnotify {
		weechat::register "rnotify" {Gotisch gotisch@gmail.com} 1.4 GPL3 {Sends highlights to (remote) client} {} {}
		proc highlight { data buffer date tags displayed highlight prefix message } {
			set buffername [weechat::buffer_get_string $buffer short_name]
			if {$buffername != $prefix} {
				set buffername "$prefix in $buffername"
				if {$highlight == 0} { return $::weechat::WEECHAT_RC_OK }
			} else {
				if {![string equal -nocase [weechat::config_get_plugin privmessage] "all"]} {
					if {![string equal -nocase [weechat::config_get_plugin privmessage] "inactive"] || [string equal [weechat::buffer_get_string [weechat::current_buffer] short_name] $buffername]} {
						if {$highlight == 0} { return $::weechat::WEECHAT_RC_OK }
					}
				}
				set buffername "$prefix in query"
			}
			notify $buffername $message
			return $::weechat::WEECHAT_RC_OK
		}
		proc notify {title text} {
			set title "\u200B$title"
			set text "\u200B$text"
			if {[weechat::config_get_plugin port] == "local"} {
				catch {
					exec notify-send -u normal -c IRC -i gtk-help "$title" "$text"
				}
			} else {
				catch {
					set sock [socket -async localhost [weechat::config_get_plugin port]]
					puts $sock [list normal gtk-help $title $text]
					close $sock
				}
			}
		}
		weechat::hook_print "" "irc_privmsg" "" 1 [namespace current]::highlight {}
	}
} else {
	# We probably have been called from the shell
	set port 1234
	if {[llength $argv] == 1} { set port $argv }
	proc notify_server {port} {
		set s [socket -server accept $port]
		puts "Listening on $port for Connections..."
		vwait forever
	}
	proc accept {sock addr port} {
		fileevent $sock readable [list recieve $sock]
	}
	proc recieve {sock} {
		if {[eof $sock] || [catch {gets $sock line}]} {
			close $sock
		} else {
			foreach {urgency icon title text} $line {
				exec notify-send -u $urgency -c IRC -i $icon "$title" "$text"
			}
		}
	}
	notify_server $port
}