package de.thedevstack.conversationsplus.services.muc.listener; import android.util.Log; import de.thedevstack.conversationsplus.Config; import de.thedevstack.conversationsplus.entities.Account; import de.thedevstack.conversationsplus.entities.Conversation; import de.thedevstack.conversationsplus.entities.MucOptions; import de.thedevstack.conversationsplus.generator.PresenceGenerator; import de.thedevstack.conversationsplus.persistance.DatabaseBackend; import de.thedevstack.conversationsplus.services.muc.OnConferenceConfigurationFetched; import de.thedevstack.conversationsplus.services.muc.OnConferenceJoined; import de.thedevstack.conversationsplus.utils.XmppConnectionServiceAccessor; import de.thedevstack.conversationsplus.utils.XmppSendUtil; import de.thedevstack.conversationsplus.xml.Element; import de.thedevstack.conversationsplus.xmpp.OnIqPacketReceived; import de.thedevstack.conversationsplus.xmpp.jid.Jid; import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; import de.thedevstack.conversationsplus.xmpp.stanzas.PresencePacket; /** */ public class ConferenceConfigurationFetched implements OnConferenceConfigurationFetched { private OnConferenceJoined onConferenceJoined; public ConferenceConfigurationFetched(OnConferenceJoined onConferenceJoined) { this.onConferenceJoined = onConferenceJoined; } private void join(Conversation conversation) { Account account = conversation.getAccount(); final MucOptions mucOptions = conversation.getMucOptions(); final Jid joinJid = mucOptions.getSelf().getFullJid(); Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": joining conversation " + joinJid.toString()); long lastMessageTransmitted = conversation.getLastMessageTransmitted(); PresencePacket packet = PresenceGenerator.generateMucJoin(account, joinJid, mucOptions, lastMessageTransmitted); XmppSendUtil.sendPresencePacket(account, packet); if (onConferenceJoined != null) { onConferenceJoined.onConferenceJoined(conversation); } if (!joinJid.equals(conversation.getJid())) { conversation.setContactJid(joinJid); DatabaseBackend.getInstance().updateConversation(conversation); } if (mucOptions.mamSupport()) { XmppConnectionServiceAccessor.xmppConnectionService.getMessageArchiveService().catchupMUC(conversation); } if (mucOptions.membersOnly() && mucOptions.nonanonymous()) { fetchConferenceMembers(conversation); } XmppConnectionServiceAccessor.xmppConnectionService.sendUnsentMessages(conversation); } @Override public void onConferenceConfigurationFetched(Conversation conversation) { join(conversation); } @Override public void onFetchFailed(final Conversation conversation, Element error) { join(conversation); XmppConnectionServiceAccessor.xmppConnectionService.fetchConferenceConfiguration(conversation); } private void fetchConferenceMembers(final Conversation conversation) { final Account account = conversation.getAccount(); final String[] affiliations = {"member","admin","owner"}; OnIqPacketReceived callback = new OnIqPacketReceived() { private int i = 0; @Override public void onIqPacketReceived(Account account, IqPacket packet) { Element query = packet.query("http://jabber.org/protocol/muc#admin"); if (packet.getType() == IqPacket.TYPE.RESULT && query != null) { for(Element child : query.getChildren()) { if ("item".equals(child.getName())) { conversation.getMucOptions().putMember(child.getAttributeAsJid("jid")); } } } else { Log.d(Config.LOGTAG,account.getJid().toBareJid()+": could not request affiliation "+affiliations[i]+" in "+conversation.getJid().toBareJid()); } ++i; if (i >= affiliations.length) { Log.d(Config.LOGTAG,account.getJid().toBareJid()+": retrieved members for "+conversation.getJid().toBareJid()+": "+conversation.getMucOptions().getMembers()); } } }; for(String affiliation : affiliations) { XmppSendUtil.sendIqPacket(account, XmppConnectionServiceAccessor.xmppConnectionService.getIqGenerator().queryAffiliation(conversation, affiliation), callback); } Log.d(Config.LOGTAG,account.getJid().toBareJid()+": fetching members for "+conversation.getName()); } }