Handle MUC PM attempts

This commit is contained in:
Bohdan Horbeshko 2023-09-30 06:29:40 -04:00
parent 02578440cd
commit b8a57c06b6
2 changed files with 47 additions and 25 deletions

View file

@ -44,35 +44,40 @@ var MessageOutgoingPermissionVersion = 0
// SendMessage creates and sends a message stanza // SendMessage creates and sends a message stanza
func SendMessage(to, from, body, id string, component *xmpp.Component, reply *Reply, timestamp int64, isCarbon, isGroupchat bool, originalFrom string) { func SendMessage(to, from, body, id string, component *xmpp.Component, reply *Reply, timestamp int64, isCarbon, isGroupchat bool, originalFrom string) {
sendMessageWrapper(to, from, body, "", id, component, reply, timestamp, "", isCarbon, isGroupchat, false, originalFrom, 0) sendMessageWrapper(to, from, body, "", "", id, component, reply, timestamp, "", isCarbon, isGroupchat, false, originalFrom, 0)
} }
// SendServiceMessage creates and sends a simple message stanza from transport // SendServiceMessage creates and sends a simple message stanza from transport
func SendServiceMessage(to, body string, component *xmpp.Component) { func SendServiceMessage(to, body string, component *xmpp.Component) {
sendMessageWrapper(to, "", body, "", "", component, nil, 0, "", false, false, false, "", 0) sendMessageWrapper(to, "", body, "", "", "", component, nil, 0, "", false, false, false, "", 0)
} }
// SendTextMessage creates and sends a simple message stanza // SendTextMessage creates and sends a simple message stanza
func SendTextMessage(to, from, body string, component *xmpp.Component) { func SendTextMessage(to, from, body string, component *xmpp.Component) {
sendMessageWrapper(to, from, body, "", "", component, nil, 0, "", false, false, false, "", 0) sendMessageWrapper(to, from, body, "", "", "", component, nil, 0, "", false, false, false, "", 0)
} }
// SendErrorMessage creates and sends an error message stanza // SendErrorMessage creates and sends an error message stanza
func SendErrorMessage(to, from, text string, code int, isGroupchat bool, component *xmpp.Component) { func SendErrorMessage(to, from, text string, code int, isGroupchat bool, component *xmpp.Component) {
sendMessageWrapper(to, from, text, "", "", component, nil, 0, "", false, isGroupchat, false, "", code) sendMessageWrapper(to, from, "", "", text, "", component, nil, 0, "", false, isGroupchat, false, "", code)
}
// SendErrorMessageWithBody creates and sends an error message stanza with body payload
func SendErrorMessageWithBody(to, from, body, errorText, id string, code int, isGroupchat bool, component *xmpp.Component) {
sendMessageWrapper(to, from, body, "", errorText, id, component, nil, 0, "", false, isGroupchat, false, "", code)
} }
// SendMessageWithOOB creates and sends a message stanza with OOB URL // SendMessageWithOOB creates and sends a message stanza with OOB URL
func SendMessageWithOOB(to, from, body, id string, component *xmpp.Component, reply *Reply, timestamp int64, oob string, isCarbon, isGroupchat bool, originalFrom string) { func SendMessageWithOOB(to, from, body, id string, component *xmpp.Component, reply *Reply, timestamp int64, oob string, isCarbon, isGroupchat bool, originalFrom string) {
sendMessageWrapper(to, from, body, "", id, component, reply, timestamp, oob, isCarbon, isGroupchat, false, originalFrom, 0) sendMessageWrapper(to, from, body, "", "", id, component, reply, timestamp, oob, isCarbon, isGroupchat, false, originalFrom, 0)
} }
// SendSubjectMessage creates and sends a MUC subject // SendSubjectMessage creates and sends a MUC subject
func SendSubjectMessage(to, from, subject, id string, component *xmpp.Component, timestamp int64) { func SendSubjectMessage(to, from, subject, id string, component *xmpp.Component, timestamp int64) {
sendMessageWrapper(to, from, "", subject, id, component, nil, timestamp, "", false, true, true, "", 0) sendMessageWrapper(to, from, "", subject, "", id, component, nil, timestamp, "", false, true, true, "", 0)
} }
func sendMessageWrapper(to, from, body, subject, id string, component *xmpp.Component, reply *Reply, timestamp int64, oob string, isCarbon, isGroupchat, forceSubject bool, originalFrom string, errorCode int) { func sendMessageWrapper(to, from, body, subject, errorText, id string, component *xmpp.Component, reply *Reply, timestamp int64, oob string, isCarbon, isGroupchat, forceSubject bool, originalFrom string, errorCode int) {
toJid, err := stanza.NewJid(to) toJid, err := stanza.NewJid(to)
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
@ -128,13 +133,12 @@ func sendMessageWrapper(to, from, body, subject, id string, component *xmpp.Comp
Id: id, Id: id,
}, },
Subject: subject, Subject: subject,
Body: body,
} }
if errorCode == 0 { if errorCode != 0 {
message.Body = body
} else {
message.Error = stanza.Err{ message.Error = stanza.Err{
Code: errorCode, Code: errorCode,
Text: body, Text: errorText,
} }
switch errorCode { switch errorCode {
case 400: case 400:
@ -146,6 +150,9 @@ func sendMessageWrapper(to, from, body, subject, id string, component *xmpp.Comp
case 404: case 404:
message.Error.Type = stanza.ErrorTypeCancel message.Error.Type = stanza.ErrorTypeCancel
message.Error.Reason = "item-not-found" message.Error.Reason = "item-not-found"
case 406:
message.Error.Type = stanza.ErrorTypeModify
message.Error.Reason = "not-acceptable"
case 500: case 500:
message.Error.Type = stanza.ErrorTypeWait message.Error.Type = stanza.ErrorTypeWait
message.Error.Reason = "internal-server-error" message.Error.Reason = "internal-server-error"

View file

@ -123,6 +123,26 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
toID, ok := toToID(msg.To) toID, ok := toToID(msg.To)
if ok { if ok {
toJid, err := stanza.NewJid(msg.To)
if err != nil {
log.Error("Invalid to JID!")
return
}
isGroupchat := msg.Type == "groupchat"
if session.Session.MUC && toJid.Resource != "" {
chat, _, err := session.GetContactByID(toID, nil)
if err == nil && session.IsGroup(chat) {
if isGroupchat {
gateway.SendErrorMessageWithBody(msg.From, msg.To, msg.Body, "", msg.Id, 400, true, component)
} else {
gateway.SendErrorMessage(msg.From, msg.To, "PMing room members is not supported, use the real JID", 406, true, component)
}
return
}
}
var reply extensions.Reply var reply extensions.Reply
var fallback extensions.Fallback var fallback extensions.Fallback
var replace extensions.Replace var replace extensions.Replace
@ -134,7 +154,6 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
log.Debugf("replace: %#v", replace) log.Debugf("replace: %#v", replace)
var replyId int64 var replyId int64
var err error
text := msg.Body text := msg.Body
if len(reply.Id) > 0 { if len(reply.Id) > 0 {
chatId, msgId, err := gateway.IdsDB.GetByXmppId(session.Session.Login, bare, reply.Id) chatId, msgId, err := gateway.IdsDB.GetByXmppId(session.Session.Login, bare, reply.Id)
@ -194,7 +213,6 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
return return
} }
} }
isGroupchat := msg.Type == "groupchat"
session.SendMessageLock.Lock() session.SendMessageLock.Lock()
defer session.SendMessageLock.Unlock() defer session.SendMessageLock.Unlock()
@ -215,18 +233,15 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
} }
// pong groupchat messages back // pong groupchat messages back
if isGroupchat { if isGroupchat && toJid.Resource == "" {
toJid, err := stanza.NewJid(msg.To) session.SendMessageToGateway(
if err == nil && toJid.Resource == "" { toID,
session.SendMessageToGateway( tgMessage,
toID, msg.Id,
tgMessage, false,
msg.Id, msg.To + "/" + session.GetMUCNickname(session.GetSenderId(tgMessage)),
false, []string{msg.From},
msg.To + "/" + session.GetMUCNickname(session.GetSenderId(tgMessage)), )
[]string{msg.From},
)
}
} }
} else { } else {
/* /*