Thu, 26 Dec 2024 12:53:02 +0100
handle presence messages
--- a/src/main/java/de/unixwork/im/App.java Thu Dec 26 12:29:05 2024 +0100 +++ b/src/main/java/de/unixwork/im/App.java Thu Dec 26 12:53:02 2024 +0100 @@ -4,8 +4,10 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.roster.RosterEntry; import org.jxmpp.jid.Jid; +import org.jxmpp.jid.parts.Resourcepart; public class App { @@ -13,6 +15,7 @@ private final ContactListFrame contactListFrame; private final Map<String, ConversationFrame> conversations; + private final Map<String, PresenceInfo> presence = new HashMap<>(32); private final Xmpp xmpp; @@ -81,4 +84,31 @@ public void runOnUiThread(Runnable action) { SwingUtilities.invokeLater(action); } + + public String getStatusForXID(String xid) { + PresenceInfo ps = presence.get(xid); + if(ps == null) { + return "<offline> "; + } + return ps.getOnlineStatus(); + } + + void handlePresence(Jid from, Presence.Type type) { + System.out.println("presence from: " + from.toString() + " type: " + type.toString()); + + String xid = from.asBareJid().toString(); + PresenceInfo ps = presence.get(xid); + if(ps == null) { + ps = new PresenceInfo(); + presence.put(xid, ps); + } + + // update presence + Resourcepart resource = from.getResourceOrNull(); + if(resource != null) { + ps.setStatus(resource.toString(), type); + } else { + // TODO + } + } }
--- a/src/main/java/de/unixwork/im/ContactListFrame.java Thu Dec 26 12:29:05 2024 +0100 +++ b/src/main/java/de/unixwork/im/ContactListFrame.java Thu Dec 26 12:53:02 2024 +0100 @@ -6,6 +6,7 @@ import java.awt.event.MouseEvent; import java.util.List; import org.jivesoftware.smack.roster.RosterEntry; +import org.jxmpp.jid.Jid; // Main class for the XMPP contact list window public class ContactListFrame extends JFrame { @@ -29,7 +30,9 @@ public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if (value instanceof RosterEntry) { - setText(((RosterEntry) value).toString()); + RosterEntry entry = (RosterEntry)value; + String xid = entry.getJid().asBareJid().toString(); + setText(App.getInstance().getStatusForXID(xid) + xid); } return c; }
--- a/src/main/java/de/unixwork/im/Xmpp.java Thu Dec 26 12:29:05 2024 +0100 +++ b/src/main/java/de/unixwork/im/Xmpp.java Thu Dec 26 12:53:02 2024 +0100 @@ -24,10 +24,12 @@ import org.jivesoftware.smack.filter.MessageWithBodiesFilter; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.MessageBuilder; +import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.roster.Roster; import org.jivesoftware.smack.roster.RosterEntry; import org.jivesoftware.smack.tcp.XMPPTCPConnection; import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration; +import org.jxmpp.jid.Jid; import org.jxmpp.stringprep.XmppStringprepException; public class Xmpp extends Thread { @@ -46,7 +48,6 @@ config = xmppConfig; otr = new OTR(this); otrSM = new OtrSessionManagerImpl(otr); - } public OTR getOTR() { @@ -133,7 +134,15 @@ String body = ((Message)stanza).getBody(); App.getInstance().dispatchMessage(jid, body, true); } - }), MessageWithBodiesFilter.INSTANCE); + }), MessageWithBodiesFilter.INSTANCE); + connection.addAsyncStanzaListener(stanza -> { + if (stanza instanceof Presence) { + Presence presence = (Presence) stanza; + Jid from = presence.getFrom(); + Presence.Type type = presence.getType(); + App.getInstance().handlePresence(from, type); + } + }, stanza -> stanza instanceof Presence); List<RosterEntry> roster = getRosterItems(); if(roster != null) { App.getInstance().setContacts(roster);