/** * OLAT - Online Learning and Training
* http://www.olat.org *

* This software is protected by the OLAT software license.
* Use is subject to license terms.
* See LICENSE.TXT in this distribution for details. *

* Copyright (c) 1999-2006 at Multimedia- & E-Learning Services (MELS),
* University of Zurich, Switzerland.
* All rights reserved. *

*/ package org.olat.instantMessaging; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import org.jivesoftware.smack.packet.Presence; import org.olat.basesecurity.Manager; import org.olat.basesecurity.ManagerFactory; import org.olat.basesecurity.SecurityGroup; import org.olat.core.gui.UserRequest; import org.olat.core.gui.control.Controller; import org.olat.core.gui.control.WindowControl; import org.olat.core.id.Identity; import org.olat.core.id.OLATResourceable; import org.olat.core.logging.Tracing; import org.olat.group.BusinessGroup; import org.olat.instantMessaging.ui.ConferenceRoomController; import org.olat.instantMessaging.ui.FlashClientController; import org.olat.instantMessaging.ui.InstantMessagingClientController; /** * * Implementation of the InstantMessaging Interface based on * the SMACK instant messaging library from jivesoftware.org * *

* Initial Date: 18.01.2005
* @author guido */ public class SmackInstantMessagingImpl implements InstantMessaging { private final SynchronizeBuddygroupsOverHTTP buddyListSync = new SynchronizeBuddygroupsOverHTTP(); /** * for clarity since needed for dynamic class loading */ public SmackInstantMessagingImpl() { super(); } /** * @see org.olat.instantMessaging.InstantMessaging#createClientController(org.olat.core.gui.UserRequest, * org.olat.core.gui.control.WindowControl) */ public Controller createClientController(UserRequest ureq, WindowControl wControl) { return new InstantMessagingClientController(ureq, wControl); } /** * @see org.olat.instantMessaging.InstantMessaging#createChatRoomLinkController(boolean, * java.lang.String, java.lang.String, org.olat.core.gui.UserRequest, * org.olat.core.gui.control.WindowControl, int) */ public ConferenceRoomController createChatRoomLinkController(boolean passwordProtected, String resourceId, String chatRoomName, UserRequest ureq, WindowControl wControl, int mode) { if (passwordProtected) { // TODO:gs save password in database for later usage // Authentication auth = ManagerFactory.getManager() // .findAuthentication(identity, PROVIDER_CHATROOMS); // String pw = RandomStringUtils.randomAlphanumeric(6); // ConferenceRoomManager.getInstance().createChatRoom(resourceId, // chatRoomName, pw); } else { // ConferenceRoomManager.getInstance().createChatRoom(resourceId, // chatRoomName); } return new ConferenceRoomController(ureq, wControl, resourceId, chatRoomName, mode); } /** * @see org.olat.instantMessaging.InstantMessaging#createFlashClientController(org.olat.core.gui.UserRequest, * org.olat.core.gui.control.WindowControl, String, String) */ public Controller createFlashClientController(UserRequest ureq, WindowControl wControl, String chatPartner, String chatRoom) { InstantMessagingClient imc = ClientManager.getInstance().getInstantMessagingClient(ureq.getIdentity().getName()); String initialMessage = null; if (imc.hasMessage()) initialMessage = imc.getMessage().getBody(); return new FlashClientController(ureq, wControl, imc.getUsername(), imc.getPassword(), chatPartner, chatRoom, initialMessage); } /** * @see org.olat.instantMessaging.InstantMessaging#addUserToFriendsRoster(java.lang.String, * java.lang.String, java.lang.String, java.lang.String) */ public void addUserToFriendsRoster(String groupOwner, String groupId, String groupname, String username) { // we have to make shure the user has an account on the instant messaging // server // by calling this it gets created if not yet exists. ClientManager.getInstance().getInstantMessagingCredentialForUser(username); // we do not check wether a group already exists, we create it each time buddyListSync.createSharedGroup(groupId, groupname, new String[] { groupOwner }); buddyListSync.addUserToSharedGroup(groupId, username.toLowerCase()); if (Tracing.isDebugEnabled(SmackInstantMessagingImpl.class)){ Tracing.logDebug(SmackInstantMessagingImpl.class, "Adding user to roster group::" + groupId + "username: " + username); } } /** * @see org.olat.instantMessaging.InstantMessaging#removeUserFromFriendsRoster(java.lang.String, * java.lang.String) */ public void removeUserFromFriendsRoster(String groupId, String username) { buddyListSync.removeUserFromSharedGroup(groupId, username.toLowerCase()); if (Tracing.isDebugEnabled(SmackInstantMessagingImpl.class)){ Tracing.logDebug(SmackInstantMessagingImpl.class, "Deleting user from roster group::" + groupId + "username: " + username); } } /** * @see org.olat.instantMessaging.InstantMessaging#deleteRosterGroup(java.lang.String) */ public void deleteRosterGroup(String groupId) { buddyListSync.deleteSharedGroup(groupId); if (Tracing.isDebugEnabled(SmackInstantMessagingImpl.class)){ Tracing.logDebug(SmackInstantMessagingImpl.class, "Deleting roster group from instant messaging server::" + groupId); } } /** * @param groupId * @param displayName */ public void renameRosterGroup(String groupId, String displayName) { buddyListSync.renameSharedGroup(groupId, displayName); if (Tracing.isDebugEnabled(SmackInstantMessagingImpl.class)){ Tracing.logDebug(SmackInstantMessagingImpl.class, "Renaming roster group on instant messaging server::" + groupId); } } /** * @see org.olat.instantMessaging.InstantMessaging#sendStatus(java.lang.String, * java.lang.String) */ public void sendStatus(String username, String message) { InstantMessagingClient imc = ClientManager.getInstance().getInstantMessagingClient(username); String recentStatus = imc.getStatus(); //awareness presence packets get only sended if not "unavailable". Otherwise the unavailable status gets overwritten by an available one. if (!recentStatus.equals(InstantMessagingConstants.PRESENCE_MODE_UNAVAILABLE)) imc.sendPresence(Presence.Type.AVAILABLE, message, 0, Presence.Mode.fromString(imc.getStatus())); } /** * @see org.olat.instantMessaging.InstantMessaging#joinRoom(java.lang.String, * java.lang.String, java.lang.String) */ public void joinRoom(String username, String roomName, String password) { // TODO:gs make password protected rooms possible ClientManager.getInstance().getInstantMessagingClient(username).sendConferenceParamsToFlashClient(roomName); if (Tracing.isDebugEnabled(SmackInstantMessagingImpl.class)){ Tracing.logDebug(SmackInstantMessagingImpl.class, "Joining Chatroom::" + username + "with roomname" + roomName + "and password" + password); } } /** * @see org.olat.instantMessaging.InstantMessaging#startChat(java.lang.String, * java.lang.String) */ public void startChat(String username, String chatPartnerJid) { InstantMessagingClient imClient = ClientManager.getInstance().getInstantMessagingClient(username); if (imClient.getFlashClientIsRunning()) { String message = null; if (imClient.hasMessage()) message = imClient.getMessage().getBody(); imClient.sendChatParamsToFlashClient(chatPartnerJid, message); } if (Tracing.isDebugEnabled(SmackInstantMessagingImpl.class)){ Tracing.logDebug(SmackInstantMessagingImpl.class, "Starting chat::" + username + "with user" + chatPartnerJid); } } /** * @see org.olat.instantMessaging.InstantMessaging#isFlashClientRunning(java.lang.String) */ public boolean isFlashClientRunning(String username) { return ClientManager.getInstance().getInstantMessagingClient(username).getFlashClientIsRunning(); } /** * @see org.olat.instantMessaging.InstantMessaging#getIMPassword(java.lang.String) */ public String getIMPassword(String username) { return ClientManager.getInstance().getInstantMessagingClient(username).getPassword(); } /** * @return Set containing the usernames */ public Set getUsernamesFromConnectedUsers() { return ClientManager.getInstance().getUsernamesOfConnectedUsers(); } /** * @see org.olat.instantMessaging.InstantMessaging#getClients() */ public Map getClients() { return ClientManager.getInstance().getClients(); } /** * @see org.olat.instantMessaging.InstantMessaging#enableChat(java.lang.String) */ public void enableChat(String username) { ClientManager.getInstance().getInstantMessagingClient(username).enableCollaboration(); if (Tracing.isDebugEnabled(SmackInstantMessagingImpl.class)){ Tracing.logDebug(SmackInstantMessagingImpl.class, "Enabling chat for user::" + username); } } /** * @param username * @param reason A resason why the chat is disabled like "Doing test" * @see org.olat.instantMessaging.InstantMessaging#disableChat(java.lang.String, * java.lang.String) */ public void disableChat(String username, String reason) { ClientManager.getInstance().getInstantMessagingClient(username).disableCollaboration(reason); if (Tracing.isDebugEnabled(SmackInstantMessagingImpl.class)){ Tracing.logDebug(SmackInstantMessagingImpl.class, "Disabling chat for user::" + username + "and reason" + reason); } } /** * @see org.olat.instantMessaging.InstantMessaging#countConnectedUsers() */ public int countConnectedUsers() { return ClientManager.getInstance().countConnectedUsers(); } /** * @see org.olat.instantMessaging.InstantMessaging#synchonizeBuddyRoster(org.olat.group.BusinessGroup) */ public void synchonizeBuddyRoster(BusinessGroup group) { Manager securityManager = ManagerFactory.getManager(); SecurityGroup owners = group.getOwnerGroup(); SecurityGroup participants = group.getPartipiciantGroup(); List users = securityManager.getIdentitiesOfSecurityGroup(owners); users.addAll(securityManager.getIdentitiesOfSecurityGroup(participants)); String[] usernames = new String[users.size()]; int i = 0; for (Iterator iter = users.iterator(); iter.hasNext();) { Identity element = (Identity) iter.next(); // as jive only adds users to a group that already exist we have to make // shure they are added. ClientManager.getInstance().getInstantMessagingCredentialForUser(element.getName()); usernames[i] = element.getName(); i++; } String groupId = InstantMessagingModule.getAdapter().createChatRoomString(group); buddyListSync.createSharedGroup(groupId, group.getName(), usernames); if (Tracing.isDebugEnabled(SmackInstantMessagingImpl.class)){ Tracing.logDebug(SmackInstantMessagingImpl.class, "synchronizing group::" + group.toString()); } } /** * @see org.olat.instantMessaging.InstantMessaging#createChatRoomString(org.olat.core.id.OLATResourceable */ public String createChatRoomString(OLATResourceable ores) { return ores.getResourceableTypeName() + "-" + ores.getResourceableId(); } /** * @see org.olat.instantMessaging.InstantMessaging#countUsersRunningFlashClient() */ public int countUsersRunningFlashClient() { int flashUsers = 0; for (Iterator iter = getClients().values().iterator(); iter.hasNext();) { InstantMessagingClient client = (InstantMessagingClient) iter.next(); if (client.getFlashClientIsRunning()) flashUsers++; } return flashUsers; } }