Working on nick change support and improve personal chatrooms.

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

@ -102,12 +102,6 @@ public class ChatRoomIrcImpl
private ArrayList<ChatRoomMember> bannedMembers
= new ArrayList<ChatRoomMember>();
/**
* Indicates if this chat room is a private one (i.e. created with the
* query command ).
*/
private final boolean isPrivate;
/**
* Indicates if this chat room is a system one (i.e. corresponding to the
* server channel).
@ -134,7 +128,7 @@ public class ChatRoomIrcImpl
public ChatRoomIrcImpl( String chatRoomName,
ProtocolProviderServiceIrcImpl parentProvider)
{
this(chatRoomName, parentProvider, false, false);
this(chatRoomName, parentProvider, false);
}
// TODO Need to implement equals() in order to correct compare 2 instances of ChatRoomIrcImpl as representing the same chatroom (room name + parent provider)
@ -151,12 +145,10 @@ public ChatRoomIrcImpl( String chatRoomName,
*/
public ChatRoomIrcImpl( String chatRoomName,
ProtocolProviderServiceIrcImpl parentProvider,
boolean isPrivate,
boolean isSystem)
{
this.parentProvider = parentProvider;
this.chatRoomName = chatRoomName;
this.isPrivate = isPrivate;
this.isSystem = isSystem;
}
@ -169,7 +161,7 @@ public String getName()
{
return chatRoomName;
}
/**
* Returns the identifier of this <tt>ChatRoom</tt>.
*
@ -1013,7 +1005,10 @@ public void fireMemberRoleEvent( ChatRoomMember member,
*/
public boolean isPrivate()
{
return isPrivate;
// TODO Since we cannot change the chat room name/identifier maybe we
// should compute this upon construction and save the result, instead of
// doing a string operation every time the method is called.
return this.chatRoomName.startsWith("#") == false;
}
/**

@ -17,6 +17,10 @@
public class ChatRoomMemberIrcImpl
implements ChatRoomMember
{
// TODO Since we instantiate new instances for every chat room, we should
// override equals() and hashCode() to make sure that we can compare users
// that are actually the same.
/**
* The ChatRoom.
*/

@ -27,6 +27,7 @@ public class IrcStack
private final ProtocolProviderServiceIrcImpl provider;
// TODO should make this thing a map or set?
/**
* Container for joined channels.
*/
@ -310,9 +311,11 @@ public void join(final ChatRoomIrcImpl chatroom, final String password)
if (password == null)
throw new IllegalArgumentException("password cannot be null");
// Make sure that we have not already joined the channel.
if (this.joined.contains(chatroom))
if (this.joined.contains(chatroom) || chatroom.isPrivate())
{
// If we already joined this particular chatroom or if it is a
// private chat room (i.e. message from one user to another), no
// further action is required.
return;
}
@ -445,7 +448,12 @@ public void onFailure(Exception e)
*/
public void leave(ChatRoomIrcImpl chatroom)
{
leave(chatroom.getIdentifier());
if (chatroom.isPrivate() == false)
{
// You only actually join non-private chat rooms, so only these ones
// need to be left.
leave(chatroom.getIdentifier());
}
}
/**
@ -555,86 +563,49 @@ public void onServerNumericMessage(ServerNumericMessage msg)
+ ((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)
public void onUserPrivMessage(UserPrivMsg msg)
{
if (msg == null)
return;
// Find all affected channels.
HashMap<ChatRoomIrcImpl, ChatRoomMemberIrcImpl> affected =
new HashMap<ChatRoomIrcImpl, ChatRoomMemberIrcImpl>();
String oldNick = msg.getSource().getNick();
ChatRoomIrcImpl chatroom = null;
String user = msg.getSource().getNick();
String text = msg.getText();
synchronized (IrcStack.this.joined)
{
for (ChatRoomIrcImpl channel : IrcStack.this.joined)
// Find the chatroom matching the user.
for (ChatRoomIrcImpl room : IrcStack.this.joined)
{
ChatRoomMemberIrcImpl member =
(ChatRoomMemberIrcImpl) channel
.getChatRoomMember(oldNick);
if (member != null)
if (room.isPrivate() == false)
continue;
if (user.equals(room.getIdentifier()))
{
affected.put(channel, member);
chatroom = room;
break;
}
}
}
// 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()))
if (chatroom == null)
{
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);
}
chatroom = initiatePrivateChatRoom(user);
}
deliverReceivedMessageToPrivateChat(chatroom, user, text);
}
private void sendMessageToPrivateChat(ChatRoomIrcImpl chatroom, String user,
String text)
private void deliverReceivedMessageToPrivateChat(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);
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)
private ChatRoomIrcImpl initiatePrivateChatRoom(String user)
{
ChatRoomIrcImpl chatroom = (ChatRoomIrcImpl)IrcStack.this.provider.getMUC().findRoom(user);
ChatRoomIrcImpl chatroom =
(ChatRoomIrcImpl) IrcStack.this.provider.getMUC()
.findRoom(user);
IrcStack.this.joined.add(chatroom);
ChatRoomMemberIrcImpl member =
new ChatRoomMemberIrcImpl(IrcStack.this.provider, chatroom,
@ -644,15 +615,9 @@ private void initiatePrivateChatWithInitialMessage(String user, String message)
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);
return chatroom;
}
}
/**
* A chat room listener.
@ -797,6 +762,31 @@ public void onUserQuit(QuitMessage msg)
}
}
@Override
public void onNickChange(NickMessage msg)
{
if (msg == null)
return;
String oldNick = msg.getSource().getNick();
String newNick = msg.getNewNick();
ChatRoomMemberIrcImpl member =
(ChatRoomMemberIrcImpl) this.chatroom
.getChatRoomMember(oldNick);
if (member != null)
{
member.setName(newNick);
this.chatroom.updateChatRoomMemberName(oldNick);
ChatRoomMemberPropertyChangeEvent evt =
new ChatRoomMemberPropertyChangeEvent(member,
this.chatroom,
ChatRoomMemberPropertyChangeEvent.MEMBER_NICKNAME,
oldNick, newNick);
this.chatroom.fireMemberPropertyChangeEvent(evt);
}
}
@Override
public void onChannelMessage(ChannelPrivMsg msg)
{
@ -812,27 +802,6 @@ public void onChannelMessage(ChannelPrivMsg msg)
this.chatroom.fireMessageReceivedEvent(message, member, new Date(),
ChatRoomMessageReceivedEvent.CONVERSATION_MESSAGE_RECEIVED);
}
@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());
String sourceNick = msg.getSource().getNick();
ChatRoomMember source = this.chatroom.getChatRoomMember(sourceNick);
if (source != null)
{
MessageIrcImpl message =
new MessageIrcImpl("PRIVATE: " + msg.getText(),
"text/plain", "UTF-8", null);
this.chatroom.fireMessageReceivedEvent(message, source,
new Date(),
ChatRoomMessageReceivedEvent.ACTION_MESSAGE_RECEIVED);
}
}
private void processModeMessage(ChannelModeMessage msg)
{

@ -249,7 +249,6 @@ protected ChatRoomIrcImpl findSystemRoom()
serverChatRoom = new ChatRoomIrcImpl(
ircProvider.getAccountID().getService(),
ircProvider,
false, // is private room
true); // is system room
this.fireLocalUserPresenceEvent(

Loading…
Cancel
Save