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 org.pushingpixels.substance.api.*;
import org.pushingpixels.substance.api.skin.*;
import javax.swing.border.Border;
import org.jnativehook.keyboard.NativeKeyEvent;
import java.text.*;
import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import org.jnativehook.GlobalScreen;
import org.jnativehook.keyboard.NativeKeyAdapter;
import java.awt.datatransfer.StringSelection;
class main {
static File nohupJavax(String javaxargs) {
return directNohupJavax(javaxargs);
}
// not using pre-spun VM if vmArgs present
static File nohupJavax(String javaxargs, String vmArgs) {
return directNohupJavax(javaxargs, vmArgs);
}
static class Suggestion extends Concept {
static String _fieldOrder = "globalID date text author actionCode";
String globalID = aGlobalID();
long date;
String text;
String author;
String actionCode;
}
static Suggestion showing;
static ReliableSingleThread rstUpdate = new ReliableSingleThread("updateImpl");
static PastValueKeeper < Suggestion > past = new PastValueKeeper(3600.0);
public static void main(final String[] args) throws Exception { substance();
defaultFrameTitle("Suggestion");
autoRestart();
db();
botWithInput("Suggestion Master.",
" me suggests hello with action [[infoBox(\"hello\")]] ");
}
static String answer(String s) {
final Matches m = new Matches();
if (match("* suggests * with action *", s, m)) {
Suggestion sugg = cnew(Suggestion.class, "date" , now(), "text" , m.unq(1), "author" , m.unq(0), "actionCode" , m.unq(2));
rstUpdate.get();
return "OK, ID: " + sugg.globalID;
}
if (match("show last suggestion", s)) { return showLastSuggestion(); }
return null;
}
static void updateImpl() {
final Suggestion s = highestByField(list(Suggestion.class), "date");
if (s != showing) {
showing = s;
print("Showing suggestion: " + s.text);
logQuotedWithDate(javaxDataDir("suggestions.log"), "Showing: " + struct(s));
past.add(s);
showSuggestion_master(s.text, new Runnable() { public void run() { try {
printIndent(s.actionCode);
logQuotedWithDate(javaxDataDir("suggestions.log"), "Running: " + struct(s));
optimizedJavaEval(s.actionCode);
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "printIndent(s.actionCode);\n logQuotedWithDate(javaxDataDir(\"suggestions...."; }});
}
}
static String showLastSuggestion() {
double lookback = 1.0;
print("showLastSuggestion");
Suggestion s = past.valueAt_orFirst(toMS(lookback));
if (s != null) {
Suggestion sugg = cnew(Suggestion.class, "date" , now());
ccopyFields(s, sugg, "text", "author", "actionCode");
cset(sugg, "copiedFrom" , s.globalID);
rstUpdate.get();
return "OK, ID: " + sugg.globalID;
}
return "Nothing there";
}
static String aGlobalID() {
return randomID(16);
}
static String struct(Object o) {
return structure(o);
}
static String struct(Object o, structure_Data data) {
return structure(o, data);
}
static File javaxDataDir_dir; // can be set to work on different base dir
static File javaxDataDir() {
return javaxDataDir_dir != null ? javaxDataDir_dir : new File(userHome(), "JavaX-Data");
}
static File javaxDataDir(String sub) {
return newFile(javaxDataDir(), sub);
}
static A highestByField(String field, Collection l) {
A best = null;
Object bestValue = null;
if (l != null) for(A a : l) {
Object val = getOpt(a, field);
if (val != null && (bestValue == null || cmp(val, bestValue) > 0)) {
best = a;
bestValue = val;
}
}
return best;
}
static A highestByField(Collection l, String field) {
return highestByField(field, l);
}
static A printIndent(A o) {
print(indentx(str(o)));
return o;
}
static void printIndent(String indent, Object o) {
print(indentx(indent, str(o)));
}
static void printIndent(int indent, Object o) {
print(indentx(indent, str(o)));
}
static void botWithInput(String input) {
botWithInput(defaultBotName(), input);
}
static void botWithInput(String botName, String input) {
setConsoleInputIfEmpty(input);
consoleAddAskButton();
bot(botName);
}
static JLabel showSuggestion_current;
static boolean showSuggestion_keyListenerInstalled;
// action is called in new thread
static JLabel showSuggestion_master(final String suggestionText, final Object action) {
hideSuggestion();
final JLabel label = showSuggestion_individual(suggestionText, action);
JButton btnOK = firstButton(getWindow(label));
setText(btnOK, getText(btnOK) + appendBracketed(hotKey_teach()));
showSuggestion_current = label;
showSuggestion_installHotkey();
return label;
}
static void showSuggestion_installHotkey() {
if (showSuggestion_keyListenerInstalled) return;
showSuggestion_keyListenerInstalled = true;
onGlobalHotKey(hotKey_teach(), new Runnable() { public void run() { try { swingLater(new Runnable() { public void run() { try {
boolean b = isComponentShowing(showSuggestion_current);
print("hotkey: " + b);
if (b)
clickFirstButton(getWindow(showSuggestion_current));
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "boolean b = isComponentShowing(showSuggestion_current);\r\n print(\"hotkey: \"..."; }});
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "swingLater(r {\r\n boolean b = isComponentShowing(showSuggestion_current);\r\n..."; }});
}
static long now_virtualTime;
static long now() {
return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis();
}
static long toMS(double seconds) {
return (long) (seconds*1000);
}
static Object rpcNoArgs(String botName, String method) {
return rpc(botName, method);
}
static void logQuotedWithDate(String s) {
logQuotedWithTime(s);
}
static void logQuotedWithDate(String logFile, String s) {
logQuotedWithTime(logFile, s);
}
static void logQuotedWithDate(File logFile, String s) {
logQuotedWithTime(logFile, s);
}
static double autoRestart_interval = 10;
static boolean autoRestart_on, autoRestart_debug, autoRestart_simulate;
static java.util.Timer autoRestart_timer;
static void autoRestart(double interval) {
autoRestart_interval = interval;
autoRestart();
}
static void autoRestart() {
if (!isMain() || autoRestart_on) return;
autoRestart_on = true;
autoRestart_schedule();
preloadProgramTitle();
}
static void autoRestart_off() {
if (!autoRestart_on) return;
stopTimer(autoRestart_timer);
autoRestart_timer = null;
}
static void autoRestart_schedule() {
autoRestart_timer = doLater_daemon(toMS(autoRestart_interval), "autoRestart_check");
}
static void autoRestart_check() {
try {
String newMD5;
try {
newMD5 = loadPageSilently("http://botcompany.de/1010693/raw?id=" + psI(programID()));
} catch (Throwable e) {
return;
}
if (!isMD5(newMD5)) { if (autoRestart_debug) print("autoRestart: no server transpilation"); return; }
if (autoRestart_localMD5 == null)
autoRestart_localMD5 = md5(loadCachedTranspilation(programID()));
String localMD5 = autoRestart_localMD5();
if (neq(localMD5, newMD5)) {
if (autoRestart_simulate)
print("Would upgrade now. " + localMD5 + " -> " + newMD5);
else {
infoBox("Upgrading " + programTitle());
restartWithDelay(500);
sleep();
}
}
} finally {
if (autoRestart_debug) print("autoRestart: Done");
autoRestart_schedule();
}
}
static RuntimeException rethrow(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static void substance() {
substanceLAF();
}
static void substance(String skinName) {
substanceLAF(skinName);
}
static Map _registerThread_threads = newWeakHashMap();
static Thread _registerThread(Thread t) {
_registerThread_threads.put(t, true);
return t;
}
static void _registerThread() { _registerThread(Thread.currentThread()); }
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 String defaultFrameTitle() {
return autoFrameTitle();
}
static void defaultFrameTitle(String title) {
autoFrameTitle_value = title;
}
// returns number of changes
static int cset(Concept c, Object... values) { try {
if (c == null) return 0;
int changes = 0;
values = expandParams(c.getClass(), values);
warnIfOddCount(values);
for (int i = 0; i+1 < l(values); i += 2) {
String field = (String) values[i];
Object value = values[i+1];
Field f = setOpt_findField(c.getClass(), field);
//print("cset: " + c.id + " " + field + " " + struct(value) + " " + f);
if (value instanceof RC) value = c._concepts.getConcept((RC) value);
value = deref(value);
if (value instanceof String && l((String) value) >= concepts_internStringsLongerThan) value = intern((String) value);
if (f == null) {
// TODO: keep ref if it exists
mapPut2(c.fieldValues, field, value instanceof Concept ? c.new Ref((Concept) value) : value);
c.change();
} else if (isSubtypeOf(f.getType(), Concept.Ref.class)) {
((Concept.Ref) f.get(c)).set((Concept) derefRef(value));
c.change(); ++changes;
} else {
Object old = f.get(c);
if (neq(value, old)) {
f.set(c, value);
if ((f.getModifiers() & java.lang.reflect.Modifier.TRANSIENT) == 0) c.change();
++changes;
}
}
}
return changes;
} catch (Exception __e) { throw rethrow(__e); } }
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile StringBuffer 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 int print_maxLineLength = 0; // 0 = unset
static boolean print_silent; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal