@Override public void leaveChatGroupAsync(ChatGroup group) { String chatRoomJid = group.getAddress().getBareAddress(); if (mMUCs.containsKey(chatRoomJid)) { MultiUserChat muc = mMUCs.get(chatRoomJid); try { muc.leave(); } catch (Exception nce) { Log.e(ImApp.LOG_TAG,"not connected error trying to leave group",nce); } mMUCs.remove(chatRoomJid); } }
public void init() throws XMPPException { new TrackRooms().addTo(conn); new TrackStatus(getMonitorRoomJID().toLowerCase()).addTo(conn); new ListenForChat().addTo(conn); monitorRoom = new MultiUserChat(conn, getMonitorRoomJID()); monitorRoom.addMessageListener(this); monitorRoom.addParticipantStatusListener(this); monitorRoom.join(StringUtils.parseName(conn.getUser())); try { // This is necessary to create the room if it doesn't already exist monitorRoom.sendConfigurationForm(new Form(Form.TYPE_SUBMIT)); } catch (XMPPException ex) { // 403 code means the room already exists and user is not an owner if (ex.getXMPPError().getCode() != 403) { throw ex; } } sendStatus(me); }
public void process(Packet packet) { if (roomResponseFilter.accept(packet)) { final DiscoverItems result = (DiscoverItems) packet; final JabberPlayer player = playerMgr.getPlayer(packet.getFrom()); // Collect the entityID for each returned item for (Iterator<DiscoverItems.Item> items = result.getItems(); items.hasNext();) { final String roomJID = items.next().getEntityID(); final JabberRoom room = roomMgr.getRoomByJID(JabberClient.this, roomJID); try { room.setInfo(MultiUserChat.getRoomInfo(JabberClient.this .getConnection(), roomJID)); } catch (XMPPException e) { // Ignore Error } if (!roomJID.equals(monitorRoom.getRoom())) { player.join(roomMgr.getRoomByJID(JabberClient.this, roomJID)); } } fireRoomsUpdated(); } else if (newPlayerFilter.accept(packet)) { sendRoomQuery(getAbsolutePlayerJID(packet.getFrom())); } }
public synchronized JabberRoom getRoomByJID(JabberClient client, String jid, String defaultName) { if (jid == null) { return null; } JabberRoom newRoom = jidToRoom.get(jid); if (newRoom == null) { String roomName = defaultName == null ? "" : defaultName; //$NON-NLS-1$ RoomInfo info = null; try { info = MultiUserChat.getRoomInfo(client.getConnection(), jid); } // FIXME: review error message catch (XMPPException e) { e.printStackTrace(); } newRoom = new JabberRoom(roomName, jid, info, client); jidToRoom.put(jid, newRoom); } return newRoom; }
/** * 加入聊天室 * @param xmppConnection * @param roomName * @param password * @return */ public static MultiUserChat joinMultiUserChat(XMPPConnection xmppConnection,String roomName,String password,PacketListener packetListener){ try { // 使用XMPPConnection创建一个MultiUserChat窗口 MultiUserChat muc = new MultiUserChat(xmppConnection, roomName+ "@conference." + xmppConnection.getServiceName()); // 聊天室服务将会决定要接受的历史记录数量 DiscussionHistory history = new DiscussionHistory(); history.setMaxChars(0); // 用户加入聊天室 muc.join(xmppConnection.getUser(), password, history, SmackConfiguration.getPacketReplyTimeout()); if(packetListener!=null){ muc.addMessageListener(packetListener); } return muc; } catch (XMPPException e) { e.printStackTrace(); return null; } }
/** * 获取用户的所有聊天室 * @param xmppConnection * @return */ public static List<HostedRoom> getHostRooms(XMPPConnection xmppConnection){ List<HostedRoom> roominfos = new ArrayList<HostedRoom>(); try { new ServiceDiscoveryManager(xmppConnection); Collection<HostedRoom> hostrooms = MultiUserChat.getHostedRooms(xmppConnection,xmppConnection.getServiceName()); for (HostedRoom entry : hostrooms) { roominfos.add(entry); Log.i("room", "名字:" + entry.getName() + " - ID:" + entry.getJid()); } Log.i("room", "服务会议数量:" + roominfos.size()); } catch (XMPPException e) { Log.e("getHostRooms",e.getMessage()); e.printStackTrace(); } return roominfos; }
/** * 加入聊天室 * @param xmppConnection * @param roomName * @param password * @param packetListener 消息监听器 * @return */ public static MultiUserChat joinMultiUserChat(XMPPConnection xmppConnection,String roomName,String password,PacketListener packetListener){ try { // 使用XMPPConnection创建一个MultiUserChat窗口 MultiUserChat muc = new MultiUserChat(xmppConnection, roomName+ "@conference." + xmppConnection.getServiceName()); // 聊天室服务将会决定要接受的历史记录数量 DiscussionHistory history = new DiscussionHistory(); history.setMaxChars(0); // history.setSince(new Date()); // 用户加入聊天室 muc.join(xmppConnection.getUser(), password, history, SmackConfiguration.getPacketReplyTimeout()); Log.i("MultiUserChat", "会议室【"+roomName+"】加入成功........"); if(packetListener!=null){ muc.addMessageListener(packetListener); } return muc; } catch (XMPPException e) { Log.e("MultiUserChat", "会议室【"+roomName+"】加入失败........"); e.printStackTrace(); return null; } }
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 sendPublicMessage(String body, String chatJid, long timestamp, String stanzaId) { Log.d(TAG, "Sending message to : " + chatJid); MultiUserChat mucChat = publicChats.get(chatJid); QuickbloxChatExtension extension = new QuickbloxChatExtension(); extension.setProperty("date_sent", timestamp + ""); extension.setProperty("save_to_history", "1"); Message message = new Message(chatJid); message.setStanzaId(stanzaId); message.setBody(body); message.setType(Message.Type.groupchat); message.addExtension(extension); try { if (mucChat != null) { mucChat.sendMessage(message); } } catch (SmackException.NotConnectedException ex) { offlineMessages.add(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); } }
/** * Send invites to clients for joining multi user chat room * * @param muc * @param userList * @param inviteMessage */ public void sendRoomInvite(MultiUserChat muc, ArrayList<String> userList, String inviteMessage) { if (muc != null && muc.getRoom() != null && !muc.getRoom().isEmpty()) { if (userList != null && !userList.isEmpty()) { for (String user : userList) { try { muc.invite(user, inviteMessage); } catch (NotConnectedException e) { e.printStackTrace(); } } logger.info(userList.size() + " clients were invited to room(" + muc.getRoom() + ")"); } } else { logger.info("There is no available room for invitation"); } }
public String resolveRoom(XMPPConnection connection) throws XMPPException, SmackException { ObjectHelper.notEmpty(room, "room"); if (room.indexOf('@', 0) != -1) { return room; } Iterator<String> iterator = MultiUserChat.getServiceNames(connection).iterator(); if (!iterator.hasNext()) { throw new XMPPErrorException("Cannot find Multi User Chat service", new XMPPError(new XMPPError.Condition("Cannot find Multi User Chat service on connection: " + getConnectionMessage(connection)))); } String chatServer = iterator.next(); LOG.debug("Detected chat server: {}", chatServer); return room + "@" + chatServer; }
public void join(final Context context,String user,String rpwd,String rname) { final MultiUserChat muc = XmppConnectionManager.getInstance().joinRoom(user, rpwd, rname); runOnUiThread(new Runnable(){ @Override public void run() { if(null != muc){ XmppConnectionManager.getInstance().setMuc(muc); Toast.makeText(context, "����ɹ�", Toast.LENGTH_SHORT).show(); ((Activity) context).finish(); Intent intent = new Intent(context, RoomChatActivity.class); context.startActivity(intent); } else{ Toast.makeText(context, "����ʧ��", Toast.LENGTH_SHORT).show(); } }}); }
@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.muc_room); con = XmppConnectionManager.getInstance().getConnection(); muc = XmppConnectionManager.getInstance().getMuc(); muc.addInvitationRejectionListener(this); MultiUserChat.addInvitationListener(con, this); muc.addParticipantStatusListener(this); muc.addMessageListener(this); app = EimApplication.getInstance(); membersList = (ListView) findViewById(R.id.members); chatsList = (ListView) findViewById(R.id.chats); content = (EditText) findViewById(R.id.content); membersList.setOnItemClickListener(this); initData(); }
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 protected void removeGroupMemberAsync(ChatGroup group, Contact contact) { String chatRoomJid = group.getAddress().getAddress(); if (mMUCs.containsKey(chatRoomJid)) { MultiUserChat muc = mMUCs.get(chatRoomJid); try { String reason = ""; muc.kickParticipant(Resourcepart.from(contact.getName()),reason); // muc.kickParticipant(chatRoomJid, contact.getAddress().getBareAddress()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public void setGroupSubject(final ChatGroup group, final String subject) { execute (new Runnable() { public void run() { String chatRoomJid = group.getAddress().getAddress(); if (mMUCs.containsKey(chatRoomJid)) { MultiUserChat muc = mMUCs.get(chatRoomJid); try { muc.changeSubject(subject); } catch (Exception e) { e.printStackTrace(); } } } }); }
@Override public void joined(EntityFullJid entityFullJid) { XmppAddress xa = new XmppAddress(entityFullJid.toString()); ChatGroup chatGroup = mChatGroupManager.getChatGroup(xa); MultiUserChat muc = mChatGroupManager.getMultiUserChat(entityFullJid.asBareJid().toString()); Occupant occupant = muc.getOccupant(entityFullJid); Jid jidSource = (occupant != null) ? occupant.getJid() : null; if (jidSource != null) xa = new XmppAddress(jidSource.toString()); else xa = new XmppAddress(entityFullJid.toString()); Contact mucContact = new Contact(xa, xa.getUser(), Imps.Contacts.TYPE_NORMAL); chatGroup.notifyMemberJoined(entityFullJid.toString(),mucContact); if (occupant != null) { chatGroup.notifyMemberRoleUpdate(mucContact, occupant.getRole().name(), occupant.getAffiliation().toString()); } }
public static void customerIsTyping(String chatID) { ChatSession chatSession = getChatSession(chatID); if (chatSession == null) { return; } final MultiUserChat chat = chatSession.getGroupChat(); final Iterator iter = chat.getOccupants(); while (iter.hasNext()) { String from = (String)iter.next(); String tFrom = StringUtils.parseResource(from); String nickname = chat.getNickname(); if (tFrom != null && !tFrom.equals(nickname)) { MessageEventManager messageEventManager = chatSession.getMessageEventManager(); messageEventManager.sendComposingNotification(from, "l0k1"); } } }
private synchronized void reInitMultiUserChat() { synchronized (mLocker) { Iterator<String> iterator = mNeedReInitMultiChatRooms.iterator(); while (iterator.hasNext()) { String roomJid = iterator.next(); MultiUserChat chat = mMultiUserChats.get(roomJid); if (chat == null) { // 如果重新初始化成功,则移除 chat = initMultiUserChat(roomJid, true); if (chat != null && YiIMUtils.isMultiChatJoined(this, roomJid)) { iterator.remove(); } } } } }
public int createRoom(String user, String roomName, String roomDesc, String password) { try { MultiUserChat chat = YiIMUtils.createMultiUserChat( mService.get(), user, roomName, roomDesc, password); if (chat != null) { synchronized (mService.get().mLocker) { mService.get().mMultiUserChats .put(chat.getRoom(), chat); } return 0; } return -1; } catch (Exception e) { if ("room already exist".equals(e.getMessage())) { return -2; } YiLog.getInstance().e(e, "create room failed"); return -1; } }
public Iterator<String> getRoomMembers(String roomJid) { MultiUserChat chat = null; synchronized (mService.get().mLocker) { chat = mService.get().mMultiUserChats.get(roomJid); } if (chat == null) { chat = mService.get().initMultiUserChat(roomJid, false); } if (chat != null) { try { return chat.getOccupants(); } catch (Exception e) { // TODO: handle exception } } return null; }
public void inviteFriend(String roomJid, String user, boolean autoJoin) { MultiUserChat chat = null; synchronized (mService.get().mLocker) { chat = mService.get().mMultiUserChats.get(roomJid); } if (chat == null && autoJoin) { chat = mService.get().initMultiUserChat(roomJid, false); } if (chat != null) { try { chat.invite(user, ""); } catch (Exception e) { } } }
public static boolean isRoomExist(String roomJid) throws Exception { XMPPConnection connection = XmppConnectionUtils.getInstance() .getRawConnection(); if (connection == null || !connection.isAuthenticated()) { throw new Exception("no connections"); } try { RoomInfo roomInfo = MultiUserChat.getRoomInfo(connection, roomJid); if (roomInfo != null) { return true; } return false; } catch (Exception e) { return false; } }
long sendMessage(TextMessage textMessage) throws Exception { MultiUserChat muc = multichats.get(textMessage.getContactUid()); if (muc != null) { Message m = muc.createMessage(); m.setBody(textMessage.getText()); muc.sendMessage(m); return m.getPacketID().hashCode(); } Chat chat = chats.get(textMessage.getContactUid()); if (chat == null) { chat = getInternalService().getConnection().getChatManager().createChat(textMessage.getContactUid(), this); chats.put(textMessage.getContactUid(), chat); } Message packet = getInternalService().getService().getEntityAdapter().textMessage2XMPPMessage(textMessage, chat.getThreadID(), chat.getParticipant(), Message.Type.chat); chat.sendMessage(packet); return packet.getPacketID().hashCode(); }
List<MultiChatRoom> getJoinedChatRooms() { Iterator<String> joinedRooms = MultiUserChat.getJoinedRooms(getInternalService().getConnection(), getInternalService().getService().getProtocolUid()); List<MultiChatRoom> multiChatBuddies = new ArrayList<MultiChatRoom>(); for (; joinedRooms.hasNext();) { String roomJid = joinedRooms.next(); try { RoomInfo info = MultiUserChat.getRoomInfo(getInternalService().getConnection(), roomJid); multiChatBuddies.add(getInternalService().getService().getEntityAdapter().chatRoomInfo2Buddy(info, getInternalService().getService().getProtocolUid(), getInternalService().getService().getServiceId(), true)); } catch (XMPPException e) { Logger.log(e); } } for (final Buddy room : multiChatBuddies) { MultiUserChat chat = new MultiUserChat(getInternalService().getConnection(), room.getProtocolUid()); fillWithListeners(chat); multichats.put(room.getProtocolUid(), chat); } return multiChatBuddies; }
public void leaveChat(final String chatId) { Runnable r = new Runnable() { @Override public void run() { MultiUserChat muc = multichats.remove(chatId); if (muc != null) { muc.leave(); OnlineInfo info = getInternalService().getRosterListener().getPresenceCache().get(chatId); info.getFeatures().putByte(ApiConstants.FEATURE_STATUS, (byte) -1); info.getFeatures().remove(XMPPApiConstants.FEATURE_CONFIGURE_CHAT_ROOM_ACTION); info.getFeatures().remove(XMPPApiConstants.FEATURE_CONFIGURE_CHAT_ROOM_RESULT); //getInternalService().getService().getCoreService().buddyAction(ItemAction.LEFT, buddy); getInternalService().getService().getCoreService().buddyStateChanged(Arrays.asList(info)); } } }; Executors.defaultThreadFactory().newThread(r).start(); }
public void getChatConfigurationForm(final String chatJid) { Runnable r = new Runnable() { @Override public void run() { MultiUserChat muc = multichats.get(chatJid); if (muc != null) { try { Form form = muc.getConfigurationForm(); InputFormFeature feature = getInternalService().getService().getEntityAdapter().chatRoomConfigurationForm2InputFormFeature(form, getInternalService().getService().getContext()); getInternalService().getService().getCoreService().showFeatureInputForm(chatJid, feature); } catch (XMPPException e) { getInternalService().onXmppException(e); } } else { Logger.log("Could not find a room with jid #" + chatJid, LoggerLevel.INFO); } } }; Executors.defaultThreadFactory().newThread(r).start(); }
public void destroyChatRoom(final String chatJid) { Runnable r = new Runnable() { @Override public void run() { MultiUserChat muc = multichats.get(chatJid); if (muc != null) { try { Buddy b = getInternalService().getService().getEntityAdapter().chatInfo2Buddy(chatJid, chatJid, getInternalService().getService().getProtocolUid(), getInternalService().getService().getServiceId(), false); muc.destroy(null, null); getInternalService().getService().getCoreService().buddyAction(ItemAction.DELETED, b); } catch (XMPPException e) { getInternalService().onXmppException(e); } } else { Logger.log("Could not find a room with jid #" + chatJid, LoggerLevel.INFO); } } }; Executors.defaultThreadFactory().newThread(r).start(); }
public static Room createRoom(String name, MultiUserChat chat, XMPPConnection con) { final Room r = new Room(name, chat); try { r.info = MultiUserChat.getRoomInfo(con, (name.indexOf("@") != -1 ? name : name + "@" + CONF_URL)); } catch (XMPPException e) { e.printStackTrace(); } r.subject = r.info.getSubject(); chat.addSubjectUpdatedListener(new SubjectUpdatedListener() { public void subjectUpdated(String newsubject, String from) { r.subject = newsubject; } }); for (String user : r.getConnectedUsers()) { r.users.add(user); } r.startThread(); return r; }
public void displayRoomInformation(final String roomJID) { SwingWorker worker = new SwingWorker() { RoomInfo roomInfo = null; DiscoverItems items = null; public Object construct() { try { roomInfo = MultiUserChat.getRoomInfo(SparkManager.getConnection(), roomJID); ServiceDiscoveryManager manager = ServiceDiscoveryManager.getInstanceFor(SparkManager.getConnection()); items = manager.discoverItems(roomJID); } catch (XMPPException e) { Log.error(e); } return "ok"; } public void finished() { setupRoomInformationUI(roomJID, roomInfo, items); } }; worker.start(); }
/** * Sends the Answer Form * @param answer <u>must be an answer-form</u> * @param chat */ private void sendAnswerForm(Form answer, MultiUserChat chat) { ChatRoom room = SparkManager.getChatManager().getChatRoom(chat.getRoom()); for (String key : _map.keySet()) { String value = getValueFromComponent(key); answer.setAnswer(key, value); } try { chat.sendRegistrationForm(answer); String reg = Res.getString("message.groupchat.registered.member", chat.getRoom()); room.getTranscriptWindow().insertNotificationMessage(reg,ChatManager.NOTIFICATION_COLOR); } catch (XMPPException e) { room.getTranscriptWindow().insertNotificationMessage(e.getMessage(),ChatManager.ERROR_COLOR); } }
/** * Action taking when a user clicks on the reject button. */ private void rejectInvitation() { removeUI(); try { ChatRoom chatRoom = SparkManager.getChatManager().getGroupChat(room); if (chatRoom instanceof GroupChatRoom) { GroupChatRoom gcr = (GroupChatRoom)chatRoom; if (!gcr.getMultiUserChat().isJoined()) { chatRoom.closeChatRoom(); } } } catch (ChatNotFoundException e) { // Ignore } MultiUserChat.decline(SparkManager.getConnection(), room, inviter, "No thank you"); }
/** * Creates a new public Conference Room. * * @param roomName the name of the room. * @param serviceName the service name to use (ex.conference.jivesoftware.com) * @return the new ChatRoom created. If an error occured, null will be returned. */ public ChatRoom createConferenceRoom(String roomName, String serviceName) { final MultiUserChat chatRoom = new MultiUserChat(SparkManager.getConnection(), roomName + "@" + serviceName); final GroupChatRoom room = UIComponentRegistry.createGroupChatRoom(chatRoom); try { LocalPreferences pref = SettingsManager.getLocalPreferences(); chatRoom.create(pref.getNickname()); // Send an empty room configuration form which indicates that we want // an instant room chatRoom.sendConfigurationForm(new Form(Form.TYPE_SUBMIT)); } catch (XMPPException e1) { Log.error("Unable to send conference room chat configuration form.", e1); return null; } getChatContainer().addChatRoom(room); return room; }
/** * Returns the default conference service. (ex. conference.jivesoftware.com) * * @return the default conference service to interact with MUC. */ public String getDefaultConferenceService() { if (conferenceService == null) { try { Collection<String> col = MultiUserChat.getServiceNames(SparkManager.getConnection()); if (col.size() > 0) { conferenceService = col.iterator().next(); } } catch (XMPPException e) { Log.error(e); } } return conferenceService; }
@Override public SampleResult perform(JMeterXMPPSampler sampler, SampleResult res) throws Exception { String room = sampler.getPropertyAsString(ROOM); String nick = sampler.getPropertyAsString(NICKNAME); res.setSamplerData("Join Room: " + room + "/" + nick); MultiUserChat muc = new MultiUserChat(sampler.getXMPPConnection(), room); muc.join(nick); return res; }