Added initial support for personal chat rooms.

fix-message-formatting
Danny van Heumen 12 years ago
parent bda9567a61
commit e7d1c8a633

@ -9,7 +9,6 @@
import java.util.Map.Entry;
import net.java.sip.communicator.impl.protocol.irc.ModeParser.ModeEntry;
import net.java.sip.communicator.impl.protocol.irc.listener.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
@ -119,7 +118,7 @@ public void connect(String host, int port, String password,
synchronized (this.irc)
{
// register a server listener in order to catch server and cross-/multi-channel messages
this.irc.addListener(new ServerListener(this.joined));
this.irc.addListener(new ServerListener());
// start connecting to the specified server ...
this.irc.connect(this.params, new Callback<IIRCState>()
{
@ -526,6 +525,135 @@ private static ChatRoomMemberRole convertMemberMode(char modeSymbol)
return Mode.bySymbol(modeSymbol).getRole();
}
/**
* A listener for server-level messages (any messages that are related to the
* server, the connection, that are not related to any chatroom in
* particular) or that are personal message from user to local user.
*/
private class ServerListener
extends VariousMessageListenerAdapter
{
/**
* Print out server notices for debugging purposes and for simply
* keeping track of the connections.
*/
@Override
public void onServerNotice(ServerNotice msg)
{
System.out.println("NOTICE: " + ((ServerNotice) msg).getText());
}
/**
* Print out server numeric messages for debugging purposes and for
* simply keeping track of the connection.
*/
@Override
public void onServerNumericMessage(ServerNumericMessage msg)
{
System.out.println("NUM MSG: "
+ ((ServerNumericMessage) msg).getNumericCode() + ": "
+ ((ServerNumericMessage) msg).getText());
}
/**
* Act on nick change messages.
*
* Nick change messages span multiple chat rooms. Specifically every
* chat room where this particular user is joined, needs to get an
* update for the nick change.
*/
@Override
public void onNickChange(NickMessage msg)
{
if (msg == null)
return;
// Find all affected channels.
HashMap<ChatRoomIrcImpl, ChatRoomMemberIrcImpl> affected =
new HashMap<ChatRoomIrcImpl, ChatRoomMemberIrcImpl>();
String oldNick = msg.getSource().getNick();
synchronized (IrcStack.this.joined)
{
for (ChatRoomIrcImpl channel : IrcStack.this.joined)
{
ChatRoomMemberIrcImpl member =
(ChatRoomMemberIrcImpl) channel
.getChatRoomMember(oldNick);
if (member != null)
{
affected.put(channel, member);
}
}
}
// Process nick change for all of those channels and fire
// corresponding
// property change event.
String newNick = msg.getNewNick();
for (Entry<ChatRoomIrcImpl, ChatRoomMemberIrcImpl> record : affected
.entrySet())
{
ChatRoomIrcImpl channel = record.getKey();
ChatRoomMemberIrcImpl member = record.getValue();
member.setName(newNick);
channel.updateChatRoomMemberName(oldNick);
ChatRoomMemberPropertyChangeEvent evt =
new ChatRoomMemberPropertyChangeEvent(member, channel,
ChatRoomMemberPropertyChangeEvent.MEMBER_NICKNAME,
oldNick, newNick);
channel.fireMemberPropertyChangeEvent(evt);
}
}
@Override
public void onUserPrivMessage(UserPrivMsg msg)
{
if (IrcStack.this.getNick().equals(msg.getToUser()))
{
String user = msg.getSource().getNick();
String text = msg.getText();
int index = IrcStack.this.joined.indexOf(user);
if (index == -1)
{
initiatePrivateChatWithInitialMessage(user, text);
}
else
{
sendMessageToPrivateChat(IrcStack.this.joined.get(index), user, text);
}
}
}
private void sendMessageToPrivateChat(ChatRoomIrcImpl chatroom, String user,
String text)
{
ChatRoomMember member = chatroom.getChatRoomMember(user);
MessageIrcImpl message = new MessageIrcImpl(text, "text/plain", "UTF-8", null);
chatroom.fireMessageReceivedEvent(message, member, new Date(), ChatRoomMessageReceivedEvent.CONVERSATION_MESSAGE_RECEIVED);
}
private void initiatePrivateChatWithInitialMessage(String user, String message)
{
ChatRoomIrcImpl chatroom = (ChatRoomIrcImpl)IrcStack.this.provider.getMUC().findRoom(user);
IrcStack.this.joined.add(chatroom);
ChatRoomMemberIrcImpl member =
new ChatRoomMemberIrcImpl(IrcStack.this.provider, chatroom,
user, ChatRoomMemberRole.MEMBER);
chatroom.addChatRoomMember(member.getContactAddress(), member);
IrcStack.this.provider.getMUC().fireLocalUserPresenceEvent(
chatroom,
LocalUserChatRoomPresenceChangeEvent.LOCAL_USER_JOINED,
"Private conversation initiated.");
MessageIrcImpl ircMsg =
new MessageIrcImpl(message, "text/plain", "UTF-8",
"Private chat with " + user);
chatroom.fireMessageReceivedEvent(ircMsg, member, new Date(),
ChatRoomMessageReceivedEvent.CONVERSATION_MESSAGE_RECEIVED);
}
}
/**
* A chat room listener.
*
@ -688,6 +816,8 @@ public void onChannelMessage(ChannelPrivMsg msg)
@Override
public void onUserPrivMessage(UserPrivMsg msg)
{
//TODO Temporary since we'll soon have personal chat rooms.
//TODO DEBUG code
System.out.printf("Received private message from: %s", msg
.getSource().getNick());

@ -36,14 +36,6 @@ public class OperationSetMultiUserChatIrcImpl
private final Map<String, ChatRoom> chatRoomCache
= new Hashtable<String, ChatRoom>();
/**
* A list of all private rooms opened by user on this server. These rooms
* are a result of exchange of private messages between the local user and
* some of the other chat room members.
*/
private final Map<String, ChatRoomIrcImpl> privateRoomCache
= new Hashtable<String, ChatRoomIrcImpl>();
/**
* The <tt>ChatRoom</tt> corresponding to the IRC server channel. This chat
* room is not returned by any of methods getExistingChatRooms(),
@ -222,43 +214,6 @@ private ChatRoom createLocalChatRoomInstance(String chatRoomName)
}
}
/**
* Returns the private room corresponding to the given nick name.
*
* @param nickIdentifier the nickName of the person for which the private
* room is.
* @return the private room corresponding to the given nick name
*/
protected ChatRoomIrcImpl findPrivateChatRoom(String nickIdentifier)
{
ChatRoomIrcImpl chatRoom;
synchronized(privateRoomCache)
{
if(privateRoomCache.containsKey(nickIdentifier))
return privateRoomCache.get(nickIdentifier);
chatRoom =
new ChatRoomIrcImpl(
nickIdentifier,
ircProvider,
true,
false);
privateRoomCache.put(nickIdentifier, chatRoom);
}
/*
* As a rule of thumb, firing inside synchronized blocks increases the
* chances of creating deadlocks.
*/
fireLocalUserPresenceEvent(
chatRoom,
LocalUserChatRoomPresenceChangeEvent.LOCAL_USER_JOINED,
"Private conversation initiated.");
return chatRoom;
}
/**
* Delivers a <tt>ChatRoomInvitationReceivedEvent</tt> to all
* registered <tt>ChatRoomInvitationListener</tt>s.

@ -1,107 +0,0 @@
package net.java.sip.communicator.impl.protocol.irc.listener;
import java.util.*;
import java.util.Map.Entry;
import net.java.sip.communicator.impl.protocol.irc.*;
import net.java.sip.communicator.service.protocol.event.*;
import com.ircclouds.irc.api.domain.messages.*;
import com.ircclouds.irc.api.listeners.*;
/**
* A listener for server-level messages (any messages that are related to the
* server, the connection or that are not related to any chatroom in
* particular).
*/
public class ServerListener
extends VariousMessageListenerAdapter
{
/**
* Reference to the list of joined channels from the IRC connection.
*/
private final List<ChatRoomIrcImpl> channels;
/**
* ServerListener
*
* @param joinedChannels Reference to the list of joined channels.
*/
public ServerListener(List<ChatRoomIrcImpl> joinedChannels)
{
if (joinedChannels == null)
throw new IllegalArgumentException(
"joined channels reference cannot be null");
this.channels = joinedChannels;
}
/**
* Print out server notices for debugging purposes and for simply keeping
* track of the connections.
*/
@Override
public void onServerNotice(ServerNotice msg)
{
System.out.println("NOTICE: " + ((ServerNotice) msg).getText());
}
/**
* Print out server numeric messages for debugging purposes and for simply
* keeping track of the connection.
*/
@Override
public void onServerNumericMessage(ServerNumericMessage msg)
{
System.out.println("NUM MSG: "
+ ((ServerNumericMessage) msg).getNumericCode() + ": "
+ ((ServerNumericMessage) msg).getText());
}
/**
* Act on nick change messages.
*
* Nick change messages span multiple chat rooms. Specifically every chat
* room where this particular user is joined, needs to get an update for the
* nick change.
*/
@Override
public void onNickChange(NickMessage msg)
{
if (msg == null)
return;
// Find all affected channels.
HashMap<ChatRoomIrcImpl, ChatRoomMemberIrcImpl> affected =
new HashMap<ChatRoomIrcImpl, ChatRoomMemberIrcImpl>();
String oldNick = msg.getSource().getNick();
synchronized (this.channels)
{
for (ChatRoomIrcImpl channel : this.channels)
{
ChatRoomMemberIrcImpl member =
(ChatRoomMemberIrcImpl) channel.getChatRoomMember(oldNick);
if (member != null)
{
affected.put(channel, member);
}
}
}
// Process nick change for all of those channels and fire corresponding
// property change event.
String newNick = msg.getNewNick();
for (Entry<ChatRoomIrcImpl, ChatRoomMemberIrcImpl> record : affected
.entrySet())
{
ChatRoomIrcImpl channel = record.getKey();
ChatRoomMemberIrcImpl member = record.getValue();
member.setName(newNick);
channel.updateChatRoomMemberName(oldNick);
ChatRoomMemberPropertyChangeEvent evt =
new ChatRoomMemberPropertyChangeEvent(member, channel,
ChatRoomMemberPropertyChangeEvent.MEMBER_NICKNAME, oldNick,
newNick);
channel.fireMemberPropertyChangeEvent(evt);
}
}
}

@ -1,8 +0,0 @@
/**
* Listeners for irc-api.
*/
/**
* @author danny
*
*/
package net.java.sip.communicator.impl.protocol.irc.listener;
Loading…
Cancel
Save