some accomodations for zmq2 -> zmq3

This commit is contained in:
Vincent Batts 2013-07-18 13:28:44 -04:00
parent a197592e7b
commit a8de7de3ec
4 changed files with 153 additions and 70 deletions

View File

@ -7,8 +7,15 @@ Golang + ZeroMQ + libnotify tool to connect to a zmq_notify.rb weechat script
Install
-------
if you have ZeroMQ v3 installed, run:
go get github.com/vbatts/flaming-happiness/noti
for ZeroMQ v2
go get github.com/vbatts/flaming-happiness/noti2
Running
-------

81
common/common.go Normal file
View File

@ -0,0 +1,81 @@
package common
import (
notify "github.com/mqu/go-notify"
"log"
"os"
"os/signal"
"strings"
"time"
)
var (
ignoreList = []string{}
)
func SetIgnores(ignores string) {
ignoreList = strings.Split(ignores, ",")
}
func Display(noti_msg IrcNotify, linger int64, quiet bool) {
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(linger) * time.Second)
notify.NotificationClose(hello)
}
func ShouldIgnore(channel string) bool {
for _, ignore := range ignoreList {
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")
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)
}
}()
}

View File

@ -4,18 +4,18 @@ import (
"bytes"
"encoding/json"
"flag"
notify "github.com/mqu/go-notify"
zmq "github.com/pebbe/zmq3"
"github.com/vbatts/flaming-happiness/common"
"log"
"os"
"os/signal"
"strings"
"time"
)
func main() {
flag.Parse()
if len(ignoreChannels) > 0 {
common.SetIgnores(ignoreChannels)
}
subscriber, _ := zmq.NewSocket(zmq.SUB)
if len(flag.Args()) == 1 {
subscriber.Connect(flag.Args()[0])
@ -32,82 +32,23 @@ func main() {
if err != nil {
break
}
go func() {
noti_msg := IrcNotify{}
json.Unmarshal(bytes.NewBufferString(msg[0]).Bytes(), &noti_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)
}()
noti_msg := common.IrcNotify{}
json.Unmarshal(bytes.NewBufferString(msg[0]).Bytes(), &noti_msg)
go common.Display(noti_msg, linger, quiet)
}
}
var (
delay int64 = 5
linger 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.Int64Var(&linger, "linger",
linger, "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)
}
}()
}

54
noti2/main.go Normal file
View File

@ -0,0 +1,54 @@
package main
import (
"bytes"
"encoding/json"
"flag"
zmq "github.com/pebbe/zmq2"
"github.com/vbatts/flaming-happiness/common"
"log"
)
func main() {
flag.Parse()
if len(ignoreChannels) > 0 {
common.SetIgnores(ignoreChannels)
}
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
}
noti_msg := common.IrcNotify{}
json.Unmarshal(bytes.NewBufferString(msg[0]).Bytes(), &noti_msg)
go common.Display(noti_msg, linger, quiet)
}
}
var (
linger int64 = 5
quiet bool = false
ignoreChannels string
)
func init() {
flag.Int64Var(&linger, "linger",
linger, "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")
}