mirror of
https://github.com/moparisthebest/Yaaic
synced 2024-11-26 02:42:16 -05:00
Removed damned windows carriage returns (\r)
This commit is contained in:
parent
648ffc8eda
commit
59674e25d1
@ -1,112 +1,112 @@
|
||||
/*
|
||||
Copyright Paul James Mutton, 2001-2007, http://www.jibble.org/
|
||||
|
||||
This file is part of PircBot.
|
||||
|
||||
This software is dual-licensed, allowing you to choose between the GNU
|
||||
General Public License (GPL) and the www.jibble.org Commercial License.
|
||||
Since the GPL may be too restrictive for use in a proprietary application,
|
||||
a commercial license is also provided. Full license information can be
|
||||
found at http://www.jibble.org/licenses/
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package org.jibble.pircbot;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* A simple IdentServer (also know as "The Identification Protocol").
|
||||
* An ident server provides a means to determine the identity of a
|
||||
* user of a particular TCP connection.
|
||||
* <p>
|
||||
* Most IRC servers attempt to contact the ident server on connecting
|
||||
* hosts in order to determine the user's identity. A few IRC servers
|
||||
* will not allow you to connect unless this information is provided.
|
||||
* <p>
|
||||
* So when a PircBot is run on a machine that does not run an ident server,
|
||||
* it may be necessary to provide a "faked" response by starting up its
|
||||
* own ident server and sending out apparently correct responses.
|
||||
* <p>
|
||||
* An instance of this class can be used to start up an ident server
|
||||
* only if it is possible to do so. Reasons for not being able to do
|
||||
* so are if there is already an ident server running on port 113, or
|
||||
* if you are running as an unprivileged user who is unable to create
|
||||
* a server socket on that port number.
|
||||
*
|
||||
* @since 0.9c
|
||||
* @author Paul James Mutton,
|
||||
* <a href="http://www.jibble.org/">http://www.jibble.org/</a>
|
||||
* @version 1.4.6 (Build time: Wed Apr 11 19:20:59 2007)
|
||||
*/
|
||||
public class IdentServer extends Thread {
|
||||
|
||||
/**
|
||||
* Constructs and starts an instance of an IdentServer that will
|
||||
* respond to a client with the provided login. Rather than calling
|
||||
* this constructor explicitly from your code, it is recommended that
|
||||
* you use the startIdentServer method in the PircBot class.
|
||||
* <p>
|
||||
* The ident server will wait for up to 60 seconds before shutting
|
||||
* down. Otherwise, it will shut down as soon as it has responded
|
||||
* to an ident request.
|
||||
*
|
||||
* @param bot The PircBot instance that will be used to log to.
|
||||
* @param login The login that the ident server will respond with.
|
||||
*/
|
||||
IdentServer(String login) {
|
||||
_login = login;
|
||||
|
||||
try {
|
||||
_ss = new ServerSocket(113);
|
||||
_ss.setSoTimeout(60000);
|
||||
}
|
||||
catch (Exception e) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.setName(this.getClass() + "-Thread");
|
||||
this.start();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Waits for a client to connect to the ident server before making an
|
||||
* appropriate response. Note that this method is started by the class
|
||||
* constructor.
|
||||
*/
|
||||
public void run() {
|
||||
try {
|
||||
Socket socket = _ss.accept();
|
||||
socket.setSoTimeout(60000);
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
|
||||
String line = reader.readLine();
|
||||
if (line != null) {
|
||||
line = line + " : USERID : UNIX : " + _login;
|
||||
writer.write(line + "\r\n");
|
||||
writer.flush();
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
// We're not really concerned with what went wrong, are we?
|
||||
}
|
||||
|
||||
try {
|
||||
_ss.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Doesn't really matter...
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String _login;
|
||||
private ServerSocket _ss = null;
|
||||
|
||||
}
|
||||
/*
|
||||
Copyright Paul James Mutton, 2001-2007, http://www.jibble.org/
|
||||
|
||||
This file is part of PircBot.
|
||||
|
||||
This software is dual-licensed, allowing you to choose between the GNU
|
||||
General Public License (GPL) and the www.jibble.org Commercial License.
|
||||
Since the GPL may be too restrictive for use in a proprietary application,
|
||||
a commercial license is also provided. Full license information can be
|
||||
found at http://www.jibble.org/licenses/
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package org.jibble.pircbot;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* A simple IdentServer (also know as "The Identification Protocol").
|
||||
* An ident server provides a means to determine the identity of a
|
||||
* user of a particular TCP connection.
|
||||
* <p>
|
||||
* Most IRC servers attempt to contact the ident server on connecting
|
||||
* hosts in order to determine the user's identity. A few IRC servers
|
||||
* will not allow you to connect unless this information is provided.
|
||||
* <p>
|
||||
* So when a PircBot is run on a machine that does not run an ident server,
|
||||
* it may be necessary to provide a "faked" response by starting up its
|
||||
* own ident server and sending out apparently correct responses.
|
||||
* <p>
|
||||
* An instance of this class can be used to start up an ident server
|
||||
* only if it is possible to do so. Reasons for not being able to do
|
||||
* so are if there is already an ident server running on port 113, or
|
||||
* if you are running as an unprivileged user who is unable to create
|
||||
* a server socket on that port number.
|
||||
*
|
||||
* @since 0.9c
|
||||
* @author Paul James Mutton,
|
||||
* <a href="http://www.jibble.org/">http://www.jibble.org/</a>
|
||||
* @version 1.4.6 (Build time: Wed Apr 11 19:20:59 2007)
|
||||
*/
|
||||
public class IdentServer extends Thread {
|
||||
|
||||
/**
|
||||
* Constructs and starts an instance of an IdentServer that will
|
||||
* respond to a client with the provided login. Rather than calling
|
||||
* this constructor explicitly from your code, it is recommended that
|
||||
* you use the startIdentServer method in the PircBot class.
|
||||
* <p>
|
||||
* The ident server will wait for up to 60 seconds before shutting
|
||||
* down. Otherwise, it will shut down as soon as it has responded
|
||||
* to an ident request.
|
||||
*
|
||||
* @param bot The PircBot instance that will be used to log to.
|
||||
* @param login The login that the ident server will respond with.
|
||||
*/
|
||||
IdentServer(String login) {
|
||||
_login = login;
|
||||
|
||||
try {
|
||||
_ss = new ServerSocket(113);
|
||||
_ss.setSoTimeout(60000);
|
||||
}
|
||||
catch (Exception e) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.setName(this.getClass() + "-Thread");
|
||||
this.start();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Waits for a client to connect to the ident server before making an
|
||||
* appropriate response. Note that this method is started by the class
|
||||
* constructor.
|
||||
*/
|
||||
public void run() {
|
||||
try {
|
||||
Socket socket = _ss.accept();
|
||||
socket.setSoTimeout(60000);
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
|
||||
String line = reader.readLine();
|
||||
if (line != null) {
|
||||
line = line + " : USERID : UNIX : " + _login;
|
||||
writer.write(line + "\r\n");
|
||||
writer.flush();
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
// We're not really concerned with what went wrong, are we?
|
||||
}
|
||||
|
||||
try {
|
||||
_ss.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Doesn't really matter...
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String _login;
|
||||
private ServerSocket _ss = null;
|
||||
|
||||
}
|
||||
|
@ -1,156 +1,156 @@
|
||||
/*
|
||||
Copyright Paul James Mutton, 2001-2007, http://www.jibble.org/
|
||||
|
||||
This file is part of PircBot.
|
||||
|
||||
This software is dual-licensed, allowing you to choose between the GNU
|
||||
General Public License (GPL) and the www.jibble.org Commercial License.
|
||||
Since the GPL may be too restrictive for use in a proprietary application,
|
||||
a commercial license is also provided. Full license information can be
|
||||
found at http://www.jibble.org/licenses/
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package org.jibble.pircbot;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
/**
|
||||
* A Thread which reads lines from the IRC server. It then
|
||||
* passes these lines to the PircBot without changing them.
|
||||
* This running Thread also detects disconnection from the server
|
||||
* and is thus used by the OutputThread to send lines to the server.
|
||||
*
|
||||
* @author Paul James Mutton,
|
||||
* <a href="http://www.jibble.org/">http://www.jibble.org/</a>
|
||||
* @version 1.4.6 (Build time: Wed Apr 11 19:20:59 2007)
|
||||
*/
|
||||
public class InputThread extends Thread {
|
||||
|
||||
/**
|
||||
* The InputThread reads lines from the IRC server and allows the
|
||||
* PircBot to handle them.
|
||||
*
|
||||
* @param bot An instance of the underlying PircBot.
|
||||
* @param breader The BufferedReader that reads lines from the server.
|
||||
* @param bwriter The BufferedWriter that sends lines to the server.
|
||||
*/
|
||||
InputThread(PircBot bot, Socket socket, BufferedReader breader, BufferedWriter bwriter) {
|
||||
_bot = bot;
|
||||
_socket = socket;
|
||||
_breader = breader;
|
||||
_bwriter = bwriter;
|
||||
this.setName(this.getClass() + "-Thread");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sends a raw line to the IRC server as soon as possible, bypassing the
|
||||
* outgoing message queue.
|
||||
*
|
||||
* @param line The raw line to send to the IRC server.
|
||||
*/
|
||||
void sendRawLine(String line) {
|
||||
OutputThread.sendRawLine(_bot, _bwriter, line);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if this InputThread is connected to an IRC server.
|
||||
* The result of this method should only act as a rough guide,
|
||||
* as the result may not be valid by the time you act upon it.
|
||||
*
|
||||
* @return True if still connected.
|
||||
*/
|
||||
boolean isConnected() {
|
||||
return _isConnected;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called to start this Thread reading lines from the IRC server.
|
||||
* When a line is read, this method calls the handleLine method
|
||||
* in the PircBot, which may subsequently call an 'onXxx' method
|
||||
* in the PircBot subclass. If any subclass of Throwable (i.e.
|
||||
* any Exception or Error) is thrown by your method, then this
|
||||
* method will print the stack trace to the standard output. It
|
||||
* is probable that the PircBot may still be functioning normally
|
||||
* after such a problem, but the existance of any uncaught exceptions
|
||||
* in your code is something you should really fix.
|
||||
*/
|
||||
public void run() {
|
||||
try {
|
||||
boolean running = true;
|
||||
while (running) {
|
||||
try {
|
||||
String line = null;
|
||||
while ((line = _breader.readLine()) != null) {
|
||||
try {
|
||||
_bot.handleLine(line);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
// Stick the whole stack trace into a String so we can output it nicely.
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
t.printStackTrace(pw);
|
||||
pw.flush();
|
||||
}
|
||||
}
|
||||
if (line == null) {
|
||||
// The server must have disconnected us.
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
catch (InterruptedIOException iioe) {
|
||||
// This will happen if we haven't received anything from the server for a while.
|
||||
// So we shall send it a ping to check that we are still connected.
|
||||
this.sendRawLine("PING " + (System.currentTimeMillis() / 1000));
|
||||
// Now we go back to listening for stuff from the server...
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
// If we reach this point, then we must have disconnected.
|
||||
try {
|
||||
_socket.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Just assume the socket was already closed.
|
||||
}
|
||||
|
||||
if (!_disposed) {
|
||||
_isConnected = false;
|
||||
_bot.onDisconnect();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Closes the socket without onDisconnect being called subsequently.
|
||||
*/
|
||||
public void dispose () {
|
||||
try {
|
||||
_disposed = true;
|
||||
_socket.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Do nothing.
|
||||
}
|
||||
}
|
||||
|
||||
private PircBot _bot = null;
|
||||
private Socket _socket = null;
|
||||
private BufferedReader _breader = null;
|
||||
private BufferedWriter _bwriter = null;
|
||||
private boolean _isConnected = true;
|
||||
private boolean _disposed = false;
|
||||
|
||||
public static final int MAX_LINE_LENGTH = 512;
|
||||
|
||||
/*
|
||||
Copyright Paul James Mutton, 2001-2007, http://www.jibble.org/
|
||||
|
||||
This file is part of PircBot.
|
||||
|
||||
This software is dual-licensed, allowing you to choose between the GNU
|
||||
General Public License (GPL) and the www.jibble.org Commercial License.
|
||||
Since the GPL may be too restrictive for use in a proprietary application,
|
||||
a commercial license is also provided. Full license information can be
|
||||
found at http://www.jibble.org/licenses/
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package org.jibble.pircbot;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
/**
|
||||
* A Thread which reads lines from the IRC server. It then
|
||||
* passes these lines to the PircBot without changing them.
|
||||
* This running Thread also detects disconnection from the server
|
||||
* and is thus used by the OutputThread to send lines to the server.
|
||||
*
|
||||
* @author Paul James Mutton,
|
||||
* <a href="http://www.jibble.org/">http://www.jibble.org/</a>
|
||||
* @version 1.4.6 (Build time: Wed Apr 11 19:20:59 2007)
|
||||
*/
|
||||
public class InputThread extends Thread {
|
||||
|
||||
/**
|
||||
* The InputThread reads lines from the IRC server and allows the
|
||||
* PircBot to handle them.
|
||||
*
|
||||
* @param bot An instance of the underlying PircBot.
|
||||
* @param breader The BufferedReader that reads lines from the server.
|
||||
* @param bwriter The BufferedWriter that sends lines to the server.
|
||||
*/
|
||||
InputThread(PircBot bot, Socket socket, BufferedReader breader, BufferedWriter bwriter) {
|
||||
_bot = bot;
|
||||
_socket = socket;
|
||||
_breader = breader;
|
||||
_bwriter = bwriter;
|
||||
this.setName(this.getClass() + "-Thread");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sends a raw line to the IRC server as soon as possible, bypassing the
|
||||
* outgoing message queue.
|
||||
*
|
||||
* @param line The raw line to send to the IRC server.
|
||||
*/
|
||||
void sendRawLine(String line) {
|
||||
OutputThread.sendRawLine(_bot, _bwriter, line);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if this InputThread is connected to an IRC server.
|
||||
* The result of this method should only act as a rough guide,
|
||||
* as the result may not be valid by the time you act upon it.
|
||||
*
|
||||
* @return True if still connected.
|
||||
*/
|
||||
boolean isConnected() {
|
||||
return _isConnected;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called to start this Thread reading lines from the IRC server.
|
||||
* When a line is read, this method calls the handleLine method
|
||||
* in the PircBot, which may subsequently call an 'onXxx' method
|
||||
* in the PircBot subclass. If any subclass of Throwable (i.e.
|
||||
* any Exception or Error) is thrown by your method, then this
|
||||
* method will print the stack trace to the standard output. It
|
||||
* is probable that the PircBot may still be functioning normally
|
||||
* after such a problem, but the existance of any uncaught exceptions
|
||||
* in your code is something you should really fix.
|
||||
*/
|
||||
public void run() {
|
||||
try {
|
||||
boolean running = true;
|
||||
while (running) {
|
||||
try {
|
||||
String line = null;
|
||||
while ((line = _breader.readLine()) != null) {
|
||||
try {
|
||||
_bot.handleLine(line);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
// Stick the whole stack trace into a String so we can output it nicely.
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
t.printStackTrace(pw);
|
||||
pw.flush();
|
||||
}
|
||||
}
|
||||
if (line == null) {
|
||||
// The server must have disconnected us.
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
catch (InterruptedIOException iioe) {
|
||||
// This will happen if we haven't received anything from the server for a while.
|
||||
// So we shall send it a ping to check that we are still connected.
|
||||
this.sendRawLine("PING " + (System.currentTimeMillis() / 1000));
|
||||
// Now we go back to listening for stuff from the server...
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
// If we reach this point, then we must have disconnected.
|
||||
try {
|
||||
_socket.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Just assume the socket was already closed.
|
||||
}
|
||||
|
||||
if (!_disposed) {
|
||||
_isConnected = false;
|
||||
_bot.onDisconnect();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Closes the socket without onDisconnect being called subsequently.
|
||||
*/
|
||||
public void dispose () {
|
||||
try {
|
||||
_disposed = true;
|
||||
_socket.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Do nothing.
|
||||
}
|
||||
}
|
||||
|
||||
private PircBot _bot = null;
|
||||
private Socket _socket = null;
|
||||
private BufferedReader _breader = null;
|
||||
private BufferedWriter _bwriter = null;
|
||||
private boolean _isConnected = true;
|
||||
private boolean _disposed = false;
|
||||
|
||||
public static final int MAX_LINE_LENGTH = 512;
|
||||
|
||||
}
|
@ -1,103 +1,103 @@
|
||||
/*
|
||||
Copyright Paul James Mutton, 2001-2007, http://www.jibble.org/
|
||||
|
||||
This file is part of PircBot.
|
||||
|
||||
This software is dual-licensed, allowing you to choose between the GNU
|
||||
General Public License (GPL) and the www.jibble.org Commercial License.
|
||||
Since the GPL may be too restrictive for use in a proprietary application,
|
||||
a commercial license is also provided. Full license information can be
|
||||
found at http://www.jibble.org/licenses/
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package org.jibble.pircbot;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* A Thread which is responsible for sending messages to the IRC server.
|
||||
* Messages are obtained from the outgoing message queue and sent
|
||||
* immediately if possible. If there is a flood of messages, then to
|
||||
* avoid getting kicked from a channel, we put a small delay between
|
||||
* each one.
|
||||
*
|
||||
* @author Paul James Mutton,
|
||||
* <a href="http://www.jibble.org/">http://www.jibble.org/</a>
|
||||
* @version 1.4.6 (Build time: Wed Apr 11 19:20:59 2007)
|
||||
*/
|
||||
public class OutputThread extends Thread {
|
||||
|
||||
|
||||
/**
|
||||
* Constructs an OutputThread for the underlying PircBot. All messages
|
||||
* sent to the IRC server are sent by this OutputThread to avoid hammering
|
||||
* the server. Messages are sent immediately if possible. If there are
|
||||
* multiple messages queued, then there is a delay imposed.
|
||||
*
|
||||
* @param bot The underlying PircBot instance.
|
||||
* @param outQueue The Queue from which we will obtain our messages.
|
||||
*/
|
||||
OutputThread(PircBot bot, Queue outQueue) {
|
||||
_bot = bot;
|
||||
_outQueue = outQueue;
|
||||
this.setName(this.getClass() + "-Thread");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A static method to write a line to a BufferedOutputStream and then pass
|
||||
* the line to the log method of the supplied PircBot instance.
|
||||
*
|
||||
* @param bot The underlying PircBot instance.
|
||||
* @param out The BufferedOutputStream to write to.
|
||||
* @param line The line to be written. "\r\n" is appended to the end.
|
||||
* @param encoding The charset to use when encoing this string into a
|
||||
* byte array.
|
||||
*/
|
||||
static void sendRawLine(PircBot bot, BufferedWriter bwriter, String line) {
|
||||
if (line.length() > bot.getMaxLineLength() - 2) {
|
||||
line = line.substring(0, bot.getMaxLineLength() - 2);
|
||||
}
|
||||
synchronized(bwriter) {
|
||||
try {
|
||||
bwriter.write(line + "\r\n");
|
||||
bwriter.flush();
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Silent response - just lose the line.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method starts the Thread consuming from the outgoing message
|
||||
* Queue and sending lines to the server.
|
||||
*/
|
||||
public void run() {
|
||||
try {
|
||||
boolean running = true;
|
||||
while (running) {
|
||||
// Small delay to prevent spamming of the channel
|
||||
Thread.sleep(_bot.getMessageDelay());
|
||||
|
||||
String line = (String) _outQueue.next();
|
||||
if (line != null) {
|
||||
_bot.sendRawLine(line);
|
||||
}
|
||||
else {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
// Just let the method return naturally...
|
||||
}
|
||||
}
|
||||
|
||||
private PircBot _bot = null;
|
||||
private Queue _outQueue = null;
|
||||
|
||||
}
|
||||
/*
|
||||
Copyright Paul James Mutton, 2001-2007, http://www.jibble.org/
|
||||
|
||||
This file is part of PircBot.
|
||||
|
||||
This software is dual-licensed, allowing you to choose between the GNU
|
||||
General Public License (GPL) and the www.jibble.org Commercial License.
|
||||
Since the GPL may be too restrictive for use in a proprietary application,
|
||||
a commercial license is also provided. Full license information can be
|
||||
found at http://www.jibble.org/licenses/
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package org.jibble.pircbot;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* A Thread which is responsible for sending messages to the IRC server.
|
||||
* Messages are obtained from the outgoing message queue and sent
|
||||
* immediately if possible. If there is a flood of messages, then to
|
||||
* avoid getting kicked from a channel, we put a small delay between
|
||||
* each one.
|
||||
*
|
||||
* @author Paul James Mutton,
|
||||
* <a href="http://www.jibble.org/">http://www.jibble.org/</a>
|
||||
* @version 1.4.6 (Build time: Wed Apr 11 19:20:59 2007)
|
||||
*/
|
||||
public class OutputThread extends Thread {
|
||||
|
||||
|
||||
/**
|
||||
* Constructs an OutputThread for the underlying PircBot. All messages
|
||||
* sent to the IRC server are sent by this OutputThread to avoid hammering
|
||||
* the server. Messages are sent immediately if possible. If there are
|
||||
* multiple messages queued, then there is a delay imposed.
|
||||
*
|
||||
* @param bot The underlying PircBot instance.
|
||||
* @param outQueue The Queue from which we will obtain our messages.
|
||||
*/
|
||||
OutputThread(PircBot bot, Queue outQueue) {
|
||||
_bot = bot;
|
||||
_outQueue = outQueue;
|
||||
this.setName(this.getClass() + "-Thread");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A static method to write a line to a BufferedOutputStream and then pass
|
||||
* the line to the log method of the supplied PircBot instance.
|
||||
*
|
||||
* @param bot The underlying PircBot instance.
|
||||
* @param out The BufferedOutputStream to write to.
|
||||
* @param line The line to be written. "\r\n" is appended to the end.
|
||||
* @param encoding The charset to use when encoing this string into a
|
||||
* byte array.
|
||||
*/
|
||||
static void sendRawLine(PircBot bot, BufferedWriter bwriter, String line) {
|
||||
if (line.length() > bot.getMaxLineLength() - 2) {
|
||||
line = line.substring(0, bot.getMaxLineLength() - 2);
|
||||
}
|
||||
synchronized(bwriter) {
|
||||
try {
|
||||
bwriter.write(line + "\r\n");
|
||||
bwriter.flush();
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Silent response - just lose the line.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method starts the Thread consuming from the outgoing message
|
||||
* Queue and sending lines to the server.
|
||||
*/
|
||||
public void run() {
|
||||
try {
|
||||
boolean running = true;
|
||||
while (running) {
|
||||
// Small delay to prevent spamming of the channel
|
||||
Thread.sleep(_bot.getMessageDelay());
|
||||
|
||||
String line = (String) _outQueue.next();
|
||||
if (line != null) {
|
||||
_bot.sendRawLine(line);
|
||||
}
|
||||
else {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
// Just let the method return naturally...
|
||||
}
|
||||
}
|
||||
|
||||
private PircBot _bot = null;
|
||||
private Queue _outQueue = null;
|
||||
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user