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

split long message from Discord to IRC #149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
76 changes: 50 additions & 26 deletions bridge/irc_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"regexp"
"strings"
"time"
"math"

"github.com/mozillazg/go-unidecode"
"github.com/pkg/errors"
Expand Down Expand Up @@ -378,6 +379,23 @@ func (m *IRCManager) generateNickname(discord DiscordUser) string {
return newNick
}

// Split a message
func splitByWidthMake(str string, size int) []string {
strLength := len(str)
splitedLength := int(math.Ceil(float64(strLength) / float64(size)))
splited := make([]string, splitedLength)
var start, stop int
for i := 0; i < splitedLength; i += 1 {
start = i * size
stop = start + size
if stop > strLength {
stop = strLength
}
splited[i] = str[start : stop]
}
return splited
}

// SendMessage sends a broken down Discord Message to a particular IRC channel.
func (m *IRCManager) SendMessage(channel string, msg *DiscordMessage) {
if m.ircIgnoredDiscord(msg.Author.ID) {
Expand All @@ -394,12 +412,15 @@ func (m *IRCManager) SendMessage(channel string, msg *DiscordMessage) {
if !ok {
length := len(msg.Author.Username)
for _, line := range strings.Split(content, "\n") {
m.bridge.ircListener.Privmsg(channel, fmt.Sprintf(
"<%s#%s> %s",
msg.Author.Username[:1]+"\u200B"+msg.Author.Username[1:length],
msg.Author.Discriminator,
line,
))
chunks := splitByWidthMake(line, 400)
for _, s := range chunks {
m.bridge.ircListener.Privmsg(channel, fmt.Sprintf(
"<%s#%s> %s",
msg.Author.Username[:1]+"\u200B"+msg.Author.Username[1:length],
msg.Author.Discriminator,
s,
))
}
}
return
}
Expand All @@ -410,29 +431,32 @@ func (m *IRCManager) SendMessage(channel string, msg *DiscordMessage) {
}

for _, line := range strings.Split(content, "\n") {
ircMessage := IRCMessage{
IRCChannel: channel,
Message: line,
IsAction: msg.IsAction,
}
chunks := splitByWidthMake(line, 400)
for _, s := range chunks {
ircMessage := IRCMessage{
IRCChannel: channel,
Message: s,
IsAction: msg.IsAction,
}

if strings.HasPrefix(line, "/me ") && len(line) > 4 {
ircMessage.IsAction = true
ircMessage.Message = line[4:]
}
if strings.HasPrefix(line, "/me ") && len(line) > 4 {
ircMessage.IsAction = true
ircMessage.Message = line[4:]
}

if m.isFilteredDiscordMessage(line) {
continue
}
if m.isFilteredDiscordMessage(line) {
continue
}

select {
// Try to send the message immediately
case con.messages <- ircMessage:
// If it can't after 5ms, do it in a separate goroutine
case <-time.After(time.Millisecond * 5):
go func() {
con.messages <- ircMessage
}()
select {
// Try to send the message immediately
case con.messages <- ircMessage:
// If it can't after 5ms, do it in a separate goroutine
case <-time.After(time.Millisecond * 5):
go func() {
con.messages <- ircMessage
}()
}
}
}
}
Expand Down