[Cherbot-commit] SF.net SVN: cherbot: [112] trunk/src/net/sf/cherbot
Status: Alpha
Brought to you by:
christianhujer
|
From: <chr...@us...> - 2007-07-01 17:36:08
|
Revision: 112
http://svn.sourceforge.net/cherbot/?rev=112&view=rev
Author: christianhujer
Date: 2007-07-01 10:36:00 -0700 (Sun, 01 Jul 2007)
Log Message:
-----------
Moved connection related stuff into a separate package.
Modified Paths:
--------------
trunk/src/net/sf/cherbot/ClanModule.java
trunk/src/net/sf/cherbot/EchoModule.java
trunk/src/net/sf/cherbot/Module.java
trunk/src/net/sf/cherbot/PlayerModule.java
trunk/src/net/sf/cherbot/ServerModule.java
Added Paths:
-----------
trunk/src/net/sf/cherbot/connection/
trunk/src/net/sf/cherbot/connection/AbstractCFConnection.java
trunk/src/net/sf/cherbot/connection/AbstractChannel.java
trunk/src/net/sf/cherbot/connection/AbstractConnection.java
trunk/src/net/sf/cherbot/connection/Channel.java
trunk/src/net/sf/cherbot/connection/CrossfireConnection.java
trunk/src/net/sf/cherbot/connection/DaimoninConnection.java
trunk/src/net/sf/cherbot/connection/IRCConnection.java
trunk/src/net/sf/cherbot/connection/package-info.java
Modified: trunk/src/net/sf/cherbot/ClanModule.java
===================================================================
--- trunk/src/net/sf/cherbot/ClanModule.java 2007-06-30 20:12:40 UTC (rev 111)
+++ trunk/src/net/sf/cherbot/ClanModule.java 2007-07-01 17:36:00 UTC (rev 112)
@@ -8,6 +8,7 @@
import org.jetbrains.annotations.NotNull;
import java.sql.Connection;
+import net.sf.cherbot.connection.Channel;
/** Module for Clans.
* @author <a href="mailto:ch...@ri...">Christian Hujer</a>
Modified: trunk/src/net/sf/cherbot/EchoModule.java
===================================================================
--- trunk/src/net/sf/cherbot/EchoModule.java 2007-06-30 20:12:40 UTC (rev 111)
+++ trunk/src/net/sf/cherbot/EchoModule.java 2007-07-01 17:36:00 UTC (rev 112)
@@ -7,6 +7,7 @@
package net.sf.cherbot;
import org.jetbrains.annotations.NotNull;
+import net.sf.cherbot.connection.Channel;
/** Module that simply echoes a message.
* @author <a href="mailto:ch...@ri...">Christian Hujer</a>
Modified: trunk/src/net/sf/cherbot/Module.java
===================================================================
--- trunk/src/net/sf/cherbot/Module.java 2007-06-30 20:12:40 UTC (rev 111)
+++ trunk/src/net/sf/cherbot/Module.java 2007-07-01 17:36:00 UTC (rev 112)
@@ -7,6 +7,7 @@
package net.sf.cherbot;
import org.jetbrains.annotations.NotNull;
+import net.sf.cherbot.connection.Channel;
/** Interface for Modules.
* @author <a href="mailto:ch...@ri...">Christian Hujer</a>
Modified: trunk/src/net/sf/cherbot/PlayerModule.java
===================================================================
--- trunk/src/net/sf/cherbot/PlayerModule.java 2007-06-30 20:12:40 UTC (rev 111)
+++ trunk/src/net/sf/cherbot/PlayerModule.java 2007-07-01 17:36:00 UTC (rev 112)
@@ -13,6 +13,7 @@
import java.sql.ResultSet;
import java.util.List;
import java.util.ArrayList;
+import net.sf.cherbot.connection.Channel;
/** Module for managing players.
* @author <a href="mailto:ch...@ri...">Christian Hujer</a>
Modified: trunk/src/net/sf/cherbot/ServerModule.java
===================================================================
--- trunk/src/net/sf/cherbot/ServerModule.java 2007-06-30 20:12:40 UTC (rev 111)
+++ trunk/src/net/sf/cherbot/ServerModule.java 2007-07-01 17:36:00 UTC (rev 112)
@@ -13,6 +13,7 @@
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
+import net.sf.cherbot.connection.Channel;
/** Module for managing servers.
* @author <a href="mailto:ch...@ri...">Christian Hujer</a>
Copied: trunk/src/net/sf/cherbot/connection/AbstractCFConnection.java (from rev 111, trunk/src/net/sf/cherbot/AbstractCFConnection.java)
===================================================================
--- trunk/src/net/sf/cherbot/connection/AbstractCFConnection.java (rev 0)
+++ trunk/src/net/sf/cherbot/connection/AbstractCFConnection.java 2007-07-01 17:36:00 UTC (rev 112)
@@ -0,0 +1,141 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package net.sf.cherbot.connection;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.BufferedOutputStream;
+import java.net.Socket;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.annotations.NotNull;
+import net.sf.japi.io.args.Option;
+import net.sf.japi.io.args.OptionType;
+
+/**
+ * Common base class for Connections that use Crossfire-style protocols such as Angelion or Daimonin and of course Crossfire itself.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public abstract class AbstractCFConnection extends AbstractConnection {
+
+ /** The username to authenticate with. */
+ @NotNull private String username;
+
+ /** The password to authenticate with. */
+ @NotNull private String password;
+
+ /** The PrintStream to write to. */
+ @Nullable private OutputStream out;
+
+ /** The BufferedReader to read from. */
+ @Nullable private InputStream in;
+
+ /** Creates an AbstractCFConnection. */
+ protected AbstractCFConnection() {
+ }
+
+ /** Reads and discards a single message.
+ * @throws IOException In case of I/O problems.
+ */
+ protected void readToNull() throws IOException {
+ receivePacket();
+ }
+
+ /** Reads the next data packet.
+ * @throws IOException In case of I/O problems.
+ * @return The data of the next data packet.
+ */
+ @NotNull protected byte[] receivePacket() throws IOException {
+ final InputStream in = this.in;
+ assert in != null;
+ final int h1 = in.read();
+ final int length = h1 != -1 && (h1 & 0x80) == 0x80 ? h1 << 16 | in.read() << 8 | in.read() : h1 << 8 | in.read();
+ if (length == -1) {
+ throw new EOFException();
+ }
+ final byte[] data = new byte[length];
+ for (int read = 0; read < length;) {
+ final int bytesRead = in.read(data, read, length - read);
+ if (bytesRead == -1) {
+ throw new EOFException();
+ }
+ read += bytesRead;
+ }
+ return data;
+ }
+
+ /** Sends a simple text message.
+ * @param msg Message to send
+ * @throws IOException In case of I/O problems.
+ */
+ protected void textMsg(@NotNull final String msg) throws IOException {
+ final OutputStream out = this.out;
+ assert out != null;
+ final byte[] data = msg.getBytes("utf-8");
+ out.write(0xFF & data.length >> 8);
+ out.write(0xFF & data.length);
+ out.write(data);
+ out.flush();
+ }
+
+ /** {@inheritDoc} */
+ public void close() throws IOException {
+ try {
+ super.close();
+ } finally {
+ in = null;
+ out = null;
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Override public void setSocket(@NotNull final Socket socket) throws IOException {
+ super.setSocket(socket);
+ out = new BufferedOutputStream(socket.getOutputStream());
+ in = socket.getInputStream();
+ }
+
+ /** Sets the username for connecting to this CF-Style server.
+ * @param username Username for connecting to this CF-Style server.
+ */
+ @Option(type = OptionType.REQUIRED, value = {"username"})
+ public void setUsername(@NotNull final String username) {
+ this.username = username;
+ }
+
+ /** Sets the password for connecting to this CF-Style server.
+ * @param password Password for connecting to this CF-Style server.
+ */
+ @Option(type = OptionType.REQUIRED, value = {"password"})
+ public void setPassword(@NotNull final String password) {
+ this.password = password;
+ }
+
+ /** {@inheritDoc} */
+ // Overridden because for CF-Style servers the host is optional.
+ @Option({"host"})
+ public void setHost(@NotNull final String host) {
+ super.setHost(host);
+ }
+
+ @NotNull public String getUsername() {
+ return username;
+ }
+
+ @NotNull public String getPassword() {
+ return password;
+ }
+
+ @Nullable protected OutputStream getOut() {
+ return out;
+ }
+
+ @Nullable protected InputStream getIn() {
+ return in;
+ }
+} // class AbstractCFConnection
Property changes on: trunk/src/net/sf/cherbot/connection/AbstractCFConnection.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Copied: trunk/src/net/sf/cherbot/connection/AbstractChannel.java (from rev 108, trunk/src/net/sf/cherbot/AbstractChannel.java)
===================================================================
--- trunk/src/net/sf/cherbot/connection/AbstractChannel.java (rev 0)
+++ trunk/src/net/sf/cherbot/connection/AbstractChannel.java 2007-07-01 17:36:00 UTC (rev 112)
@@ -0,0 +1,33 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package net.sf.cherbot.connection;
+
+import org.jetbrains.annotations.Nullable;
+
+/** Abstract base implementation of {@link Channel}.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public abstract class AbstractChannel implements Channel {
+
+ /** The name of this channel.
+ * Maybe <code>null</code> if it's the default / no channel.
+ */
+ @Nullable private String channelName;
+
+ /** Creates a Channel.
+ * @param channelName name of this channel.
+ */
+ protected AbstractChannel(@Nullable final String channelName) {
+ this.channelName = channelName;
+ }
+
+ /** {@inheritDoc} */
+ @Nullable public String getChannelName() {
+ return channelName;
+ }
+
+} // class Channel
Property changes on: trunk/src/net/sf/cherbot/connection/AbstractChannel.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Copied: trunk/src/net/sf/cherbot/connection/AbstractConnection.java (from rev 108, trunk/src/net/sf/cherbot/AbstractConnection.java)
===================================================================
--- trunk/src/net/sf/cherbot/connection/AbstractConnection.java (rev 0)
+++ trunk/src/net/sf/cherbot/connection/AbstractConnection.java 2007-07-01 17:36:00 UTC (rev 112)
@@ -0,0 +1,126 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package net.sf.cherbot.connection;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.Serializable;
+import java.net.Socket;
+import net.sf.japi.io.args.BasicCommand;
+import net.sf.japi.io.args.Option;
+import net.sf.japi.io.args.OptionType;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/** An AbstractConnection represents the connection to a server and has one or more channels.
+ * Connections are implemented as command bean.
+ * That means:
+ * <ul>
+ * <li>
+ * An object does not reflect an established connection but has a state.
+ * The connection state can be established or not established.
+ * </li>
+ * <li>
+ * For testing, trying or standalone usage it is possible to "run" a connection as a standalone java program from the command line.
+ * </li>
+ * </ul>
+ * To query whether the connection is open, get the Socket of the connection and check the socket.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public abstract class AbstractConnection extends BasicCommand implements Closeable, Serializable {
+
+ /** The host to connect to.
+ * @serial include
+ */
+ @Nullable private String host;
+
+ /** The port to connect to.
+ * @serial include
+ */
+ private int port;
+
+ /** The socket for the connection. */
+ @Nullable private transient Socket socket;
+
+ /** Sets the host of the IRC server to connect to.
+ * @param host Host of the IRC server.
+ */
+ @Option(type = OptionType.REQUIRED, value = {"host"})
+ public void setHost(@NotNull final String host) {
+ this.host = host;
+ }
+
+ /** Returns the host to connect to.
+ * @return The host to connect to.
+ */
+ @Nullable public String getHost() {
+ return host;
+ }
+
+ /** Sets the port of the IRC server to connect to.
+ * @param port Port of the IRC server.
+ */
+ @Option({"port"})
+ public void setPort(@NotNull final Integer port) {
+ this.port = port;
+ }
+
+ /** Returns the port to connect to.
+ * @return The port to connect to.
+ */
+ public int getPort() {
+ return port;
+ }
+
+ /** Establishes this connection.
+ * Implementations of this method must invoke {@link #setSocket(Socket)}.
+ * @return The socket created for this connection.
+ * @throws IOException in case of connection problems.
+ */
+ @NotNull public Socket connect() throws IOException {
+ final Socket socket = new Socket(host, port);
+ setSocket(socket);
+ return socket;
+ }
+
+ /** {@inheritDoc} */
+ public void close() throws IOException {
+ try {
+ final Socket socket = this.socket;
+ if (socket != null) {
+ socket.close();
+ }
+ } finally {
+ socket = null;
+ }
+ }
+
+ /** Returns whether this connection is established.
+ * @return <code>true</code> if this connection is established, otherwise <code>false</code>.
+ * @see Socket#isConnected()
+ */
+ public boolean isConnected() {
+ return socket != null && socket.isConnected();
+ }
+
+ /** Sets the socket for the connection.
+ * Use this method if you want to create a connection on an existing socket instead of letting this Connection automatically create its own socket.
+ * @param socket Socket for the connection.
+ * @throws IOException In case of I/O problems when setting the socket. This is useful for overriding methods which may be interested in using the socket when it's being set.
+ */
+ public void setSocket(@NotNull final Socket socket) throws IOException {
+ this.socket = socket;
+ }
+
+ /** Returns the socket for the connection.
+ * @return The socket for the connection.
+ */
+ @Nullable public Socket getSocket() {
+ return socket;
+ }
+
+} // class AbstractConnection
Property changes on: trunk/src/net/sf/cherbot/connection/AbstractConnection.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Copied: trunk/src/net/sf/cherbot/connection/Channel.java (from rev 108, trunk/src/net/sf/cherbot/Channel.java)
===================================================================
--- trunk/src/net/sf/cherbot/connection/Channel.java (rev 0)
+++ trunk/src/net/sf/cherbot/connection/Channel.java 2007-07-01 17:36:00 UTC (rev 112)
@@ -0,0 +1,28 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package net.sf.cherbot.connection;
+
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/** A Channel uniquely identifies a protocol (type) / server / channel combination.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public interface Channel {
+
+ /** Sends a message to this channel.
+ * @param msg Message to send
+ */
+ 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 String getChannelName();
+
+} // interface Channel
Property changes on: trunk/src/net/sf/cherbot/connection/Channel.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Copied: trunk/src/net/sf/cherbot/connection/CrossfireConnection.java (from rev 111, trunk/src/net/sf/cherbot/CrossfireConnection.java)
===================================================================
--- trunk/src/net/sf/cherbot/connection/CrossfireConnection.java (rev 0)
+++ trunk/src/net/sf/cherbot/connection/CrossfireConnection.java 2007-07-01 17:36:00 UTC (rev 112)
@@ -0,0 +1,66 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package net.sf.cherbot.connection;
+
+import java.io.IOException;
+import java.net.Socket;
+import java.util.List;
+import net.sf.japi.io.args.ArgParser;
+import org.jetbrains.annotations.NotNull;
+
+/** A CrossfireConnection represents a connection to a Crossfire server.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public class CrossfireConnection extends AbstractCFConnection {
+
+ /** Default server host. */
+ //@NotNull public static final String DEFAULT_CROSSFIRE_HOST = "crossfire.metalforge.net";
+ @NotNull public static final String DEFAULT_CROSSFIRE_HOST = "127.0.0.1";
+
+ /** Default server port. */
+ public static final int DEFAULT_CROSSFIRE_PORT = 13327;
+
+ /** Creates a CrossfireConnection. */
+ public CrossfireConnection() {
+ setHost(DEFAULT_CROSSFIRE_HOST);
+ setPort(DEFAULT_CROSSFIRE_PORT);
+ }
+
+ /** Main program.
+ * @param args Command line arguments.
+ */
+ public static void main(final String... args) {
+ ArgParser.simpleParseAndRun(new CrossfireConnection(), args);
+ }
+
+ /** {@inheritDoc} */
+ @NotNull @Override public Socket connect() throws IOException {
+ final Socket socket = super.connect();
+ readToNull();
+ textMsg("version 1023 1023 CherBot");
+ textMsg("setup bot 1");
+ readToNull();
+ readToNull();
+ textMsg("addme");
+ readToNull();
+ textMsg("reply " + getUsername());
+ readToNull();
+ textMsg("reply " + getPassword());
+ return socket;
+ }
+
+ /** {@inheritDoc} */
+ public int run(@NotNull final List<String> args) throws Exception {
+ connect();
+ for (int i = 0; i < 10000; i++) {
+ readToNull();
+ }
+ close();
+ return 0;
+ }
+
+} // class CrossfireConnection
Property changes on: trunk/src/net/sf/cherbot/connection/CrossfireConnection.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Copied: trunk/src/net/sf/cherbot/connection/DaimoninConnection.java (from rev 111, trunk/src/net/sf/cherbot/DaimoninConnection.java)
===================================================================
--- trunk/src/net/sf/cherbot/connection/DaimoninConnection.java (rev 0)
+++ trunk/src/net/sf/cherbot/connection/DaimoninConnection.java 2007-07-01 17:36:00 UTC (rev 112)
@@ -0,0 +1,64 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package net.sf.cherbot.connection;
+
+import java.io.IOException;
+import java.net.Socket;
+import java.util.List;
+import net.sf.japi.io.args.ArgParser;
+import org.jetbrains.annotations.NotNull;
+
+/** A DaimoninConnection represents a connection to a Daimonin server.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public class DaimoninConnection extends AbstractCFConnection {
+
+ /** Default server host. */
+ //@NotNull public static final String DEFAULT_DAIMONIN_HOST = "daimonin.game-server.cc";
+ @NotNull public static final String DEFAULT_DAIMONIN_HOST = "127.0.0.1";
+
+ /** Default server port. */
+ public static final int DEFAULT_DAIMONIN_PORT = 13327;
+
+ /** Creates a DaimoninConnection. */
+ public DaimoninConnection() {
+ setHost(DEFAULT_DAIMONIN_HOST);
+ setPort(DEFAULT_DAIMONIN_PORT);
+ }
+
+ /** Main program.
+ * @param args Command line arguments.
+ */
+ public static void main(final String... args) {
+ ArgParser.simpleParseAndRun(new DaimoninConnection(), args);
+ }
+
+ /** {@inheritDoc} */
+ @NotNull @Override public Socket connect() throws IOException {
+ final Socket socket = super.connect();
+ readToNull(); // "991023 991023 Daimonin Server"
+ textMsg("version 991023 991023 Daimonin SDL Client"); // we have to cheat or the server won't let us in.
+ textMsg("setup bot 1"); // We are a bot
+ readToNull(); // "0x17 bot FALSE"
+ textMsg("addme");
+ readToNull(); // 0x00 0x06 0x18 "4 QNO"
+ readToNull(); // 0x00 0x01 0x15
+ textMsg("reply L" + getUsername());
+ readToNull(); // 0x00 0x06 0x18 "4 QP0"
+ textMsg("reply " + getPassword());
+ return socket;
+ }
+
+ /** {@inheritDoc} */
+ public int run(@NotNull final List<String> args) throws Exception {
+ connect();
+ Thread.sleep(100000);
+ close();
+ return 0;
+ }
+
+} // class DaimoninConnection
Property changes on: trunk/src/net/sf/cherbot/connection/DaimoninConnection.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Copied: trunk/src/net/sf/cherbot/connection/IRCConnection.java (from rev 110, trunk/src/net/sf/cherbot/IRCConnection.java)
===================================================================
--- trunk/src/net/sf/cherbot/connection/IRCConnection.java (rev 0)
+++ trunk/src/net/sf/cherbot/connection/IRCConnection.java 2007-07-01 17:36:00 UTC (rev 112)
@@ -0,0 +1,270 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package net.sf.cherbot.connection;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+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 net.sf.cherbot.redel.PatternDelegator;
+import net.sf.cherbot.redel.Patterns;
+import net.sf.japi.io.args.ArgParser;
+import net.sf.japi.io.args.Option;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/** An IRCConnection represents a connection to an IRC server.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ * @see <a href="ftp://ftp.rfc-editor.org/in-notes/rfc2812.txt">RFC 2812: Internet Relay Chat: Client Protocol</a>
+ */
+public class IRCConnection extends AbstractConnection {
+
+ /** The default IRC port. */
+ public static final int DEFAULT_IRC_PORT = 6667;
+
+ /** The username to use for IRC authentication.
+ * This is the IRC username, not the IRC nick.
+ */
+ @NotNull private String username = "cherbot";
+
+ /** The realname to use for IRC authentication.
+ * This is the realname, not the IRC nick.
+ */
+ @NotNull private String realname = "CherBot";
+
+ /** The nickname to use for IRC. */
+ @NotNull private String nickname = "CherBot";
+
+ /** The password to authenticate with NickServ or <code>null</code> if no authentication is desired. */
+ @Nullable private String nickPassword;
+
+ /** The PrintStream to write to. */
+ @Nullable private PrintWriter out;
+
+ /** 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);
+ }
+
+ /** Main program.
+ * For testing purposes.
+ * @param args Command line arguments
+ * @throws IOException In case of I/O problems.
+ */
+ public static void main(final String... args) throws IOException {
+ ArgParser.simpleParseAndRun(new IRCConnection(), args);
+ }
+
+ /** {@inheritDoc} */
+ @Override @NotNull public Socket connect() throws IOException {
+ final Socket socket = super.connect();
+ final PrintWriter out = this.out;
+ assert this.out != null;
+ out.println("USER " + username + " 0 * :" + realname);
+ out.println("NICK " + nickname);
+ if (nickPassword != null) {
+ out.println("PRIVMSG NICKSERV :IDENTIFY " + nickPassword);
+ }
+ out.flush();
+ return socket;
+ }
+
+ /** {@inheritDoc} */
+ @Override public void close() throws IOException {
+ channels.clear();
+ try {
+ super.close();
+ } finally {
+ in = null;
+ out = null;
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Override public void setSocket(@NotNull final Socket socket) throws IOException {
+ super.setSocket(socket);
+ out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "utf-8"));
+ in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));
+ }
+
+ /** {@inheritDoc} */
+ public int run(@NotNull final List<String> args) throws Exception {
+ connect();
+ try {
+ final PrintWriter out = this.out;
+ assert out != null;
+ final BufferedReader in = this.in;
+ assert in != null;
+ for (final String channelName : args) {
+ join(channelName);
+ }
+ final PatternDelegator delegator = new PatternDelegator(this);
+ for (String line; (line = in.readLine()) != null;) {
+ System.out.print("> ");
+ System.out.println(line);
+ delegator.process(line);
+ }
+ return 0;
+ } finally {
+ close();
+ }
+ }
+
+ /** Processes an INVITE irc message.
+ * @param actor Actor of this JOIN.
+ * @param actorIdentity Identity of the actor.
+ * @param target Target of the invitation.
+ * @param channel Channel to join.
+ */
+ @Patterns({"^:([^!]+)!([^ ]+) INVITE ([^ ]+) :(.*)$"})
+ public void processINVITE(@NotNull final String actor, @NotNull final String actorIdentity, @NotNull final String target, @NotNull final String channel) {
+ System.err.println("Received invitation to " + target + " at " + channel + " by " + actor + " (" + actorIdentity + ")");
+ join(channel);
+ }
+
+ /** Processes a JOIN irc message.
+ * @param actor Actor of this JOIN.
+ * @param actorIdentity Identity of the actor.
+ * @param channel Channel to join.
+ */
+ @Patterns({"^:([^!]+)!([^ ]+) JOIN :(.*)$"})
+ public void processJOIN(@NotNull final String actor, @NotNull final String actorIdentity, @NotNull final String channel) {
+ System.err.println("Joined channel " + channel);
+ // Nothing to do for a JOIN.
+ }
+
+ /** Process a MODE irc message.
+ * @param actor Actor of this MODE.
+ * @param actorIdentity Identity of the actor.
+ * @param channel Channel of the mode change.
+ * @param change Mode change.
+ * @param target Target of the mode change.
+ */
+ @Patterns({"^:([^!]+)!([^ ]+) MODE ([^ ]+) ([^ ]+) (.*?) ?$"})
+ public void processMODE(@NotNull final String actor, @NotNull final String actorIdentity, @NotNull final String channel, @NotNull final String change, @NotNull final String target) {
+ System.err.println("Mode change at " + channel + ": " + change + " on " + target + " by " + actor + " (" + actorIdentity + ")");
+ // Nothing to do for a MODE.
+ }
+
+ /** Processes a PING irc message.
+ * @param server Server that requests a PONG.
+ */
+ @Patterns({"^PING :(.*)$"})
+ public void processPING(@NotNull final String server) {
+ final PrintWriter out = this.out;
+ if (out != null) {
+ out.println("PONG " + server);
+ out.flush();
+ }
+ }
+
+ /** Processes a PRIVMSG irc message.
+ * @param actor Actor of this message.
+ * @param actorIdentity Identity of the actor.
+ * @param channelName Name of the channel.
+ * @param message Message text.
+ */
+ @Patterns({"^:([^!]+)!([^ ]+) PRIVMSG ([^ ]+) :(.*)$"})
+ public void processPRIVMSG(@NotNull final String actor, @NotNull final String actorIdentity, @NotNull final String channelName, @NotNull final String message) {
+ Channel channel = channels.get(channelName);
+ if (channel == null) {
+ // TODO: Create channel on demand.
+ } else {
+ // channel.send(message); // echo
+ }
+ System.out.println(channel);
+ }
+
+ /** Joins an IRC channel.
+ * @param rawChannelName name of the channel to join.
+ * @return The channel that was just joined.
+ */
+ public Channel join(@NotNull final String rawChannelName) {
+ final String channelName = rawChannelName.toLowerCase();
+ final PrintWriter out = this.out;
+ assert out != null;
+ out.println("JOIN " + channelName);
+ out.flush();
+ 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.
+ */
+ @Option({"username"})
+ public void setUsername(@NotNull final String username) {
+ this.username = username;
+ }
+
+ /** Sets the realname to use for connecting to the IRC server.
+ * Note that this is the IRC realname, not the nick.
+ * @param realname Realname to use for IRC.
+ */
+ @Option({"realname"})
+ public void setRealname(@NotNull final String realname) {
+ this.realname = realname;
+ }
+
+ /** Sets the nickname to use for connecting to the IRC server.
+ * @param nickname Nickname to use for IRC.
+ */
+ @Option({"nickname"})
+ public void setNickname(@NotNull final String nickname) {
+ this.nickname = nickname;
+ }
+
+ /** Sets the password to use for authentication with NickServ.
+ * Setting it to <code>null</code> means to not authenticate with NickServ.
+ * @param nickPassword Password to use for authentication with NickServ.
+ */
+ @Option({"nickPassword"})
+ public void setNickPassword(@Nullable final String nickPassword) {
+ 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 AbstractChannel {
+
+ /** 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);
+ out.flush();
+ }
+ }
+
+ } // class IRCChannel
+
+} // class IRCConnection
Property changes on: trunk/src/net/sf/cherbot/connection/IRCConnection.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Added: trunk/src/net/sf/cherbot/connection/package-info.java
===================================================================
--- trunk/src/net/sf/cherbot/connection/package-info.java (rev 0)
+++ trunk/src/net/sf/cherbot/connection/package-info.java 2007-07-01 17:36:00 UTC (rev 112)
@@ -0,0 +1,10 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+/** Connection and Channel related stuff.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+package net.sf.cherbot.connection;
Property changes on: trunk/src/net/sf/cherbot/connection/package-info.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|