Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Action.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Copyright (c) 2006-2007 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.actions;
  23. import com.dmdirc.ServerManager;
  24. import com.dmdirc.WritableFrameContainer;
  25. import com.dmdirc.commandparser.CommandParser;
  26. import com.dmdirc.commandparser.GlobalCommandParser;
  27. import com.dmdirc.logger.ErrorLevel;
  28. import com.dmdirc.logger.Logger;
  29. import com.dmdirc.ui.MainFrame;
  30. import com.dmdirc.ui.interfaces.InputWindow;
  31. import com.dmdirc.ui.interfaces.Window;
  32. import java.io.File;
  33. import java.io.FileInputStream;
  34. import java.io.FileOutputStream;
  35. import java.io.IOException;
  36. import java.io.Serializable;
  37. import java.util.ArrayList;
  38. import java.util.List;
  39. import java.util.Properties;
  40. /**
  41. * Describes a single action.
  42. *
  43. * @author chris
  44. */
  45. public class Action implements Serializable {
  46. /**
  47. * A version number for this class. It should be changed whenever the class
  48. * structure is changed (or anything else that would prevent serialized
  49. * objects being unserialized with the new class).
  50. */
  51. private static final long serialVersionUID = 1;
  52. /** The group this action belongs to. */
  53. private String group;
  54. /** The name of this action. */
  55. private String name;
  56. /** The file containing this action. */
  57. private final File file;
  58. /** The properties read for this action. */
  59. private Properties properties;
  60. /** The ActionTypes that trigger this action. */
  61. private ActionType[] triggers;
  62. /** The commands to execute if this action is triggered. */
  63. private String[] response;
  64. /** The change that should be made to the format string, if any. */
  65. private String newFormat = null;
  66. /** The conditions for this action. */
  67. private List<ActionCondition> conditions = new ArrayList<ActionCondition>();
  68. /**
  69. * Creates a new instance of Action. The group and name specified must
  70. * be the group and name of a valid action already saved to disk.
  71. *
  72. * @param group The group the action belongs to
  73. * @param name The name of the action
  74. */
  75. public Action(final String group, final String name) {
  76. this.group = group;
  77. this.name = name;
  78. final String fs = System.getProperty("file.separator");
  79. final String location = ActionManager.getDirectory() + group + fs + name;
  80. file = new File(location);
  81. try {
  82. final FileInputStream inputStream = new FileInputStream(file);
  83. properties = new Properties();
  84. properties.load(inputStream);
  85. loadAction();
  86. inputStream.close();
  87. } catch (IOException ex) {
  88. Logger.userError(ErrorLevel.HIGH, "I/O error when loading action: "
  89. + group + "/" + name + ": " + ex.getMessage());
  90. }
  91. }
  92. /**
  93. * Creates a new instance of Action with the specified properties and saves
  94. * it to disk.
  95. *
  96. * @param group The group the action belongs to
  97. * @param name The name of the action
  98. * @param triggers The triggers to use
  99. * @param response The response to use
  100. * @param conditions The conditions to use
  101. * @param newFormat The new formatter to use
  102. */
  103. public Action(final String group, final String name,
  104. final ActionType[] triggers, final String[] response,
  105. final List<ActionCondition> conditions, final String newFormat) {
  106. this.group = group;
  107. this.name = name;
  108. this.triggers = triggers.clone();
  109. this.response = response.clone();
  110. this.conditions = conditions;
  111. this.newFormat = newFormat;
  112. final String fs = System.getProperty("file.separator");
  113. final String location = ActionManager.getDirectory() + group + fs + name;
  114. file = new File(location);
  115. save();
  116. ActionManager.registerAction(this);
  117. }
  118. /**
  119. * Loads the various attributes of this action from the properties instance.
  120. */
  121. private void loadAction() {
  122. // Read the triggers
  123. if (properties.containsKey("trigger")) {
  124. final String[] triggerStrings = properties.getProperty("trigger").split("\\|");
  125. Class[] args = null;
  126. triggers = new ActionType[triggerStrings.length];
  127. for (int i = 0; i < triggerStrings.length; i++) {
  128. triggers[i] = ActionManager.getActionType(triggerStrings[i]);
  129. if (triggers[i] == null) {
  130. error("Invalid trigger specified");
  131. return;
  132. } else {
  133. if (i == 0) {
  134. args = triggers[i].getType().getArgTypes();
  135. } else {
  136. if (!triggers[i].getType().equals(triggers[0].getType())) {
  137. error("Triggers are not compatible");
  138. return;
  139. }
  140. }
  141. }
  142. }
  143. } else {
  144. error("No trigger specified");
  145. return;
  146. }
  147. // Read the response
  148. if (properties.containsKey("response")) {
  149. response = properties.getProperty("response").split("\n");
  150. } else {
  151. error("No response specified");
  152. properties.list(System.out);
  153. return;
  154. }
  155. // Read the format change
  156. if (properties.containsKey("format")) {
  157. newFormat = properties.getProperty("format");
  158. }
  159. // Read the conditions
  160. int numConditions = 0;
  161. if (properties.containsKey("conditions")) {
  162. try {
  163. numConditions = Integer.parseInt(properties.getProperty("conditions"));
  164. } catch (NumberFormatException ex) {
  165. error("Invalid number of conditions specified");
  166. return;
  167. }
  168. }
  169. boolean valid = true;
  170. for (int i = 0; i < numConditions; i++) {
  171. valid = valid & readCondition(i);
  172. }
  173. if (valid) {
  174. ActionManager.registerAction(this);
  175. }
  176. }
  177. /**
  178. * Called to save the action.
  179. */
  180. public void save() {
  181. final Properties properties = new Properties();
  182. final StringBuffer triggerString = new StringBuffer();
  183. final StringBuffer responseString = new StringBuffer();
  184. for (ActionType trigger : triggers) {
  185. triggerString.append('|');
  186. triggerString.append(trigger.toString());
  187. }
  188. for (String line : response) {
  189. responseString.append('\n');
  190. responseString.append(line);
  191. }
  192. properties.setProperty("trigger", triggerString.substring(1));
  193. properties.setProperty("conditions", "" + conditions.size());
  194. properties.setProperty("response", responseString.substring(1));
  195. if (newFormat != null) {
  196. properties.setProperty("format", newFormat);
  197. }
  198. int i = 0;
  199. for (ActionCondition condition : conditions) {
  200. properties.setProperty("condition" + i + "-arg", "" + condition.getArg());
  201. properties.setProperty("condition" + i + "-component", condition.getComponent().toString());
  202. properties.setProperty("condition" + i + "-comparison", condition.getComparison().toString());
  203. properties.setProperty("condition" + i + "-target", condition.getTarget());
  204. i++;
  205. }
  206. try {
  207. final FileOutputStream outputStream = new FileOutputStream(file);
  208. properties.store(outputStream, "Created by GUI actions editor");
  209. outputStream.close();
  210. } catch (IOException ex) {
  211. Logger.userError(ErrorLevel.HIGH, "I/O error when saving action: "
  212. + group + "/" + name + ": " + ex.getMessage());
  213. }
  214. }
  215. /**
  216. * Reads the specified condition.
  217. *
  218. * @param condition Condition number to read
  219. * @return True if the condition was read successfully.
  220. */
  221. private boolean readCondition(final int condition) {
  222. // It may help to close your eyes while reading this method.
  223. int arg = -1;
  224. ActionComponent component = null;
  225. ActionComparison comparison = null;
  226. String target = "";
  227. if (properties.containsKey("condition" + condition + "-arg")) {
  228. try {
  229. arg = Integer.parseInt(properties.getProperty("condition" + condition + "-arg"));
  230. } catch (NumberFormatException ex) {
  231. error("Invalid argument number for condition " + condition);
  232. return false;
  233. }
  234. }
  235. if (arg < 0 || arg >= triggers[0].getType().getArity()) {
  236. error("Invalid argument number for condition " + condition);
  237. return false;
  238. }
  239. if (properties.containsKey("condition" + condition + "-component")) {
  240. component = ActionManager.getActionComponent(properties.getProperty("condition" + condition + "-component"));
  241. if (component == null) {
  242. error("Invalid component for condition " + condition);
  243. return false;
  244. }
  245. if (!component.appliesTo().equals(triggers[0].getType().getArgTypes()[arg])) {
  246. error("Component cannot be applied to specified arg in condition " + condition);
  247. return false;
  248. }
  249. } else {
  250. error("No component specified for condition " + condition);
  251. return false;
  252. }
  253. if (properties.containsKey("condition" + condition + "-comparison")) {
  254. comparison = ActionManager.getActionComparison(properties.getProperty("condition" + condition + "-comparison"));
  255. if (comparison == null) {
  256. error("Invalid comparison for condition " + condition);
  257. return false;
  258. }
  259. if (!comparison.appliesTo().equals(component.getType())) {
  260. error("Comparison cannot be applied to specified component in condition " + condition);
  261. return false;
  262. }
  263. } else {
  264. error("No comparison specified for condition " + condition);
  265. return false;
  266. }
  267. if (properties.containsKey("condition" + condition + "-target")) {
  268. target = properties.getProperty("condition" + condition + "-target");
  269. } else {
  270. error("No target specified for condition " + condition);
  271. return false;
  272. }
  273. conditions.add(new ActionCondition(arg, component, comparison, target));
  274. return true;
  275. }
  276. /**
  277. * Raises a trivial error, informing the user of the problem.
  278. *
  279. * @param message The message to be raised
  280. */
  281. private void error(final String message) {
  282. Logger.userError(ErrorLevel.LOW, "Error when parsing action: "
  283. + group + "/" + name + ": " + message);
  284. }
  285. /**
  286. * Renames this action to the specified new name.
  287. *
  288. * @param newName The new name for this action
  289. */
  290. public void rename(final String newName) {
  291. file.renameTo(new File(file.getParent() + System.getProperty("file.separator") + newName));
  292. name = newName;
  293. }
  294. /**
  295. * Sets the group of this action.
  296. *
  297. * @param newGroup The new group for this action
  298. */
  299. public void setGroup(final String newGroup) {
  300. group = newGroup;
  301. final String fs = System.getProperty("file.separator");
  302. final String location = ActionManager.getDirectory() + group + fs + name;
  303. file.renameTo(new File(location));
  304. }
  305. /**
  306. * Deletes this action.
  307. */
  308. public void delete() {
  309. file.delete();
  310. }
  311. /**
  312. * Retrieves a list of this action's conditions.
  313. *
  314. * @return A list of this action's conditions
  315. */
  316. public List<ActionCondition> getConditions() {
  317. return conditions;
  318. }
  319. /**
  320. * Sets this action's conditions.
  321. *
  322. * @param conditions A list of conditions to use
  323. */
  324. public void setConditions(final List<ActionCondition> conditions) {
  325. this.conditions = conditions;
  326. }
  327. /**
  328. * Retrieves this action's triggers.
  329. *
  330. * @return The triggers used by this action
  331. */
  332. public ActionType[] getTriggers() {
  333. return triggers.clone();
  334. }
  335. /**
  336. * Sets this action's triggers.
  337. *
  338. * @param triggers The new triggers to use
  339. */
  340. public void setTriggers(final ActionType[] triggers) {
  341. this.triggers = triggers.clone();
  342. ActionManager.unregisterAction(this);
  343. ActionManager.registerAction(this);
  344. }
  345. /**
  346. * Retrieves this action's new format setting.
  347. *
  348. * @return The format that this action will use, or null if no change
  349. */
  350. public String getNewFormat() {
  351. return newFormat;
  352. }
  353. /**
  354. * Sets this action's new format setting.
  355. *
  356. * @param newFormat The new 'new format' setting
  357. */
  358. public void setNewFormat(final String newFormat) {
  359. this.newFormat = newFormat;
  360. }
  361. /**
  362. * Retrieves this action's response.
  363. *
  364. * @return The commands that will be executed if this action is triggered
  365. */
  366. public String[] getResponse() {
  367. return response.clone();
  368. }
  369. /**
  370. * Sets this action's response.
  371. *
  372. * @param response The new response to use
  373. */
  374. public void setResponse(final String[] response) {
  375. this.response = response.clone();
  376. }
  377. /**
  378. * Retrieves this action's group name.
  379. *
  380. * @return This action's group name
  381. */
  382. public String getGroup() {
  383. return group;
  384. }
  385. /**
  386. * Retrieves this action's name.
  387. *
  388. * @return This action's name
  389. */
  390. public String getName() {
  391. return name;
  392. }
  393. /**
  394. * Triggers this action.
  395. *
  396. * @param format The format of the message that's going to be displayed.
  397. * @param arguments The arguments from the action that caused this trigger.
  398. */
  399. public void trigger(final StringBuffer format, final Object ... arguments) {
  400. for (ActionCondition condition : conditions) {
  401. if (!condition.test(arguments)) {
  402. return;
  403. }
  404. }
  405. final Window active = MainFrame.getMainFrame().getActiveFrame();
  406. InputWindow cw;
  407. CommandParser cp = null;
  408. if (arguments.length > 0 && arguments[0] instanceof WritableFrameContainer) {
  409. cw = ((WritableFrameContainer) arguments[0]).getFrame();
  410. } else if (active != null && active instanceof InputWindow) {
  411. cw = (InputWindow) MainFrame.getMainFrame().getActiveFrame();
  412. } else if (ServerManager.getServerManager().numServers() > 0) {
  413. cw = ServerManager.getServerManager().getServers().get(0).getFrame();
  414. } else {
  415. cw = null;
  416. cp = GlobalCommandParser.getGlobalCommandParser();
  417. }
  418. if (cw != null) {
  419. cp = cw.getCommandParser();
  420. }
  421. for (String command : response) {
  422. cp.parseCommand(cw, ActionManager.substituteVars(command, arguments));
  423. }
  424. if (newFormat != null && format != null) {
  425. format.setLength(0);
  426. format.append(newFormat);
  427. }
  428. }
  429. }