8
votes

After installing Open Archive plugin in the Openfire server I can see the chat conversation between two user from the openfire admin panel which is pretty easy and that is web based too. Now I want to retrive those conversation or chat history from chat client application(written in java) where I've used Smack library. I didn't found any helpfull resource for that. Any advice will be helpfull.

5
Is your problem solved ? I am facing this tooMithun Sarker Shuvro
You need to retrieve the chat history using your own custom implementation, in my case I had own central web server which can give me the data from the database.Dhiren Hamal

5 Answers

6
votes

Smack just implemented MAM feature [XEP 0313] but yet not released, hope to get it on next release if you want to use this feature build the smack library from source or you can use custom IQ to get archived messages from server.

4
votes

It might be a late answer but now as SMACK API supports XEP-0136 and XEP-0313, so below code can help people landing to this page.

public MamManager.MamQueryResult getArchivedMessages(String jid, int maxResults) {

        MamManager mamManager = MamManager.getInstanceFor(connection);
        try {
            DataForm form = new DataForm(DataForm.Type.submit);
            FormField field = new FormField(FormField.FORM_TYPE);
            field.setType(FormField.Type.hidden);
            field.addValue(MamElements.NAMESPACE);
            form.addField(field);

            FormField formField = new FormField("with");
            formField.addValue(jid);
            form.addField(formField);

            // "" empty string for before
            RSMSet rsmSet = new RSMSet(maxResults, "", RSMSet.PageDirection.before);
            MamManager.MamQueryResult mamQueryResult = mamManager.page(form, rsmSet);

            return mamQueryResult;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
2
votes

The solution you're looking for come under XMPP specification's XEP-0136 Message archiving but Smack has not implemented this features yet. but you can retrieve the message history from server using "custom-stanza" features provided by SMACK API. The following links describe how to send the custom stanza. "How retrieve Chat History using Java Smack library from openfire server?".

1
votes

Finally I got the answer. Archive Messaging features are currently not implemented in Smack library.

https://community.igniterealtime.org/message/249993#249993

1
votes

Maybe I am late to answer this question, but may be it will be helpful for others.

get function result of getArchivedMessages(jid, maxResults); from https://stackoverflow.com/a/41777582/6771052

public List<ChatMessage> getChatHistoryWithJID(String jid, int maxResults) {
    List<ChatMessage> chatMessageList = new ArrayList<>();
    MamManager.MamQueryResult mamQueryResult = getArchivedMessages(jid, maxResults);
    String userSendTo = XmppUtils.parseNameFromJID(jid);

    try {
        if (mamQueryResult != null && userSendTo != null) {
            for (Forwarded forwarded : mamQueryResult.forwardedMessages) {
                if (forwarded.getForwardedStanza() instanceof Message) {
                    Message msg = (Message) forwarded.getForwardedStanza();
                    Log.d(TAG, "onCreate: " + msg.toString());
                    Log.d(TAG, "processStanza: " + msg.getFrom() + " Say:" + msg.getBody() + " String length:" + (msg.getBody() != null ? msg.getBody().length() : ""));
                    ChatMessage chatMessage;
                    if (XmppUtils.parseNameFromJID(msg.getFrom().toString()).equalsIgnoreCase(userSendTo)) {
                        chatMessage = new ChatMessage(msg.getBody(), forwarded.getDelayInformation().getStamp().getTime(), ChatMessage.Type.RECEIVED);
                    } else {
                        chatMessage = new ChatMessage(msg.getBody(), forwarded.getDelayInformation().getStamp().getTime(), ChatMessage.Type.SENT);
                    }
                    chatMessageList.add(chatMessage);

                }
            }
        } else {
            return chatMessageList;
        }

        return chatMessageList;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return chatMessageList;
}

Now the Query will become like:

<iq  id='ri7F7-270' type='set'>
<query  xmlns='urn:xmpp:mam:1' queryid='afd9c922-21cb-437e-b5c4-3a5bf9994e40'>
    <x  xmlns='jabber:x:data' type='submit'>
        <field  var='FORM_TYPE' type='hidden'>
            <value>urn:xmpp:mam:1</value>
        </field>
        <field  var='with'>
            <value>vishal@jabberid</value>
        </field>
    </x>
    <set  xmlns='http://jabber.org/protocol/rsm'>
        <before>
        </before>
        <max>100</max>
    </set>
</query>

And the response will be like:

<message to="vishal@jabberid">
<result xmlns="urn:xmpp:mam:1"  queryid="afd9c922-21cb-437e-b5c4-3a5bf9994e40" id="992">
<forwarded xmlns="urn:xmpp:forward:0">
  <delay xmlns="urn:xmpp:delay" stamp="2019-04-05T06:38:40.612Z"/>
  <message xmlns="jabber:client" to="vishal@jabberid" id="h58k4-104" type="chat" from="vishal@jabberid">
    <body>Hi</body>
  </message>
</forwarded>
</result>
</message>

And more you can be read from this link https://xmpp.org/extensions/xep-0313.html