[Cherbot-commit] SF.net SVN: cherbot: [77] trunk/src/net/sf/cherbot
Status: Alpha
Brought to you by:
christianhujer
|
From: <chr...@us...> - 2007-06-17 17:35:39
|
Revision: 77
http://svn.sourceforge.net/cherbot/?rev=77&view=rev
Author: christianhujer
Date: 2007-06-17 10:35:37 -0700 (Sun, 17 Jun 2007)
Log Message:
-----------
Added Channels.
Modified Paths:
--------------
trunk/src/net/sf/cherbot/Channel.java
trunk/src/net/sf/cherbot/IRCConnection.java
Modified: trunk/src/net/sf/cherbot/Channel.java
===================================================================
--- trunk/src/net/sf/cherbot/Channel.java 2007-06-17 17:09:14 UTC (rev 76)
+++ trunk/src/net/sf/cherbot/Channel.java 2007-06-17 17:35:37 UTC (rev 77)
@@ -14,33 +14,29 @@
*/
public abstract class Channel {
- /** The types of channels. */
- public static enum Type {
-
- /** A Crossfire channel. */
- CROSSFIRE,
-
- /** A Daimonin channel. */
- DAIMONIN,
-
- /** An IRC channel. */
- IRC
- } // enum Type
-
- /** The type of this channel. */
- @NotNull private Type type;
-
- /** The server of this channel. */
- @NotNull private String server;
-
- /** The channel of this channel.
+ /** The name of this channel.
* Maybe <code>null</code> if it's the default / no channel.
*/
- @Nullable private String channel;
+ @Nullable private String channelName;
+ /** Creates a Channel.
+ * @param channelName name of this channel.
+ */
+ protected Channel(@Nullable final String channelName) {
+ this.channelName = channelName;
+ }
+
/** Sends a message to this channel.
* @param msg Message to send
*/
- public abstract void send(@NotNull final String msg);
+ public abstract void send(@NotNull String msg);
+ /** Returns the name of this channel.
+ * Maybe <code>null</code> if it's the default / no channel.
+ * @return The channel of this channel or <code>null</code> if default / no channel.
+ */
+ @Nullable public String getChannelName() {
+ return channelName;
+ }
+
} // class Channel
Modified: trunk/src/net/sf/cherbot/IRCConnection.java
===================================================================
--- trunk/src/net/sf/cherbot/IRCConnection.java 2007-06-17 17:09:14 UTC (rev 76)
+++ trunk/src/net/sf/cherbot/IRCConnection.java 2007-06-17 17:35:37 UTC (rev 77)
@@ -12,7 +12,9 @@
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.japi.io.args.ArgParser;
@@ -50,6 +52,9 @@
/** The BufferedReader to read from. */
@Nullable private BufferedReader in;
+ /** The channels of this connection. */
+ private Map<String, IRCChannel> channels = new HashMap<String, IRCChannel>();
+
/** Creates an IRCConnection. */
public IRCConnection() {
setPort(DEFAULT_IRC_PORT);
@@ -84,6 +89,7 @@
/** {@inheritDoc} */
@Override public void close() throws IOException {
+ channels.clear();
try {
super.close();
} finally {
@@ -128,6 +134,22 @@
}
}
+ /** Joins an IRC channel.
+ * @param channelName name of the channel to join.
+ * @return The channel that was just joined.
+ */
+ public Channel join(@NotNull final String channelName) {
+ final PrintWriter out = this.out;
+ assert out != null;
+ out.println("JOIN " + channelName);
+ IRCChannel channel = channels.get(channelName);
+ if (channels.get(channelName) == null) {
+ channel = new IRCChannel(channelName);
+ channels.put(channelName, channel);
+ }
+ return channel;
+ }
+
/** Sets the username to use for connecting to the IRC server.
* Note that this is the IRC username, not the nick.
* @param username Username to use for IRC.
@@ -163,4 +185,27 @@
this.nickPassword = nickPassword;
}
+ /** Represents a Channel on IRC.
+ * This can be used for both, real channels (whith channel names starting with '#') and direct communication channels with nicks.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+ private class IRCChannel extends Channel {
+
+ /** Creates an IRCChannel.
+ * @param channelName name of this channel.
+ */
+ IRCChannel(@NotNull final String channelName) {
+ super(channelName);
+ }
+
+ /** {@inheritDoc} */
+ public void send(@NotNull final String msg) {
+ final PrintWriter out = IRCConnection.this.out;
+ if (out != null) {
+ out.println("PRIVMSG " + getChannelName() + " :" + msg);
+ }
+ }
+
+ } // class IRCChannel
+
} // class IRCConnection
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|