Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hattya committed Aug 9, 2014
1 parent bcfd2f6 commit 8adb408
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -5,4 +5,4 @@ before_install:
- go get github.com/axw/gocov/gocov
- go get github.com/mattn/goveralls
script:
- $HOME/gopath/bin/goveralls kjg9f8xjiJvCnPuyLhIwZa14jIMlOeBV4
- $HOME/gopath/bin/goveralls -repotoken kjg9f8xjiJvCnPuyLhIwZa14jIMlOeBV4
21 changes: 11 additions & 10 deletions gntp-send/gntp-send.go
Expand Up @@ -4,12 +4,13 @@ import (
"bufio"
"flag"
"fmt"
"github.com/mattn/go-gntp"
"os"
"strings"

"github.com/mattn/go-gntp"
)

const DEFAULT_NOTIFY_NAME = "default"
const DefaultNotifyName = "default"

func main() {
server := flag.String("s", "localhost:23053", "growl server(host:port)")
Expand All @@ -19,16 +20,16 @@ func main() {
encryptAlgorithm := flag.String("ea", "AES", "encrypt algorithm")
displayName := flag.String("d", "", "display name")
password := flag.String("p", "", "password")
event := flag.String("e", DEFAULT_NOTIFY_NAME, "event")
read_stdin := flag.Bool("i", false, "read from stdin")
event := flag.String("e", DefaultNotifyName, "event")
readStdin := flag.Bool("i", false, "read from stdin")
flag.Parse()
if !*read_stdin && flag.NArg() < 2 {
if !*readStdin && flag.NArg() < 2 {
fmt.Fprintf(os.Stderr, "usage: gntp-send [options] title message [icon] [url]\n")
flag.PrintDefaults()
return
}
var title, message, icon, url string
if *read_stdin {
if *readStdin {
stdin := bufio.NewReader(os.Stdin)
title, _ = stdin.ReadString('\n')
message, _ = stdin.ReadString(0)
Expand All @@ -50,21 +51,21 @@ func main() {
client.HashAlgorithm = *hashAlgorithm
client.EncryptAlgorithm = *encryptAlgorithm

n := []gntp.Notification{{DEFAULT_NOTIFY_NAME, *displayName, true}}
if *event != DEFAULT_NOTIFY_NAME {
n := []gntp.Notification{{DefaultNotifyName, *displayName, true}}
if *event != DefaultNotifyName {
n = append(n, gntp.Notification{*event, *event, true})
}
var err error
if *noRegister == false {
err = client.Register(n)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
fmt.Fprintln(os.Stderr, err)
return
}
}
err = client.Notify(&gntp.Message{*event, title, message, icon, url, *displayName})
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
fmt.Fprintln(os.Stderr, err)
return
}
}
71 changes: 35 additions & 36 deletions gntp.go
@@ -1,6 +1,7 @@
package gntp

import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/des"
Expand Down Expand Up @@ -41,8 +42,8 @@ type Message struct {

func makeRand(size int) []byte {
r := make([]byte, size)
for n := 0; n < len(r); n++ {
r[n] = uint8(rand.Int() % 256)
for i := range r {
r[i] = byte(rand.Int() % 256)
}
return r
}
Expand All @@ -54,8 +55,8 @@ func sanitize(str string) string {
func makeSalt(size int) []byte {
s := make([]byte, size)
cc := "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
for n := 0; n < len(s); n++ {
s[n] = uint8(cc[rand.Int()%len(cc)])
for i := range s {
s[i] = byte(cc[rand.Int()%len(cc)])
}
return s
}
Expand Down Expand Up @@ -156,12 +157,11 @@ func (c *Client) send(method string, stm []byte) (ret []byte, err error) {
return nil, errors.New("unknown encrypt algorithm")
}

conn.Write([]byte("GNTP/1.0 " + method + " " + encHdr + " " + hashHdr + "\r\n"))
conn.Write([]byte(fmt.Sprintf("GNTP/1.0 %s %s %s\r\n", method, encHdr, hashHdr)))
conn.Write(out)
conn.Write([]byte("\r\n\r\n"))
} else {
conn.Write([]byte(
"GNTP/1.0 " + method + " NONE\r\n" + string(stm) + "\r\n"))
conn.Write([]byte(fmt.Sprintf("GNTP/1.0 %s NONE\r\n%s\r\n", method, string(stm))))
}
return ioutil.ReadAll(conn)
}
Expand All @@ -171,22 +171,22 @@ func NewClient() *Client {
}

func (c *Client) Register(n []Notification) error {
s := fmt.Sprintf(
"Application-Name: %s\r\n"+
"Notifications-Count: %d\r\n\r\n", sanitize(c.AppName), len(n))
data := new(bytes.Buffer)
fmt.Fprintf(data, "Application-Name: %s\r\n", sanitize(c.AppName))
fmt.Fprintf(data, "Notifications-Count: %d\r\n\r\n", len(n))
for _, i := range n {
s += "Notification-Name: " + sanitize(i.Event) + "\r\n" +
"Notification-Display-Name: " + sanitize(i.DisplayName) + "\r\n" +
"Notification-Enabled: True\r\n\r\n"
fmt.Fprintf(data, "Notification-Name: %s\r\n", sanitize(i.Event))
fmt.Fprintf(data, "Notification-Display-Name: %s\r\n", sanitize(i.DisplayName))
fmt.Fprintf(data, "Notification-Enabled: True\r\n\r\n")
}
b, err := c.send("REGISTER", []byte(s))
b, err := c.send("REGISTER", data.Bytes())
if err == nil {
res := string(b)
if len(res) > 15 && res[0:15] == "GNTP/1.0 -ERROR" {
if strings.HasPrefix(res, "GNTP/1.0 -ERROR") {
lines := strings.Split(res, "\r\n")
for n := range lines {
if len(lines[n]) > 18 && lines[n][0:18] == "Error-Description:" {
err = errors.New(lines[n][19:])
for _, l := range lines {
if strings.HasPrefix(l, "Error-Description:") {
err = errors.New(l[19:])
break
}
}
Expand All @@ -202,29 +202,28 @@ func (c *Client) Notify(m *Message) error {
ha.Write(identify)
m.Icon = fmt.Sprintf("x-growl-resource://%X", ha.Sum(nil))
}
data := []byte(
"Application-Name: "+sanitize(c.AppName)+"\r\n"+
"Notification-Name: "+sanitize(m.Event)+"\r\n"+
"Notification-Title: "+sanitize(m.Title)+"\r\n"+
"Notification-Text: "+sanitize(m.Text)+"\r\n"+
"Notification-Icon: "+sanitize(m.Icon)+"\r\n"+
"Notification-Callback-Target: "+sanitize(m.Callback)+"\r\n"+
"Notification-Display-Name: "+sanitize(m.DisplayName)+"\r\n\r\n")
data := new(bytes.Buffer)
fmt.Fprintf(data, "Application-Name: %s\r\n", sanitize(c.AppName))
fmt.Fprintf(data, "Notification-Name: %s\r\n", sanitize(m.Event))
fmt.Fprintf(data, "Notification-Title: %s\r\n", sanitize(m.Title))
fmt.Fprintf(data, "Notification-Text: %s\r\n", sanitize(m.Text))
fmt.Fprintf(data, "Notification-Icon: %s\r\n", sanitize(m.Icon))
fmt.Fprintf(data, "Notification-Callback-Target: %s\r\n", sanitize(m.Callback))
fmt.Fprintf(data, "Notification-Display-Name: %s\r\n\r\n", sanitize(m.DisplayName))
if len(identify) > 0 {
data = append(data, []byte(
"Identifier: " + string(m.Icon[19:]) + "\r\n"+
fmt.Sprintf("Length: %d\r\n\r\n", len(identify)))...)
data = append(data, identify...)
data = append(data, []byte("\r\n\r\n")...)
fmt.Fprintf(data, "Identifier: %s\r\n", m.Icon[19:])
fmt.Fprintf(data, "Length: %d\r\n\r\n", len(identify))
data.Write(identify)
data.Write([]byte("\r\n\r\n"))
}
b, err := c.send("NOTIFY", data)
b, err := c.send("NOTIFY", data.Bytes())
if err == nil {
res := string(b)
if res[0:15] == "GNTP/1.0 -ERROR" {
if strings.HasPrefix(res, "GNTP/1.0 -ERROR") {
lines := strings.Split(res, "\r\n")
for n := range lines {
if len(lines[n]) > 18 && lines[n][0:18] == "Error-Description:" {
err = errors.New(lines[n][19:])
for _, l := range lines {
if strings.HasPrefix(l, "Error-Description:") {
err = errors.New(l[19:])
break
}
}
Expand Down

0 comments on commit 8adb408

Please sign in to comment.