Handling of nick change messages.

fix-message-formatting
Danny van Heumen 12 years ago
parent b80e29ce11
commit 2a92b93693

@ -136,6 +136,8 @@ public ChatRoomIrcImpl( String chatRoomName,
{ {
this(chatRoomName, parentProvider, false, false); this(chatRoomName, parentProvider, false, false);
} }
// TODO Need to implement equals() in order to correct compare 2 instances of ChatRoomIrcImpl as representing the same chatroom (room name + parent provider)
/** /**
* Creates an instance of <tt>ChatRoomIrcImpl</tt>, by specifying the room * Creates an instance of <tt>ChatRoomIrcImpl</tt>, by specifying the room
@ -624,7 +626,7 @@ public void removeMessageListener(ChatRoomMessageListener listener)
* @param memberID the identifier of the member * @param memberID the identifier of the member
* @return the <tt>ChatRoomMember</tt> corresponding to the given member id. * @return the <tt>ChatRoomMember</tt> corresponding to the given member id.
*/ */
protected ChatRoomMember getChatRoomMember(String memberID) public ChatRoomMember getChatRoomMember(String memberID)
{ {
return chatRoomMembers.get(memberID); return chatRoomMembers.get(memberID);
} }
@ -1278,4 +1280,14 @@ public void updateSubject(String subject)
firePropertyChangeEvent(topicChangeEvent); firePropertyChangeEvent(topicChangeEvent);
} }
} }
public void updateChatRoomMemberName(String oldName, String newName)
{
synchronized(this.chatRoomMembers)
{
ChatRoomMember member = this.chatRoomMembers.remove(oldName);
if (member != null)
this.chatRoomMembers.put(newName, member);
}
}
} }

