Get last message of each conversation with XEP-0313? - xmpp

I'm using an XMPP server that implements XEP-0313 for retrieving conversation history. I would like to fetch only the last message of each conversation, so that I can build a list of your most recent conversations previewing the last message.
I've managed to fetch all messages of all conversations and based on that I could build the list, but it's a big waste of data and not an option. I'm not sure this is the right extension for accomplishing this, so if there is another extension I should be looking at, please guide me in the right direction.

One thing you can do easily is first retrieve the user's roster and then for each contact retrieve the latest message.
<iq from='juliet#example.com/balcony'
id='bv1bs71f'
type='get'>
<query xmlns='jabber:iq:roster'/>
</iq>
Result:
<iq id='bv1bs71f'
to='juliet#example.com/chamber'
type='result'>
<query xmlns='jabber:iq:roster' ver='ver7'>
<item jid='nurse#example.com'/>
<item jid='romeo#example.net'/>
</query>
</iq>
Retrieve the last message from or to nurse#example.com:
<iq type='set' id='juliet1'>
<query xmlns='urn:xmpp:mam:1'>
<x xmlns='jabber:x:data' type='submit'>
<field var='FORM_TYPE' type='hidden'>
<value>urn:xmpp:mam:1</value>
</field>
<field var='with'>
<value>nurse#example.com</value>
</field>
</x>
<set xmlns='http://jabber.org/protocol/rsm'>
<max>1</max>
<before/>
</set>
</query>
</iq>
Of course users can have conversations with people not on their roster, but in practice this is quite rare on XMPP.

Related

How to query xmpp archive to get latest 20 message per user

If I have 20 users, each user have 100 archived messages.
How can I get the latest 20 archived messages of all 20 users in one query?
This query doesn't work that way...
<iq type='set' id='test1'>
<query xmlns='urn:xmpp:mam:0'>
<set xmlns='http://jabber.org/protocol/rsm'>
<max>20</max>
<before/>
</set>
</query>
</iq>
XMPP does not support this kind of Query.You may need to change source code and introduce custom request to achieve this behavior.

Enabling PEP on ejabberd server

I'm creating a chat app using the Electron Framework and node-xmpp module for XMPP communication.
I have managed to do almost everything except making Personal Eventing Protocol to work. Specifically sending a new nickname to the roster.
When I send the PEP stanza
<iq from='test#localhost' type='set' id='pub1'>
<pubsub xmlns='http://jabber.org/protocol/pubsub'>
<publish node='http://jabber.org/protocol/nick'>
<item>
<nick xmlns='http://jabber.org/protocol/nick'>I am a test user</nick>
</item>
</publish>
</pubsub>
</iq>
I get a response IQ stanza:
<iq from="test#localhost" type="result" to="test#localhost/testapp" id="pub1">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<publish node="http://jabber.org/protocol/nick">
<item id="5D4E0BB8EB3C6"/>
</publish>
</pubsub>
</iq>
Now, according to XEP-172 example 6 I should get a Message with the nickname that is also sent to all my contacts. It should look like this:
<message from='test#localhost' to='otheruser#localhost' type='headline' id='foo'>
<event xmlns='http://jabber.org/protocol/pubsub#event'>
<items node='http://jabber.org/protocol/nick'>
<item>
<nick xmlns='http://jabber.org/protocol/nick'>I am a test user</nick>
</item>
</items>
</event>
<addresses xmlns='http://jabber.org/protocol/address'>
<address type='replyto' jid='test#localhost/chatapp'/>
</addresses>
</message>
The thing is I'm not getting any message to other contacts in the user's roster. Not even to myself.
Is there any step I'm missing to enable PEP on a ejabberd server? Any extra stanza or information I should include?
Thanks!
If you want to receive other users' nickname events you should include http://jabber.org/protocol/nick+notify string into your Entity Capabilities list.
See Filtered notifications for detailed description.

