f00f.net.irc.martyr.dcc
Class AutoDccSetup

java.lang.Object
  extended by f00f.net.irc.martyr.GenericCommandAutoService
      extended by f00f.net.irc.martyr.dcc.AutoDccSetup
All Implemented Interfaces:
java.util.Observer

public abstract class AutoDccSetup
extends GenericCommandAutoService

AutoDccSetup enables an application to "listen" for when another client is offering a DCC connection to us. AutoDccSetup checks incoming CTCPs to see if they are DCC connection requests, and if so, sets up the connection. You can use the AutoDccSetup by subclassing it with your own AutoDccSetup and overriding the appropriate methods.

In order to use this class you must provide an implementation for authorizeConnect. Additionally, you will want to override getDccChatHandler and/or getDccSendHandler, so that a customized version of the stock DccChatHandler and DccSendHandler is provided. This gives you easy access to the chat stream and file stream, respectively.

Note that all the methods in this class are executed in the event handler thread. The dcc session should occur in a seperate thread, which is why getDccChatHandler and getDccSendHandler return seperate classes. The handler classes are threads which are spawned by AutoDccSetup and should self-terminate.

DccTestBot provides a simple example of how DCC works.

See Also:
getDccChatHandler(java.net.Socket), getDccSendHandler(java.net.Socket, java.lang.String, int)

Field Summary
 
Fields inherited from class f00f.net.irc.martyr.GenericCommandAutoService
connection, enabled
 
Constructor Summary
AutoDccSetup(IRCConnection connection)
           
 
Method Summary
protected abstract  boolean authorizeConnect(DccType type, java.lang.String argument, java.net.InetAddress netaddr, int port, int filesize)
          This is called after parsing the request to find out if the connection should be performed.
protected  DccChatHandler getDccChatHandler(java.net.Socket socket)
          Returns a DCC chat handler.
protected  DccSendHandler getDccSendHandler(java.net.Socket socket, java.lang.String filename, int filesize)
          Returns a DCC file "send" (receive to us) handler.
protected  void handleCtcp(CtcpMessage ctcpMsg)
           
protected  void handleDccConnection(java.net.Socket socket, DccType type, java.lang.String filename, int filesize)
          Handles a chat or send connection.
protected  java.net.Socket makeSocket(java.net.InetAddress dest, int port)
          Generates a connected socket, connected to the machine that made the request for a DCC.
 java.lang.String toString()
           
 void updateCommand(InCommand updated)
          Checks to see if the message is a CTCP message, and if so, calls handleCtcp.
 
Methods inherited from class f00f.net.irc.martyr.GenericCommandAutoService
disable, enable, getConnection, isEnabled, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AutoDccSetup

public AutoDccSetup(IRCConnection connection)
Method Detail

updateCommand

public void updateCommand(InCommand updated)
Checks to see if the message is a CTCP message, and if so, calls handleCtcp. Exceptions thrown by handleCtcp are ignored. If you wish to handle exceptions, you should override this method. Simply check if updated is an instance of CtcpMessage, and then call handleCtcp.

Specified by:
updateCommand in class GenericCommandAutoService

handleCtcp

protected void handleCtcp(CtcpMessage ctcpMsg)
                   throws DccException,
                          java.io.IOException
Parameters:
ctcpMsg - Message to be handled
Throws:
InvalidDccException - if the DCC of not of a known type
UnauthorizedDccException - if the DCC is not authorized
java.io.IOException - if the connection failed.
DccException

getDccChatHandler

protected DccChatHandler getDccChatHandler(java.net.Socket socket)
Returns a DCC chat handler. A chat session consists of each side sending a line of text over the socket, whenever desired.

Parameters:
socket - Socket where the dcc chat will be handled
Returns:
DccChatHandler instance tied to a socket

getDccSendHandler

protected DccSendHandler getDccSendHandler(java.net.Socket socket,
                                           java.lang.String filename,
                                           int filesize)
Returns a DCC file "send" (receive to us) handler. A DCC file transfer session is fairly slow, but effective, peer to peer scheme. A DccSendHandler subclass does not need to worry about the details of the protocol, a subclass merely needs to provide a stream that the file can be written out to.

Parameters:
socket - Socket where dcc send will be handled
filename - Filename to send
filesize - Size of file to send
Returns:
DccSendHandler instance tied to socket

handleDccConnection

protected void handleDccConnection(java.net.Socket socket,
                                   DccType type,
                                   java.lang.String filename,
                                   int filesize)
Handles a chat or send connection. It calls either getDccSendHandler or getDccChatHandler and calls start() on the result.

Parameters:
socket - Socket where dcc connection will be handled
type - Type of dcc event
filename - Name of file involved in dcc event
filesize - Size of file involved in dcc event

makeSocket

protected java.net.Socket makeSocket(java.net.InetAddress dest,
                                     int port)
                              throws java.io.IOException
Generates a connected socket, connected to the machine that made the request for a DCC.

Parameters:
dest - Address we will connect to
port - Port we will connect to
Returns:
Socket connected to address and port
Throws:
java.io.IOException - if unable to create socket

authorizeConnect

protected abstract boolean authorizeConnect(DccType type,
                                            java.lang.String argument,
                                            java.net.InetAddress netaddr,
                                            int port,
                                            int filesize)
This is called after parsing the request to find out if the connection should be performed. Note that execution of this occurrs in the event thread, so if an option is presented to the user, a subclass should override update(...) so that processing occurs in its own thread and does not block the event handler.

Parameters:
type - Either DccType.SEND or DccType.CHAT
argument - "CHAT" if a chat, a filename if a file.
netaddr - The address, although only the raw IP will be useful.
port - The port to connect on
filesize - The filesize, if one was provided.
Returns:
True or false if connection is authorized

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2000-2007 Ben Damm, Daniel Henninger, et al.