import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
import java.text.SimpleDateFormat;
import java.text.NumberFormat;
import se.unlogic.eagledns.EagleDNS;
class main {
static double refreshEvery = 60;
static int smartBotTimeOut = 60*5;
static String zone = "\r\nbotcompany.de. IN SOA ai1.lol. info.ai1.lol. (\r\n 20091118 ; Serial\r\n 5M ; Refresh\r\n 2M ; Retry\r\n 5M ; Expiry\r\n 5M ) ; Minimum\r\n@ IN NS ai1.lol.\r\n@ IN NS tinybrain.de.\r\n@ IN A 69.10.46.185\r\n* IN A 69.10.46.185\r\n@ MX 10 mail1.domainssaubillig.de.\r\n";
// dnsjava
// EagleDNS
// log4j
static File zonesDir;
static EagleDNS eagleDNS;
public static void main(final String[] args) throws Exception {
tryToAcquirePort5000();
String conf = loadSnippet("#1010767");
zonesDir = programFile("zones");
saveZone();
conf = conf.replace(
"zones",
"" + f2s(zonesDir) + "");
File configFile = saveProgramTextFile("config.xml", conf);
//eagleDNS = nuObject("se.unlogic.eagledns.EagleDNS", f2s(configFile));
eagleDNS = new EagleDNS(f2s(configFile));
bot("Eagle DNS.");
doEvery_daemon(refreshEvery, "refresh");
}
static String answer(String s) {
final Matches m = new Matches();
if (match("test", s)) return "OK";
if (match("refresh", s)) {
refresh();
return "OK";
}
return null;
}
static void refresh() {
saveZone();
eagleDNS.reloadZones();
}
static String makeZone() {
String z = rtrim(zone);
String ip = smartBotIP();
if (ip != null) {
z += ("\nsmartbot-direct " + smartBotTimeOut + " IN A ") + ip;
z += ("\nsmartbot " + smartBotTimeOut + " IN A ") + ip;
}
return z;
}
static void saveZone() {
saveTextFile(newFile(zonesDir, "botcompany.de"), makeZone());
}
static void tryToAcquirePort5000() {
startThread("Move to port 5000", new Runnable() { public void run() { try {
while (licensed()) { try {
if (vmPort() == 5000) return;
if (portIsFree(5000)) {
print("Trying to move to port 5000...");
restartVMAndroid();
print("...went to port: " + vmPort() + ".");
}
} catch (Throwable __e) { _handleException(__e); } sleepSeconds(10); }
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "repeat { try {\r\n if (vmPort() == 5000) return;\r\n if (portIsFree(500..."; }});
}
static boolean preferCached = false;
static boolean loadSnippet_debug = false;
static ThreadLocal loadSnippet_silent = new ThreadLocal();
static ThreadLocal loadSnippet_publicOnly = new ThreadLocal();
static int loadSnippet_timeout = 30000;
static String loadSnippet(String snippetID) { try {
if (snippetID == null) return null;
return loadSnippet(parseSnippetID(snippetID), preferCached);
} catch (Exception __e) { throw rethrow(__e); } }
static String loadSnippet(String snippetID, boolean preferCached) throws IOException {
return loadSnippet(parseSnippetID(snippetID), preferCached);
}
static IF1 loadSnippet;
static String loadSnippet(long snippetID) { return loadSnippet != null ? loadSnippet.get(snippetID) : loadSnippet_base(snippetID); }
final static String loadSnippet_fallback(IF1 _f, long snippetID) { return _f != null ? _f.get(snippetID) : loadSnippet_base(snippetID); }
static String loadSnippet_base(long snippetID) { try {
return loadSnippet(snippetID, preferCached);
} catch (Exception __e) { throw rethrow(__e); } }
static String loadSnippet(long snippetID, boolean preferCached) throws IOException {
String text;
if (isLocalSnippetID(snippetID))
return loadLocalSnippet(snippetID);
IResourceLoader rl = vm_getResourceLoader();
if (rl != null)
return rl.loadSnippet(fsI(snippetID));
// boss bot disabled for now for shorter transpilations
/*text = getSnippetFromBossBot(snippetID);
if (text != null) return text;*/
initSnippetCache();
text = DiskSnippetCache_get(snippetID);
if (preferCached && text != null)
return text;
try {
if (loadSnippet_debug && text != null) System.err.println("md5: " + md5(text));
String url = tb_mainServer() + "/getraw.php?id=" + snippetID + "&utf8=1";
if (nempty(text)) url += "&md5=" + md5(text);
if (!isTrue(loadSnippet_publicOnly.get()))
url += standardCredentials();
String text2 = loadSnippet_loadFromServer(url);
boolean same = eq(text2, "==*#*==");
if (loadSnippet_debug) print("loadSnippet: same=" + same);
if (!same) text = text2;
} catch (RuntimeException e) {
e.printStackTrace();
throw new IOException("Snippet #" + snippetID + " not found or not public");
}
try {
initSnippetCache();
DiskSnippetCache_put(snippetID, text);
} catch (IOException e) {
System.err.println("Minor warning: Couldn't save snippet to cache (" + DiskSnippetCache_getDir() + ")");
}
return text;
}
static File DiskSnippetCache_dir;
public static void initDiskSnippetCache(File dir) {
DiskSnippetCache_dir = dir;
dir.mkdirs();
}
public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException {
return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null);
}
private static File DiskSnippetCache_getFile(long snippetID) {
return new File(DiskSnippetCache_dir, "" + snippetID);
}
public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException {
saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet);
}
public static File DiskSnippetCache_getDir() {
return DiskSnippetCache_dir;
}
public static void initSnippetCache() {
if (DiskSnippetCache_dir == null)
initDiskSnippetCache(getGlobalCache());
}
static String loadSnippet_loadFromServer(String url) {
Integer oldTimeout = setThreadLocal(loadPage_forcedTimeout_byThread, loadSnippet_timeout);
try {
return isTrue(loadSnippet_silent.get()) ? loadPageSilently(url) : loadPage(url);
} finally {
loadPage_forcedTimeout_byThread.set(oldTimeout);
}
}
static File programFile(String name) {
return prepareProgramFile(name);
}
static File programFile(String progID, String name) {
return prepareProgramFile(progID, name);
}
static String f2s(File f) {
return f == null ? null : f.getAbsolutePath();
}
static String f2s(String s) { return f2s(newFile(s)); }
static String f2s(java.nio.file.Path p) {
return p == null ? null : f2s(p.toFile());
}
static File saveProgramTextFile(String name, String contents) {
return saveTextFile(getProgramFile(name), contents);
}
static File saveProgramTextFile(String progID, String name, String contents) {
return saveTextFile(getProgramFile(progID, name), contents);
}
static Android3 bot(String greeting) {
return makeAndroid3(greeting);
}
static Android3 bot(Android3 a) {
return makeBot(a);
}
static Android3 bot(String greeting, Object responder) {
return makeBot(greeting, responder);
}
static Android3 bot() {
return makeBot();
}
// firstDelay = delay
static FixedRateTimer doEvery_daemon(long delay, final Object r) {
return doEvery_daemon(delay, delay, r);
}
static FixedRateTimer doEvery_daemon(long delay, long firstDelay, final Object r) {
FixedRateTimer timer = new FixedRateTimer(true);
timer.scheduleAtFixedRate(smartTimerTask(r, timer, delay), firstDelay, delay);
return timer;
}
static FixedRateTimer doEvery_daemon(double delaySeconds, final Object r) {
return doEvery_daemon(toMS(delaySeconds), r);
}
static boolean match(String pat, String s) {
return match3(pat, s);
}
static boolean match(String pat, String s, Matches matches) {
return match3(pat, s, matches);
}
static boolean match(String pat, List toks, Matches matches) {
return match3(pat, toks, matches);
}
public static String rtrim(String s) {
if (s == null) return null;
int i = s.length();
while (i > 0 && " \t\r\n".indexOf(s.charAt(i-1)) >= 0)
--i;
return i < s.length() ? s.substring(0, i) : s;
}
static String smartBotIP() { try {
URL url = new URL(smartBotURL());
String host = url.getHost();
return isIPv4(host) ? host : null;
} catch (Throwable __e) { return null; } }
/** writes safely (to temp file, then rename) */
static File saveTextFile(String fileName, String contents) throws IOException {
CriticalAction action = beginCriticalAction("Saving file " + fileName + " (" + l(contents) + " chars)");
try {
File file = new File(fileName);
mkdirsForFile(file);
String tempFileName = fileName + "_temp";
File tempFile = new File(tempFileName);
if (contents != null) {
if (tempFile.exists()) try {
String saveName = tempFileName + ".saved." + now();
copyFile(tempFile, new File(saveName));
} catch (Throwable e) { printStackTrace(e); }
FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath());
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
PrintWriter printWriter = new PrintWriter(outputStreamWriter);
printWriter.print(contents);
printWriter.close();
}
if (file.exists() && !file.delete())
throw new IOException("Can't delete " + fileName);
if (contents != null)
if (!tempFile.renameTo(file))
throw new IOException("Can't rename " + tempFile + " to " + file);
vmBus_send("wroteFile", file);
return file;
} finally {
action.done();
}
}
static File saveTextFile(File fileName, String contents) { try {
saveTextFile(fileName.getPath(), contents);
return fileName;
} catch (Exception __e) { throw rethrow(__e); } }
static File newFile(File base, String... names) {
for (String name : names) base = new File(base, name);
return base;
}
static File newFile(String name) {
return name == null ? null : new File(name);
}
static File newFile(String base, String... names) {
return newFile(newFile(base), names);
}
static Thread startThread(Object runnable) {
return startThread(defaultThreadName(), runnable);
}
static Thread startThread(String name, Object runnable) {
runnable = wrapAsActivity(runnable);
return startThread(newThread(toRunnable(runnable), name));
}
static Thread startThread(Thread t) {
_registerThread(t);
t.start();
return t;
}
static volatile boolean licensed_yes = true;
static boolean licensed() {
if (!licensed_yes) return false;
ping_okInCleanUp();
return true;
}
static void licensed_off() {
licensed_yes = false;
}
static int vmPort() {
return myVMPort();
}
static boolean portIsFree(int port) {
try {
new ServerSocket(port).close();
return true;
} catch (IOException e) {
return false;
}
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal