diff options
author | @42wim <wim@42.be> | 2016-03-21 23:49:52 +0100 |
---|---|---|
committer | @42wim <wim@42.be> | 2016-03-21 23:49:52 +0100 |
commit | 71a504945bac791391d11c022ebc4849e4110a3b (patch) | |
tree | 850465ef5e8a382a860f07156924dee29f007892 | |
parent | 4984473c1bf48bbf1e530eaecba841baca2fe3e6 (diff) | |
parent | 99ac7dc114c7c8efde8a61d465ba388599ebebd5 (diff) | |
download | matterbridge-msglm-71a504945bac791391d11c022ebc4849e4110a3b.tar.gz matterbridge-msglm-71a504945bac791391d11c022ebc4849e4110a3b.tar.bz2 matterbridge-msglm-71a504945bac791391d11c022ebc4849e4110a3b.zip |
Merge pull request #15 from fdevibe/async-connect
Handle connects asynchroneously.
-rw-r--r-- | matterbridge.go | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/matterbridge.go b/matterbridge.go index 4ca90dcf..89c1eb7b 100644 --- a/matterbridge.go +++ b/matterbridge.go @@ -9,20 +9,21 @@ import ( "log" "strconv" "strings" - "time" ) type Bridge struct { - i *irc.Connection - m *matterhook.Client - cmap map[string]string + i *irc.Connection + m *matterhook.Client + cmap map[string]string *Config + ircnick string } func NewBridge(name string, config *Config) *Bridge { b := &Bridge{} b.Config = config b.cmap = make(map[string]string) + b.ircnick = b.Config.IRC.Nick if len(b.Config.Token) > 0 { for _, val := range b.Config.Token { b.cmap[val.IRCChannel] = val.MMChannel @@ -44,12 +45,22 @@ func (b *Bridge) createIRC(name string) *irc.Connection { if b.Config.IRC.Password != "" { i.Password = b.Config.IRC.Password } + i.AddCallback("*", b.handleOther) i.Connect(b.Config.IRC.Server + ":" + strconv.Itoa(b.Config.IRC.Port)) - time.Sleep(time.Second) - log.Println("Joining", b.Config.IRC.Channel, "as", b.Config.IRC.Nick) + return i +} + +func (b *Bridge) handleNewConnection(event *irc.Event) { + b.ircnick = event.Arguments[0] + b.setupChannels() +} + +func (b *Bridge) setupChannels() { + i := b.i + log.Println("Joining", b.Config.IRC.Channel, "as", b.ircnick) i.Join(b.Config.IRC.Channel) for _, val := range b.Config.Token { - log.Println("Joining", val.IRCChannel, "as", b.Config.IRC.Nick) + log.Println("Joining", val.IRCChannel, "as", b.ircnick) i.Join(val.IRCChannel) } i.AddCallback("PRIVMSG", b.handlePrivMsg) @@ -58,8 +69,6 @@ func (b *Bridge) createIRC(name string) *irc.Connection { i.AddCallback("JOIN", b.handleJoinPart) i.AddCallback("PART", b.handleJoinPart) } - i.AddCallback("*", b.handleOther) - return i } func (b *Bridge) handlePrivMsg(event *irc.Event) { @@ -72,8 +81,8 @@ func (b *Bridge) handlePrivMsg(event *irc.Event) { } func (b *Bridge) handleJoinPart(event *irc.Event) { - b.Send(b.Config.IRC.Nick, "irc-"+event.Nick+" "+strings.ToLower(event.Code)+"s "+event.Message(), b.getMMChannel(event.Arguments[0])) - //b.SendType(b.Config.IRC.Nick, "irc-"+event.Nick+" "+strings.ToLower(event.Code)+"s "+event.Message(), b.getMMChannel(event.Arguments[0]), "join_leave") + b.Send(b.ircnick, "irc-"+event.Nick+" "+strings.ToLower(event.Code)+"s "+event.Message(), b.getMMChannel(event.Arguments[0])) + //b.SendType(b.ircnick, "irc-"+event.Nick+" "+strings.ToLower(event.Code)+"s "+event.Message(), b.getMMChannel(event.Arguments[0]), "join_leave") } func tableformatter (nicks_s string, nicksPerRow int) string { @@ -118,9 +127,11 @@ func (b *Bridge) formatnicks (nicks string) string { func (b *Bridge) handleOther(event *irc.Event) { switch event.Code { + case "001": + b.handleNewConnection(event) case "353": log.Println("handleOther", b.getMMChannel(event.Arguments[0])) - b.Send(b.Config.IRC.Nick, b.formatnicks(event.Message()), b.getMMChannel(event.Arguments[0])) + b.Send(b.ircnick, b.formatnicks(event.Message()), b.getMMChannel(event.Arguments[0])) default: log.Printf("got unknown event: %+v\n", event); } @@ -183,7 +194,7 @@ func (b *Bridge) handleMatter() { return case "!gif": message.Text = b.giphyRandom(strings.Fields(strings.Replace(message.Text, "!gif ", "", 1))) - b.Send(b.Config.IRC.Nick, message.Text, b.getIRCChannel(message.Token)) + b.Send(b.ircnick, message.Text, b.getIRCChannel(message.Token)) return } texts := strings.Split(message.Text, "\n") |