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:
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();
}
}
}
}