github.com/kvattikuti/drone@v0.2.1-0.20140603034306-d400229a327a/pkg/plugin/notify/irc.go (about) 1 package notify 2 3 import ( 4 "fmt" 5 6 irc "github.com/fluffle/goirc/client" 7 ) 8 9 const ( 10 ircStartedMessage = "Building: %s, commit %s, author %s" 11 ircSuccessMessage = "Success: %s, commit %s, author %s" 12 ircFailureMessage = "Failed: %s, commit %s, author %s" 13 ) 14 15 type IRC struct { 16 Channel string `yaml:"channel,omitempty"` 17 Nick string `yaml:"nick,omitempty"` 18 Server string `yaml:"server,omitempty"` 19 Started bool `yaml:"on_started,omitempty"` 20 Success bool `yaml:"on_success,omitempty"` 21 Failure bool `yaml:"on_failure,omitempty"` 22 SSL bool `yaml:"ssl,omitempty"` 23 ClientStarted bool 24 Client *irc.Conn 25 } 26 27 func (i *IRC) Connect() { 28 c := irc.SimpleClient(i.Nick) 29 c.SSL = i.SSL 30 connected := make(chan bool) 31 c.AddHandler(irc.CONNECTED, 32 func(conn *irc.Conn, line *irc.Line) { 33 conn.Join(i.Channel) 34 connected <- true 35 }) 36 c.Connect(i.Server) 37 <-connected 38 i.ClientStarted = true 39 i.Client = c 40 } 41 42 func (i *IRC) Send(context *Context) error { 43 switch { 44 case context.Commit.Status == "Started" && i.Started: 45 return i.sendStarted(context) 46 case context.Commit.Status == "Success" && i.Success: 47 return i.sendSuccess(context) 48 case context.Commit.Status == "Failure" && i.Failure: 49 return i.sendFailure(context) 50 } 51 return nil 52 } 53 54 func (i *IRC) sendStarted(context *Context) error { 55 msg := fmt.Sprintf(ircStartedMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author) 56 i.send(i.Channel, msg) 57 return nil 58 } 59 60 func (i *IRC) sendFailure(context *Context) error { 61 msg := fmt.Sprintf(ircFailureMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author) 62 i.send(i.Channel, msg) 63 if i.ClientStarted { 64 i.Client.Quit() 65 } 66 return nil 67 } 68 69 func (i *IRC) sendSuccess(context *Context) error { 70 msg := fmt.Sprintf(ircSuccessMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author) 71 i.send(i.Channel, msg) 72 if i.ClientStarted { 73 i.Client.Quit() 74 } 75 return nil 76 } 77 78 func (i *IRC) send(channel string, message string) error { 79 if !i.ClientStarted { 80 i.Connect() 81 } 82 i.Client.Notice(channel, message) 83 return nil 84 }