You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScriptCommand.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.scriptplugin;
  23. import com.dmdirc.commandparser.CommandArguments;
  24. import com.dmdirc.config.IdentityManager;
  25. import com.dmdirc.commandparser.CommandManager;
  26. import com.dmdirc.commandparser.commands.GlobalCommand;
  27. import com.dmdirc.ui.interfaces.InputWindow;
  28. import com.dmdirc.commandparser.commands.IntelligentCommand;
  29. import com.dmdirc.ui.input.AdditionalTabTargets;
  30. import com.dmdirc.ui.input.TabCompletionType;
  31. import java.util.LinkedList;
  32. import java.util.List;
  33. import java.util.Map;
  34. import java.io.File;
  35. import java.io.FileWriter;
  36. import java.io.IOException;
  37. import java.lang.reflect.Method;
  38. /**
  39. * The Script Command allows controlling of the scriptplugin.
  40. *
  41. * @author Shane 'Dataforce' McCormack
  42. */
  43. public final class ScriptCommand extends GlobalCommand implements IntelligentCommand {
  44. /** My Plugin */
  45. final ScriptPlugin myPlugin;
  46. /**
  47. * Creates a new instance of ScriptCommand.
  48. */
  49. public ScriptCommand(final ScriptPlugin plugin) {
  50. super();
  51. myPlugin = plugin;
  52. CommandManager.registerCommand(this);
  53. }
  54. /**
  55. * Executes this command.
  56. *
  57. * @param origin The frame in which this command was issued
  58. * @param server The server object that this command is associated with
  59. * @param isSilent Whether this command is silenced or not
  60. * @param args The user supplied arguments
  61. */
  62. @Override
  63. public void execute(final InputWindow origin, final boolean isSilent, final CommandArguments commandArgs) {
  64. final String[] args = commandArgs.getArguments();
  65. if (args.length > 0 && (args[0].equalsIgnoreCase("rehash") || args[0].equalsIgnoreCase("reload"))) {
  66. sendLine(origin, isSilent, FORMAT_OUTPUT, "Reloading scripts");
  67. myPlugin.rehash();
  68. } else if (args.length > 0 && args[0].equalsIgnoreCase("load")) {
  69. if (args.length > 1) {
  70. final String filename = commandArgs.getArgumentsAsString(1);
  71. sendLine(origin, isSilent, FORMAT_OUTPUT, "Loading: "+filename+" ["+myPlugin.loadScript(myPlugin.getScriptDir()+filename)+"]");
  72. } else {
  73. sendLine(origin, isSilent, FORMAT_ERROR, "You must specify a script to load");
  74. }
  75. } else if (args.length > 0 && args[0].equalsIgnoreCase("unload")) {
  76. if (args.length > 1) {
  77. final String filename = commandArgs.getArgumentsAsString(1);
  78. sendLine(origin, isSilent, FORMAT_OUTPUT, "Unloading: "+filename+" ["+myPlugin.loadScript(myPlugin.getScriptDir()+filename)+"]");
  79. } else {
  80. sendLine(origin, isSilent, FORMAT_ERROR, "You must specify a script to unload");
  81. }
  82. } else if (args.length > 0 && args[0].equalsIgnoreCase("eval")) {
  83. if (args.length > 1) {
  84. final String script = commandArgs.getArgumentsAsString(1);
  85. sendLine(origin, isSilent, FORMAT_OUTPUT, "Evaluating: "+script);
  86. try {
  87. ScriptEngineWrapper wrapper;
  88. if (IdentityManager.getGlobalConfig().hasOptionString(myPlugin.getDomain(), "eval.baseFile")) {
  89. final String baseFile = myPlugin.getScriptDir()+'/'+IdentityManager.getGlobalConfig().getOption(myPlugin.getDomain(), "eval.baseFile");
  90. if ((new File(baseFile)).exists()) {
  91. wrapper = new ScriptEngineWrapper(myPlugin, baseFile);
  92. } else {
  93. wrapper = new ScriptEngineWrapper(myPlugin, null);
  94. }
  95. } else {
  96. wrapper = new ScriptEngineWrapper(myPlugin, null);
  97. }
  98. wrapper.getScriptEngine().put("cmd_origin", origin);
  99. wrapper.getScriptEngine().put("cmd_isSilent", isSilent);
  100. wrapper.getScriptEngine().put("cmd_args", args);
  101. sendLine(origin, isSilent, FORMAT_OUTPUT, "Result: "+wrapper.getScriptEngine().eval(script));
  102. } catch (Exception e) {
  103. sendLine(origin, isSilent, FORMAT_OUTPUT, "Exception: "+e+" -> "+e.getMessage());
  104. if (IdentityManager.getGlobalConfig().getOptionBool(myPlugin.getDomain(), "eval.showStackTrace")) {
  105. try {
  106. final Class<?> logger = Class.forName("com.dmdirc.logger.Logger");
  107. if (logger != null) {
  108. final Method exceptionToStringArray = logger.getDeclaredMethod("exceptionToStringArray", new Class[]{Throwable.class});
  109. exceptionToStringArray.setAccessible(true);
  110. final String[] stacktrace = (String[])exceptionToStringArray.invoke(null, e);
  111. for (String line : stacktrace) {
  112. sendLine(origin, isSilent, FORMAT_OUTPUT, "Stack trace: "+line);
  113. }
  114. }
  115. } catch (Exception ex) {
  116. sendLine(origin, isSilent, FORMAT_OUTPUT, "Stack trace: Exception showing stack trace: "+ex+" -> "+ex.getMessage());
  117. }
  118. }
  119. }
  120. } else {
  121. sendLine(origin, isSilent, FORMAT_ERROR, "You must specify some script to eval.");
  122. }
  123. } else if (args.length > 0 && args[0].equalsIgnoreCase("savetobasefile")) {
  124. if (args.length > 2) {
  125. final String[] bits = args[1].split("/");
  126. final String functionName = bits[0];
  127. final String script = commandArgs.getArgumentsAsString(2);
  128. sendLine(origin, isSilent, FORMAT_OUTPUT, "Saving as '"+functionName+"': "+script);
  129. if (IdentityManager.getGlobalConfig().hasOptionString(myPlugin.getDomain(), "eval.baseFile")) {
  130. try {
  131. final String baseFile = myPlugin.getScriptDir()+'/'+IdentityManager.getGlobalConfig().getOption(myPlugin.getDomain(), "eval.baseFile");
  132. final FileWriter writer = new FileWriter(baseFile, true);
  133. writer.write("function ");
  134. writer.write(functionName);
  135. writer.write("(");
  136. for (int i = 1; i < bits.length; i++) {
  137. writer.write(bits[i]);
  138. writer.write(" ");
  139. }
  140. writer.write(") {\n");
  141. writer.write(script);
  142. writer.write("\n}\n");
  143. writer.flush();
  144. writer.close();
  145. } catch (IOException ioe) {
  146. sendLine(origin, isSilent, FORMAT_ERROR, "IOException: "+ioe.getMessage());
  147. }
  148. } else {
  149. sendLine(origin, isSilent, FORMAT_ERROR, "No baseFile specified, please /set "+myPlugin.getDomain()+" eval.baseFile filename (stored in scripts dir of profile)");
  150. }
  151. } else if (args.length > 1) {
  152. sendLine(origin, isSilent, FORMAT_ERROR, "You must specify some script to save.");
  153. } else {
  154. sendLine(origin, isSilent, FORMAT_ERROR, "You must specify a function name and some script to save.");
  155. }
  156. } else if (args.length > 0 && args[0].equalsIgnoreCase("help")) {
  157. sendLine(origin, isSilent, FORMAT_OUTPUT, "This command allows you to interact with the script plugin");
  158. sendLine(origin, isSilent, FORMAT_OUTPUT, "-------------------");
  159. sendLine(origin, isSilent, FORMAT_OUTPUT, "reload/rehash - Reload all loaded scripts");
  160. sendLine(origin, isSilent, FORMAT_OUTPUT, "load <script> - load scripts/<script> (file name relative to scripts dir)");
  161. sendLine(origin, isSilent, FORMAT_OUTPUT, "unload <script> - unload <script> (full file name)");
  162. sendLine(origin, isSilent, FORMAT_OUTPUT, "eval <script> - evaluate the code <script> and return the result");
  163. sendLine(origin, isSilent, FORMAT_OUTPUT, "savetobasefile <name> <script> - save the code <script> to the eval basefile ("+myPlugin.getDomain()+".eval.basefile)");
  164. sendLine(origin, isSilent, FORMAT_OUTPUT, " as the function <name> (name/foo/bar will save it as 'name' with foo and");
  165. sendLine(origin, isSilent, FORMAT_OUTPUT, " bar as arguments.");
  166. sendLine(origin, isSilent, FORMAT_OUTPUT, "-------------------");
  167. } else {
  168. sendLine(origin, isSilent, FORMAT_ERROR, "Unknown subcommand.");
  169. }
  170. }
  171. /**
  172. * Returns a list of suggestions for the specified argument, given the list
  173. * of previous arguments.
  174. * @param arg The argument that is being completed
  175. * @param previousArgs The contents of the previous arguments, if any
  176. * @return A list of suggestions for the argument
  177. */
  178. @Override
  179. public AdditionalTabTargets getSuggestions(final int arg, final List<String> previousArgs) {
  180. final AdditionalTabTargets res = new AdditionalTabTargets();
  181. res.excludeAll();
  182. if (arg == 0) {
  183. res.add("help");
  184. res.add("rehash");
  185. res.add("reload");
  186. res.add("load");
  187. res.add("unload");
  188. res.add("eval");
  189. res.add("savetobasefile");
  190. } else if (arg == 1) {
  191. final Map<String,ScriptEngineWrapper> scripts = myPlugin.getScripts();
  192. if (previousArgs.get(0).equalsIgnoreCase("load")) {
  193. for (String filename : getPossibleScripts()) {
  194. res.add(filename);
  195. }
  196. } else if (previousArgs.get(0).equalsIgnoreCase("unload")) {
  197. for (String filename : scripts.keySet()) {
  198. res.add(filename);
  199. }
  200. }
  201. }
  202. return res;
  203. }
  204. /**
  205. * Retrieves a list of all installed scripts.
  206. * Any file under the main plugin directory (~/.DMDirc/scripts or similar)
  207. * that matches *.js is deemed to be a valid script.
  208. *
  209. * @return A list of all installed scripts
  210. */
  211. private List<String> getPossibleScripts() {
  212. final List<String> res = new LinkedList<String>();
  213. final LinkedList<File> dirs = new LinkedList<File>();
  214. dirs.add(new File(myPlugin.getScriptDir()));
  215. while (!dirs.isEmpty()) {
  216. final File dir = dirs.pop();
  217. if (dir.isDirectory()) {
  218. for (File file : dir.listFiles()) {
  219. dirs.add(file);
  220. }
  221. } else if (dir.isFile() && dir.getName().endsWith(".js")) {
  222. final String target = dir.getPath();
  223. res.add(target.substring(myPlugin.getScriptDir().length(), target.length()));
  224. }
  225. }
  226. return res;
  227. }
  228. /**
  229. * Returns this command's name.
  230. *
  231. * @return The name of this command
  232. */
  233. @Override
  234. public String getName() { return "script"; }
  235. /**
  236. * Returns whether or not this command should be shown in help messages.
  237. *
  238. * @return True iff the command should be shown, false otherwise
  239. */
  240. @Override
  241. public boolean showInHelp() { return true; }
  242. /**
  243. * Returns a string representing the help message for this command.
  244. *
  245. * @return the help message for this command
  246. */
  247. @Override
  248. public String getHelp() { return "script - Allows controlling the script plugin"; }
  249. }