Skip to content

Commit

Permalink
Improve bare muc handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tmolitor-stud-tu committed Oct 15, 2024
1 parent 395dfd0 commit eb5da94
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions Monal/Classes/MLMessageProcessor.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ +(MLMessage* _Nullable) processMessage:(XMPPMessage*) messageNode andOuterMessag
}

//ignore messages from our own device, see this github issue: https://github.com/monal-im/Monal/issues/941
if(![messageNode check:@"/<type=groupchat>"] && !isMLhistory && [messageNode.from isEqualToString:account.connectionProperties.identity.fullJid] && [messageNode.toUser isEqualToString:account.connectionProperties.identity.jid])
if(!isMLhistory && [messageNode.from isEqualToString:account.connectionProperties.identity.fullJid] && [messageNode.toUser isEqualToString:account.connectionProperties.identity.jid])
return nil;

//handle incoming jmi calls (TODO: add entry to local history, once the UI for this is implemented)
Expand Down Expand Up @@ -207,8 +207,10 @@ +(MLMessage* _Nullable) processMessage:(XMPPMessage*) messageNode andOuterMessag

//ignore muc PMs (after discussion with holger we don't want to support that)
if(
![messageNode check:@"/<type=groupchat>"] && [messageNode check:@"{http://jabber.org/protocol/muc#user}x"] &&
![messageNode check:@"{http://jabber.org/protocol/muc#user}x/invite"] && [messageNode check:@"body#"]
![messageNode check:@"/<type=groupchat>"] &&
([messageNode check:@"{http://jabber.org/protocol/muc#user}x"] || messageNode.fromResource != nil) &&
![messageNode check:@"{http://jabber.org/protocol/muc#user}x/invite"] &&
[messageNode check:@"body#"]
)
{
DDLogWarn(@"Ignoring muc pm marked as such...");
Expand Down Expand Up @@ -242,7 +244,8 @@ +(MLMessage* _Nullable) processMessage:(XMPPMessage*) messageNode andOuterMessag
DDLogVerbose(@"Not a carbon copy of a muc pm for contact: %@", carbonTestContact);
}

if(([messageNode check:@"/<type=groupchat>"] || [messageNode check:@"{http://jabber.org/protocol/muc#user}x"]) && ![messageNode check:@"{http://jabber.org/protocol/muc#user}x/invite"])

if((possiblyUnknownContact.isMuc || [messageNode check:@"{http://jabber.org/protocol/muc#user}x"]) && ![messageNode check:@"{http://jabber.org/protocol/muc#user}x/invite"])
{
// Ignore all group chat msgs from unkown groups
if(![[[DataLayer sharedInstance] listMucsForAccount:account.accountID] containsObject:messageNode.fromUser])
Expand Down Expand Up @@ -272,9 +275,9 @@ +(MLMessage* _Nullable) processMessage:(XMPPMessage*) messageNode andOuterMessag
//check stanza-id @by according to the rules outlined in XEP-0359
if(!stanzaid)
{
if(![messageNode check:@"/<type=groupchat>"] && [messageNode check:@"{urn:xmpp:sid:0}stanza-id<by=%@>", account.connectionProperties.identity.jid])
if(!possiblyUnknownContact.isMuc && [messageNode check:@"{urn:xmpp:sid:0}stanza-id<by=%@>", account.connectionProperties.identity.jid])
stanzaid = [messageNode findFirst:@"{urn:xmpp:sid:0}stanza-id<by=%@>@id", account.connectionProperties.identity.jid];
else if([messageNode check:@"/<type=groupchat>"] && [messageNode check:@"{urn:xmpp:sid:0}stanza-id<by=%@>", messageNode.fromUser] && [[account.mucProcessor getRoomFeaturesForMuc:messageNode.fromUser] containsObject:@"urn:xmpp:sid:0"])
else if(possiblyUnknownContact.isMuc && [messageNode check:@"{urn:xmpp:sid:0}stanza-id<by=%@>", messageNode.fromUser] && [[account.mucProcessor getRoomFeaturesForMuc:messageNode.fromUser] containsObject:@"urn:xmpp:sid:0"])
stanzaid = [messageNode findFirst:@"{urn:xmpp:sid:0}stanza-id<by=%@>@id", messageNode.fromUser];
}

Expand Down Expand Up @@ -314,9 +317,9 @@ +(MLMessage* _Nullable) processMessage:(XMPPMessage*) messageNode andOuterMessag
NSString* actualFrom = messageNode.fromUser;
NSString* participantJid = nil;
NSString* occupantId = nil;
if([messageNode check:@"/<type=groupchat>"] && messageNode.fromResource)
if(possiblyUnknownContact.isMuc)
{
actualFrom = messageNode.fromResource;
actualFrom = messageNode.fromResource ?: @"";

ownNick = [[DataLayer sharedInstance] ownNickNameforMuc:messageNode.fromUser forAccount:account.accountID];
ownOccupantId = [[DataLayer sharedInstance] getOwnOccupantIdForMuc:messageNode.fromUser onAccountID:account.accountID];
Expand All @@ -334,13 +337,15 @@ +(MLMessage* _Nullable) processMessage:(XMPPMessage*) messageNode andOuterMessag
//mam catchups will contain a muc#user item listing the jid of the participant
//this can't be reconstructed from *current* participant lists because someone new could have taken the same nick
//we don't accept this in non-mam context to make sure this can't be spoofed somehow
//we also don't do that, if this was a message from the bare muc jid
//NOTE: this will override the participantJid extracted using the occupantId above,
//NOTE: but those should ALWAYS be the same (that's the exact purpose of occupant ids)
if([outerMessageNode check:@"{urn:xmpp:mam:2}result"])
participantJid = [messageNode findFirst:@"/<type=groupchat>/{http://jabber.org/protocol/muc#user}x/item@jid"];
if([outerMessageNode check:@"{urn:xmpp:mam:2}result"] && ![@"" isEqualToString:actualFrom])
participantJid = [messageNode findFirst:@"{http://jabber.org/protocol/muc#user}x/item@jid"];

//try to get the jid of the current participant if the occupant-id based approach above did not work
if(![outerMessageNode check:@"{urn:xmpp:mam:2}result"] && occupantId == nil && participantJid == nil)
//but don't do so, if this was a message from the bare muc jid
if(![outerMessageNode check:@"{urn:xmpp:mam:2}result"] && occupantId == nil && participantJid == nil && ![@"" isEqualToString:actualFrom])
{
NSDictionary* mucParticipant = [[DataLayer sharedInstance] getParticipantForNick:actualFrom inRoom:messageNode.fromUser forAccountID:account.accountID];
participantJid = mucParticipant ? mucParticipant[@"participant_jid"] : nil;
Expand Down Expand Up @@ -574,8 +579,8 @@ +(MLMessage* _Nullable) processMessage:(XMPPMessage*) messageNode andOuterMessag
else if([lowercaseBody hasPrefix:@"https://"])
messageType = kMessageTypeUrl;
}
//messages from room jids are classified as status messages
if([messageNode check:@"/<type=groupchat>"] && !messageNode.fromResource)
//messages from the bare muc jid are classified as status messages
if(possiblyUnknownContact.isMuc && [@"" isEqualToString:actualFrom])
messageType = kMessageTypeStatus;
DDLogInfo(@"Got message of type: %@", messageType);

Expand Down

0 comments on commit eb5da94

Please sign in to comment.