From 9e225f4668c36962cf68c10f893fd124a5b84fbb Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 18 Jul 2013 07:39:49 -0400 Subject: [PATCH] adding flags. notification linger, quiet and ignore channels --- noti/main.go | 120 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 77 insertions(+), 43 deletions(-) diff --git a/noti/main.go b/noti/main.go index cfe6017..adfd8b2 100644 --- a/noti/main.go +++ b/noti/main.go @@ -3,14 +3,82 @@ package main import ( "bytes" "encoding/json" + "flag" notify "github.com/mqu/go-notify" - zmq "github.com/pebbe/zmq3" + zmq "github.com/pebbe/zmq2" "log" "os" "os/signal" + "strings" "time" ) +func main() { + flag.Parse() + + subscriber, _ := zmq.NewSocket(zmq.SUB) + if len(flag.Args()) == 1 { + subscriber.Connect(flag.Args()[0]) + if !quiet { + log.Printf("Connected to [%s]", flag.Args()[0]) + } + } else { + log.Fatalf("provide the zmq_notify publisher! like tcp://example.com:2428") + } + subscriber.SetSubscribe("") + + for { + msg, err := subscriber.RecvMessage(0) + if err != nil { + break + } + go func() { + noti_msg := IrcNotify{} + json.Unmarshal(bytes.NewBufferString(msg[0]).Bytes(), ¬i_msg) + if !quiet { + log.Printf("%#v", noti_msg) + } + if ShouldIgnore(noti_msg.Channel) { + if !quiet { + log.Printf("Ignoring: %s", noti_msg.Channel) + } + return + } + hello := notify.NotificationNew(noti_msg.Server+","+noti_msg.Channel, + noti_msg.Message, + "") + + if hello == nil { + log.Println("ERROR: Unable to create a new notification") + return + } + notify.NotificationSetTimeout(hello, 0) + + if e := notify.NotificationShow(hello); e != nil && len(e.Message()) > 0 { + log.Printf("ERROR: %s", e.Message()) + return + } + time.Sleep(time.Duration(delay) * time.Second) + notify.NotificationClose(hello) + }() + } +} + +var ( + delay int64 = 5 + quiet bool = false + ignoreChannels string +) + +func ShouldIgnore(channel string) bool { + for _, ignore := range strings.Split(ignoreChannels, ",") { + if strings.Contains(channel, ignore) { + return true + } + } + return false +} + type IrcNotify struct { Highlight bool `json:"highlight"` Type string `json:"type"` @@ -23,6 +91,14 @@ type IrcNotify struct { func init() { notify.Init("IRC-noti") + + flag.Int64Var(&delay, "delay", + delay, "time to let the notification linger") + flag.BoolVar(&quiet, "quiet", + false, "less output") + flag.StringVar(&ignoreChannels, "ignore", + "", "comma seperated list of pattern of channels to ignore") + c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) signal.Notify(c, os.Kill) @@ -35,45 +111,3 @@ func init() { } }() } - -const ( - DELAY = 3000 -) - -func main() { - subscriber, _ := zmq.NewSocket(zmq.SUB) - if len(os.Args) == 2 { - subscriber.Connect(os.Args[1]) - log.Printf("Connected to [%s]", os.Args[1]) - } else { - log.Fatalf("provide the zmq_notify publisher! like tcp://example.com:2428") - } - subscriber.SetSubscribe("") - for { - msg, err := subscriber.RecvMessage(0) - if err != nil { - break - } - go func() { - noti_msg := IrcNotify{} - json.Unmarshal(bytes.NewBufferString(msg[0]).Bytes(), ¬i_msg) - log.Printf("%#v", noti_msg) - hello := notify.NotificationNew(noti_msg.Server + "," + noti_msg.Channel, - noti_msg.Message, - "") - - if hello == nil { - log.Println("ERROR: Unable to create a new notification") - return - } - notify.NotificationSetTimeout(hello, DELAY) - - if e := notify.NotificationShow(hello); e != nil && len(e.Message()) > 0 { - log.Printf("ERROR: %s", e.Message()) - return - } - time.Sleep(DELAY * time.Second) - notify.NotificationClose(hello) - }() - } -}