public void sendMessage(String body, String chatJid, long timestamp) throws SmackException { Random random = new Random(timestamp + body.length() + chatJid.length()); Log.d(TAG, "Sending message to : " + chatJid); MultiUserChat chat = MultiUserChatManager.getInstanceFor(groupChatConnection) .getMultiUserChat(chatJid); chat.addMessageListener(this); Message message = new Message(); QuickbloxChatExtension extension = new QuickbloxChatExtension(); extension.setProperty("date_sent", timestamp + ""); message.setStanzaId(StanzaIdUtil.newStanzaId()); message.setBody(body); message.addExtension(extension); message.setType(Message.Type.chat); chat.sendMessage(message); }
public void joinPublicChat(String chatJid, long lastMessageDate) { try { MultiUserChat mucChat; if ((mucChat = publicChats.get(chatJid)) == null) { mucChat = MultiUserChatManager.getInstanceFor(privateChatConnection).getMultiUserChat(chatJid); mucChat.addMessageListener(this); publicChats.put(chatJid, mucChat); } DiscussionHistory history = new DiscussionHistory(); if(lastMessageDate != 0) history.setSince(new Date(lastMessageDate * 1000)); else history.setMaxChars(0); mucChat.join( CurrentUser.getInstance().getCurrentUserId().toString(), null, history, privateChatConnection.getPacketReplyTimeout()); } catch (Exception ex) { Logger.logExceptionToFabric(ex); } }
public void reconnectAll () { MultiUserChatManager mucMgr = MultiUserChatManager.getInstanceFor(mConnection); mucMgr.setAutoJoinOnReconnect(true); Enumeration<MultiUserChat> eMuc = mMUCs.elements(); while (eMuc.hasMoreElements()) { MultiUserChat muc = eMuc.nextElement(); MultiUserChat reMuc = mucMgr.getMultiUserChat(muc.getRoom()); try { DiscussionHistory history = new DiscussionHistory(); reMuc.join(Resourcepart.from(mUser.getName()), null, history, SmackConfiguration.getDefaultPacketReplyTimeout()); mMUCs.put(muc.getRoom().toString(),reMuc); ChatGroup group = mGroups.get(muc.getRoom().toString()); addMucListeners(reMuc, group); } catch (Exception e) { Log.w(TAG,"unable to join MUC: " + e.getMessage()); } } }
@Override public void rejectInvitationAsync(Invitation invitation) { Address addressGroup = invitation.getGroupAddress(); String reason = ""; // no reason for now MultiUserChatManager mucMgr = MultiUserChatManager.getInstanceFor(mConnection); try { mucMgr.decline(JidCreate.entityBareFrom(addressGroup.getAddress()), JidCreate.entityBareFrom(invitation.getSender().getAddress()), reason); } catch (Exception nce) { Log.e(ImApp.LOG_TAG,"not connected error trying to reject invite",nce); } }
/** * Initialize PacketListener for a given {@link org.jivesoftware.smack.XMPPConnection} * and a Command prefix * * @param prefix the command prefix used to filter message * @param connection the connection on which PacketListener will be registered */ private void initListeners(final String prefix, final XMPPConnection connection){ StanzaFilter filter = new AndFilter( new OrFilter(MessageTypeFilter.GROUPCHAT,MessageTypeFilter.CHAT), new StanzaFilter() { @Override public boolean accept(Stanza stanza) { return stanza instanceof Message && ((Message) stanza).getBody().startsWith(prefix); } } ); XmppMessageListener commandListener = new XmppMessageListener(this,listeners); connection.addAsyncStanzaListener(commandListener,filter); MultiUserChatManager.getInstanceFor(connection).addInvitationListener(new InvitationListener(this,listeners)); }
@Override public String getDefaultGroupChatService () { try { // Create a MultiUserChat using a Connection for a room //TODO fix this with new smack MultiUserChatManager mucMgr = MultiUserChatManager.getInstanceFor(mConnection); if (!mucMgr.providesMucService(JidCreate.domainBareFrom(mUserJid))) return DEFAULT_CONFERENCE_SERVER; Collection<DomainBareJid> servers = mucMgr.getXMPPServiceDomains(); //just grab the first one for (DomainBareJid server : servers) return server.toString(); } catch (Exception xe) { Log.w(TAG, "Error discovering MUC server",xe); //unable to find conference server return DEFAULT_CONFERENCE_SERVER; } return DEFAULT_CONFERENCE_SERVER; }
public Privilege getResourcePrivileges(Resource resource, Resource target){ MultiUserChat chatroom = MultiUserChatManager.getInstanceFor(connection).getMultiUserChat(resource.getAddress()); String occupantAddress = resource.getAddress()+"/"+resource.getName(); Occupant o = chatroom.getOccupant(occupantAddress); switch(o.getRole()){ case moderator: return Privilege.MODERATOR; default: return Privilege.USER; } }
@Override public void joinChatGroupAsync(Address address, String reason) { String chatRoomJid = address.getBareAddress(); String[] parts = chatRoomJid.split("@"); String room = parts[0]; try { // Create a MultiUserChat using a Connection for a room MultiUserChatManager mucMgr = MultiUserChatManager.getInstanceFor(mConnection); mucMgr.setAutoJoinOnReconnect(true); MultiUserChat muc = mucMgr.getMultiUserChat( JidCreate.entityBareFrom(chatRoomJid)); DiscussionHistory history = new DiscussionHistory(); muc.join(Resourcepart.from(mUser.getName()), null, history, SmackConfiguration.getDefaultPacketReplyTimeout()); String subject = muc.getSubject(); if (TextUtils.isEmpty(subject)) subject = room; ChatGroup chatGroup = mGroups.get(chatRoomJid); if (chatGroup == null) { chatGroup = new ChatGroup(address, subject, this); mGroups.put(chatRoomJid, chatGroup); } mMUCs.put(chatRoomJid, muc); addMucListeners(muc, chatGroup); } catch (Exception e) { debug(TAG,"error joining MUC",e); } }