XMPPFramework - 检索Openfire消息档案 [英] XMPPFramework - Retrieving Openfire Message Archives

查看:219
本文介绍了XMPPFramework - 检索Openfire消息档案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

花了好几个小时试图解决这个问题,我很难过!

Spent hours trying to solve this problem and I'm stumped!

试图抓住我的OpenFire服务器上2个用户之间的聊天记录,我读到了我的插件需要这样做。

Trying to grab the Chat History between 2 users on my OpenFire server and I read that I plugin was needed to do this.

所以,我在OpenFire服务器上安装了Open Archive插件并发送以下XML(根据XMPP-0136协议文档) :

So, I installed the 'Open Archive' plugin on my OpenFire server and send the following XML (as per the XMPP-0136 protocol documentation):

<iq type="get" id="page1">
   <retrieve xmlns="urn:xmpp:archive" with="username@server.com" start="1469-07-21T02:56:15Z">
      <set xmlns="http://jabber.org/protocol/rsm">
         <max>100</max>
      </set>
   </retrieve>
</iq>

在代码中,这是通过以下方式实现的:

In code, this is achieved via the following:

NSXMLElement *iQ = [NSXMLElement elementWithName:@"iq"];
[iQ addAttributeWithName:@"type" stringValue:@"get"];
[iQ addAttributeWithName:@"id" stringValue:@"page1"];

NSXMLElement *retrieve = [NSXMLElement elementWithName:@"retrieve"];
[retrieve addAttributeWithName:@"xmlns" stringValue:@"urn:xmpp:archive"];
[retrieve addAttributeWithName:@"with" stringValue:@"username@server.com"];
[retrieve addAttributeWithName:@"start" stringValue:@"1469-07-21T02:56:15Z"];

NSXMLElement *set = [NSXMLElement elementWithName:@"set"];
[set addAttributeWithName:@"xmlns" stringValue:@"http://jabber.org/protocol/rsm"];
NSXMLElement *max = [NSXMLElement elementWithName:@"max"];
max.stringValue = @"100";
[set addChild:max];

[retrieve addChild:set];
[iQ addChild:retrieve];

[[[self appDelegate] xmppStream] sendElement:iQ];

返回以下错误:

<iq xmlns="jabber:client" type="error" id="page1" to="username@server.com">
   <error code="404" type="cancel">
      <item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
   </error>
</iq>

我的Xcode项目可以成功发送/接收消息给我正在尝试接收聊天记录的用户所以我真的不知道我做错了什么。此外,插件使我能够通过聊天消息(通过OpenFire管理员)搜索成功的结果,因此它似乎正在工作并存储消息。

My Xcode project can successfully send/receive messages to the user I'm trying to receive chat history from so I really don't know what I'm doing wrong. Also the Plugin enables me to search through Chat Messages (via OpenFire admin) with successful results so it seems to be working and storing the messages.

任何帮助将不胜感激。谢谢!

Any help would be appreciated. Thanks!

推荐答案

如果您正在寻找聊天记录,我认为您必须将消息保存到核心数据并检索它们从那里。要使用内置功能的XMPPFramework保存数据,您必须使用以下代码:

If you are looking for a chat history, I think you have to save the messages to core data and retrieve them from there. For saving data using the XMPPFramework inbuilt functionality, you have to use this code:

XMPPMessageArchivingCoreDataStorage *storage = [XMPPMessageArchivingCoreDataStorage   sharedInstance];
NSManagedObjectContext *moc = [storage mainThreadManagedObjectContext];

xmppMessageArchivingStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
xmppMessageArchivingModule = [[XMPPMessageArchiving alloc] initWithMessageArchivingStorage:xmppMessageArchivingStorage];
[xmppMessageArchivingModule activate:xmppStream];
[xmppMessageArchivingModule  addDelegate:self delegateQueue:dispatch_get_main_queue()];

现在你必须从核心数据中检索这些消息:

Now you have to retrieve that messages from core data by this:

-(void)loadarchivemsg
{  
    XMPPMessageArchivingCoreDataStorage *storage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
    NSManagedObjectContext *moc = [storage mainThreadManagedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"XMPPMessageArchiving_Message_CoreDataObject"
                                                      inManagedObjectContext:moc];
    NSFetchRequest *request = [[NSFetchRequest alloc]init];

    NSString *predicateFrmt = @"bareJidStr like %@ ";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFrmt, chatWithUser];
    request.predicate = predicate;
    NSLog(@"%@",[[NSUserDefaults standardUserDefaults] stringForKey:@"kXMPPmyJID"]);
    [request setEntity:entityDescription];
    NSError *error;
    NSArray *messages_arc = [moc executeFetchRequest:request error:&error];

    [self print:[[NSMutableArray alloc]initWithArray:messages_arc]];
}

-(void)print:(NSMutableArray*)messages_arc{
    @autoreleasepool {
        for (XMPPMessageArchiving_Message_CoreDataObject *message in messages_arc) {
            NSXMLElement *element = [[NSXMLElement alloc] initWithXMLString:message.messageStr error:nil];
            NSLog(@"to param is %@",[element attributeStringValueForName:@"to"]);

            NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
            [m setObject:message.body forKey:@"msg"];

            if ([[element attributeStringValueForName:@"to"] isEqualToString:chatWithUser]) {               
                [m setObject:@"you" forKey:@"sender"];
            }
            else {
                [m setObject:chatWithUser forKey:@"sender"];
            }

            [messages addObject:m];

            NSLog(@"bareJid param is %@",message.bareJid);
            NSLog(@"bareJidStr param is %@",message.bareJidStr);
            NSLog(@"body param is %@",message.body);
            NSLog(@"timestamp param is %@",message.timestamp);
            NSLog(@"outgoing param is %d",[message.outgoing intValue]);
            NSLog(@"***************************************************");
        }
    }
}

这篇关于XMPPFramework - 检索Openfire消息档案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