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

* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. *

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

*/ 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.components.panel.Panel; 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.groupchat.InstantMessagingGroupChatController; import org.olat.instantMessaging.rosterandchat.InstantMessagingMainController; /** * * 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 InstantMessagingMainController(ureq, 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 Controller createChatRoomLinkController(boolean passwordProtected, OLATResourceable ores, String chatRoomName, UserRequest ureq, WindowControl wControl, int mode, Panel fixcsspanel, boolean compact, boolean anonymous) { 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, ores, chatRoomName, mode); return new InstantMessagingGroupChatController(ureq, wControl, ores, chatRoomName, fixcsspanel, compact, anonymous); } /** * @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("Adding user to roster group::" + groupId + "username: " + username, SmackInstantMessagingImpl.class); } } /** * @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("Deleting user from roster group::" + groupId + "username: " + username, SmackInstantMessagingImpl.class); } } /** * @see org.olat.instantMessaging.InstantMessaging#deleteRosterGroup(java.lang.String) */ public void deleteRosterGroup(String groupId) { buddyListSync.deleteSharedGroup(groupId); if (Tracing.isDebugEnabled(SmackInstantMessagingImpl.class)){ Tracing.logDebug("Deleting roster group from instant messaging server::" + groupId, SmackInstantMessagingImpl.class); } } /** * @param groupId * @param displayName */ public void renameRosterGroup(String groupId, String displayName) { buddyListSync.renameSharedGroup(groupId, displayName); if (Tracing.isDebugEnabled(SmackInstantMessagingImpl.class)){ Tracing.logDebug("Renaming roster group on instant messaging server::" + groupId, SmackInstantMessagingImpl.class); } } /** * @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.valueOf(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("Joining Chatroom::" + username + "with roomname" + roomName + "and password" + password, SmackInstantMessagingImpl.class); // } // // } /** * @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("Starting chat::" + username + "with user" + chatPartnerJid, SmackInstantMessagingImpl.class); // } // // } /** * @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("Enabling chat for user::" + username, SmackInstantMessagingImpl.class); } } /** * @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("Disabling chat for user::" + username + "and reason" + reason, SmackInstantMessagingImpl.class); } } /** * @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("synchronizing group::" + group.toString(), SmackInstantMessagingImpl.class); } } /** * @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; // } }