@ -25,7 +25,7 @@ public class ChatRoomMemberIrcImpl
/** /**
* The id of the contact. * The id of the contact.
*/ */
private final String contactID; private String contactID;
/** /**
* The provider that created us. * The provider that created us.
@ -108,6 +108,18 @@ public String getName()
{ {
return this.contactID; return this.contactID;
} }
/**
* Set a new name for this ChatRoomMember.
*
* @param newName
*/
public void setName(String newName)
{
if (newName == null)
throw new IllegalArgumentException("newName cannot be null");
this.contactID = newName;
}
/** /**
* Returns the role of this chat room member in its containing room. * Returns the role of this chat room member in its containing room.

@ -5,43 +5,19 @@
*/ */
package net.java.sip.communicator.impl.protocol.irc; package net.java.sip.communicator.impl.protocol.irc;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set;
import net.java.sip.communicator.impl.protocol.irc.ModeParser.ModeEntry; import net.java.sip.communicator.impl.protocol.irc.ModeParser.ModeEntry;
import net.java.sip.communicator.impl.protocol.irc.listener.GenericListener; import net.java.sip.communicator.impl.protocol.irc.listener.*;
import net.java.sip.communicator.service.protocol.ChatRoomMember; import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.ChatRoomMemberRole; import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.service.protocol.RegistrationState;
import net.java.sip.communicator.service.protocol.event.ChatRoomMemberPresenceChangeEvent; import com.ircclouds.irc.api.*;
import net.java.sip.communicator.service.protocol.event.ChatRoomMessageReceivedEvent; import com.ircclouds.irc.api.domain.*;
import net.java.sip.communicator.service.protocol.event.LocalUserChatRoomPresenceChangeEvent; import com.ircclouds.irc.api.domain.messages.*;
import net.java.sip.communicator.service.protocol.event.MessageReceivedEvent; import com.ircclouds.irc.api.listeners.*;
import com.ircclouds.irc.api.state.*;
import com.ircclouds.irc.api.Callback;
import com.ircclouds.irc.api.IRCApi;
import com.ircclouds.irc.api.IRCApiImpl;
import com.ircclouds.irc.api.IServerParameters;
import com.ircclouds.irc.api.domain.IRCChannel;
import com.ircclouds.irc.api.domain.IRCServer;
import com.ircclouds.irc.api.domain.IRCTopic;
import com.ircclouds.irc.api.domain.IRCUser;
import com.ircclouds.irc.api.domain.IRCUserStatus;
import com.ircclouds.irc.api.domain.messages.ChanJoinMessage;
import com.ircclouds.irc.api.domain.messages.ChanPartMessage;
import com.ircclouds.irc.api.domain.messages.ChannelKick;
import com.ircclouds.irc.api.domain.messages.ChannelModeMessage;
import com.ircclouds.irc.api.domain.messages.ChannelPrivMsg;
import com.ircclouds.irc.api.domain.messages.QuitMessage;
import com.ircclouds.irc.api.domain.messages.TopicMessage;
import com.ircclouds.irc.api.listeners.VariousMessageListenerAdapter;
import com.ircclouds.irc.api.state.IIRCState;
/** /**
* An implementation of the PircBot IRC stack. * An implementation of the PircBot IRC stack.
@ -52,15 +28,15 @@ public class IrcStack
private final ProtocolProviderServiceIrcImpl provider; private final ProtocolProviderServiceIrcImpl provider;
private final Map<String, IRCChannel> joined = Collections private final List<ChatRoomIrcImpl> joined = Collections
.synchronizedMap(new HashMap<String, IRCChannel>()); .synchronizedList(new ArrayList<ChatRoomIrcImpl>());
private final ServerParameters params; private final ServerParameters params;
private IRCApi irc; private IRCApi irc;
private IIRCState connectionState; private IIRCState connectionState;
public IrcStack(final ProtocolProviderServiceIrcImpl parentProvider, public IrcStack(final ProtocolProviderServiceIrcImpl parentProvider,
final String nick, final String login, final String version, final String nick, final String login, final String version,
final String finger) final String finger)
@ -91,6 +67,9 @@ public void connect(String host, int port, String password,
&& this.connectionState.isConnected()) && this.connectionState.isConnected())
return; return;
// Make sure we start with an empty joined-channel list.
this.joined.clear();
// A container for storing the exception if connecting fails. // A container for storing the exception if connecting fails.
final Exception[] exceptionContainer = new Exception[1]; final Exception[] exceptionContainer = new Exception[1];
@ -98,7 +77,7 @@ public void connect(String host, int port, String password,
this.params.setServer(new IRCServer(host, port, password, false)); this.params.setServer(new IRCServer(host, port, password, false));
synchronized (this.irc) synchronized (this.irc)
{ {
this.irc.addListener(new GenericListener()); this.irc.addListener(new GenericListener(this.joined));
// start connecting to the specified server ... // start connecting to the specified server ...
this.irc.connect(this.params, new Callback<IIRCState>() this.irc.connect(this.params, new Callback<IIRCState>()
{ {
@ -166,11 +145,13 @@ public void disconnect()
if (this.connectionState == null && this.irc == null) if (this.connectionState == null && this.irc == null)
return; return;
for (String channel : this.joined.keySet()) synchronized(this.joined)
{ {
leave(channel); for (ChatRoomIrcImpl channel : this.joined)
{
leave(channel);
}
} }
this.joined.clear();
this.irc.disconnect(); this.irc.disconnect();
this.irc = null; this.irc = null;
this.connectionState = null; this.connectionState = null;
@ -214,8 +195,7 @@ public void setSubject(ChatRoomIrcImpl chatroom, String subject)
public boolean isJoined(ChatRoomIrcImpl chatroom) public boolean isJoined(ChatRoomIrcImpl chatroom)
{ {
String chatRoomName = chatroom.getIdentifier(); return this.joined.contains(chatroom);
return this.joined.containsKey(chatRoomName);
} }
public List<String> getServerChatRoomList() public List<String> getServerChatRoomList()
@ -263,13 +243,10 @@ public void onSuccess(IRCChannel channel)
{ {
try try
{ {
String name = channel.getName(); IrcStack.this.joined.add(chatroom);
IrcStack.this.joined.put(name, channel);
IrcStack.this.irc IrcStack.this.irc
.addListener(new ChatRoomListener( .addListener(new ChatRoomListener(
chatroom)); chatroom));
IRCTopic topic = channel.getTopic(); IRCTopic topic = channel.getTopic();
chatroom.updateSubject(topic.getValue()); chatroom.updateSubject(topic.getValue());
@ -468,17 +445,27 @@ public void onChannelPart(ChanPartMessage msg)
IrcStack.this.provider.getMUC().fireLocalUserPresenceEvent( IrcStack.this.provider.getMUC().fireLocalUserPresenceEvent(
this.chatroom, this.chatroom,
LocalUserChatRoomPresenceChangeEvent.LOCAL_USER_LEFT, null); LocalUserChatRoomPresenceChangeEvent.LOCAL_USER_LEFT, null);
IrcStack.this.joined.remove(this.chatroom.getIdentifier());
IrcStack.this.irc.deleteListener(this); IrcStack.this.irc.deleteListener(this);
IrcStack.this.joined.remove(this.chatroom);
} }
else else
{ {
String user = msg.getSource().getNick(); String user = msg.getSource().getNick();
ChatRoomMember member = this.chatroom.getChatRoomMember(user); ChatRoomMember member = this.chatroom.getChatRoomMember(user);
// Possibility that 'member' is null (should be fixed now that race condition in irc-api is fixed) try
this.chatroom.fireMemberPresenceEvent(member, null, {
ChatRoomMemberPresenceChangeEvent.MEMBER_LEFT, // Possibility that 'member' is null (should be fixed now
msg.getPartMsg()); // that race condition in irc-api is fixed)
this.chatroom.fireMemberPresenceEvent(member, null,
ChatRoomMemberPresenceChangeEvent.MEMBER_LEFT,
msg.getPartMsg());
}
catch (NullPointerException e)
{
System.err
.println("This should not have happened. Please report this as it is a bug.");
e.printStackTrace();
}
} }
} }
@ -495,8 +482,8 @@ public void onChannelKick(ChannelKick msg)
this.chatroom, this.chatroom,
LocalUserChatRoomPresenceChangeEvent.LOCAL_USER_KICKED, LocalUserChatRoomPresenceChangeEvent.LOCAL_USER_KICKED,
msg.getText()); msg.getText());
IrcStack.this.joined.remove(this.chatroom.getIdentifier());
IrcStack.this.irc.deleteListener(this); IrcStack.this.irc.deleteListener(this);
IrcStack.this.joined.remove(this.chatroom);
} }
else else
{ {
@ -527,7 +514,7 @@ public void onUserQuit(QuitMessage msg)
msg.getQuitMsg()); msg.getQuitMsg());
} }
} }
@Override @Override
public void onChannelMessage(ChannelPrivMsg msg) public void onChannelMessage(ChannelPrivMsg msg)
{ {

@ -1,29 +1,80 @@
package net.java.sip.communicator.impl.protocol.irc.listener; package net.java.sip.communicator.impl.protocol.irc.listener;
import com.ircclouds.irc.api.domain.messages.ServerNotice; import java.util.*;
import com.ircclouds.irc.api.domain.messages.ServerNumericMessage; import java.util.Map.Entry;
import com.ircclouds.irc.api.domain.messages.interfaces.IMessage;
import com.ircclouds.irc.api.listeners.IMessageListener; 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.*;
public class GenericListener public class GenericListener
implements IMessageListener extends VariousMessageListenerAdapter
{ {
private final List<ChatRoomIrcImpl> channels;
public GenericListener(List<ChatRoomIrcImpl> joinedChannels)
{
this.channels = joinedChannels;
}
// TODO Maybe implement this as a IRC server listener and connect a system // TODO Maybe implement this as a IRC server listener and connect a system
// chatroom to each listener in order to inform the user of server // chatroom to each listener in order to inform the user of server
// (chatroom-independent) messages, notices, etc. // (chatroom-independent) messages, notices, etc.
@Override @Override
public void onMessage(IMessage msg) public void onServerNotice(ServerNotice msg)
{ {
if (msg instanceof ServerNotice) System.out.println("NOTICE: " + ((ServerNotice) msg).getText());
}
@Override
public void onServerNumericMessage(ServerNumericMessage msg)
{
System.out.println("NUM MSG: "
+ ((ServerNumericMessage) msg).getNumericCode() + ": "
+ ((ServerNumericMessage) msg).getText());
}
@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)
{ {
System.out.println("NOTICE: " + ((ServerNotice) msg).getText()); for (ChatRoomIrcImpl channel : this.channels)
{
ChatRoomMemberIrcImpl member =
(ChatRoomMemberIrcImpl) channel.getChatRoomMember(oldNick);
if (member != null)
{
affected.put(channel, member);
}
}
} }
else if (msg instanceof ServerNumericMessage)
// 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())
{ {
System.out.println("NUM MSG: " ChatRoomIrcImpl channel = record.getKey();
+ ((ServerNumericMessage) msg).getNumericCode() + ": " ChatRoomMemberIrcImpl member = record.getValue();
+ ((ServerNumericMessage) msg).getText()); member.setName(newNick);
channel.updateChatRoomMemberName(oldNick, newNick);
ChatRoomMemberPropertyChangeEvent evt =
new ChatRoomMemberPropertyChangeEvent(member, channel,
ChatRoomMemberPropertyChangeEvent.MEMBER_NICKNAME, oldNick,
newNick);
channel.fireMemberPropertyChangeEvent(evt);
} }
} }
} }

Loading…
Cancel
Save