2013-07-18 02:26:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2013-07-18 11:39:49 +00:00
|
|
|
"flag"
|
2013-07-18 02:26:55 +00:00
|
|
|
notify "github.com/mqu/go-notify"
|
2013-07-18 11:41:38 +00:00
|
|
|
zmq "github.com/pebbe/zmq3"
|
2013-07-18 02:26:55 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2013-07-18 11:39:49 +00:00
|
|
|
"strings"
|
2013-07-18 02:26:55 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2013-07-18 11:39:49 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2013-07-18 02:26:55 +00:00
|
|
|
subscriber, _ := zmq.NewSocket(zmq.SUB)
|
2013-07-18 11:39:49 +00:00
|
|
|
if len(flag.Args()) == 1 {
|
|
|
|
subscriber.Connect(flag.Args()[0])
|
|
|
|
if !quiet {
|
|
|
|
log.Printf("Connected to [%s]", flag.Args()[0])
|
|
|
|
}
|
2013-07-18 02:26:55 +00:00
|
|
|
} else {
|
2013-07-18 11:39:49 +00:00
|
|
|
log.Fatalf("provide the zmq_notify publisher! like tcp://example.com:2428")
|
2013-07-18 02:26:55 +00:00
|
|
|
}
|
|
|
|
subscriber.SetSubscribe("")
|
2013-07-18 11:39:49 +00:00
|
|
|
|
2013-07-18 02:26:55 +00:00
|
|
|
for {
|
|
|
|
msg, err := subscriber.RecvMessage(0)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
noti_msg := IrcNotify{}
|
|
|
|
json.Unmarshal(bytes.NewBufferString(msg[0]).Bytes(), ¬i_msg)
|
2013-07-18 11:39:49 +00:00
|
|
|
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,
|
|
|
|
"")
|
2013-07-18 02:26:55 +00:00
|
|
|
|
|
|
|
if hello == nil {
|
|
|
|
log.Println("ERROR: Unable to create a new notification")
|
|
|
|
return
|
|
|
|
}
|
2013-07-18 11:39:49 +00:00
|
|
|
notify.NotificationSetTimeout(hello, 0)
|
2013-07-18 02:26:55 +00:00
|
|
|
|
|
|
|
if e := notify.NotificationShow(hello); e != nil && len(e.Message()) > 0 {
|
|
|
|
log.Printf("ERROR: %s", e.Message())
|
|
|
|
return
|
|
|
|
}
|
2013-07-18 11:39:49 +00:00
|
|
|
time.Sleep(time.Duration(delay) * time.Second)
|
2013-07-18 02:26:55 +00:00
|
|
|
notify.NotificationClose(hello)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
2013-07-18 11:39:49 +00:00
|
|
|
|
|
|
|
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"`
|
|
|
|
Channel string `json:"channel"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Server string `json:"server"`
|
|
|
|
Date string `json:"date"`
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
go func() {
|
|
|
|
for sig := range c {
|
|
|
|
// sig is a ^C, handle it
|
|
|
|
log.Printf("captured %v, stopping profiler and exiting..", sig)
|
|
|
|
notify.UnInit()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|