package de.thedevstack.conversationsplus.generator; import de.thedevstack.conversationsplus.entities.Account; import de.thedevstack.conversationsplus.entities.Contact; import de.thedevstack.conversationsplus.entities.MucOptions; import de.thedevstack.conversationsplus.entities.Presence; import de.thedevstack.conversationsplus.xml.Element; import de.thedevstack.conversationsplus.xmpp.jid.Jid; import de.thedevstack.conversationsplus.xmpp.stanzas.PresencePacket; public class PresenceGenerator extends AbstractGenerator { private static PresencePacket generatePresencePacket(String type, Jid from, Jid to) { PresencePacket packet = new PresencePacket(); if (null != type) { packet.setAttribute("type", type); } if (null != to) { packet.setTo(to); } if (null != from) { packet.setFrom(from); } return packet; } private static PresencePacket subscription(String type, Contact contact) { return generatePresencePacket(type, contact.getAccount().getJid().toBareJid(), contact.getJid()); } public static PresencePacket requestPresenceUpdatesFrom(Contact contact) { return subscription("subscribe", contact); } public static PresencePacket stopPresenceUpdatesFrom(Contact contact) { return subscription("unsubscribe", contact); } public static PresencePacket stopPresenceUpdatesTo(Contact contact) { return subscription("unsubscribed", contact); } public static PresencePacket sendPresenceUpdatesTo(Contact contact) { return subscription("subscribed", contact); } public static PresencePacket selfPresence(Account account, Presence.Status status) { PresencePacket packet = new PresencePacket(status); packet.setFrom(account.getJid()); String sig = account.getPgpSignature(); if (sig != null) { packet.addChild("x", "jabber:x:signed").setContent(sig); } String capHash = getCapHash(); if (capHash != null) { Element cap = packet.addChild("c", "http://jabber.org/protocol/caps"); cap.setAttribute("hash", "sha-1"); cap.setAttribute("node", "http://conversations.im"); cap.setAttribute("ver", capHash); } return packet; } private static PresencePacket generateOfflinePresencePacket(Jid from, Jid to) { return generatePresencePacket("unavailable", from, to); } public static PresencePacket generateOfflinePresencePacketTo(Account account, Jid to) { return generateOfflinePresencePacket(account.getJid(), to); } public static PresencePacket generateOfflinePresencePacket(Account account) { return generateOfflinePresencePacket(account.getJid(), null); } public static PresencePacket generateMucJoin(Account account, Jid joinJid, MucOptions mucOptions, long lastMessageTransmitted) { PresencePacket packet = generatePresencePacket(null, account.getJid(), joinJid); Element x = packet.addChild("x", "http://jabber.org/protocol/muc"); if (mucOptions.getPassword() != null) { x.addChild("password").setContent(mucOptions.getPassword()); } Element historyElement = x.addChild("history"); if (mucOptions.mamSupport()) { // Use MAM instead of the limited muc history to get history historyElement.setAttribute("maxchars", "0"); } else { // Fallback to muc history historyElement.setAttribute("since", PresenceGenerator.getTimestamp(lastMessageTransmitted)); } String sig = account.getPgpSignature(); if (sig != null) { packet.addChild("x", "jabber:x:signed").setContent(sig); } return packet; } public static PresencePacket generateMucRename(Account account, Jid joinJid) { PresencePacket packet = generatePresencePacket(null, account.getJid(), joinJid); String sig = account.getPgpSignature(); if (sig != null) { packet.addChild("status").setContent("online"); packet.addChild("x", "jabber:x:signed").setContent(sig); } return packet; } }