From 993f5a053eca334b3876af2bb2b50bea67059caa Mon Sep 17 00:00:00 2001 From: Emil Ivov Date: Sat, 10 Mar 2007 14:34:08 +0000 Subject: [PATCH] chat room support for jabber - work in progress (only committing for possible backup) --- .../OperationSetMultiUserChatJabberImpl.java | 160 ++++++++++++++---- .../service/protocol/ChatRoom.java | 42 ++++- .../OperationSetBasicInstantMessaging.java | 6 +- .../protocol/OperationSetMultiUserChat.java | 4 +- 4 files changed, 173 insertions(+), 39 deletions(-) diff --git a/src/net/java/sip/communicator/impl/protocol/jabber/OperationSetMultiUserChatJabberImpl.java b/src/net/java/sip/communicator/impl/protocol/jabber/OperationSetMultiUserChatJabberImpl.java index 2bc25935f..4922e5b61 100644 --- a/src/net/java/sip/communicator/impl/protocol/jabber/OperationSetMultiUserChatJabberImpl.java +++ b/src/net/java/sip/communicator/impl/protocol/jabber/OperationSetMultiUserChatJabberImpl.java @@ -10,7 +10,11 @@ import net.java.sip.communicator.service.protocol.*; import net.java.sip.communicator.service.protocol.event.*; +import org.jivesoftware.smack.*; +import net.java.sip.communicator.util.*; import org.jivesoftware.smackx.muc.MultiUserChat; +import org.jivesoftware.smackx.muc.RoomInfo; +import org.jivesoftware.smack.packet.*; /** * A jabber implementation of the multi user chat operation set. @@ -20,6 +24,8 @@ public class OperationSetMultiUserChatJabberImpl implements OperationSetMultiUserChat { + private static final Logger logger + = Logger.getLogger(OperationSetMultiUserChatJabberImpl.class); /** * The currently valid jabber protocol provider service implementation. @@ -37,16 +43,27 @@ public class OperationSetMultiUserChatJabberImpl */ private Vector invitationRejectionListeners = new Vector(); + /** + * A list of the rooms that are currently open by this account. (We have + * not necessarily joined these rooms). + */ + private Vector currentlyOpenChatRooms = new Vector(); + /** * Instantiates the user operation set with a currently valid instance of * the jabber protocol provider. - * @param jImpl a currently valid instance of + * @param jabberProvider a currently valid instance of * ProtocolProviderServiceJabberImpl. */ OperationSetMultiUserChatJabberImpl( ProtocolProviderServiceJabberImpl jabberProvider) { this.jabberProvider = jabberProvider; + +// throw new RuntimeException("implement invitation listeners"); + /** @todo implement invitation listeners */ +// MultiUserChat.addInvitationListener(jabberProvider.getConnection() +// , this); } /** @@ -63,6 +80,20 @@ public void addInvitationListener(InvitationListener listener) } } + /** + * Removes listener from the list of invitation listeners + * registered to receive invitation events. + * + * @param listener the invitation listener to remove. + */ + public void removeInvitationListener(InvitationListener listener) + { + synchronized(invitationListeners) + { + invitationListeners.remove(listener); + } + } + /** * Subscribes listener so that it would receive events indicating * rejection of a multi user chat invitation that we've sent earlier. @@ -78,7 +109,21 @@ public void addInvitationRejectionListener( if (!invitationRejectionListeners.contains(listener)) invitationRejectionListeners.add(listener); } + } + /** + * Removes listener from the list of invitation listeners + * registered to receive invitation rejection events. + * + * @param listener the invitation listener to remove. + */ + public void removeInvitationRejectionListener(InvitationRejectionListener + listener) + { + synchronized(invitationRejectionListeners) + { + invitationRejectionListeners.remove(listener); + } } /** @@ -89,16 +134,90 @@ public void addInvitationRejectionListener( * @param roomName the name of the ChatRoom to create. * @param roomProperties properties specifying how the room should be * created. + * * @throws OperationFailedException if the room couldn't be created for * some reason (e.g. room already exists; user already joined to an * existant room or user has no permissions to create a chat room). + * @throws OperationNotSupportedException if chat room creation is not + * supported by this server + * * @return ChatRoom the chat room that we've just created. */ public ChatRoom createChatRoom(String roomName, Hashtable roomProperties) - throws OperationFailedException + throws OperationFailedException, OperationNotSupportedException { -// return MultiUserChat.c; - return null; + if(MultiUserChat.isServiceEnabled( + getXmppConnection() + , jabberProvider.getAccountID().getUserID())) + { + throw new OperationNotSupportedException( + "Impossible to create chat rooms on server " + + jabberProvider.getAccountID().getService() + + " for user " + + jabberProvider.getAccountID().getUserID()); + } + + //retrieve room info in order to determine whether the room actually + //exists +// RoomInfo roomInfo = null; +// try +// { +// roomInfo +// = MultiUserChat.getRoomInfo(getXmppConnection(), roomName); +// logger.error("RoomInfo=" + roomInfo.toString()); +// } +// catch (XMPPException ex) +// { +// logger.error("Failed to retrieve room info.", ex); +// if(ex.getXMPPError().getCode() == 404) +// logger.warn("niama iaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); +// throw new OperationFailedException("Failed to retrieve room info." +// , OperationFailedException.GENERAL_ERROR +// , ex); +// } +// + MultiUserChat muc + = new MultiUserChat(getXmppConnection(), roomName); + + try + { + logger.error("MUCI=" + muc.toString()); + logger.debug("muc.getRoom()=" + muc.getRoom()); + logger.debug("muc.getConfigurationForm();=" + + muc.getConfigurationForm()); + logger.debug("muc.getSubject();=" + muc.getSubject()); + logger.debug("muc.getOwners();=" + muc.getOwners()); +// muc.is + } + catch (XMPPException ex2) + { + ex2.printStackTrace(System.out); + throw new OperationFailedException("", 0, ex2); + } + + + try + { + try + { + muc.create("kiki"); + } + catch (XMPPException ex1) + { + } + muc.join("kiki"); + muc.sendMessage("created a room"); + } + catch (XMPPException ex) + { + logger.error("Failed to create or join a chat room", ex); + throw new OperationFailedException( + "Failed to create or join a chat room" + , OperationFailedException.FORBIDDEN); + } + + ChatRoomJabberImpl chatRoom = new ChatRoomJabberImpl(muc); + return chatRoom; } /** @@ -199,33 +318,16 @@ public void rejectInvitation(ChatRoomInvitation invitation) } /** - * Removes listener from the list of invitation listeners - * registered to receive invitation events. - * - * @param listener the invitation listener to remove. - * @todo Implement this - * net.java.sip.communicator.service.protocol.OperationSetMultiUserChat - * method - */ - public void removeInvitationListener(InvitationListener listener) - { - } - - /** - * Removes listener from the list of invitation listeners - * registered to receive invitation rejection events. + * Almost all MultiUserChat methods require an xmpp connection + * param so I added this method only for the sake of utility. * - * @param listener the invitation listener to remove. - * @todo Implement this - * net.java.sip.communicator.service.protocol.OperationSetMultiUserChat - * method + * @return the XMPPConnection currently in use by the jabber provider or + * null if jabber provider has yet to be initialized. */ - public void removeInvitationRejectionListener(InvitationRejectionListener - listener) - { - } - - public static void main(String[] args) + private XMPPConnection getXmppConnection() { + return (jabberProvider == null) + ? null + :jabberProvider.getConnection(); } } diff --git a/src/net/java/sip/communicator/service/protocol/ChatRoom.java b/src/net/java/sip/communicator/service/protocol/ChatRoom.java index 2e5a5e451..d8bf5f858 100644 --- a/src/net/java/sip/communicator/service/protocol/ChatRoom.java +++ b/src/net/java/sip/communicator/service/protocol/ChatRoom.java @@ -6,11 +6,8 @@ */ package net.java.sip.communicator.service.protocol; -import net.java.sip.communicator.service.protocol.event.ChatRoomPropertyChangeListener; -import net.java.sip.communicator.service.protocol.event.ChatRoomLocalUserStatusListener; -import net.java.sip.communicator.service.protocol.event.ChatRoomParticipantStatusListener; -import java.util.*; import net.java.sip.communicator.service.protocol.event.*; +import java.util.*; /** * Represents a chat channel/room/rendez-vous point/ where multiple chat users @@ -32,7 +29,6 @@ public interface ChatRoom * Joins this chat room with the nickname of the local user so that the * user would start receiving events and messages for it. * - * @param nickname the nickname to use. * @throws OperationFailedException with the corresponding code if an error * occurs while joining the room. */ @@ -72,6 +68,8 @@ public void joinAs(String nickname) * OperationFailedException with code IDENTIFICATION_CONFLICT. * * @param nickname the nickname to use. + * @param password a password necessary to authenticate when joining the + * room. * @throws OperationFailedException with the corresponding code if an error * occurs while joining the room. */ @@ -157,7 +155,8 @@ public void setSubject(final String subject) * method throws an OperationFailedException with the corresponding code. * * @param nickname the new nickname within the room. - * @throws OperationFaileEexception if the setting the new nickname changes + * + * @throws OperationFailedException if the setting the new nickname changes * for some reason. */ public void setNickname(String nickname) @@ -247,6 +246,37 @@ public void removeParticipantStatusListener( */ public void removeMessageListener(ChatRoomMessageListener listener); + /** + * Create a Message instance for sending arbitrary MIME-encoding content. + * + * @param content content value + * @param contentType the MIME-type for content + * @param contentEncoding encoding used for content + * @param subject a String subject or null for now subject. + * @return the newly created message. + */ + public Message createMessage(byte[] content, String contentType, + String contentEncoding, String subject); + + /** + * Create a Message instance for sending a simple text messages with default + * (text/plain) content type and encoding. + * + * @param messageText the string content of the message. + * @return Message the newly created message + */ + public Message createMessage(String messageText); + + /** + * Sends the message to the destination indicated by the + * to contact. + * @param message the Message to send. + * @throws java.lang.IllegalStateException if the underlying stack is + * not registered or initialized or if the chat room is not joined. + */ + public void sendMessage(Message message) + throws IllegalStateException; + //include - roominfo /** @todo include room info */ } diff --git a/src/net/java/sip/communicator/service/protocol/OperationSetBasicInstantMessaging.java b/src/net/java/sip/communicator/service/protocol/OperationSetBasicInstantMessaging.java index 55590a6d7..13a546aed 100644 --- a/src/net/java/sip/communicator/service/protocol/OperationSetBasicInstantMessaging.java +++ b/src/net/java/sip/communicator/service/protocol/OperationSetBasicInstantMessaging.java @@ -35,8 +35,8 @@ public interface OperationSetBasicInstantMessaging * @param subject a String subject or null for now subject. * @return the newly created message. */ - Message createMessage(byte[] content, String contentType, - String contentEncoding, String subject); + public Message createMessage(byte[] content, String contentType, + String contentEncoding, String subject); /** * Create a Message instance for sending a simple text messages with default @@ -45,7 +45,7 @@ Message createMessage(byte[] content, String contentType, * @param messageText the string content of the message. * @return Message the newly created message */ - Message createMessage(String messageText); + public Message createMessage(String messageText); /** * Sends the message to the destination indicated by the diff --git a/src/net/java/sip/communicator/service/protocol/OperationSetMultiUserChat.java b/src/net/java/sip/communicator/service/protocol/OperationSetMultiUserChat.java index 35d875ce6..5cf6e4afb 100644 --- a/src/net/java/sip/communicator/service/protocol/OperationSetMultiUserChat.java +++ b/src/net/java/sip/communicator/service/protocol/OperationSetMultiUserChat.java @@ -63,11 +63,13 @@ public List getExistingChatRooms() * @throws OperationFailedException if the room couldn't be created for some * reason (e.g. room already exists; user already joined to an existant * room or user has no permissions to create a chat room). + * @throws OperationNotSupportedException if chat room creation is not + * supported by this server * * @return the newly created ChatRoom named roomName. */ public ChatRoom createChatRoom(String roomName, Hashtable roomProperties) - throws OperationFailedException; + throws OperationFailedException, OperationNotSupportedException; /** * Returns a reference to a chatRoom named roomName or null if