Skip to content

Commit

Permalink
Ensure prefixcontext/suffixcontent shown for all lines in a multi-lin…
Browse files Browse the repository at this point in the history
…e message
  • Loading branch information
hloeung committed Oct 16, 2022
1 parent 3458a76 commit fed5e54
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 70 deletions.
26 changes: 11 additions & 15 deletions mm-go-irckit/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,24 +441,20 @@ func (ch *channel) Len() int {
}

func (ch *channel) Spoof(from string, text string, cmd string) {
text = wordwrap.String(text, 440)
lines := strings.Split(text, "\n")
for _, l := range lines {
msg := &irc.Message{
Prefix: &irc.Prefix{Name: from, User: from, Host: from},
Command: cmd,
Params: []string{ch.name},
Trailing: l + "\n",
}

ch.mu.RLock()
msg := &irc.Message{
Prefix: &irc.Prefix{Name: from, User: from, Host: from},
Command: cmd,
Params: []string{ch.name},
Trailing: text,
}

for _, to := range ch.usersIdx {
to.Encode(msg)
}
ch.mu.RLock()

ch.mu.RUnlock()
for _, to := range ch.usersIdx {
to.Encode(msg) //nolint:errcheck
}

ch.mu.RUnlock()
}

func (ch *channel) SpoofMessage(from string, text string) {
Expand Down
119 changes: 64 additions & 55 deletions mm-go-irckit/userbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,35 +139,41 @@ func (u *User) handleDirectMessageEvent(event *bridge.DirectMessageEvent) {
}
}

if u.v.GetBool(u.br.Protocol()+".prefixcontext") || u.v.GetBool(u.br.Protocol()+".suffixcontext") {
prefixUser := event.Sender.User
text := wordwrap.String(event.Text, 440)
lines := strings.Split(text, "\n")
for _, text := range lines {
if u.v.GetBool(u.br.Protocol()+".prefixcontext") || u.v.GetBool(u.br.Protocol()+".suffixcontext") {
prefixUser := event.Sender.User

if event.Sender.Me {
prefixUser = event.Receiver.User
}
if event.Sender.Me {
prefixUser = event.Receiver.User
}

prefix := u.prefixContext(prefixUser, event.MessageID, event.ParentID, event.Event)

switch {
case u.v.GetBool(u.br.Protocol()+".prefixcontext") && strings.HasPrefix(event.Text, "\x01"):
event.Text = strings.Replace(event.Text, "\x01ACTION ", "\x01ACTION "+prefix+" ", 1)
case u.v.GetBool(u.br.Protocol() + ".prefixcontext"):
event.Text = prefix + " " + event.Text
case u.v.GetBool(u.br.Protocol()+".suffixcontext") && strings.HasSuffix(event.Text, "\x01"):
event.Text = strings.Replace(event.Text, " \x01", " "+prefix+" \x01", 1)
case u.v.GetBool(u.br.Protocol() + ".suffixcontext"):
event.Text = event.Text + " " + prefix
prefix := u.prefixContext(prefixUser, event.MessageID, event.ParentID, event.Event)

switch {
case u.v.GetBool(u.br.Protocol()+".prefixcontext") && strings.HasPrefix(text, "\x01"):
text = strings.Replace(text, "\x01ACTION ", "\x01ACTION "+prefix+" ", 1)
case u.v.GetBool(u.br.Protocol() + ".prefixcontext"):
text = prefix + " " + text
case u.v.GetBool(u.br.Protocol()+".suffixcontext") && strings.HasSuffix(text, "\x01"):
text = strings.Replace(text, " \x01", " "+prefix+" \x01", 1)
case u.v.GetBool(u.br.Protocol() + ".suffixcontext"):
text = text + " " + prefix
}
}
}

if event.Sender.Me {
if event.Receiver.Me {
u.MsgSpoofUser(u, u.Nick, event.Text)
text += "\n"

if event.Sender.Me {
if event.Receiver.Me {
u.MsgSpoofUser(u, u.Nick, text)
} else {
u.MsgSpoofUser(u, event.Receiver.Nick, text)
}
} else {
u.MsgSpoofUser(u, event.Receiver.Nick, event.Text)
u.MsgSpoofUser(u.createUserFromInfo(event.Sender), u.Nick, text)
}
} else {
u.MsgSpoofUser(u.createUserFromInfo(event.Sender), u.Nick, event.Text)
}

if !u.v.GetBool(u.br.Protocol() + ".disableautoview") {
Expand Down Expand Up @@ -276,25 +282,31 @@ func (u *User) handleChannelMessageEvent(event *bridge.ChannelMessageEvent) {
}
}

if (u.v.GetBool(u.br.Protocol()+".prefixcontext") || u.v.GetBool(u.br.Protocol()+".suffixcontext")) && u.Nick != systemUser {
prefix := u.prefixContext(event.ChannelID, event.MessageID, event.ParentID, event.Event)
switch {
case u.v.GetBool(u.br.Protocol()+".prefixcontext") && strings.HasPrefix(event.Text, "\x01"):
event.Text = strings.Replace(event.Text, "\x01ACTION ", "\x01ACTION "+prefix+" ", 1)
case u.v.GetBool(u.br.Protocol() + ".prefixcontext"):
event.Text = prefix + " " + event.Text
case u.v.GetBool(u.br.Protocol()+".suffixcontext") && strings.HasSuffix(event.Text, "\x01"):
event.Text = strings.Replace(event.Text, " \x01", " "+prefix+" \x01", 1)
case u.v.GetBool(u.br.Protocol() + ".suffixcontext"):
event.Text = event.Text + " " + prefix
text := wordwrap.String(event.Text, 440)
lines := strings.Split(text, "\n")
for _, text := range lines {
if (u.v.GetBool(u.br.Protocol()+".prefixcontext") || u.v.GetBool(u.br.Protocol()+".suffixcontext")) && u.Nick != systemUser {
prefix := u.prefixContext(event.ChannelID, event.MessageID, event.ParentID, event.Event)
switch {
case u.v.GetBool(u.br.Protocol()+".prefixcontext") && strings.HasPrefix(text, "\x01"):
text = strings.Replace(text, "\x01ACTION ", "\x01ACTION "+prefix+" ", 1)
case u.v.GetBool(u.br.Protocol() + ".prefixcontext"):
text = prefix + " " + text
case u.v.GetBool(u.br.Protocol()+".suffixcontext") && strings.HasSuffix(text, "\x01"):
text = strings.Replace(text, " \x01", " "+prefix+" \x01", 1)
case u.v.GetBool(u.br.Protocol() + ".suffixcontext"):
text = text + " " + prefix
}
}
}

switch event.MessageType {
case "notice":
ch.SpoofNotice(nick, event.Text)
default:
ch.SpoofMessage(nick, event.Text)
text += "\n"

switch event.MessageType {
case "notice":
ch.SpoofNotice(nick, text)
default:
ch.SpoofMessage(nick, text)
}
}

if !u.v.GetBool(u.br.Protocol() + ".disableautoview") {
Expand Down Expand Up @@ -832,12 +844,13 @@ func (u *User) addUserToChannelWorker6(channels <-chan *bridge.ChannelInfo, thro
spoof("matterircd", fmt.Sprintf("\x02Replaying since %s\x0f", date))
channame = fmt.Sprintf("#%s", brchannel.Name)
}
logger.Infof("Replaying logs for %s (%s) since %s", brchannel.ID, channame, date)
logger.Infof("Replaying logs for %s (%s) since %s %s", brchannel.ID, channame, date, u.v.GetString(u.br.Protocol()+".threadcontext"))
showReplayHdr = false
}

replayMsg := fmt.Sprintf("[%s] %s", ts.Format("15:04"), post)
if (u.v.GetBool(u.br.Protocol()+".prefixcontext") || u.v.GetBool(u.br.Protocol()+".suffixcontext")) && (u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post") && nick != systemUser {
logger.Infof("p %s r %s end", p.Id, p.RootId)
threadMsgID := u.prefixContext("", p.Id, p.RootId, "")
replayMsg = u.formatContextMessage(ts.Format("15:04"), threadMsgID, post)
}
Expand Down Expand Up @@ -877,20 +890,16 @@ func (u *User) MsgUser(toUser *User, msg string) {
}

func (u *User) MsgSpoofUser(sender *User, rcvuser string, msg string) {
msg = wordwrap.String(msg, 440)
lines := strings.Split(msg, "\n")
for _, l := range lines {
u.Encode(&irc.Message{
Prefix: &irc.Prefix{
Name: sender.Nick,
User: sender.Nick,
Host: sender.Host,
},
Command: irc.PRIVMSG,
Params: []string{rcvuser},
Trailing: l + "\n",
})
}
u.Encode(&irc.Message{
Prefix: &irc.Prefix{
Name: sender.Nick,
User: sender.Nick,
Host: sender.Host,
},
Command: irc.PRIVMSG,
Params: []string{rcvuser},
Trailing: msg,
}) //nolint:errcheck
}

func (u *User) syncChannel(id string, name string) {
Expand Down

0 comments on commit fed5e54

Please sign in to comment.