/**
*
*
* 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) 2003 OLAT
* Zentrum, University of Zurich, Switzerland. All rights reserved.
*
*
*
* Description:
Implementation of the InstantMessaging Interface based on
* the SMACK instant messaging library from jivesoftware.org
*
* @version $Revision: 1.39 $ Initial Date: 18.01.2005 Last modified: $Date:
* 2005/02/23 14:01:42 $
* @author guido
*/
package org.olat.instantMessaging;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import org.jivesoftware.smack.packet.Presence;
import org.olat.basesecurity.Identity;
import org.olat.basesecurity.Manager;
import org.olat.basesecurity.ManagerFactory;
import org.olat.basesecurity.SecurityGroup;
import org.olat.group.BusinessGroup;
import org.olat.gui.UserRequest;
import org.olat.gui.control.Controller;
import org.olat.gui.control.WindowControl;
import org.olat.instantMessaging.ui.ConferenceRoomController;
import org.olat.instantMessaging.ui.FlashClientController;
import org.olat.instantMessaging.ui.InstantMessagingClientController;
import org.olat.resource.OLATResourceable;
import org.olat.util.Util;
/**
*
*/
public class SmackInstantMessagingImpl implements InstantMessaging {
private final SynchronizeBuddygroupsOverHTTP buddyListSync = new SynchronizeBuddygroupsOverHTTP();
private static final Logger log = Util.getLogger(SmackInstantMessagingImpl.class);
/**
* for clarity since needed for dynamic class loading
*/
public SmackInstantMessagingImpl() {
super();
}
/**
* @see org.olat.instantMessaging.InstantMessaging#createClientController(org.olat.gui.UserRequest,
* org.olat.gui.control.WindowControl)
*/
public Controller createClientController(UserRequest ureq, WindowControl wControl) {
return new InstantMessagingClientController(ureq);
}
/**
* @see org.olat.instantMessaging.InstantMessaging#createChatRoomLinkController(boolean,
* java.lang.String, java.lang.String, org.olat.gui.UserRequest,
* org.olat.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, resourceId, chatRoomName, mode);
}
/**
* @see org.olat.instantMessaging.InstantMessaging#createFlashClientController(org.olat.gui.UserRequest,
* org.olat.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, 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 (log.isDebugEnabled()) log.debug("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 (log.isDebugEnabled()) {
log.debug("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 (log.isDebugEnabled()) {
log.debug("Deleting roster group from instant messaging server::" + groupId);
}
}
/**
* @param groupId
* @param displayName
*/
public void renameRosterGroup(String groupId, String displayName){
buddyListSync.renameSharedGroup(groupId, displayName);
if (log.isDebugEnabled()) {
log.debug("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);
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 (log.isDebugEnabled()) {
log.debug("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 (log.isDebugEnabled()) {
log.debug("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 (log.isDebugEnabled()) {
log.debug("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 (log.isDebugEnabled()) {
log.debug("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 (log.isDebugEnabled()) {
log.debug("synchronizing group::" + group.toString());
}
}
/**
* @see org.olat.instantMessaging.InstantMessaging#createChatRoomString(org.olat.resource.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;
}
}