org.jivesoftware.openfire.clearspace;
java.util.ArrayList;
java.util.Date;
java.util.HashSet;
java.util.List;
java.util.Set;
java.util.TimerTask;
org.dom4j.Element;
org.jivesoftware.openfire.XMPPServer;
org.jivesoftware.openfire.muc.MUCEventDispatcher;
org.jivesoftware.openfire.muc.MUCEventListener;
org.jivesoftware.openfire.muc.MUCRoom;
org.jivesoftware.openfire.muc.MultiUserChatManager;
org.jivesoftware.openfire.muc.MultiUserChatService;
org.jivesoftware.openfire.user.UserNotFoundException;
org.jivesoftware.util.JiveConstants;
org.jivesoftware.util.JiveGlobals;
org.jivesoftware.util.TaskEngine;
org.slf4j.Logger;
org.slf4j.LoggerFactory;
org.xmpp.packet.IQ;
org.xmpp.packet.JID;
org.xmpp.packet.Message;
MUCEventListener {
Logger Log LoggerFactory.getLogger(ClearspaceMUCTranscriptManager.);
ListClearspaceMUCTranscriptEvent roomEvents;
TaskEngine taskEngine;
TimerTask transcriptUpdateTask;
MAX_QUEUE_SIZE ;
FLUSH_PERIOD
JiveGlobals.getLongProperty(, JiveConstants.MINUTE );
csMucDomain;
csComponentAddress;
ClearspaceMUCTranscriptManager(TaskEngine taskEngine) {
.taskEngine taskEngine;
roomEvents ArrayListClearspaceMUCTranscriptEvent();
xmppDomain XMPPServer.getInstance().getServerInfo().getXMPPDomain();
csMucDomain ClearspaceManager.MUC_SUBDOMAIN xmppDomain;
csComponentAddress ClearspaceManager.CLEARSPACE_COMPONENT xmppDomain;
}
start() {
MUCEventDispatcher.addListener();
transcriptUpdateTask TimerTask() {
run() {
(roomEvents.isEmpty()) {
;
}
Set presenceRoomJids HashSet();
IQ packet IQ();
packet.setTo(csComponentAddress);
packet.setFrom(csMucDomain);
packet.setType(IQ.Type.set);
Element transcriptElement packet.setChildElement(, );
(ClearspaceMUCTranscriptEvent event : roomEvents) {
Element mucEventElement ;
(event.type) {
messageReceived:
mucEventElement transcriptElement.addElement();
mucEventElement.addElement().setText(event.content);
;
occupantJoined:
mucEventElement transcriptElement.addElement();
presenceRoomJids.add(event.roomJID.toBareJID());
;
occupantLeft:
mucEventElement transcriptElement.addElement();
mucEventElement.addAttribute(, );
presenceRoomJids.add(event.roomJID.toBareJID());
;
roomSubjectChanged:
mucEventElement transcriptElement.addElement();
mucEventElement.addElement().setText(event.content);
;
}
(mucEventElement ) {
(event.user ) {
mucEventElement.addAttribute(, event.user.toBareJID());
}
(event.roomJID ) {
mucEventElement.addElement().setText(event.roomJID.toBareJID());
}
mucEventElement.addElement().setText(.toString(event.timestamp));
}
}
MultiUserChatManager mucManager XMPPServer.getInstance().getMultiUserChatManager();
( roomJid : presenceRoomJids) {
JID jid JID(roomJid);
MultiUserChatService mucService mucManager.getMultiUserChatService(jid);
MUCRoom room mucService.getChatRoom(jid.getNode());
totalOccupants room.getOccupantsCount();
(JID owner : room.getOwners()) {
{
(room.getOccupantsByBareJID(owner).isEmpty()) {
totalOccupants;
}
} (UserNotFoundException e) {
}
}
Element occUpdateElement transcriptElement.addElement();
occUpdateElement.addElement().setText(roomJid);
occUpdateElement.addElement().setText(.toString(totalOccupants));
}
IQ result ClearspaceManager.getInstance().query(packet, );
(result ) {
Log.warn();
;
}
(result.getType() IQ.Type.error) {
Log.warn( result.toXML());
;
}
roomEvents.clear();
}
};
taskEngine.schedule(transcriptUpdateTask, FLUSH_PERIOD, FLUSH_PERIOD);
}
stop() {
MUCEventDispatcher.removeListener();
}
roomCreated(JID roomJID) {
}
roomDestroyed(JID roomJID) {
}
occupantJoined(JID roomJID, JID user, nickname) {
(isClearspaceRoom(roomJID) isRoomOwner(roomJID, user)) {
addGroupChatEvent(ClearspaceMUCTranscriptEvent.occupantJoined(roomJID, user, Date().getTime()));
}
}
occupantLeft(JID roomJID, JID user) {
(isClearspaceRoom(roomJID) isRoomOwner(roomJID, user)) {
addGroupChatEvent(ClearspaceMUCTranscriptEvent.occupantLeft(roomJID, user, Date().getTime()));
}
}
nicknameChanged(JID roomJID, JID user, oldNickname, newNickname) {
}
messageReceived(JID roomJID, JID user, nickname, Message message) {
(isClearspaceRoom(roomJID) isRoomOwner(roomJID, user)) {
addGroupChatEvent(ClearspaceMUCTranscriptEvent.messageReceived(roomJID, user, message.getBody(),
Date().getTime()));
}
}
privateMessageRecieved(JID fromJID, JID toJID, Message message) {
}
roomSubjectChanged(JID roomJID, JID user, newSubject) {
(isClearspaceRoom(roomJID) isRoomOwner(roomJID, user)) {
addGroupChatEvent(ClearspaceMUCTranscriptEvent.roomSubjectChanged(roomJID, user, newSubject,
Date().getTime()));
}
}
isRoomOwner(JID roomJID, JID user) {
(user roomJID ) {
;
}
MultiUserChatService chatService
XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(roomJID);
MUCRoom room chatService.getChatRoom(roomJID.getNode());
room room.getOwners().contains(user.toBareJID());
}
isClearspaceRoom(JID roomJID) {
roomJID.getDomain().equals(csMucDomain);
}
addGroupChatEvent(ClearspaceMUCTranscriptEvent event) {
roomEvents.add(event);
(roomEvents.size() MAX_QUEUE_SIZE) {
forceQueueFlush();
}
}
forceQueueFlush() {
transcriptUpdateTask.cancel();
transcriptUpdateTask.run();
taskEngine.schedule(transcriptUpdateTask, FLUSH_PERIOD, FLUSH_PERIOD);
}
}