简体   繁体   中英

How to fetch member list of a MUC room with a member using iOS XMPP Robbie Hanson library

I am using the XMPP library of Robby Hanson that is available on git and I am trying to implement MUC or group chat rooms.

I am creating the room using one user, and then try to join, without invite with another user. The problem is, that if I try to connect with another user, not the creator of the room I get the error:

<iq xmlns="jabber:client" type="error" id="A7F05488-4A84-4EC0-8A6C-0F1541690534" from="newroom4@conference.administrator" to="newuser229@administrator/abdbd1bc"><query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="member"/></query><error code="403" type="auth"><forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>

Also I searched the error and I found that error 403 can occur if the user is banned. This is not the case here. So the error occur when I try to fetch room informations like fetchConfigurationForm or fetchMembersList.

So, here is the code that I am using:

- (void)testGroupButtonFunction{
    XMPPRoomMemoryStorage *roomStorage = [[XMPPRoomMemoryStorage alloc] init];
    XMPPJID *roomJID = [XMPPJID jidWithString:@"newRoom4@conference.administrator"];

    xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomStorage
                                                 jid:roomJID                                                    
                                    dispatchQueue:dispatch_get_main_queue()];
    [xmppRoom activate:[self appDelegate].xmppStream];
    [xmppRoom addDelegate:self
            delegateQueue:dispatch_get_main_queue()];
    [xmppRoom joinRoomUsingNickname:[self appDelegate].xmppStream.myJID.user
                            history:nil
                           password:nil];
}

- (void)handleDidJoinRoom:(XMPPRoom *)room withNickname:(NSString *)nickname{

    NSLog(@"handleDidJoinRoom");

}

- (void)handleIncomingMessage:(XMPPMessage *)message room:(XMPPRoom *)room{

    NSLog(@"Incomming message: %@", message.debugDescription);

}

- (void)handleOutgoingMessage:(XMPPMessage *)message room:(XMPPRoom *)room{

    NSLog(@"Outgoing message: %@", message.debugDescription);

}

- (void)xmppRoom:(XMPPRoom *)sender didFetchMembersList:(NSArray *)items{

    NSLog(@"didFetchMembersList: %@", items.debugDescription);

}

- (void)xmppRoom:(XMPPRoom *)sender didNotFetchMembersList:(XMPPIQ *)iqError{

    NSLog(@"didNotFetchMembersList error: %@", iqError.debugDescription);

}

- (void)xmppRoomDidCreate:(XMPPRoom *)sender{

    NSLog(@"xmppRoomDidCreate");

}

- (void)xmppRoom:(XMPPRoom *)sender didConfigure:(XMPPIQ *)iqResult{

    NSLog(@"didConfigure: %@", iqResult.debugDescription);

}

- (void)xmppRoomDidJoin:(XMPPRoom *)sender {

    NSLog(@"xmppRoomDidJoin");
// I use the same code to create or join a room that's why I commented the next line
//    [xmppRoom fetchConfigurationForm];
    //Next line generates the error:
    [xmppRoom fetchMembersList];

}

- (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm{

    NSLog(@"didFetchConfigurationForm");

    NSXMLElement *newConfig = [configForm copy];
    NSArray *fields = [newConfig elementsForName:@"field"];
    for (NSXMLElement *field in fields)
    {
        NSString *var = [field attributeStringValueForName:@"var"];
        NSLog(@"didFetchConfigurationForm: %@", var);
        // Make Room Persistent
        if ([var isEqualToString:@"muc#roomconfig_persistentroom"]) {
            [field removeChildAtIndex:0];
            [field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];
        }
        if ([var isEqualToString:@"muc#roomconfig_roomdesc"]) {
            [field removeChildAtIndex:0];
            [field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"Apple"]];
        }
    }

    [sender configureRoomUsingOptions:newConfig];

}

- (void)xmppRoom:(XMPPRoom *)sender didNotConfigure:(XMPPIQ *)iqResult{

    NSLog(@"didNotConfigure: %@",iqResult.debugDescription);

}

I use the same code to create or join a room that's why I commented the next line:

[xmppRoom fetchConfigurationForm];

Also I want to add that I set:

publicRoom : 1 moderated : 0 membersOnly : 0 canInvite : 1 roomPassword : nil canRegister : 1 canDiscoverJID : 1 logEnabled : 1

Also if I try to send a message from one device, when I retrieve the message on a second device that is logged with another user(that User is not the creator/admin of the group) I see the incoming message in the console using LOG_LEVEL_VERBOSE, but it doesn't call the delegate method. Any idea why the delegate methods are not called? (and I do add XMPPRoomDelegate in .h) Can anyone help me with this errors? Thank you very much in advance for patience and support!

Is is because it does not follow the XEP0045 instructions. You shoud do a category implementing this method:

- (void)joinRoomByContact:(Contact *)contact history:(NSXMLElement *)history
{
    dispatch_block_t block = ^{ @autoreleasepool {

        // Check state and update variables

        // <presence to='darkcave@chat.shakespeare.lit/firstwitch'>
        //   <x xmlns='http://jabber.org/protocol/muc'/>
        //     <history/>
        //     <password>passwd</password>
        //   </x>
        // </presence>

        NSXMLElement *x = [NSXMLElement elementWithName:@"x" xmlns:XMPPMUCNamespace];
        if (history)
        {
            [x addChild:history];
        }

        //XMPPPresence *presence = [XMPPPresence presenceWithType:nil to:myRoomJID];
        XMPPElement * presence = [[XMPPElement alloc] initWithName:@"presence"];
        [presence addAttributeWithName:@"to" stringValue:[NSString stringWithFormat:@"%@/%@",roomJID.bare,contact.name]];
        [presence addAttributeWithName:@"from" stringValue:contact.jid];
        [presence addChild:x];


        [xmppStream sendElement:presence];


        state |= (1 << 3);

    }};

    if (dispatch_get_specific(moduleQueueTag))
        block();
    else
        dispatch_async(moduleQueue, block);
}

I hope that helps you ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM