Skip to content

Commit ae15b5e

Browse files
add support for a command to be run after connect, e.g. for identification
1 parent 84eb6cf commit ae15b5e

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

Diff for: conf/config.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ type Cfg struct {
99
}
1010

1111
type Network struct {
12-
Host string `mapstructure:"host"`
13-
Port int `mapstructure:"port"`
14-
SSL bool `mapstructure:"ssl"`
15-
Channels []string `mapstructure:"channels"`
12+
Host string `mapstructure:"host"`
13+
Port int `mapstructure:"port"`
14+
SSL bool `mapstructure:"ssl"`
15+
Channels []string `mapstructure:"channels"`
16+
OnConnect []string `mapstructure:"onConnect"`
1617
}
1718

1819
type Logging struct {

Diff for: gossip/bot.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func New(cfg *conf.Cfg) *Bot {
2727
addr: fmt.Sprintf("%s:%d", cfg.Network.Host, cfg.Network.Port),
2828
nick: cfg.Nick,
2929
msgChan: make(chan *irc.Message),
30-
triggers: []Trigger{pingPong, NewJoin(cfg.Network.Channels), invite, NewPush(cfg), userPingPong, htmlTitle, die, part, rename, karma, toggle, source},
30+
triggers: []Trigger{pingPong, onConnect, NewJoin(cfg.Network.Channels), invite, NewPush(cfg), userPingPong, htmlTitle, die, part, rename, karma, toggle, source},
3131
cfg: cfg,
3232
}
3333
/* Feature todo:

Diff for: gossip/join.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package gossip
22

33
import (
44
"github.com/raidancampbell/gossip/data"
5+
"github.com/sirupsen/logrus"
56
"gopkg.in/sorcix/irc.v2"
67
"sync"
8+
"time"
79
)
810

911
// join desired channels on startup
@@ -31,12 +33,19 @@ func (j joinChannels) Condition(_ *Bot, msg *irc.Message) (shouldApply bool) {
3133

3234
func (j joinChannels) Action(g *Bot, _ *irc.Message) (shouldContinue bool) {
3335
j.o.Do(func() {
34-
for _, chn := range j.channels {
35-
g.msgChan <- &irc.Message{
36-
Command: irc.JOIN,
37-
Params: []string{chn},
36+
go func() {
37+
if len(g.cfg.Network.OnConnect) > 0 {
38+
waitTime := 10 * time.Second
39+
logrus.Infof("waiting %s before joining channels to allow onConnect command to run...", waitTime.String())
40+
time.Sleep(waitTime)
3841
}
39-
}
42+
for _, chn := range j.channels {
43+
g.msgChan <- &irc.Message{
44+
Command: irc.JOIN,
45+
Params: []string{chn},
46+
}
47+
}
48+
}()
4049
})
4150
return true
4251
}

Diff for: gossip/onConnect.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package gossip
2+
3+
import (
4+
"github.com/raidancampbell/gossip/data"
5+
"gopkg.in/sorcix/irc.v2"
6+
"time"
7+
)
8+
9+
var onConnect = &SyncTrigger{
10+
Cond: func(g *Bot, msg *irc.Message) bool {
11+
return msg.Command == irc.RPL_WELCOME
12+
},
13+
Act: func(g *Bot, msg *irc.Message) bool {
14+
time.Sleep(1 * time.Second)
15+
for _, rawCMD := range g.cfg.Network.OnConnect {
16+
g.msgChan <- irc.ParseMessage(rawCMD)
17+
}
18+
return true
19+
},
20+
meta: &data.TriggerMeta{
21+
Disabled: false,
22+
Priority: 0,
23+
Name: "onConnect",
24+
},
25+
}

0 commit comments

Comments
 (0)