Maintenance, tweaks & Mode enumerator.

fix-message-formatting
Danny van Heumen 12 years ago
parent cd55394686
commit c37d6f45a3

@ -14,7 +14,8 @@
import java.util.Map.Entry;
import java.util.Set;
import net.java.sip.communicator.impl.protocol.irc.ModeParser.Mode;
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.service.protocol.ChatRoomMember;
import net.java.sip.communicator.service.protocol.ChatRoomMemberRole;
import net.java.sip.communicator.service.protocol.RegistrationState;
@ -38,11 +39,7 @@
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;
import com.ircclouds.irc.api.state.IIRCState;
@ -53,13 +50,6 @@ public class IrcStack
{
// private static final Logger LOGGER = Logger.getLogger(IrcStack.class);
// TODO Create an enum for this.
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 Map<String, IRCChannel> joined = Collections
@ -101,32 +91,14 @@ public void connect(String host, int port, String password,
&& this.connectionState.isConnected())
return;
//A container for storing the exception if connecting fails.
// A container for storing the exception if connecting fails.
final Exception[] exceptionContainer = new Exception[1];
this.irc = new IRCApiImpl(true);
this.params.setServer(new IRCServer(host, port, password, false));
synchronized (this.irc)
{
this.irc.addListener(new IMessageListener()
{
@Override
public void onMessage(IMessage msg)
{
if (msg instanceof ServerNotice)
{
System.out.println("NOTICE: "
+ ((ServerNotice) msg).getText());
}
else if (msg instanceof ServerNumericMessage)
{
System.out.println("NUM MSG: "
+ ((ServerNumericMessage) msg).getNumericCode()
+ ": " + ((ServerNumericMessage) msg).getText());
}
}
});
this.irc.addListener(new GenericListener());
// start connecting to the specified server ...
this.irc.connect(this.params, new Callback<IIRCState>()
{
@ -147,8 +119,8 @@ public void onFailure(Exception e)
{
synchronized (IrcStack.this.irc)
{
System.out.println("IRC connection FAILED!");
e.printStackTrace();
System.out.println("IRC connection FAILED! ("
+ e.getMessage() + ")");
exceptionContainer[0] = e;
IrcStack.this.connectionState = null;
IrcStack.this.irc.notifyAll();
@ -171,8 +143,9 @@ public void onFailure(Exception e)
{
this.provider
.setCurrentRegistrationState(RegistrationState.UNREGISTERED);
if (exceptionContainer[0] != null) {
if (exceptionContainer[0] != null)
{
// If an exception happens to be available that explains
// the connection problem, throw it.
throw exceptionContainer[0];
@ -247,7 +220,7 @@ public boolean isJoined(ChatRoomIrcImpl chatroom)
public List<String> getServerChatRoomList()
{
// TODO Implement this.
// TODO Implement this. (Also probably cache the list, to prevent doing too many requests.)
return new ArrayList<String>();
}
@ -261,7 +234,6 @@ public void join(final ChatRoomIrcImpl chatroom, final String password)
if (isConnected() == false)
throw new IllegalStateException(
"Please connect to an IRC server first");
// TODO password as String
if (chatroom == null)
throw new IllegalArgumentException("chatroom cannot be null");
if (password == null)
@ -372,18 +344,7 @@ public void message(ChatRoomIrcImpl chatroom, String 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
+ "'.");
}
return Mode.convertSymbolToRole(mode);
}
private class ChatRoomListener
@ -531,11 +492,11 @@ public void onChannelMessage(ChannelPrivMsg msg)
private void processModeMessage(ChannelModeMessage msg)
{
ModeParser parser = new ModeParser(msg);
for (Mode mode : parser.getModes())
for (ModeEntry mode : parser.getModes())
{
switch (mode.getMode())
{
case 'O':
case OWNER:
String ownerUserName = mode.getParams()[0];
if (isMe(ownerUserName))
{
@ -560,7 +521,7 @@ private void processModeMessage(ChannelModeMessage msg)
}
}
break;
case 'o':
case OPERATOR:
String opUserName = mode.getParams()[0];
if (isMe(opUserName))
{
@ -587,7 +548,7 @@ private void processModeMessage(ChannelModeMessage msg)
}
}
break;
case 'v':
case VOICE:
String voiceUserName = mode.getParams()[0];
if (isMe(voiceUserName))
{

@ -0,0 +1,53 @@
package net.java.sip.communicator.impl.protocol.irc;
import net.java.sip.communicator.service.protocol.ChatRoomMemberRole;
public enum Mode
{
OWNER('O', ChatRoomMemberRole.OWNER),
OPERATOR('o', ChatRoomMemberRole.ADMINISTRATOR),
VOICE('v', ChatRoomMemberRole.MEMBER);
public static ChatRoomMemberRole convertSymbolToRole(char symbol)
{
for(Mode mode : Mode.values())
{
if (mode.getSymbol() == symbol)
{
return mode.getRole();
}
}
throw new IllegalArgumentException("Invalid mode symbol provided. ('"+symbol+"')");
}
public static Mode bySymbol(char symbol)
{
for(Mode mode : Mode.values())
{
if (mode.getSymbol() == symbol)
{
return mode;
}
}
throw new IllegalArgumentException("Unknown mode symbol provided. ('"+symbol+"')");
}
final private char symbol;
final private ChatRoomMemberRole role;
private Mode(char symbol, ChatRoomMemberRole role)
{
this.symbol = symbol;
this.role = role;
}
public char getSymbol()
{
return this.symbol;
}
public ChatRoomMemberRole getRole()
{
return this.role;
}
}

@ -7,7 +7,7 @@
public class ModeParser
{
private final List<Mode> modes = new ArrayList<Mode>();
private final List<ModeEntry> modes = new ArrayList<ModeEntry>();
private int index = 0;
private String[] params;
@ -41,7 +41,7 @@ private void parse(String modestring)
default:
try
{
Mode entry = process(adding, c);
ModeEntry entry = process(adding, c);
modes.add(entry);
}
catch(IllegalArgumentException e)
@ -53,34 +53,33 @@ private void parse(String modestring)
}
}
private Mode process(boolean add, char mode)
private ModeEntry process(boolean add, char mode)
{
switch(mode)
{
case 'O':
return new Mode(add, mode, this.params[this.index++]);
return new ModeEntry(add, Mode.bySymbol(mode), this.params[this.index++]);
case 'o':
return new Mode(add, mode, this.params[this.index++]);
return new ModeEntry(add, Mode.bySymbol(mode), this.params[this.index++]);
case 'v':
return new Mode(add, mode, this.params[this.index++]);
return new ModeEntry(add, Mode.bySymbol(mode), this.params[this.index++]);
default:
throw new IllegalArgumentException(""+mode);
}
}
public List<Mode> getModes()
public List<ModeEntry> getModes()
{
return modes;
}
public static class Mode
public static class ModeEntry
{
private final boolean added;
//TODO Danny: Use enums for modes instead of characters.
private final char mode;
private final Mode mode;
private final String[] params;
protected Mode(boolean add, char mode, String... params)
protected ModeEntry(boolean add, Mode mode, String... params)
{
this.added = add;
this.mode = mode;
@ -92,7 +91,7 @@ public boolean isAdded()
return this.added;
}
public char getMode()
public Mode getMode()
{
return this.mode;
}

@ -0,0 +1,26 @@
package net.java.sip.communicator.impl.protocol.irc.listener;
import com.ircclouds.irc.api.domain.messages.ServerNotice;
import com.ircclouds.irc.api.domain.messages.ServerNumericMessage;
import com.ircclouds.irc.api.domain.messages.interfaces.IMessage;
import com.ircclouds.irc.api.listeners.IMessageListener;
public class GenericListener
implements IMessageListener
{
@Override
public void onMessage(IMessage msg)
{
if (msg instanceof ServerNotice)
{
System.out.println("NOTICE: " + ((ServerNotice) msg).getText());
}
else if (msg instanceof ServerNumericMessage)
{
System.out.println("NUM MSG: "
+ ((ServerNumericMessage) msg).getNumericCode() + ": "
+ ((ServerNumericMessage) msg).getText());
}
}
}

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