2014-02-07 10:10:01 +00:00
|
|
|
package notify
|
2014-02-22 00:10:04 +00:00
|
|
|
|
|
|
|
import (
|
2014-03-06 18:45:21 +00:00
|
|
|
"fmt"
|
2014-02-22 00:10:04 +00:00
|
|
|
|
2014-03-06 18:45:21 +00:00
|
|
|
irc "github.com/fluffle/goirc/client"
|
2014-02-22 00:10:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ircStartedMessage = "Building: %s, commit %s, author %s"
|
|
|
|
ircSuccessMessage = "Success: %s, commit %s, author %s"
|
|
|
|
ircFailureMessage = "Failed: %s, commit %s, author %s"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IRC struct {
|
2014-03-06 18:45:21 +00:00
|
|
|
Channel string `yaml:"channel,omitempty"`
|
|
|
|
Nick string `yaml:"nick,omitempty"`
|
|
|
|
Server string `yaml:"server,omitempty"`
|
|
|
|
Started bool `yaml:"on_started,omitempty"`
|
|
|
|
Success bool `yaml:"on_success,omitempty"`
|
|
|
|
Failure bool `yaml:"on_failure,omitempty"`
|
|
|
|
SSL bool `yaml:"ssl,omitempty"`
|
2014-02-27 20:00:15 +00:00
|
|
|
ClientStarted bool
|
2014-03-06 18:45:21 +00:00
|
|
|
Client *irc.Conn
|
2014-02-22 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IRC) Connect() {
|
2014-03-06 18:45:21 +00:00
|
|
|
c := irc.SimpleClient(i.Nick)
|
|
|
|
c.SSL = i.SSL
|
|
|
|
connected := make(chan bool)
|
|
|
|
c.AddHandler(irc.CONNECTED,
|
|
|
|
func(conn *irc.Conn, line *irc.Line) {
|
|
|
|
conn.Join(i.Channel)
|
|
|
|
connected <- true
|
|
|
|
})
|
|
|
|
c.Connect(i.Server)
|
|
|
|
<-connected
|
|
|
|
i.ClientStarted = true
|
|
|
|
i.Client = c
|
2014-02-22 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IRC) Send(context *Context) error {
|
|
|
|
switch {
|
|
|
|
case context.Commit.Status == "Started" && i.Started:
|
|
|
|
return i.sendStarted(context)
|
|
|
|
case context.Commit.Status == "Success" && i.Success:
|
|
|
|
return i.sendSuccess(context)
|
|
|
|
case context.Commit.Status == "Failure" && i.Failure:
|
|
|
|
return i.sendFailure(context)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IRC) sendStarted(context *Context) error {
|
|
|
|
msg := fmt.Sprintf(ircStartedMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author)
|
2014-02-27 20:00:15 +00:00
|
|
|
i.send(i.Channel, msg)
|
2014-03-06 18:45:21 +00:00
|
|
|
return nil
|
2014-02-22 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IRC) sendFailure(context *Context) error {
|
|
|
|
msg := fmt.Sprintf(ircFailureMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author)
|
2014-02-27 20:00:15 +00:00
|
|
|
i.send(i.Channel, msg)
|
|
|
|
if i.ClientStarted {
|
|
|
|
i.Client.Quit()
|
|
|
|
}
|
2014-03-06 18:45:21 +00:00
|
|
|
return nil
|
2014-02-22 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IRC) sendSuccess(context *Context) error {
|
|
|
|
msg := fmt.Sprintf(ircSuccessMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author)
|
2014-02-27 20:00:15 +00:00
|
|
|
i.send(i.Channel, msg)
|
|
|
|
if i.ClientStarted {
|
2014-03-06 18:45:21 +00:00
|
|
|
i.Client.Quit()
|
2014-02-27 20:00:15 +00:00
|
|
|
}
|
2014-03-06 18:45:21 +00:00
|
|
|
return nil
|
2014-02-22 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IRC) send(channel string, message string) error {
|
2014-02-27 20:00:15 +00:00
|
|
|
if !i.ClientStarted {
|
|
|
|
i.Connect()
|
|
|
|
}
|
|
|
|
i.Client.Notice(channel, message)
|
2014-03-06 18:45:21 +00:00
|
|
|
return nil
|
2014-02-22 00:10:04 +00:00
|
|
|
}
|