Skip to content

Commit

Permalink
URL is now notified to slack or takosan when specify @ parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
monochromegane committed Feb 28, 2015
1 parent 1a75a11 commit b9610ad
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
22 changes: 20 additions & 2 deletions cmd/hoi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/exec"
"strings"

flags "github.com/jessevdk/go-flags"
"github.com/monochromegane/hoi"
Expand Down Expand Up @@ -39,11 +40,21 @@ func main() {
os.Exit(1)
}
// make public
args, to := parseArgs(args)
var path string
if abspath, patherr := hoi.TestFile(args[0]); patherr == nil {
hoi.MakePublic(abspath)
path = hoi.MakePublic(abspath)
} else {
hoi.MakeMessage(args)
path = hoi.MakeMessage(args)
}
url := hoi.ToUrl(path)
fmt.Println(url)

// notify
if to != "" {
fmt.Fprint(os.Stderr, hoi.Notify(to, url))
}

// run hoi server as a daemon
runAsDaemon()
}
Expand All @@ -53,3 +64,10 @@ func runAsDaemon() {
cmd := exec.Command(os.Args[0], "--server")
cmd.Start()
}

func parseArgs(args []string) ([]string, string) {
if strings.HasPrefix(args[len(args)-1], "@") {
return args[:len(args)-1], args[len(args)-1]
}
return args, ""
}
17 changes: 13 additions & 4 deletions hoi.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func (h Hoi) TestFile(file string) (string, error) {

func (h Hoi) MakePublic(file string) string {
linked := h.makePublic(file)
h.printUrl(linked)
return linked
}

Expand All @@ -63,7 +62,6 @@ func (h Hoi) makePublic(src string) string {

func (h Hoi) MakeMessage(msgs []string) string {
message := h.makeMessage(msgs)
h.printUrl(message)
return message
}

Expand Down Expand Up @@ -102,9 +100,20 @@ func (h Hoi) Clear() {
}
}

func (h Hoi) printUrl(path string) {
func (h Hoi) ToUrl(path string) string {
server := h.Server()
fmt.Println(server.Url() + "/" + path)
return fmt.Sprintf("%s/%s", server.Url(), path)
}

func (h Hoi) Notify(to, message string) string {
n := NewNotifier(h.config.Notification)
if n == nil {
return ""
}
if err := n.Notify(to, message); err != nil {
return err.Error()
}
return fmt.Sprintf("Message sent successfully to %s\n", to)
}

func publicDir() string {
Expand Down
12 changes: 7 additions & 5 deletions notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/nlopes/slack"
)

const template = "Hi, you got a message from @%s\n%s"

type Notifier interface {
Notify(to, message string) error
}
Expand Down Expand Up @@ -39,13 +41,13 @@ type SlackNotifier struct {
func (s SlackNotifier) Notify(to, message string) error {
_, _, err := s.Client.PostMessage(
to,
message,
fmt.Sprintf(template, s.From, message),
slack.PostMessageParameters{
Username: s.From,
},
)
if err != nil {
return fmt.Errorf("Failed to send message to %s: %s\n", to, err)
return fmt.Errorf("Failed to send message to %s: %s", to, err)
}
return nil
}
Expand All @@ -59,16 +61,16 @@ type TakosanNotifier struct {
func (t TakosanNotifier) Notify(to, message string) error {
res, err := http.PostForm(
fmt.Sprintf("http://%s:%d/privmsg", t.Host, t.Port),
url.Values{"channel": {to}, "message": {message}},
url.Values{"channel": {to}, "message": {fmt.Sprintf(template, t.From, message)}},
)
if err != nil {
return err
}
body, _ := ioutil.ReadAll(res.Body)
defer res.Body.Close()

if res.Status == "400" {
return fmt.Errorf("%s\n", body)
if res.StatusCode == http.StatusBadRequest {
return fmt.Errorf("%s", body)
}
return nil
}

0 comments on commit b9610ad

Please sign in to comment.