2013-01-05 58 views
4

花了幾小時試圖解決這個問題,我很難過!XMPPFramework - 檢索Openfire郵件歸檔文件

試圖抓住我的OpenFire服務器上的2個用戶之間的聊天記錄,我看到我需要插件來做到這一點。

所以,我安裝我的Openfire服務器上的「開放檔案」插件,併發送下面的XML(按照XMPP-0136協議文檔):

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

在代碼中,這是通過以下實現:

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:@"[email protected]"]; 
[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="[email protected]"> 
    <error code="404" type="cancel"> 
     <item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/> 
    </error> 
</iq> 

我的Xcode項目可以成功地向用戶發送/接收消息我試圖接收聊天記錄,所以我真的不知道我在做什麼錯誤。此外,插件使我能夠通過聊天消息(通過OpenFire管理員)搜索成功的結果,因此它似乎在工作並存儲消息。

任何幫助,將不勝感激。謝謝!

+0

這裏是解決方案。 http://stackoverflow.com/questions/11397172/xmpp-retrieve-archive-messages-from-openfire-server – Karun

回答

0

XMPPFramework實現XEP-0136。您是否嘗試過使用​​來設置首選項或將服務器的存檔同步到客戶端?

+1

嗨@noa!您可以分享一個如何設置歸檔和將服務器歸檔同步到客戶端的首選項的示例。 – Karun

6

如果您正在查找聊天記錄,我認爲您必須將郵件保存到核心數據並從那裏檢索。對於使用XMPPFramework內置功能保存數據,你必須使用此代碼:

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()]; 

現在您已經檢索該從核心數據消息:

-(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(@"***************************************************"); 
     } 
    } 
} 
+0

我沒有得到一個點,我們應該在哪裏給其他用戶JID.Can你可以用評論來顯示特定的地方。 – Romance

+0

[NSPredicate predicateWithFormat:predicateFrmt,chatWithUser];這裏chatWithUser是其他用戶JID,你可以在那裏設置其他用戶JID –

+0

是的,我收到了,我面臨着一些其他問題,我使用你的代碼,但是當我回憶代碼時,我得到了最後兩條聊天記錄的消息,我是沒有得到充分的歷史。普茲給予解決。 – Romance

3

請有在細節詩節詳細:https://stackoverflow.com/a/29097289/2225439

它是平臺獨立的,只需要瞭解Stanza的結構,並且可以根據您正在使用的庫創建。

這是您需要發送以獲取存檔郵件的Stanza系列。欲瞭解更多的細節,你可以檢出XEP 0136(http://xmpp.org/extensions/xep-0136.html#manual

REQ

<iq type='get' id='[email protected]'> 
     <list xmlns='urn:xmpp:archive' 
       with='[email protected]'> 
     <set xmlns='http://jabber.org/protocol/rsm'> 
      <max>6900</max> 
     </set> 
     </list> 
    </iq> 

RES

<iq type="result" id="[email protected]" to="[email protected]/Psi"> 
<list xmlns="urn:xmpp:archive"> 
<chat with="[email protected]" start="2014-06-07T06:52:26.041Z"/> 
<chat with="[email protected]" start="2014-06-07T07:06:53.372Z"/> 
<set xmlns="http://jabber.org/protocol/rsm"> 
<first index="0">866</first> 
<last>867</last> 
<count>2</count> 
</set> 
</list> 
</iq> 

REQ

<iq type='get' id='[email protected]'> 
    <retrieve xmlns='urn:xmpp:archive' with='[email protected]' start='2014-06-07T06:52:26.041Z'> 
    <set xmlns='http://jabber.org/protocol/rsm'> 
     <max>8000</max> 
    </set> 
    </retrieve> 
</iq> 

RES

<iq type="result" id="[email protected]" to="[email protected]/Psi"> 
<chat xmlns="urn:xmpp:archive" with="[email protected]" start="2014-06-07T06:52:26.041Z"> 
<from secs="0" jid="[email protected]"> 
<body>Hello This is Cool</body> 
</from> 
<set xmlns="http://jabber.org/protocol/rsm"> 
<first index="0">0</first> 
<last>0</last> 
<count>1</count> 
</set> 
</chat> 
</iq> 

獲取所有對話

<iq type='get' id='[email protected]'> 
     <list xmlns='urn:xmpp:archive'> 
     <set xmlns='http://jabber.org/protocol/rsm'> 
      <max>6900</max> 
     </set> 
     </list> 
</iq> 
1

當你在要求提起始標記則具有這就是爲什麼它返回錯誤代碼「404」或「500」的確切時間戳記聊天匹配的列表。我從我的請求中刪除了開始標記並編寫了以下代碼,它將返回與用戶的整個聊天記錄。

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

NSXMLElement *retrieve = [NSXMLElement elementWithName:@"retrieve" xmlns:@"urn:xmpp:archive"]; 

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

[iq1 addChild:retrieve]; 
[retrieve addChild:set]; 
[set addChild:max]; 
[[[self appDelegate] xmppStream] sendElement:iq1]; 

這裏,這將在當前登錄的用戶拉胡爾和用戶之間的XML響應返回整個聊天記錄。

有關詳細信息,請參閱本博客http://question.ikende.com/question/363439343236313430