8

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.

Flow
  • 23,572
  • 15
  • 99
  • 156
Dhiren Hamal
  • 874
  • 1
  • 12
  • 31

5 Answers5

6

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.

Md Samiul Alim Sakib
  • 1,114
  • 12
  • 15
  • is it added now? @Sakib in version 4.2.0 . How will I check it? – Ankur Khandelwal Apr 19 '17 at 05:46
  • @AnkurKhandelwal Please check their release change log or issues in github. – Md Samiul Alim Sakib Apr 19 '17 at 05:53
  • @AnkurKhandelwal Hi Ankur, I am also working for the same task. Are you able to find out the api and get the Chat messages ? – Manish Apr 19 '17 at 13:15
  • 1
    @Manish http://download.igniterealtime.org/smack/docs/latest/documentation/extensions/mam.html I think this is api that is used to fetch previous message. I am able to get the stanza id of the all the previous message. Now I don't know how to parse id to actual message text. If you got any clue let me also know. – Ankur Khandelwal Apr 19 '17 at 13:22
  • @AnkurKhandelwal Can you please help me with the code that you used. I tried to implement the code but getting "java.lang.IllegalArgumentException: Must have a local (user) JID set. Either you didn't configure one or you where not connected at least once". Don't know where i have to pass it. Please help. – Manish Apr 20 '17 at 10:54
  • var mamManager:MamManager= MamManager.getInstanceFor(connection) //by jid var prevMsg=mamManager.queryArchive(JidCreate.entityBareFrom(jid)).forwardedMessages var prevFin=mamManager.queryArchive(JidCreate.entityBareFrom(jid)).mamFin for (msg in prevMsg){ println(msg.forwardedStanza) println("Id is "+msg.forwardedStanza.stanzaId) println("From "+msg.forwardedStanza.from) println("\n") } – Ankur Khandelwal Apr 20 '17 at 11:11
  • @AnkurKhandelwal In my case issue is somewhere in XMPPTCPConnectionConfiguration object creation and when i use mamManager object to check isSupport in the internal api at some place it checks for conn.getUser which returns null Due to this it's throwing "java.lang.IllegalArgumentException: Must have a local (user) JID set. Either you didn't configure one or you where not connected at least once". Can you please let me know how you created XMPPTCPConnectionConfiguration object. – Manish Apr 20 '17 at 12:05
  • `val config= XMPPTCPConnectionConfiguration.builder() .setXmppDomain(mDomainName) .setUsernameAndPassword(mUsername,mPassword) .setDebuggerEnabled(true) .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled) mConnection= XMPPTCPConnection(config.build()) mConnection!!.connect().login() ` – Ankur Khandelwal Apr 20 '17 at 12:11
  • @AnkurKhandelwal Now I am also stuck at the same place.Got the stanza id but don't know how to use it. – Manish Apr 20 '17 at 13:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142187/discussion-between-ankur-khandelwal-and-manish). – Ankur Khandelwal Apr 20 '17 at 13:51
  • @Manish are you able to receive the group message? – Ankur Khandelwal May 01 '17 at 07:45
  • @AnkurKhandelwal I am only fetching one to one chat and i am able to do . Smack API is not returning any forward message for me. So uses the monitoring plugin, modified it and my purpose is solved. – Manish May 03 '17 at 05:12
  • Monitoring plugin?? sorry i am not able to get you @Manish – Ankur Khandelwal May 03 '17 at 05:13
  • It's a openfire plugin with name "Monitoring Service".I used it's source code and wrote my own plugin for my requirement.https://www.igniterealtime.org/projects/openfire/plugins.jsp – Manish May 03 '17 at 11:26
4

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;
    }
Santosh Joshi
  • 3,290
  • 5
  • 36
  • 49
2

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?".

Community
  • 1
  • 1
Dila Gurung
  • 1,726
  • 21
  • 26
1

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

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

Dhiren Hamal
  • 874
  • 1
  • 12
  • 31
  • hi Dhiren Hamal, how to use XEP-0136 Archive Messaging to get the chat history in my android application . – Priya Sep 12 '16 at 09:11
  • @Priya ..Did u get any solution to get the chat history?. Please let me know .. Thanks :) – Ravindra Kushwaha Oct 10 '16 at 10:29
  • @DhirenHamal .. Did u get any solution to get the chat history?. Please let me know .. Thanks :) – Ravindra Kushwaha Oct 10 '16 at 10:30
  • @Dhiren Hamal .. i didnt found the solution yet.. still searching :( – Priya Oct 11 '16 at 05:01
  • @Priya you can use Open Archive plugin into your openfire server which automatically archive the messages between users conversation and after that you've to pull that stores messages from your client site. Meant to say that I've used spring web server to retrive offline messages. – Dhiren Hamal Oct 14 '16 at 12:16
  • 1
    @DhirenHamal Can you help with some code snippet. I am using smack api to implement the same. – Manish Apr 19 '17 at 12:29
1

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