I wrote a small java IRC bot to download MateoConLettuce posts before he can delete them. The bot is untested for obvious reasons, but it should work, unless I got the URL extraction wrong. Here's the code:

Code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.text.SimpleDateFormat;
import java.util.Date;

public class main implements Runnable {

    static String server = "irc.efnet.net";
    static int port = 6667;
    static String nick = "LettuceBot", user = nick, name = nick;
    static Thread thread;
    static boolean active;
    static Socket socket;
    static BufferedReader in;
    static BufferedWriter out;
    static boolean joinedChannal;
    static boolean firstPongDone;
    static boolean closeRequested;

    public static void main(String... args) throws IOException, InterruptedException {
        closeRequested = false;
        firstPongDone = false;
        joinedChannal = false;
        socket = new Socket(server, port);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        if (socket.isConnected()) {
            out.write("NICK " + nick + "\r\n");
            out.write("USER " + user + " \"\" \"\" :" + name + "\r\n");
            out.flush();
            active = true;
            System.out.println("started");
            thread = new Thread(new main());
            thread.start();
        }
    }

    public void send(String s) {
        try {
            out.write(s + "\r\n");
            out.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    void join(String chan) {
        send("JOIN " + chan);
    }

    public void run() {
        String s;
        while (true) {
            try {
                while (!closeRequested && (s = in.readLine()) != null) {
                    System.out.println(s);
                    if (s.startsWith("PING")) {
                        send("PONG " + s.substring(5));
                        firstPongDone = true;
                    }
                    if (joinedChannal) {
                        if (s.startsWith(":")) {
                            //MateoConLechuga added a post in More questions about hooks on TI-84 plus
                            String msg = "MateoConLechuga added a post in ";
                            if (s.contains(msg)) {
                                String boot = s.substring(s.indexOf(msg));
                                String url = boot.substring(boot.indexOf("( "), boot.lastIndexOf(" )"));

                                URL website = new URL(url);
                                ReadableByteChannel rbc = Channels.newChannel(website.openStream());
                                FileOutputStream fos = new FileOutputStream(new SimpleDateFormat("yyyyMMddHHmm'.txt'").format(new Date()));
                                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
                            }
                        }
                    } else {
                        if (firstPongDone) {
                            join("#cemetech");
                            joinedChannal = true;
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
You're special.
That's actually quite cool!
That's awesome! I wish there was a way to test it (because, as you said, it can't for obvious reasons), I'd love to see it in action. :3
What happened to Mateo?
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement