woodpecker/plugin/notify/irc.go

86 lines
2.1 KiB
Go
Raw Normal View History

2014-02-07 10:10:01 +00:00
package notify
import (
2014-03-06 18:45:21 +00:00
"fmt"
"github.com/drone/drone/shared/model"
2014-03-06 18:45:21 +00:00
irc "github.com/fluffle/goirc/client"
)
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"`
ClientStarted bool
2014-03-06 18:45:21 +00:00
Client *irc.Conn
}
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
}
func (i *IRC) Send(context *model.Request) 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 *model.Request) error {
2014-06-04 21:25:38 +00:00
msg := fmt.Sprintf(ircStartedMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
i.send(i.Channel, msg)
2014-03-06 18:45:21 +00:00
return nil
}
func (i *IRC) sendFailure(context *model.Request) error {
2014-06-04 21:25:38 +00:00
msg := fmt.Sprintf(ircFailureMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
i.send(i.Channel, msg)
if i.ClientStarted {
i.Client.Quit()
}
2014-03-06 18:45:21 +00:00
return nil
}
func (i *IRC) sendSuccess(context *model.Request) error {
2014-06-04 21:25:38 +00:00
msg := fmt.Sprintf(ircSuccessMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
i.send(i.Channel, msg)
if i.ClientStarted {
2014-03-06 18:45:21 +00:00
i.Client.Quit()
}
2014-03-06 18:45:21 +00:00
return nil
}
func (i *IRC) send(channel string, message string) error {
if !i.ClientStarted {
i.Connect()
}
i.Client.Notice(channel, message)
2014-03-06 18:45:21 +00:00
return nil
}