commit c3e6c27

Hubert Hirtz  ·  2020-08-19 20:28:17 +0000 UTC
parent ed39a2a
ui: Notify on highlight (or execute any command)
2 files changed,  +23, -2
M app.go
M app.go
+21, -1
 1@@ -4,6 +4,7 @@ import (
 2 	"crypto/tls"
 3 	"fmt"
 4 	"log"
 5+	"os/exec"
 6 	"strings"
 7 	"time"
 8 
 9@@ -23,7 +24,9 @@ type App struct {
10 }
11 
12 func NewApp(cfg Config) (app *App, err error) {
13-	app = &App{}
14+	app = &App{
15+		cfg: cfg,
16+	}
17 
18 	if cfg.Highlights != nil {
19 		app.highlights = make([]string, len(cfg.Highlights))
20@@ -132,6 +135,7 @@ func (app *App) handleIRCEvent(ev irc.Event) {
21 			app.win.AddLine(ui.Home, l)
22 			app.win.TypingStop(ui.Home, ev.Nick)
23 			app.lastQuery = ev.Nick
24+			app.notifyHighlight("", ev.Nick, ev.Content)
25 		} else if ev.Command == "NOTICE" {
26 			l := ui.LineFromIRCMessage(ev.Time, ev.Nick, ev.Content, true, false)
27 			app.win.AddLine("", l)
28@@ -144,6 +148,9 @@ func (app *App) handleIRCEvent(ev irc.Event) {
29 		l := ui.LineFromIRCMessage(ev.Time, ev.Nick, ev.Content, ev.Command == "NOTICE", isHighlight)
30 		app.win.AddLine(ev.Channel, l)
31 		app.win.TypingStop(ev.Channel, ev.Nick)
32+		if isHighlight {
33+			app.notifyHighlight(ev.Channel, ev.Nick, ev.Content)
34+		}
35 	case irc.QueryTagEvent:
36 		if ev.Typing == irc.TypingActive || ev.Typing == irc.TypingPaused {
37 			app.win.TypingStart(ui.Home, ev.Nick)
38@@ -259,6 +266,19 @@ func (app *App) isHighlight(nick, content string) bool {
39 	return false
40 }
41 
42+func (app *App) notifyHighlight(context, nick, content string) {
43+	sh, err := exec.LookPath("sh")
44+	if err != nil {
45+		return
46+	}
47+	command := app.cfg.OnHighlight
48+	command = strings.Replace(command, "%%", "%", -1)
49+	command = strings.Replace(command, "%c", context, -1)
50+	command = strings.Replace(command, "%n", nick, -1)
51+	command = strings.Replace(command, "%m", content, -1)
52+	exec.Command(sh, "-c", command).Run()
53+}
54+
55 func (app *App) requestHistory() {
56 	if app.s == nil {
57 		return
+2, -1
 1@@ -14,7 +14,8 @@ type Config struct {
 2 	Password *string
 3 
 4 	Highlights   []string
 5-	NickColWidth int `yaml:"nick-column-width"`
 6+	OnHighlight  string `yaml:"on-highlight"`
 7+	NickColWidth int    `yaml:"nick-column-width"`
 8 
 9 	Debug bool
10 }