@author Manjago (kirill@temnenkov.com)
| 43 | * @author Manjago (kirill@temnenkov.com) |
| 44 | */ |
| 45 | public class ScriptFix extends AbstractRobot { |
| 46 | |
| 47 | private final Logger logger = Logger |
| 48 | .getLogger(getClass()); |
| 49 | |
| 50 | private static final Pattern LIST = Pattern.compile("^%LIST$", |
| 51 | Pattern.CASE_INSENSITIVE); |
| 52 | private static final Pattern RUN = Pattern.compile("^%RUN (\\d+)$", |
| 53 | Pattern.CASE_INSENSITIVE); |
| 54 | private static final Pattern SCRIPT = Pattern.compile(".*\\{(.*)?\\}.*", |
| 55 | Pattern.DOTALL); |
| 56 | |
| 57 | private static final ConcurrentDateFormatAccess format = new ConcurrentDateFormatAccess( |
| 58 | "dd.MM.yyyy HH:mm"); |
| 59 | |
| 60 | @Override |
| 61 | public void execute(FtnMessage fmsg) throws Exception { |
| 62 | Link link = getAndCheckLink(fmsg); |
| 63 | if (link == null) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | // если скрипт - то фигарим скрипт |
| 68 | logger.l5(String.format("process message [%s]", fmsg.getText())); |
| 69 | String scriptContent = extractScript(fmsg.getText()); |
| 70 | if (scriptContent != null){ |
| 71 | logger.l5("try process script"); |
| 72 | processScript(fmsg, scriptContent); |
| 73 | } else{ |
| 74 | logger.l5("try process commands"); |
| 75 | processCommands(fmsg); |
| 76 | } |
| 77 | |
| 78 | } |
| 79 | |
| 80 | private void processScript(FtnMessage fmsg, String scriptContent) { |
| 81 | String output = executeScriptWithConsole(scriptContent, false); |
| 82 | logger.l5(String.format("got script output %s", output)); |
| 83 | FtnTools.writeReply(fmsg, |
| 84 | MessageFormat.format("{0} exec script", getRobotName()), |
| 85 | output != null ? output : "Okay"); |
| 86 | } |
| 87 | |
| 88 | public static String executeScriptWithConsole(String scriptContent, boolean force) { |
| 89 | Bindings bindings = new SimpleBindings(); |
| 90 | final JScriptConsole jScriptConsole = new JScriptConsole(); |
| 91 | bindings.put("console", jScriptConsole); |
| 92 | String result = JscriptExecutor.executeScript(scriptContent, bindings, force); |
| 93 | if (result != null){ |
| 94 | jScriptConsole.log(String.format("\n%s", result)); |
| 95 | } |
| 96 | return jScriptConsole.out(); |
| 97 | } |
| 98 | |
| 99 | private void processCommands(FtnMessage fmsg) throws SQLException { |
| 100 | for (String line : fmsg.getText().split("\n")) { |
| 101 | line = line.toLowerCase(); |
| 102 |