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

Remove Emoji from nickname before joining a room #880

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions Extensions/XEP-0045/XMPPRoom.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <unicode/utf8.h>
#import <Foundation/Foundation.h>
#import "XMPP.h"
#import "XMPPRoomMessage.h"
Expand Down Expand Up @@ -317,3 +318,11 @@ static NSString *const XMPPMUCOwnerNamespace = @"http://jabber.org/protocol/muc#
- (void)xmppRoomDidChangeSubject:(XMPPRoom *)sender;

@end


@interface NSString (EmojiExtension)
/**
* Invoked before joining a room. It removes Emoji from desiredNickname.
**/
- (NSString *)stringByRemovingEmoji;
@end
24 changes: 23 additions & 1 deletion Extensions/XEP-0045/XMPPRoom.m
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ - (void)joinRoomUsingNickname:(NSString *)desiredNickname history:(NSXMLElement

// Check state and update variables

if (![self preJoinWithNickname:desiredNickname])
if (![self preJoinWithNickname:[desiredNickname stringByRemovingEmoji]])
{
return;
}
Expand Down Expand Up @@ -1189,3 +1189,25 @@ + (NSXMLElement *)itemWithRole:(NSString *)role jid:(XMPPJID *)jid
}

@end


@implementation NSString (EmojiExtension)

- (NSString *)stringByRemovingEmoji {
NSData *d = [self dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
if(!d) return nil;
const char *buf = d.bytes;
unsigned int len = (unsigned int)[d length];
char *s = (char *)malloc(len);
unsigned int ii = 0, oi = 0; // in index, out index
UChar32 uc;
while (ii < len) {
U8_NEXT_UNSAFE(buf, ii, uc);
if(0x2100 <= uc && uc <= 0x26ff) continue;
if(0x1d000 <= uc && uc <= 0x1f77f) continue;
U8_APPEND_UNSAFE(s, oi, uc);
}
return [[NSString alloc] initWithBytesNoCopy:s length:oi encoding:NSUTF8StringEncoding freeWhenDone:YES];
}

@end
2 changes: 1 addition & 1 deletion Xcode/Examples/DesktopXMPP/MucController.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ - (id)initWithStream:(XMPPStream *)stream roomJID:(XMPPJID *)roomJID
// xmppRoomStorage automatically inherits the delegate(s) of it's parent xmppRoom

[xmppRoom activate:xmppStream];
[xmppRoom joinRoomUsingNickname:@"xmppFrameowrkMucTest" history:nil];
[xmppRoom joinRoomUsingNickname:@"xmppFrameworkMucTest" history:nil];

#if USE_HYBRID_STORAGE
messages = [[NSMutableArray alloc] init];
Expand Down