Working on basic chatroom operations: topic changes, mode changes,

join/part/quit events, messages.
fix-message-formatting
Danny van Heumen 12 years ago
parent ff62aa856f
commit b5157e82f2

@ -38,7 +38,7 @@ public class ChatRoomIrcImpl
/**
* The subject of the chat room.
*/
private String chatSubject = null;
private String chatSubject = "";
/**
* list of members of this chatRoom
@ -118,6 +118,11 @@ public class ChatRoomIrcImpl
* The nick name of the local user for this chat room.
*/
private String userNickName;
/**
* The role of the local user for this chat room.
*/
private ChatRoomMemberRole userRole = ChatRoomMemberRole.MEMBER;
/**
* Creates an instance of <tt>ChatRoomIrcImpl</tt>, by specifying the room
@ -173,6 +178,29 @@ public String getIdentifier()
{
return chatRoomName;
}
/**
* Adds a <tt>ChatRoomMember</tt> to the list of members of this chat room.
*
* @param memberID the identifier of the member
* @param member the <tt>ChatRoomMember</tt> to add.
*/
protected void addChatRoomMember(String memberID, ChatRoomMember member)
{
chatRoomMembers.put(memberID, member);
}
/**
* Removes a <tt>ChatRoomMember</tt> from the list of members of this chat
* room.
*
* @param memberID the name of the <tt>ChatRoomMember</tt> to remove.
*/
protected void removeChatRoomMember(String memberID)
{
chatRoomMembers.remove(memberID);
}
/**
* Joins this chat room with the nickname of the local user so that the user
@ -267,6 +295,7 @@ public boolean isJoined()
public void leave()
{
this.parentProvider.getIrcStack().leave(this);
this.chatRoomMembers.clear();
}
/**
@ -588,28 +617,6 @@ public void removeMessageListener(ChatRoomMessageListener listener)
}
/**
* Adds a <tt>ChatRoomMember</tt> to the list of members of this chat room.
*
* @param memberID the identifier of the member
* @param member the <tt>ChatRoomMember</tt> to add.
*/
protected void addChatRoomMember(String memberID, ChatRoomMember member)
{
chatRoomMembers.put(memberID, member);
}
/**
* Removes a <tt>ChatRoomMember</tt> from the list of members of this chat
* room.
*
* @param memberID the name of the <tt>ChatRoomMember</tt> to remove.
*/
protected void removeChatRoomMember(String memberID)
{
chatRoomMembers.remove(memberID);
}
/**
* Returns the <tt>ChatRoomMember</tt> corresponding to the given member id.
* If no member is found for the given id, returns NULL.
@ -867,6 +874,7 @@ public void fireMessageReceivedEvent( Message message,
*/
public void firePropertyChangeEvent(PropertyChangeEvent evt)
{
Iterable<ChatRoomPropertyChangeListener> listeners;
synchronized (propertyChangeListeners)
{
@ -927,6 +935,16 @@ public void fireMemberPresenceEvent(ChatRoomMember member,
String eventID,
String eventReason)
{
// TODO Separate this from fireMemberPresenceEvent ... it does state modification not informing.
if (eventID == ChatRoomMemberPresenceChangeEvent.MEMBER_JOINED)
{
this.chatRoomMembers.put(member.getContactAddress(), member);
}
else
{
this.chatRoomMembers.remove(member.getContactAddress());
}
ChatRoomMemberPresenceChangeEvent evt;
if(actorMember != null)
evt = new ChatRoomMemberPresenceChangeEvent(
@ -1064,8 +1082,16 @@ public boolean isPersistent()
*/
public ChatRoomMemberRole getUserRole()
{
//TODO Implement correctly, now uses dummy value.
return ChatRoomMemberRole.MEMBER;
return this.userRole;
}
/**
* Prepare the local user role.
* @param role
*/
void prepUserRole(ChatRoomMemberRole role)
{
this.userRole = role;
}
/**
@ -1240,4 +1266,16 @@ public List<String> getMembersWhiteList()
@Override
public void setMembersWhiteList(List<String> members)
{}
public void updateSubject(String subject)
{
if (this.chatSubject.equals(subject) == false)
{
this.chatSubject = subject;
ChatRoomPropertyChangeEvent topicChangeEvent =
new ChatRoomPropertyChangeEvent(this,
ChatRoomPropertyChangeEvent.CHAT_ROOM_SUBJECT, "", subject);
firePropertyChangeEvent(topicChangeEvent);
}
}
}

@ -14,6 +14,7 @@
import java.util.Map.Entry;
import java.util.Set;
import net.java.sip.communicator.service.muc.ChatRoomPresenceStatus;
import net.java.sip.communicator.service.protocol.ChatRoomMember;
import net.java.sip.communicator.service.protocol.ChatRoomMemberRole;
import net.java.sip.communicator.service.protocol.RegistrationState;
@ -21,6 +22,7 @@
import net.java.sip.communicator.service.protocol.event.ChatRoomMessageReceivedEvent;
import net.java.sip.communicator.service.protocol.event.ChatRoomPropertyChangeEvent;
import net.java.sip.communicator.service.protocol.event.LocalUserChatRoomPresenceChangeEvent;
import net.java.sip.communicator.service.protocol.event.RegistrationStateChangeEvent;
import com.ircclouds.irc.api.Callback;
import com.ircclouds.irc.api.IRCApi;
@ -31,10 +33,14 @@
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.ChannelModeMessage;
import com.ircclouds.irc.api.domain.messages.ChannelPrivMsg;
import com.ircclouds.irc.api.domain.messages.QuitMessage;
import com.ircclouds.irc.api.domain.messages.ServerNotice;
import com.ircclouds.irc.api.domain.messages.ServerNumericMessage;
import com.ircclouds.irc.api.domain.messages.TopicMessage;
import com.ircclouds.irc.api.domain.messages.interfaces.IMessage;
import com.ircclouds.irc.api.listeners.IMessageListener;
import com.ircclouds.irc.api.listeners.VariousMessageListenerAdapter;
@ -45,16 +51,21 @@
*/
public class IrcStack
{
//private static final Logger LOGGER = Logger.getLogger(IrcStack.class);
// private static final Logger LOGGER = Logger.getLogger(IrcStack.class);
private static final char MODE_MEMBER_OWNER = 'O';
private static final char MODE_MEMBER_OPERATOR = 'o';
private static final char MODE_MEMBER_VOICE = 'v';
private final ProtocolProviderServiceIrcImpl provider;
private final IRCApi irc = new IRCApiImpl(true);
private final Map<String,IRCChannel> joined = Collections.synchronizedMap(new HashMap<String,IRCChannel>());
private final Map<String, IRCChannel> joined = Collections
.synchronizedMap(new HashMap<String, IRCChannel>());
private final ServerParameters params;
private IRCApi irc;
private IIRCState connectionState;
public IrcStack(final ProtocolProviderServiceIrcImpl parentProvider,
@ -66,22 +77,22 @@ public IrcStack(final ProtocolProviderServiceIrcImpl parentProvider,
throw new NullPointerException("parentProvider cannot be null");
}
this.provider = parentProvider;
this.params =
new IrcStack.ServerParameters(nick, login, finger, null);
this.params = new IrcStack.ServerParameters(nick, login, finger, null);
}
public boolean isConnected()
{
return (this.connectionState != null && this.connectionState
return (this.irc != null && this.connectionState != null && this.connectionState
.isConnected());
}
public void connect(String host, int port, String password,
boolean autoNickChange)
{
if (this.connectionState != null && this.connectionState.isConnected())
if (this.irc != null && this.connectionState != null && this.connectionState.isConnected())
return;
this.irc = new IRCApiImpl(true);
this.params.setServer(new IRCServer(host, port, password, false));
synchronized (this.irc)
{
@ -165,8 +176,15 @@ public void disconnect()
{
if (this.connectionState != null)
{
for (String channel : this.joined.keySet())
{
leave(channel);
}
this.joined.clear();
this.irc.disconnect();
this.irc = null;
this.connectionState = null;
this.provider.setCurrentRegistrationState(RegistrationState.UNREGISTERED);
}
}
@ -228,48 +246,73 @@ public void join(final ChatRoomIrcImpl chatroom, final String password)
throw new IllegalArgumentException("chatroom cannot be null");
if (password == null)
throw new IllegalArgumentException("password cannot be null");
this.irc.joinChannel(chatroom.getIdentifier(), password, new Callback<IRCChannel>(){
@Override
public void onSuccess(IRCChannel channel)
this.irc.joinChannel(chatroom.getIdentifier(), password,
new Callback<IRCChannel>()
{
String name = channel.getName();
IrcStack.this.joined.put(name, channel);
IrcStack.this.provider.getMUC().fireLocalUserPresenceEvent(
chatroom,
LocalUserChatRoomPresenceChangeEvent.LOCAL_USER_JOINED,
null);
IrcStack.this.irc.addListener(new ChatRoomListener(chatroom));
IRCTopic topic = channel.getTopic();
ChatRoomPropertyChangeEvent topicChangeEvent = new ChatRoomPropertyChangeEvent(chatroom, ChatRoomPropertyChangeEvent.CHAT_ROOM_SUBJECT, "", topic.getValue());
chatroom.firePropertyChangeEvent(topicChangeEvent);
for(Entry<IRCUser, Set<IRCUserStatus>> userEntry : channel.getUsers().entrySet())
@Override
public void onSuccess(IRCChannel channel)
{
IRCUser user = userEntry.getKey();
Set<IRCUserStatus> statuses = userEntry.getValue();
ChatRoomMemberIrcImpl member = new ChatRoomMemberIrcImpl(IrcStack.this.provider, chatroom, user.getNick(), ChatRoomMemberRole.MEMBER);
chatroom.fireMemberPresenceEvent(member, null, ChatRoomMemberPresenceChangeEvent.MEMBER_JOINED, null);
String name = channel.getName();
IrcStack.this.joined.put(name, channel);
IrcStack.this.irc
.addListener(new ChatRoomListener(chatroom));
IRCTopic topic = channel.getTopic();
chatroom.updateSubject(topic.getValue());
for (Entry<IRCUser, Set<IRCUserStatus>> userEntry : channel
.getUsers().entrySet())
{
IRCUser user = userEntry.getKey();
Set<IRCUserStatus> statuses = userEntry.getValue();
ChatRoomMemberRole role =
ChatRoomMemberRole.SILENT_MEMBER;
for (IRCUserStatus status : statuses)
{
role = convertMemberMode(status.getChanModeType().charValue());
}
if (IrcStack.this.getNick().equals(user.getNick()))
{
chatroom.prepUserRole(role);
}
else
{
ChatRoomMemberIrcImpl member =
new ChatRoomMemberIrcImpl(IrcStack.this.provider,
chatroom, user.getNick(), role);
chatroom.addChatRoomMember(member.getContactAddress(),
member);
}
}
IrcStack.this.provider.getMUC().fireLocalUserPresenceEvent(
chatroom,
LocalUserChatRoomPresenceChangeEvent.LOCAL_USER_JOINED,
null);
}
}
@Override
public void onFailure(Exception aExc)
{
// TODO Auto-generated method stub
}});
@Override
public void onFailure(Exception aExc)
{
// TODO Auto-generated method stub
}
});
}
public void leave(ChatRoomIrcImpl chatroom)
{
this.irc.leaveChannel(chatroom.getIdentifier());
leave(chatroom.getIdentifier());
}
private void leave(String chatRoomName)
{
this.irc.leaveChannel(chatRoomName);
}
public void banParticipant(ChatRoomIrcImpl chatroom, ChatRoomMember member,
String reason)
{
@ -279,7 +322,7 @@ public void banParticipant(ChatRoomIrcImpl chatroom, ChatRoomMember member,
public void kickParticipant(ChatRoomIrcImpl chatroom,
ChatRoomMember member, String reason)
{
this.irc.kick(chatroom.getName(), member.getContactAddress());
this.irc.kick(chatroom.getIdentifier(), member.getContactAddress());
}
public void invite(String memberId, ChatRoomIrcImpl chatroom)
@ -296,6 +339,137 @@ public void message(ChatRoomIrcImpl chatroom, String message)
{
this.irc.message(chatroom.getIdentifier(), message);
}
private static ChatRoomMemberRole convertMemberMode(char mode)
{
switch(mode)
{
case MODE_MEMBER_OWNER:
return ChatRoomMemberRole.OWNER;
case MODE_MEMBER_OPERATOR:
return ChatRoomMemberRole.ADMINISTRATOR;
case MODE_MEMBER_VOICE:
return ChatRoomMemberRole.MEMBER;
default:
throw new IllegalArgumentException("Unknown member mode '"+mode+"'.");
}
}
private class ChatRoomListener
extends VariousMessageListenerAdapter
{
private ChatRoomIrcImpl chatroom;
private ChatRoomListener(ChatRoomIrcImpl chatroom)
{
if (chatroom == null)
throw new IllegalArgumentException("chatroom cannot be null");
this.chatroom = chatroom;
}
private boolean isThisChatRoom(String chatRoomName)
{
return this.chatroom.getIdentifier().equals(chatRoomName);
}
@Override
public void onTopicChange(TopicMessage msg)
{
if (isThisChatRoom(msg.getChannelName()))
{
this.chatroom.updateSubject(msg.getTopic().getValue());
}
}
@Override
public void onChannelMode(ChannelModeMessage msg)
{
if (isThisChatRoom(msg.getChannelName()))
{
System.out.println("MODE: " + msg.getModeStr());
}
}
@Override
public void onChannelJoin(ChanJoinMessage msg)
{
if (isThisChatRoom(msg.getChannelName()))
{
String user = msg.getSource().getNick();
if (IrcStack.this.getNick().equals(msg.getSource().getNick()))
{
//I think that this should not happen.
}
else
{
ChatRoomMemberIrcImpl member =
new ChatRoomMemberIrcImpl(IrcStack.this.provider,
this.chatroom, user, ChatRoomMemberRole.SILENT_MEMBER);
this.chatroom.fireMemberPresenceEvent(member, null,
ChatRoomMemberPresenceChangeEvent.MEMBER_JOINED, null);
}
}
}
@Override
public void onChannelPart(ChanPartMessage msg)
{
if (isThisChatRoom(msg.getChannelName()))
{
String user = msg.getSource().getNick();
if (IrcStack.this.getNick().equals(msg.getSource().getNick()))
{
IrcStack.this.provider.getMUC().fireLocalUserPresenceEvent(
this.chatroom,
LocalUserChatRoomPresenceChangeEvent.LOCAL_USER_LEFT, null);
IrcStack.this.joined.remove(this.chatroom.getIdentifier());
IrcStack.this.irc.deleteListener(this);
}
else
{
ChatRoomMember member =
this.chatroom.getChatRoomMember(user);
this.chatroom.fireMemberPresenceEvent(member, null,
ChatRoomMemberPresenceChangeEvent.MEMBER_LEFT,
msg.getPartMsg());
}
}
}
@Override
public void onUserQuit(QuitMessage msg)
{
String user = msg.getSource().getNick();
ChatRoomMember member = this.chatroom.getChatRoomMember(user);
if (member != null)
{
this.chatroom.fireMemberPresenceEvent(member, null,
ChatRoomMemberPresenceChangeEvent.MEMBER_QUIT,
msg.getQuitMsg());
}
}
@Override
public void onChannelMessage(ChannelPrivMsg msg)
{
if (isThisChatRoom(msg.getChannelName()))
{
MessageIrcImpl message =
new MessageIrcImpl(msg.getText(), "text/plain", "UTF-8",
null);
ChatRoomMemberIrcImpl member =
new ChatRoomMemberIrcImpl(IrcStack.this.provider,
this.chatroom, msg.getSource().getNick(),
ChatRoomMemberRole.MEMBER);
this.chatroom.fireMessageReceivedEvent(message, member,
new Date(),
ChatRoomMessageReceivedEvent.CONVERSATION_MESSAGE_RECEIVED);
}
}
}
private static class ServerParameters
implements IServerParameters
@ -376,49 +550,4 @@ public void setServer(IRCServer server)
this.server = server;
}
}
private class ChatRoomListener
extends VariousMessageListenerAdapter
{
private ChatRoomIrcImpl chatroom;
private ChatRoomListener(ChatRoomIrcImpl chatroom)
{
if (chatroom == null)
throw new IllegalArgumentException("chatroom cannot be null");
this.chatroom = chatroom;
}
@Override
public void onChannelPart(ChanPartMessage msg)
{
if (this.chatroom.getIdentifier().equals(msg.getChannelName()))
{
IrcStack.this.provider.getMUC().fireLocalUserPresenceEvent(
this.chatroom,
LocalUserChatRoomPresenceChangeEvent.LOCAL_USER_LEFT, null);
IrcStack.this.joined.remove(this.chatroom.getIdentifier());
IrcStack.this.irc.deleteListener(this);
}
}
@Override
public void onChannelMessage(ChannelPrivMsg msg)
{
if (this.chatroom.getIdentifier().equals(msg.getChannelName()))
{
MessageIrcImpl message =
new MessageIrcImpl(msg.getText(), "text/plain", "UTF-8",
null);
ChatRoomMemberIrcImpl member =
new ChatRoomMemberIrcImpl(IrcStack.this.provider,
this.chatroom, msg.getSource().getNick(),
ChatRoomMemberRole.MEMBER);
this.chatroom.fireMessageReceivedEvent(message, member,
new Date(),
ChatRoomMessageReceivedEvent.CONVERSATION_MESSAGE_RECEIVED);
}
}
}
}

Loading…
Cancel
Save