import java.math.*;
import javax.imageio.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.*;
import java.security.spec.*;
import java.security.*;
import java.lang.management.*;
import java.lang.ref.*;
import java.lang.reflect.*;
import java.net.*;
import java.io.*;
import javax.swing.table.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.util.List;
import java.util.zip.*;
import java.util.*;
import org.jibble.pircbot.*;
public class main {
public static String unquote(String s) {
if (s.startsWith("[")) {
int i = 1;
while (i < s.length() && s.charAt(i) == '=') ++i;
if (i < s.length() && s.charAt(i) == '[') {
String m = s.substring(1, i);
if (s.endsWith("]" + m + "]"))
return s.substring(i+1, s.length()-i-1);
}
}
if (s.startsWith("\"") && s.endsWith("\"") && s.length() > 1) {
String st = s.substring(1, s.length()-1);
StringBuilder sb = new StringBuilder(st.length());
for (int i = 0; i < st.length(); i++) {
char ch = st.charAt(i);
if (ch == '\\') {
char nextChar = (i == st.length() - 1) ? '\\' : st
.charAt(i + 1);
// Octal escape?
if (nextChar >= '0' && nextChar <= '7') {
String code = "" + nextChar;
i++;
if ((i < st.length() - 1) && st.charAt(i + 1) >= '0'
&& st.charAt(i + 1) <= '7') {
code += st.charAt(i + 1);
i++;
if ((i < st.length() - 1) && st.charAt(i + 1) >= '0'
&& st.charAt(i + 1) <= '7') {
code += st.charAt(i + 1);
i++;
}
}
sb.append((char) Integer.parseInt(code, 8));
continue;
}
switch (nextChar) {
case '\\':
ch = '\\';
break;
case 'b':
ch = '\b';
break;
case 'f':
ch = '\f';
break;
case 'n':
ch = '\n';
break;
case 'r':
ch = '\r';
break;
case 't':
ch = '\t';
break;
case '\"':
ch = '\"';
break;
case '\'':
ch = '\'';
break;
// Hex Unicode: u????
case 'u':
if (i >= st.length() - 5) {
ch = 'u';
break;
}
int code = Integer.parseInt(
"" + st.charAt(i + 2) + st.charAt(i + 3)
+ st.charAt(i + 4) + st.charAt(i + 5), 16);
sb.append(Character.toChars(code));
i += 5;
continue;
default:
ch = nextChar; // added by Stefan
}
i++;
}
sb.append(ch);
}
return sb.toString();
} else
return s; // return original
}
// a persistent list that only grows (or is clear()ed)
// Note: don't put in static initializer (programID not set yet)
static class PersistentLog extends AbstractList {
List l = new ArrayList();
File file;
PersistentLog(String fileName) {
this(getProgramFile(fileName));
}
PersistentLog(String progID, String fileName) {
this(getProgramFile(progID, fileName));
}
PersistentLog(File file) {
this.file = file;
for (String s : scanLog(file)) try {
l.add((A) unstructure(s));
} catch (Throwable __e) { printStackTrace(__e); }
}
public int size() {
return l.size();
}
public A get(int i) {
return l.get(i);
}
public boolean add(A a) {
l.add(a);
logQuoted(file, structure(a));
return true;
}
String fileContents() {
return loadTextFile(file);
}
public void clear() {
l.clear();
file.delete();
}
}
static class Matches {
String[] m;
String get(int i) { return m[i]; }
String unq(int i) { return unquote(m[i]); }
String fsi(int i) { return formatSnippetID(unq(i)); }
boolean bool(int i) { return "true".equals(unq(i)); }
String rest() { return m[m.length-1]; } // for matchStart
int psi(int i) { return Integer.parseInt(unq(i)); }
}
static boolean useBouncer = false;
static String server = useBouncer ? "second.tinybrain.de" : "irc.freenode.net";
static List channels = litlist("##linux", "#pircbot");
static String name = "Lookie";
static int reconnectTimeout1 = 60; // seconds after which to send a ping to server
static int reconnectTimeout2 = 80; // seconds after which we try to reconnect
static Map> bulk = new TreeMap(); // key = channel (with "#")
static MyBot bot;
public static void main(String[] args) throws Exception {
Thread _t_0 = new Thread("IRC Log-In") {
public void run() {
try {
bot = new MyBot();
bot.setVerbose(true);
print("PircBot connecting to " + server);
bot.connect(server);
print("PircBot connect issued");
autoReconnect(bot);
} catch (Exception _e) {
throw _e instanceof RuntimeException ? (RuntimeException) _e : new RuntimeException(_e); } }
};
_t_0.start();
}
static class MyBot extends PircBot {
long lastReceived;
MyBot() {
setName(name);
//setRealName(name); // newer version of PircBot only
setLogin(name);
setVersion(name);
setFinger("");
setAutoNickChange(true);
}
public void handleLine(String line) {
getLog("freenode").add(litmap("time", smartNow(), "line", line));
super.handleLine(line);
lastReceived = now();
}
public void onConnect() {
print("PircBot onConnect useBouncer=" + useBouncer);
if (useBouncer) {
String bouncerLogin = loadSecretTextFileMandatory("bouncer-login").trim();
print("Logging in to bouncer");
bot.sendMessage("root", bouncerLogin.trim());
// session should already be there, so resume it
print("Resuming bouncer session");
bot.sendMessage("root", "connect freenode");
}
for (String channel : channels)
bot.joinChannel(channel);
}
}
static void autoReconnect(final MyBot bot) {
Thread _t_1 = new Thread("PircBot Auto-Reconnect") {
public void run() {
try {
while (licensed()) {
sleepSeconds(10);
if (now() >= bot.lastReceived + reconnectTimeout2*1000)
try {
print("PircBot: Trying to reconnect...");
hardClose(bot);
sleepSeconds(1); // allow for input thread to halt
bot.reconnect();
} catch (Exception e) {
print("Reconnect fail: " + e);
// Couldn't reconnect!
}
else if (now() >= bot.lastReceived + reconnectTimeout1*1000)
bot.sendRawLine("TIME"); // send something
}
} catch (Exception _e) {
throw _e instanceof RuntimeException ? (RuntimeException) _e : new RuntimeException(_e); } }
};
_t_1.start();
}
static Socket getSocket(PircBot bot) {
return (Socket) get(get(bot, "_inputThread"), "_socket");
}
static String answer(String s) {
Matches m = new Matches();
if (match("irc log size *", s, m))
try { return lstr(getLog(m.unq(0))); } catch (Throwable __e) { return exceptionToUser(__e); }
return null;
}
static synchronized PersistentLog