Skip to content

Commit

Permalink
Allow connection settings (like server, port, password) to be set via…
Browse files Browse the repository at this point in the history
… a class, making it easier to add new settings.
  • Loading branch information
davidlazar committed Feb 17, 2012
1 parent 2ed01fc commit 285a24c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/org/jibble/pircbot/ConnectionSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jibble.pircbot;

public class ConnectionSettings {
public String server;
public int port = 6667;
public String password;

public ConnectionSettings(String server) {
this.server = server;
}
}
38 changes: 30 additions & 8 deletions src/org/jibble/pircbot/PircBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public PircBot() {}
* @throws NickAlreadyInUseException if our nick is already in use on the server.
*/
public final synchronized void connect(String hostname) throws IOException, IrcException, NickAlreadyInUseException {
this.connect(hostname, 6667, null);
ConnectionSettings cs = new ConnectionSettings(hostname);
this.connect(cs);
}


Expand All @@ -104,7 +105,9 @@ public final synchronized void connect(String hostname) throws IOException, IrcE
* @throws NickAlreadyInUseException if our nick is already in use on the server.
*/
public final synchronized void connect(String hostname, int port) throws IOException, IrcException, NickAlreadyInUseException {
this.connect(hostname, port, null);
ConnectionSettings cs = new ConnectionSettings(hostname);
cs.port = port;
this.connect(cs);
}


Expand All @@ -122,10 +125,29 @@ public final synchronized void connect(String hostname, int port) throws IOExcep
* @throws NickAlreadyInUseException if our nick is already in use on the server.
*/
public final synchronized void connect(String hostname, int port, String password) throws IOException, IrcException, NickAlreadyInUseException {
ConnectionSettings cs = new ConnectionSettings(hostname);
cs.port = port;
cs.password = password;
this.connect(cs);
}


/**
* Attempt to connect to an IRC server using the supplied
* connection settings.
* The onConnect method is called upon success.
*
* @param cs The connection settings to use.
*
* @throws IOException if it was not possible to connect to the server.
* @throws IrcException if the server would not let us join it.
* @throws NickAlreadyInUseException if our nick is already in use on the server.
*/
public final synchronized void connect(ConnectionSettings cs) throws IOException, IrcException, NickAlreadyInUseException {

_server = hostname;
_port = port;
_password = password;
_server = cs.server;
_port = cs.port;
_password = cs.password;

if (isConnected()) {
throw new IOException("The PircBot is already connected to an IRC server. Disconnect first.");
Expand All @@ -137,7 +159,7 @@ public final synchronized void connect(String hostname, int port, String passwor
this.removeAllChannels();

// Connect to the server.
Socket socket = new Socket(hostname, port);
Socket socket = new Socket(_server, _port);
this.log("*** Connected to server.");

_inetAddress = socket.getLocalAddress();
Expand All @@ -159,8 +181,8 @@ public final synchronized void connect(String hostname, int port, String passwor
BufferedWriter bwriter = new BufferedWriter(outputStreamWriter);

// Attempt to join the server.
if (password != null && !password.equals("")) {
OutputThread.sendRawLine(this, bwriter, "PASS " + password);
if (_password != null && !_password.equals("")) {
OutputThread.sendRawLine(this, bwriter, "PASS " + _password);
}
String nick = this.getName();
OutputThread.sendRawLine(this, bwriter, "NICK " + nick);
Expand Down

0 comments on commit 285a24c

Please sign in to comment.