matrix+irc: improve support for IRC NOTICE/[matrix] m.notice

Fixes: #1393.
This commit is contained in:
Simon THOBY
2022-08-14 18:58:54 +02:00
committed by Simon Thoby
parent 09bc5379b7
commit 00b58bce71
10 changed files with 26 additions and 10 deletions

View File

@@ -27,7 +27,7 @@ const (
EventAPIConnected = "api_connected" EventAPIConnected = "api_connected"
EventUserTyping = "user_typing" EventUserTyping = "user_typing"
EventGetChannelMembers = "get_channel_members" EventGetChannelMembers = "get_channel_members"
EventNoticeIRC = "notice_irc" EventNotice = "notice"
) )
const ParentIDNotFound = "msg-parent-not-found" const ParentIDNotFound = "msg-parent-not-found"

View File

@@ -201,7 +201,7 @@ func (b *Birc) handlePrivMsg(client *girc.Client, event girc.Event) {
// set NOTICE event // set NOTICE event
if event.Command == "NOTICE" { if event.Command == "NOTICE" {
rmsg.Event = config.EventNoticeIRC rmsg.Event = config.EventNotice
} }
// strip action, we made an event if it was an action // strip action, we made an event if it was an action

View File

@@ -255,7 +255,7 @@ func (b *Birc) doSend() {
switch msg.Event { switch msg.Event {
case config.EventUserAction: case config.EventUserAction:
b.i.Cmd.Action(msg.Channel, username+msg.Text) b.i.Cmd.Action(msg.Channel, username+msg.Text)
case config.EventNoticeIRC: case config.EventNotice:
b.Log.Debugf("Sending notice to channel %s", msg.Channel) b.Log.Debugf("Sending notice to channel %s", msg.Channel)
b.i.Cmd.Notice(msg.Channel, username+msg.Text) b.i.Cmd.Notice(msg.Channel, username+msg.Text)
default: default:

View File

@@ -150,6 +150,7 @@ func (b *Bmatrix) handleMemberChange(ev *event.Event) {
} }
} }
//nolint: funlen
func (b *Bmatrix) handleMessage(rmsg config.Message, ev *event.Event) { func (b *Bmatrix) handleMessage(rmsg config.Message, ev *event.Event) {
msg := ev.Content.AsMessage() msg := ev.Content.AsMessage()
if msg == nil { if msg == nil {
@@ -169,10 +170,10 @@ func (b *Bmatrix) handleMessage(rmsg config.Message, ev *event.Event) {
rmsg.Avatar = avatarURL rmsg.Avatar = avatarURL
} }
// Do we have a /me action
//nolint: exhaustive //nolint: exhaustive
switch msg.MsgType { switch msg.MsgType {
case event.MsgEmote: case event.MsgEmote:
// Do we have a /me action
rmsg.Event = config.EventUserAction rmsg.Event = config.EventUserAction
case event.MsgImage, event.MsgVideo, event.MsgFile: case event.MsgImage, event.MsgVideo, event.MsgFile:
// Do we have attachments? (we only allow images, videos or files msgtypes) // Do we have attachments? (we only allow images, videos or files msgtypes)
@@ -180,6 +181,9 @@ func (b *Bmatrix) handleMessage(rmsg config.Message, ev *event.Event) {
if err != nil { if err != nil {
b.Log.Errorf("download failed: %#v", err) b.Log.Errorf("download failed: %#v", err)
} }
case event.MsgNotice:
// Support for IRC NOTICE commands/[matrix] m.notice
rmsg.Event = config.EventNotice
default: default:
if msg.RelatesTo == nil { if msg.RelatesTo == nil {
break break

View File

@@ -195,6 +195,7 @@ func (b *Bmatrix) Start() error {
return nil return nil
} }
//nolint: funlen,gocognit,gocyclo
func (b *Bmatrix) Send(msg config.Message) (string, error) { func (b *Bmatrix) Send(msg config.Message) (string, error) {
b.Log.Debugf("=> Sending %#v", msg) b.Log.Debugf("=> Sending %#v", msg)
@@ -305,7 +306,7 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {
} }
// Use notices to send join/leave events // Use notices to send join/leave events
if msg.Event == config.EventJoinLeave { if msg.Event == config.EventJoinLeave || msg.Event == config.EventNotice {
m.MsgType = event.MsgNotice m.MsgType = event.MsgNotice
} else { } else {
m.MsgType = event.MsgText m.MsgType = event.MsgText

View File

@@ -1,3 +1,4 @@
//go:build !noirc
// +build !noirc // +build !noirc
package bridgemap package bridgemap
@@ -8,4 +9,5 @@ import (
func init() { func init() {
FullMap["irc"] = birc.New FullMap["irc"] = birc.New
NoticeSupport["irc"] = struct{}{}
} }

View File

@@ -1,3 +1,4 @@
//go:build !nomatrix
// +build !nomatrix // +build !nomatrix
package bridgemap package bridgemap
@@ -9,4 +10,5 @@ import (
func init() { func init() {
FullMap["matrix"] = bmatrix.New FullMap["matrix"] = bmatrix.New
UserTypingSupport["matrix"] = struct{}{} UserTypingSupport["matrix"] = struct{}{}
NoticeSupport["matrix"] = struct{}{}
} }

View File

@@ -7,4 +7,5 @@ import (
var ( var (
FullMap = map[string]bridge.Factory{} FullMap = map[string]bridge.Factory{}
UserTypingSupport = map[string]struct{}{} UserTypingSupport = map[string]struct{}{}
NoticeSupport = map[string]struct{}{}
) )

View File

@@ -450,11 +450,6 @@ func (gw *Gateway) SendMessage(
} }
} }
// Only send irc notices to irc
if msg.Event == config.EventNoticeIRC && dest.Protocol != "irc" {
return "", nil
}
// Too noisy to log like other events // Too noisy to log like other events
debugSendMessage := "" debugSendMessage := ""
if msg.Event != config.EventUserTyping { if msg.Event != config.EventUserTyping {

View File

@@ -199,6 +199,17 @@ func (gw *Gateway) handleMessage(rmsg *config.Message, dest *bridge.Bridge) []*B
} }
} }
if rmsg.Event == config.EventNotice {
if _, ok := bridgemap.NoticeSupport[dest.Protocol]; !ok {
// we are forced to clone the pointer, as `rmsg` is re-used across calls to handleMessage
newMsg := *rmsg
// if the destination doesn't support IRC/Matrix 'notices',
// emit a normal message
newMsg.Event = ""
rmsg = &newMsg
}
}
// if we have an attached file, or other info // if we have an attached file, or other info
if rmsg.Extra != nil && len(rmsg.Extra[config.EventFileFailureSize]) != 0 && rmsg.Text == "" { if rmsg.Extra != nil && len(rmsg.Extra[config.EventFileFailureSize]) != 0 && rmsg.Text == "" {
return brMsgIDs return brMsgIDs