Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gather all names before printing them. #17

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions matterbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/thoj/go-ircevent"
"strconv"
"strings"
"sort"
)

type Bridge struct {
Expand All @@ -17,6 +18,7 @@ type Bridge struct {
cmap map[string]string
ircNick string
ircNickPass string
names []string
*Config
}

Expand Down Expand Up @@ -91,8 +93,7 @@ func (b *Bridge) handleNotice(event *irc.Event) {
}
}

func tableformatter(nicks_s string, nicksPerRow int) string {
nicks := strings.Split(nicks_s, " ")
func tableformatter(nicks []string, nicksPerRow int) string {
result := "|IRC users"
if nicksPerRow < 1 {
nicksPerRow = 4
Expand All @@ -118,11 +119,11 @@ func tableformatter(nicks_s string, nicksPerRow int) string {
return result
}

func plainformatter(nicks string, nicksPerRow int) string {
return nicks + " currently on IRC"
func plainformatter(nicks []string, nicksPerRow int) string {
return strings.Join(nicks, ", ") + " currently on IRC"
}

func (b *Bridge) formatnicks(nicks string) string {
func (b *Bridge) formatnicks(nicks []string) string {
switch b.Config.Mattermost.NickFormatter {
case "table":
return tableformatter(nicks, b.Config.Mattermost.NicksPerRow)
Expand All @@ -131,13 +132,24 @@ func (b *Bridge) formatnicks(nicks string) string {
}
}

func (b *Bridge) storeNames(event *irc.Event) {
b.names = append(b.names, strings.Split(event.Message(), " ")...)
}

func (b *Bridge) endNames(event *irc.Event) {
sort.Strings(b.names)
b.Send(b.ircNick, b.formatnicks(b.names), b.getMMChannel(event.Arguments[0]))
b.names = nil
}

func (b *Bridge) handleOther(event *irc.Event) {
switch event.Code {
case "001":
b.handleNewConnection(event)
case "366":
b.endNames(event)
case "353":
log.Debug("handleOther ", b.getMMChannel(event.Arguments[0]))
b.Send(b.ircNick, b.formatnicks(event.Message()), b.getMMChannel(event.Arguments[0]))
b.storeNames(event)
case "NOTICE":
b.handleNotice(event)
default:
Expand Down Expand Up @@ -218,7 +230,9 @@ func (b *Bridge) handleMatter() {
}
texts := strings.Split(message.Text, "\n")
for _, text := range texts {
b.i.Privmsg(b.getIRCChannel(message.Token), username+text)
channel := b.getIRCChannel(message.Token)
log.Debug("Sending message from " + message.UserName + " to " + channel)
b.i.Privmsg(channel, username+text)
}
}
}
Expand Down