Deny messages from non-roster users in Openfire

How do I only allow contacts who are on my roster list to send me messages?
Is there any XEP responsible to do that? Or will I need to do this client-side?
Yes, OpenFire supports XEP-0016: Privacy Lists (see this question), which can be used to block stanzas according to various criteria.
You can't explicitly block stanzas for contacts not in your roster, but you can block by subscription status none, which can more or less accomplish the same goal. You could send something like this:
<iq from='romeo#example.net/orchard' type='set' id='msg3'>
<query xmlns='jabber:iq:privacy'>
<list name='message-sub-example'>
<item type='subscription'
value='none'
action='deny'
order='5'>
<message/>
</item>
</list>
</query>
</iq>
This creates a privacy list called message-sub-example, containing a rule to block any messages from contacts with subscription type none, including contacts not in the roster. For this list to take effect, you need to make it the active list:
<iq from='romeo#example.net/orchard' type='set' id='active1'>
<query xmlns='jabber:iq:privacy'>
<active name='message-sub-example'/>
</query>
</iq>

XMPP PubSub retrieve node's subscribers

I have a PubSub node with two subscribers: Joe and Mike.
I want to retrieve them as a list of subscribers and when I do
<iq type='get'
from='francisco#denmark.lit/barracks'
to='pubsub.shakespeare.lit'
id='subscriptions2'>
<pubsub xmlns='http://jabber.org/protocol/pubsub'>
<subscriptions node='my_node'/>
</pubsub>
</iq>
the result is
<iq type='result'
from='pubsub.shakespeare.lit'
to='joe#denmark.lit'
id='subscriptions2'>
<pubsub xmlns='http://jabber.org/protocol/pubsub'>
<subscriptions node='my_node'>
<subscription node='my_node' jid='joe#denmark.lit' subscription='subscribed' subid='123-abc'/>
</subscriptions>
</pubsub>
</iq>
It doesn't show that Mike is also subscribed to the same node.
And if I make the same query from Mike's account it shows that Mike is the only subscriber and does not include Joe.
Only the node owner can retrieve the list of subscribers.
And the namespace should be "http://jabber.org/protocol/pubsub#owner" for such request.

XMPP multi-user-chat get user list

get a list of all users and the status of XMPP multi-user-chat
I do so
<iq from='hag66#shakespeare.lit/pda'
id='kl2fax27'
to='coven#chat.shakespeare.lit'
type='get'>
<query xmlns='http://jabber.org/protocol/disco#items'/>
</iq>
getting a list but without status.
I need for all users on the statuses to know?
Please read XEP-0045, which describes the multi-user chat (MUC) protocol. You need to join the room:
<presence
from='hag66#shakespeare.lit/pda'
to='coven#chat.shakespeare.lit/thirdwitch'>
<x xmlns='http://jabber.org/protocol/muc'/>
</presence>
You'll then get a presence stanza from each occupant of the room with their current status:
<presence
from='coven#chat.shakespeare.lit/firstwitch'
to='hag66#shakespeare.lit/pda'>
<x xmlns='http://jabber.org/protocol/muc#user'>
<item affiliation='owner' role='moderator'/>
</x>
</presence>
<presence
from='coven#chat.shakespeare.lit/secondwitch'
to='hag66#shakespeare.lit/pda'>
<x xmlns='http://jabber.org/protocol/muc#user'>
<item affiliation='admin' role='moderator'/>
</x>
</presence>
And before you ask, no, there is no way to tell when you're "done" receiving these notifications, since users may come and go at any time. You are now subscribed to the presence changes of the occupants, and have to keep track of what is current on the receiving side.
Reading through the XMPP RFC, I see that statuses are conveyed by presence messages, and that you ask another entity to report its status now (rather than waiting for it to tell you in a broadcast) by sending a probe status request to which you'll get a directed response (§5.5 of the RFC gives some examples). It's up to you to interpret what they say back correctly, of course…