/**
* 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.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.RosterPacket;
import org.olat.core.commons.taskExecutor.TaskExecutorManager;
import org.olat.core.logging.Tracing;
import org.olat.instantMessaging.rosterandchat.ConnectToServerTask;
/**
* Description:
* Instant Messaging Client class based on the open source library SMACK
* (www.igniterealtime.org). It provides connection to the IM-server, sends
* presence packets, listens to messages and subscription requests. Methods that
* mention the velocity rendering stuff are uses by template files that render
* the html-response that for they dont have references in the java source code.
*
*
* @version Initial Date: 14.10.2004
* @author Guido Schnider
*/
public class InstantMessagingClient {
// username is olat global unique username
private final String username;
// password is auto generated and only used for instant messaging
private final String password;
protected XMPPConnection connection = null;
private long connectionTimestamp = 0;
private Roster roster = null;
// null means 'not connected'
private Presence.Mode presenceMode = null;
private String statusMsg = " ";
// messages are stored if more the one arrives
protected List messages = new ArrayList();
// the jabber id like username@olat.ch
private final String jabberServer;
protected List subscribedUsers = new ArrayList();
private static final String OLATBUDDIES = "OLAT-Buddies";
protected boolean collaborationDisabled = false;
protected boolean isConnected;
private boolean flashClientIsRunning = false;
private boolean showOfflineBuddies = false;
private boolean showGroupsInRoster = true;
private String recentStatusMode = Presence.Mode.available.toString();
/**
* @param username
* @param password
*/
protected InstantMessagingClient(String username, String password) {
this.username = username;
this.password = password;
jabberServer = InstantMessagingModule.getServername();
reconnect(true);
}
/**
* if connections fails upon server crash or other incidents, a new Thread
* gets created and tries to reconnect after a waiting period
*
* @param tryImmediately
*/
protected void reconnect(boolean tryImmediately) {
if (tryImmediately) {
doConnect();
}
}
/**
* connect to the JabberServer
*/
protected void doConnect() {
long initTime = System.currentTimeMillis();
// Debug enables a java window with all traffic between client
// and server.
XMPPConnection.DEBUG_ENABLED = false;
// check if currently used im client is already connected
// (reusing clients from previous session)
connection = new XMPPConnection(InstantMessagingModule.getConnectionConfiguration());
// connecting to server is done by thread pool
TaskExecutorManager.getInstance().runTask(new ConnectToServerTask(this));
connectionTimestamp = System.currentTimeMillis();
}
/**
* change jabber status. Example: sendPresencePacket(Presence.Type.AVAILABLE,
* "at meeting...", 1, Presence.Mode.AWAY);
*
* @param type
* @param status
* @param priority
* @param mode
*/
protected void sendPresence(Presence.Type type, String status, int priority, Presence.Mode mode) {
// get rif of "&" because they break xml packages!
if (status != null) status = status.replaceAll("&", "&");
if (connection == null || !connection.isConnected()) return;
if (collaborationDisabled) return;
setStatus(mode);
Presence presence = new Presence(type);
if (status == null) {
if (mode == Presence.Mode.available) {
status = InstantMessagingConstants.PRESENCE_MODE_AVAILABLE;
} else if (mode == Presence.Mode.away) {
status = InstantMessagingConstants.PRESENCE_MODE_AWAY;
} else if (mode == Presence.Mode.chat) {
status = InstantMessagingConstants.PRESENCE_MODE_CHAT;
} else if (mode == Presence.Mode.dnd) {
status = InstantMessagingConstants.PRESENCE_MODE_DND;
} else if (mode == Presence.Mode.xa) {
status = InstantMessagingConstants.PRESENCE_MODE_XAWAY;
}
presence.setStatus(status);
} else {
presence.setStatus(status);
}
setStatusMsg(presence.getStatus());
presence.setPriority(priority);
if (mode != null) presence.setMode(mode);
try {
connection.sendPacket(presence);
} catch (RuntimeException ex) {
Tracing.logWarn("Error while trying to send Instant Messaging packet for user: " + username + " .Errormessage: ", ex,
InstantMessagingClient.class);
}
}
/**
* send a presence packet "available" with a certain mode e.g. "away" to all
* buddies
*
* @param mode
*/
public void sendPresenceAvailable(Presence.Mode mode) {
sendPresence(Presence.Type.available, null, 0, mode);
}
/**
* send a presence packet "unavailable" to all buddies
*/
public void sendPresenceUnavailable() {
sendPresence(Presence.Type.unavailable, null, 0, null);
}
/**
* while listening to presence packets we are able to determine wether the
* flash client in the second browser window is running or not. Like this we
* get a clear event even when the user closes the window.
*/
// public void addPresenceListener() {
// PacketFilter filter = new PacketTypeFilter(Presence.class);
// connection.createPacketCollector(filter);
// PacketListener myListener = new PacketListener() {
// public void processPacket(Packet packet) {
// Presence presence = (Presence) packet;
// // xiff is the resource string that is hardcoded in the xiff
// // library.
// // if we receive a packet with type "unavailable" and resource
// // string
// // "xiff" the user closed
// // the flash client.
// if (presence.getType() == Presence.Type.unavailable && presence.getFrom().startsWith(getUsername())
// && presence.getFrom().endsWith("xiff")) setFlashClientIsRunning(false);
// if (presence.getType() == Presence.Type.available && presence.getFrom().startsWith(getUsername())
// && presence.getFrom().endsWith("xiff")) {
// setFlashClientIsRunning(true);
// // TODO:gs presence vom flash client wird nur übermittelt
// // beim start und beim stop des
// // clients, die presence während dem laufen werden nicht
// // übermittelt, Frage??? standardconform???
// // setStatus(presence.getMode());
//
// }
// }
// };
// connection.addPacketListener(myListener, filter);
// }
/**
* Each client listens to it's own messages
*/
// public void addMessageListener() {
// PacketFilter filter = new PacketTypeFilter(Message.class);
// connection.createPacketCollector(filter);
// PacketListener myListener = new PacketListener() {
// public void processPacket(Packet packet) {
// Message jabbmessage = (Message) packet;
// jabbmessage.setProperty("receiveTime", new Long(new Date().getTime()));
// if ((jabbmessage.getType() == Message.Type.chat || jabbmessage.getType() == Message.Type.normal) && jabbmessage.getBody() != null) {
// synchronized (messages) {
// messages.add(jabbmessage);
// }
// }
// }
// };
// connection.addPacketListener(myListener, filter);
// }
/**
* By adding this methode (right now added to the contructor) we do have auto
* subscription. All subscribe packets get automatically answered by a
* subscribed packet.
*/
public void addSubscriptionListener() {
PacketFilter filter = new PacketTypeFilter(Presence.class);
connection.createPacketCollector(filter);
PacketListener myListener = new PacketListener() {
public void processPacket(Packet packet) {
Presence presence = (Presence) packet;
if (presence.getType() == Presence.Type.subscribe) {
Presence response = new Presence(Presence.Type.subscribe);
response.setTo(presence.getFrom());
// System.out.println("subscribed to: "+presence.getFrom());
connection.sendPacket(response);
// ask also for subscription
if (!subscribedUsers.contains(presence.getFrom())) {
response = null;
response = new Presence(Presence.Type.subscribe);
response.setTo(presence.getFrom());
connection.sendPacket(response);
// update the roster with the new user
RosterPacket rosterPacket = new RosterPacket();
rosterPacket.setType(IQ.Type.SET);
RosterPacket.Item item = new RosterPacket.Item(presence.getFrom(), parseName(presence.getFrom()));
item.addGroupName(OLATBUDDIES);
item.setItemType(RosterPacket.ItemType.both);
// item.setItemStatus(RosterPacket.ItemStatus.fromString());
rosterPacket.addRosterItem(item);
connection.sendPacket(rosterPacket);
}
}
if (presence.getType() == Presence.Type.subscribe) {
subscribedUsers.add(presence.getFrom());
}
}
};
connection.addPacketListener(myListener, filter);
}
/**
* For unsubscription we have to create a packet like:
");
r = Pattern.compile(patternCrNewline);
m = r.matcher(clean);
clean = m.replaceAll("
");
r = Pattern.compile(patternQuote);
m = r.matcher(clean);
clean = m.replaceAll(" ");
r = Pattern.compile(patternDQuote);
m = r.matcher(clean);
clean = m.replaceAll("");
}
return clean;
}
/**
* @return Returns the password.
*/
public String getPassword() {
return this.password;
}
/**
* @return Returns the username.
*/
public String getUsername() {
return this.username;
}
/**
* @return Returns true when user is connected to server
*/
public boolean isConnected() {
return isConnected;
}
public void setIsConnected(boolean isConnected) {
this.isConnected = isConnected;
}
/**
* @return true if flash client is running otherwise false
*/
protected boolean getFlashClientIsRunning() {
return this.flashClientIsRunning;
}
/**
* When the user opens a second window with the flash client we set this true
*
* @param isRunning Used by Velocity renderer
*/
protected void setFlashClientIsRunning(boolean isRunning) {
this.flashClientIsRunning = isRunning;
}
/**
* TODO:gs used by velocity, change connected client list to client helper
* object
*
* @return Returns the statusMsg.
*/
protected String getStatusMsg() {
return statusMsg;
}
/**
* Free text to add more details to a specific status like status "away" and
* msg "eating lunch until 2pm..."
*
* @param statusMsg The statusMsg to set.
*/
protected void setStatusMsg(String statusMsg) {
if (statusMsg == null) statusMsg = "";
this.statusMsg = statusMsg;
}
/**
* @return Returns the showOfflineBuddies.
*/
protected boolean getShowOfflineBuddies() {
return showOfflineBuddies;
}
/**
* @param showOfflineBuddies The showOfflineBuddies to set.
*/
protected void setShowOfflineBuddies(boolean showOfflineBuddies) {
this.showOfflineBuddies = showOfflineBuddies;
}
/**
* TODO:gs used by velocity, change connected client list to client helper
* object
*
* @return minutes the user is online
*/
protected String getOnlineTime() {
long diff = System.currentTimeMillis() - connectionTimestamp;
return new Integer((int) diff / 1000 / 60).toString();
}
/**
* @return Returns the showGroupsInRoster.
*/
protected boolean isShowGroupsInRoster() {
return showGroupsInRoster;
}
/**
* @param showGroupsInRoster The showGroupsInRoster to set.
*/
protected void setShowGroupsInRoster(boolean showGroupsInRoster) {
this.showGroupsInRoster = showGroupsInRoster;
}
public XMPPConnection getConnection() {
return connection;
}
public String getServerName() {
return jabberServer;
}
}