mirror of
https://github.com/moparisthebest/Yaaic
synced 2025-02-17 23:40:20 -05:00
Make compatible with sBNC and other IRC bouncers. Closes #19.
This commit is contained in:
parent
33f181de3c
commit
efecd7c6f7
@ -147,6 +147,7 @@ public abstract class PircBot implements ReplyConstants {
|
|||||||
if (isConnected()) {
|
if (isConnected()) {
|
||||||
throw new IOException("The PircBot is already connected to an IRC server. Disconnect first.");
|
throw new IOException("The PircBot is already connected to an IRC server. Disconnect first.");
|
||||||
}
|
}
|
||||||
|
_autoNickTries = 1;
|
||||||
|
|
||||||
// Don't clear the outqueue - there might be something important in it!
|
// Don't clear the outqueue - there might be something important in it!
|
||||||
|
|
||||||
@ -156,7 +157,6 @@ public abstract class PircBot implements ReplyConstants {
|
|||||||
// Connect to the server.
|
// Connect to the server.
|
||||||
|
|
||||||
// XXX: PircBot Patch for SSL
|
// XXX: PircBot Patch for SSL
|
||||||
Socket socket;
|
|
||||||
if (_useSSL) {
|
if (_useSSL) {
|
||||||
try {
|
try {
|
||||||
SSLContext context = SSLContext.getInstance("TLS");
|
SSLContext context = SSLContext.getInstance("TLS");
|
||||||
@ -164,7 +164,7 @@ public abstract class PircBot implements ReplyConstants {
|
|||||||
SSLSocketFactory factory = context.getSocketFactory();
|
SSLSocketFactory factory = context.getSocketFactory();
|
||||||
SSLSocket ssocket = (SSLSocket) factory.createSocket(hostname, port);
|
SSLSocket ssocket = (SSLSocket) factory.createSocket(hostname, port);
|
||||||
ssocket.startHandshake();
|
ssocket.startHandshake();
|
||||||
socket = ssocket;
|
_socket = ssocket;
|
||||||
}
|
}
|
||||||
catch(Exception e)
|
catch(Exception e)
|
||||||
{
|
{
|
||||||
@ -172,22 +172,22 @@ public abstract class PircBot implements ReplyConstants {
|
|||||||
throw new IOException("Cannot open SSL socket");
|
throw new IOException("Cannot open SSL socket");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
socket = new Socket(hostname, port);
|
_socket = new Socket(hostname, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
_inetAddress = socket.getLocalAddress();
|
_inetAddress = _socket.getLocalAddress();
|
||||||
|
|
||||||
InputStreamReader inputStreamReader = null;
|
InputStreamReader inputStreamReader = null;
|
||||||
OutputStreamWriter outputStreamWriter = null;
|
OutputStreamWriter outputStreamWriter = null;
|
||||||
if (getEncoding() != null) {
|
if (getEncoding() != null) {
|
||||||
// Assume the specified encoding is valid for this JVM.
|
// Assume the specified encoding is valid for this JVM.
|
||||||
inputStreamReader = new InputStreamReader(socket.getInputStream(), getEncoding());
|
inputStreamReader = new InputStreamReader(_socket.getInputStream(), getEncoding());
|
||||||
outputStreamWriter = new OutputStreamWriter(socket.getOutputStream(), getEncoding());
|
outputStreamWriter = new OutputStreamWriter(_socket.getOutputStream(), getEncoding());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Otherwise, just use the JVM's default encoding.
|
// Otherwise, just use the JVM's default encoding.
|
||||||
inputStreamReader = new InputStreamReader(socket.getInputStream());
|
inputStreamReader = new InputStreamReader(_socket.getInputStream());
|
||||||
outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());
|
outputStreamWriter = new OutputStreamWriter(_socket.getOutputStream());
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferedReader breader = new BufferedReader(inputStreamReader);
|
BufferedReader breader = new BufferedReader(inputStreamReader);
|
||||||
@ -201,7 +201,7 @@ public abstract class PircBot implements ReplyConstants {
|
|||||||
OutputThread.sendRawLine(this, bwriter, "NICK " + nick);
|
OutputThread.sendRawLine(this, bwriter, "NICK " + nick);
|
||||||
OutputThread.sendRawLine(this, bwriter, "USER " + this.getLogin() + " 8 * :" + this.getVersion());
|
OutputThread.sendRawLine(this, bwriter, "USER " + this.getLogin() + " 8 * :" + this.getVersion());
|
||||||
|
|
||||||
_inputThread = new InputThread(this, socket, breader, bwriter);
|
_inputThread = new InputThread(this, _socket, breader, bwriter);
|
||||||
|
|
||||||
// XXX: PircBot Patch - Set nick before loop. otherwise we overwrite it in the loop again and again
|
// XXX: PircBot Patch - Set nick before loop. otherwise we overwrite it in the loop again and again
|
||||||
// But maybe we got a new nickname from the server (bouncers!)
|
// But maybe we got a new nickname from the server (bouncers!)
|
||||||
@ -209,51 +209,19 @@ public abstract class PircBot implements ReplyConstants {
|
|||||||
|
|
||||||
// Read stuff back from the server to see if we connected.
|
// Read stuff back from the server to see if we connected.
|
||||||
String line = null;
|
String line = null;
|
||||||
int tries = 1;
|
line = breader.readLine();
|
||||||
List<String> aliases = getAliases();
|
|
||||||
while ((line = breader.readLine()) != null) {
|
|
||||||
|
|
||||||
this.handleLine(line);
|
|
||||||
|
|
||||||
int firstSpace = line.indexOf(" ");
|
|
||||||
int secondSpace = line.indexOf(" ", firstSpace + 1);
|
|
||||||
if (secondSpace >= 0) {
|
|
||||||
String code = line.substring(firstSpace + 1, secondSpace);
|
|
||||||
|
|
||||||
if (code.equals("004")) {
|
|
||||||
// We're connected to the server.
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (code.equals("433")) {
|
|
||||||
if (_autoNickChange) {
|
|
||||||
tries++;
|
|
||||||
nick = ((tries - 1) <= aliases.size()) ?
|
|
||||||
aliases.get(tries - 2) :
|
|
||||||
getName() + (tries - aliases.size());
|
|
||||||
OutputThread.sendRawLine(this, bwriter, "NICK " + nick);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
socket.close();
|
|
||||||
_inputThread = null;
|
|
||||||
throw new NickAlreadyInUseException(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if ((code.startsWith("5") || code.startsWith("4")) && !code.equals("439")) {
|
|
||||||
socket.close();
|
|
||||||
_inputThread = null;
|
|
||||||
throw new IrcException("Could not log into the IRC server: " + line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// XXX: PircBot patch - We are not connected to server if nothing received
|
// XXX: PircBot patch - We are not connected to server if nothing received
|
||||||
if (line == null) {
|
if (line == null) {
|
||||||
throw new IOException("Could not connect to server");
|
throw new IOException("Could not connect to server");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Send the first line to handleLine before the InputThread is started.
|
||||||
|
this.handleLine(line);
|
||||||
|
|
||||||
// This makes the socket timeout on read operations after 5 minutes.
|
// This makes the socket timeout on read operations after 5 minutes.
|
||||||
// Maybe in some future version I will let the user change this at runtime.
|
// Maybe in some future version I will let the user change this at runtime.
|
||||||
socket.setSoTimeout(5 * 60 * 1000);
|
_socket.setSoTimeout(5 * 60 * 1000);
|
||||||
|
|
||||||
// Now start the InputThread to read all other lines from the server.
|
// Now start the InputThread to read all other lines from the server.
|
||||||
_inputThread.start();
|
_inputThread.start();
|
||||||
@ -863,8 +831,10 @@ public abstract class PircBot implements ReplyConstants {
|
|||||||
* This method may not be overridden!
|
* This method may not be overridden!
|
||||||
*
|
*
|
||||||
* @param line The raw line of text from the server.
|
* @param line The raw line of text from the server.
|
||||||
|
* @throws NickAlreadyInUseException
|
||||||
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
protected void handleLine(String line) {
|
protected void handleLine(String line) throws NickAlreadyInUseException, IOException {
|
||||||
// Check for server pings.
|
// Check for server pings.
|
||||||
if (line.startsWith("PING ")) {
|
if (line.startsWith("PING ")) {
|
||||||
// Respond to the ping and return immediately.
|
// Respond to the ping and return immediately.
|
||||||
@ -905,6 +875,23 @@ public abstract class PircBot implements ReplyConstants {
|
|||||||
if (code != -1) {
|
if (code != -1) {
|
||||||
String errorStr = token;
|
String errorStr = token;
|
||||||
String response = line.substring(line.indexOf(errorStr, senderInfo.length()) + 4, line.length());
|
String response = line.substring(line.indexOf(errorStr, senderInfo.length()) + 4, line.length());
|
||||||
|
|
||||||
|
if (code == 433) {
|
||||||
|
if (_autoNickChange) {
|
||||||
|
List<String> aliases = getAliases();
|
||||||
|
_autoNickTries++;
|
||||||
|
String nick = ((_autoNickTries - 1) <= aliases.size()) ?
|
||||||
|
aliases.get(_autoNickTries - 2) :
|
||||||
|
getName() + (_autoNickTries - aliases.size());
|
||||||
|
this.sendRawLineViaQueue("NICK " + nick);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
_socket.close();
|
||||||
|
_inputThread = null;
|
||||||
|
throw new NickAlreadyInUseException(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.processServerResponse(code, response);
|
this.processServerResponse(code, response);
|
||||||
// Return from the method.
|
// Return from the method.
|
||||||
return;
|
return;
|
||||||
@ -3067,6 +3054,7 @@ public abstract class PircBot implements ReplyConstants {
|
|||||||
private OutputThread _outputThread = null;
|
private OutputThread _outputThread = null;
|
||||||
private String _charset = null;
|
private String _charset = null;
|
||||||
private InetAddress _inetAddress = null;
|
private InetAddress _inetAddress = null;
|
||||||
|
private Socket _socket = null;
|
||||||
|
|
||||||
// Details about the last server that we connected to.
|
// Details about the last server that we connected to.
|
||||||
private String _server = null;
|
private String _server = null;
|
||||||
@ -3092,6 +3080,7 @@ public abstract class PircBot implements ReplyConstants {
|
|||||||
|
|
||||||
// Default settings for the PircBot.
|
// Default settings for the PircBot.
|
||||||
private boolean _autoNickChange = false;
|
private boolean _autoNickChange = false;
|
||||||
|
private int _autoNickTries = 1;
|
||||||
private boolean _useSSL = false;
|
private boolean _useSSL = false;
|
||||||
|
|
||||||
private String _name = "PircBot";
|
private String _name = "PircBot";
|
||||||
|
Loading…
Reference in New Issue
Block a user