[Cherbot-commit] SF.net SVN: cherbot: [71] trunk/src/net/sf/cherbot/IRCConnection.java
Status: Alpha
Brought to you by:
christianhujer
|
From: <chr...@us...> - 2007-06-17 10:19:56
|
Revision: 71
http://svn.sourceforge.net/cherbot/?rev=71&view=rev
Author: christianhujer
Date: 2007-06-17 03:19:54 -0700 (Sun, 17 Jun 2007)
Log Message:
-----------
Turned IRCConnection into a command bean. That allows for better testing.
Modified Paths:
--------------
trunk/src/net/sf/cherbot/IRCConnection.java
Modified: trunk/src/net/sf/cherbot/IRCConnection.java
===================================================================
--- trunk/src/net/sf/cherbot/IRCConnection.java 2007-06-16 22:26:06 UTC (rev 70)
+++ trunk/src/net/sf/cherbot/IRCConnection.java 2007-06-17 10:19:54 UTC (rev 71)
@@ -12,53 +12,109 @@
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
+import java.util.List;
+import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import java.util.regex.Matcher;
+import net.sf.japi.io.args.BasicCommand;
+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>
*/
-public class IRCConnection extends AbstractConnection implements Runnable {
+public class IRCConnection extends BasicCommand {
+ /** 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 IRC server host to connect to. */
+ @NotNull private String ircHost;
+
+ /** The IRC server port to connect to. */
+ private int ircPort = 6667;
+
/** The Socket for communication. */
- @NotNull private final Socket socket;
+ @Nullable private Socket socket;
/** The PrintStream to write to. */
- @NotNull private final PrintWriter out;
+ @Nullable private PrintWriter out;
/** The BufferedReader to read from. */
- @NotNull private final BufferedReader in;
+ @Nullable private BufferedReader in;
- /** Creates an IRCConnecntion.
- * @param socket Socket
+ /** Creates an IRCConnection. */
+ public IRCConnection() {
+ }
+
+ /** Main program.
+ * For testing purposes.
+ * @param args Command line arguments
* @throws IOException In case of I/O problems.
*/
- public IRCConnection(@NotNull final Socket socket) throws IOException {
- this.socket = socket;
- out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "utf-8"));
- in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));
- new Thread(this).start();
+ public static void main(final String... args) throws IOException {
+ ArgParser.simpleParseAndRun(new IRCConnection(), args);
}
- /** {@inheritDoc} */
- public void run() {
- final String username = "cherbot";
- final String realname = "CherBot";
- final String nick = "CherBot";
- final String password = "foobar";
- final boolean registerWithNickServ = true;
- final String[] initialChannels = { "#cherbot" };
+ /** Establishes a connection.
+ * @throws IOException In case of connection problems.
+ */
+ private void connect() throws IOException {
+ final Socket socket = new Socket(ircHost, ircPort);
+ this.socket = socket;
+ final PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "utf-8"));
+ this.out = out;
+ this.in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));
out.println("USER " + username + " 0 * :" + realname);
- out.println("NICK " + nick);
- for (final String initialChannel : initialChannels) {
- out.println("JOIN " + initialChannel);
+ out.println("NICK " + nickname);
+ if (nickPassword != null) {
+ out.println("PRIVMSG NICKSERV :IDENTIFY " + nickPassword);
}
- if (registerWithNickServ) {
- out.println("PRIVMSG NICKSERV :IDENTIFY " + password);
- }
out.flush();
+ }
+
+ /** Closes a connection.
+ * @throws IOException In case of connection problems.
+ */
+ private void disconnect() throws IOException {
try {
+ final Socket socket = this.socket;
+ if (socket != null) {
+ socket.close();
+ }
+ } finally {
+ socket = null;
+ in = null;
+ out = null;
+ }
+ }
+
+ /** {@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 initialChannel : args) {
+ out.println("JOIN " + initialChannel);
+ out.flush();
+ }
final Pattern pattern = Pattern.compile("^:([^!]+)!([^ ]+) PRIVMSG ([^ ]+) :(.*)$");
final Matcher matcher = pattern.matcher("");
for (String line; (line = in.readLine()) != null;) {
@@ -66,29 +122,72 @@
matcher.reset(line);
if (matcher.matches()) {
final String actor = matcher.group(1);
- if (nick.equals(actor)) {
+ if (nickname.equals(actor)) {
continue;
}
- final String actorIdentity = matcher.group(2);
- final String channel = matcher.group(3);
+ //final String actorIdentity = matcher.group(2);
+ //final String channel = matcher.group(3);
final String message = matcher.group(4);
System.out.println("<" + actor + "> " + message);
- out.println("PRIVMSG " + (channel.startsWith("#") ? channel : actor) + " :" + "you said: " + message);
- out.flush();
+ //out.println("PRIVMSG " + (channel.startsWith("#") ? channel : actor) + " :" + "you said: " + message);
+ //out.flush();
}
}
- } catch (final IOException e) {
- System.err.println(e);
+ return 0;
+ } finally {
+ disconnect();
}
}
- /** Main program.
- * For testing purposes.
- * @param args Command line arguments
- * @throws IOException In case of I/O problems.
+ /** 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.
*/
- public static void main(final String... args) throws IOException {
- new IRCConnection(new Socket(args[0], Integer.parseInt(args[1])));
+ @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;
+ }
+
+ /** Sets the host of the IRC server to connect to.
+ * @param ircHost Host of the IRC server.
+ */
+ @Option({"ircHost"})
+ public void setIrcHost(@NotNull final String ircHost) {
+ this.ircHost = ircHost;
+ }
+
+ /** Sets the port of the IRC server to connect to.
+ * @param ircPort Port of the IRC server.
+ */
+ @Option({"ircPort"})
+ public void setIrcPort(@NotNull final Integer ircPort) {
+ this.ircPort = ircPort;
+ }
+
} // class IRCConnection
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|