Previous Section  < Day Day Up >  Next Section

Hack 72 A File-Sharing Bot

figs/moderate.gif figs/hack72.gif

Want to share files on IRC without the hassle of having to send anything manually? Use this IRC bot to tell people what files you're sharing and automatically offer them for download.

IRC provides a convenient way to distribute your band's music, lyrics, videos, or whatever else you may want to share with members of your channel. This hack will show you how to make a simple IRC bot that will tell people what files the bot has to offer, as well as letting them download any of those files.

This bot will respond to the commands !files and !get. The !files command will show a list of the files available in the current runtime directory. The !get command will allow a user to download a file using DCC send.

11.6.1 The Code

Save the following as FileBot.java:

import java.io.*;

import java.net.*;

import org.jibble.pircbot.PircBot;



public class FileBot extends PircBot {



    private static final String COMMAND_FILES = "!files";

    private static final String COMMAND_GET = "!get";

    private static final int DCC_TIMEOUT = 120000;



    public FileBot( ) {

        this.setName("FileBot");

    }

    

    public void onMessage(String channel, String sender, String login, 

            String hostname, String message) {



        if(message.equalsIgnoreCase(COMMAND_FILES)) {

            // Get the current directory.

            File dir = new File(".");



            // Get all files (not directories) in the directory.

            File[] fileList = dir.listFiles( );

            if (fileList == null || fileList.length == 0) {

                sendMessage(channel, "Sorry, no files available right now.");

            } else {

                // List the files.

                for (int i = 0; i < fileList.length; i++) {

                    if(fileList[i].isFile( )) { 

                        sendMessage(channel, fileList[i].getName( ));

                    }

                }

            }

        } else if(message.toLowerCase( ).startsWith(COMMAND_GET + " ")) {

            String fileToGet = message.substring(COMMAND_GET.length( )).trim( );



            // Send the requested file over DCC.

            dccSendFile(new File(fileToGet), sender, DCC_TIMEOUT);

        }

    }

    

}

Now you just need a main method to tell the bot to connect to a server and join a channel. Save the following as FileBotMain.java:

public class FileBotMain {



    public static void main(String[] args) throws Exception {

        FileBot fBot = new FileBot( );

        fBot.setVerbose(true);

        fBot.connect("irc.freenode.net");

        fBot.joinChannel("#irchacks");

    }



}

11.6.2 Running the Hack

Compile the bot with the following command:

C:\java\FileBot> javac -classpath pircbot.jar;. *.java

Run the bot like so:

C:\java\FileBot> java -classpath pircbot.jar;. FileBotMain

The bot will then start up and be ready to serve files.

11.6.3 Hacking the Hack

Sometimes people are unable to use DCC. Firewalls are a common reason for this, so an alternative method of transport must be found. This can be achieved quite simply by incorporating a very basic web server into the IRC bot, such as the one found at http://www.jibble.org/miniwebserver.

To use this, you must add the following import statement to FileBot.java:

    import org.jibble.simplewebserver.SimpleWebServer;

To allow access to the new feature, you must provide another field to store the command name:

    private static final String COMMAND_GETHTTP = "!gethttp";

In the constructor, you can create the web server instance:

    try {

        SimpleWebServer server = new SimpleWebServer(new File("./"), 80);

    } catch (IOException e) {

        e.printStackTrace( );

    }

In the onMessage method, you can now add handling for the new command:

    ...

    } else if(message.toLowerCase( ).startsWith(COMMAND_GETHTTP + " ")) {

        String fileToGet = message.substring(COMMAND_GETHTTP.length( )).trim( );



        try {

            sendMessage(channel, "http://" +

                    InetAddress.getLocalHost( ).getHostAddress( ) + "/" + fileToGet);

        } catch (UnknownHostException e) {

            e.printStackTrace( );

        }

    }

11.6.4 The Results

When you send the !files command, the bot will list all of the files in the current directory, for example:

<DeadEd> !files

<FileBot> Image1.gif

<FileBot> Snd2.wav

<FileBot> Report.doc

<FileBot> FileBot.txt

If you want one of these files, you can use the !get filename command, for example:

<DeadEd> !get FileBot.txt

FileBot will now try and send the file via DCC send. If this fails for some reason, you can try the last resort—download the file over HTTP:

<DeadEd> !gethttp FileBot.txt

<FileBot> http://123.456.123.456/FileBot.txt

Follow that link in your browser and the file should start downloading—assuming, of course, that the web server on which FileBot is running is accessible to you over the Web.

Alex North

    Previous Section  < Day Day Up >  Next Section