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.

ActionModel.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  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.FrameContainer;
  24. import com.dmdirc.GlobalWindow;
  25. import com.dmdirc.Precondition;
  26. import com.dmdirc.ServerManager;
  27. import com.dmdirc.commandparser.parsers.CommandParser;
  28. import com.dmdirc.commandparser.parsers.GlobalCommandParser;
  29. import com.dmdirc.interfaces.actions.ActionType;
  30. import java.util.ArrayList;
  31. import java.util.Arrays;
  32. import java.util.List;
  33. import javax.inject.Provider;
  34. /**
  35. * Represents the basic model of an action, and its triggering mechanism. Saving and loading are
  36. * handled by the Action class.
  37. */
  38. public class ActionModel {
  39. // TODO: Sort out the mess of protected fields here.
  40. /** Provider of global command parsers, for use when triggering window-less actions. */
  41. private final Provider<GlobalCommandParser> globalCommandParserProvider;
  42. /** Provider of global windows for global actions. */
  43. private final Provider<GlobalWindow> globalWindowProvider;
  44. /** Factory to use to creator substitutors. */
  45. private final ActionSubstitutorFactory substitutorFactory;
  46. /** The group this action belongs to. */
  47. protected String group;
  48. /** The name of this action. */
  49. protected String name;
  50. /** The ActionTypes that trigger this action. */
  51. protected ActionType[] triggers;
  52. /** The commands to execute if this action is triggered. */
  53. protected String[] response;
  54. /** The change that should be made to the format string, if any. */
  55. protected String newFormat;
  56. /** The conditions for this action. */
  57. protected List<ActionCondition> conditions = new ArrayList<>();
  58. /** The condition tree used for evaluating conditions. */
  59. protected ConditionTree conditionTree;
  60. /** Whether this action has been modified or not. */
  61. protected boolean modified;
  62. /** Whether this action wishes the event to be stopped. */
  63. protected boolean stop;
  64. /** The concurrency group this action belongs to, if any. */
  65. protected String concurrencyGroup;
  66. /** The status of this action. */
  67. protected ActionStatus status = ActionStatus.ACTIVE;
  68. /** A description of any error that occurs while creating the action. */
  69. protected String error;
  70. /** The type of error that occurred, if any. */
  71. protected ActionErrorType errorType;
  72. /**
  73. * Creates a new instance of ActionModel with the specified properties.
  74. *
  75. * @param globalCommandParserProvider Provider of global command parsers for global actions.
  76. * @param globalWindowProvider Provider of global windows for global actions.
  77. * @param substitutorFactory Factory to use to create action substitutors.
  78. * @param group The group the action belongs to
  79. * @param name The name of the action
  80. */
  81. public ActionModel(
  82. final Provider<GlobalCommandParser> globalCommandParserProvider,
  83. final Provider<GlobalWindow> globalWindowProvider,
  84. final ActionSubstitutorFactory substitutorFactory,
  85. final String group,
  86. final String name) {
  87. this.globalCommandParserProvider = globalCommandParserProvider;
  88. this.globalWindowProvider = globalWindowProvider;
  89. this.substitutorFactory = substitutorFactory;
  90. this.group = group;
  91. this.name = name;
  92. }
  93. /**
  94. * Creates a new instance of ActionModel with the specified properties.
  95. *
  96. * @param globalCommandParserProvider Provider of global command parsers for triggering actions.
  97. * @param globalWindowProvider Provider of global windows for global actions.
  98. * @param substitutorFactory Factory to use to create action substitutors.
  99. * @param group The group the action belongs to
  100. * @param name The name of the action
  101. * @param triggers The triggers to use
  102. * @param response The response to use
  103. * @param conditions The conditions to use
  104. * @param conditionTree The condition tree to use
  105. * @param newFormat The new formatter to use
  106. */
  107. public ActionModel(
  108. final Provider<GlobalCommandParser> globalCommandParserProvider,
  109. final Provider<GlobalWindow> globalWindowProvider,
  110. final ActionSubstitutorFactory substitutorFactory,
  111. final String group, final String name,
  112. final ActionType[] triggers, final String[] response,
  113. final List<ActionCondition> conditions,
  114. final ConditionTree conditionTree, final String newFormat) {
  115. this(globalCommandParserProvider, globalWindowProvider, substitutorFactory, group, name);
  116. this.triggers = triggers.clone();
  117. this.response = response.clone();
  118. this.conditions = conditions;
  119. this.conditionTree = conditionTree;
  120. this.newFormat = newFormat;
  121. this.modified = true;
  122. }
  123. /**
  124. * Triggers this action.
  125. *
  126. * @param serverManager The server manager used to iterate servers
  127. * @param format The format of the message that's going to be displayed.
  128. * @param arguments The arguments from the action that caused this trigger.
  129. *
  130. * @return True if the execution of the event should be stopped, or false if the event may
  131. * continue
  132. */
  133. @Precondition({
  134. "This action has at least one trigger",
  135. "This action's primary trigger is non-null"
  136. })
  137. public boolean trigger(final ServerManager serverManager, final StringBuffer format,
  138. final Object... arguments) {
  139. assert triggers.length > 0;
  140. assert triggers[0] != null;
  141. if (status != ActionStatus.ACTIVE) {
  142. return false;
  143. }
  144. final ActionSubstitutor sub = substitutorFactory.getActionSubstitutor(triggers[0]);
  145. if (!test(sub, arguments)) {
  146. return false;
  147. }
  148. final FrameContainer container;
  149. if (arguments.length > 0 && arguments[0] instanceof FrameContainer
  150. && ((FrameContainer) arguments[0]).isWritable()) {
  151. container = (FrameContainer) arguments[0];
  152. } else {
  153. container = globalWindowProvider.get();
  154. }
  155. final CommandParser cp = container.getCommandParser();
  156. for (String command : response) {
  157. cp.parseCommand(container, sub.doSubstitution(command, arguments));
  158. }
  159. if (newFormat != null && format != null) {
  160. format.setLength(0);
  161. format.append(newFormat);
  162. }
  163. return stop;
  164. }
  165. /**
  166. * Tests to see if this action should be triggered or not.
  167. *
  168. * @param sub The ActionsSubstitutor to use to substitute args
  169. * @param arguments The arguments for the action event
  170. *
  171. * @return True if the action should be executed, false otherwise
  172. */
  173. public boolean test(final ActionSubstitutor sub,
  174. final Object... arguments) {
  175. final boolean[] results = new boolean[conditions.size()];
  176. int i = 0;
  177. for (ActionCondition condition : conditions) {
  178. results[i++] = condition.test(sub, arguments);
  179. }
  180. return getRealConditionTree().evaluate(results);
  181. }
  182. /**
  183. * Retrieves a list of this action's conditions.
  184. *
  185. * @return A list of this action's conditions
  186. */
  187. public List<ActionCondition> getConditions() {
  188. return conditions;
  189. }
  190. /**
  191. * Sets this action's conditions.
  192. *
  193. * @param conditions A list of conditions to use
  194. */
  195. public void setConditions(final List<ActionCondition> conditions) {
  196. this.conditions = conditions;
  197. this.modified = true;
  198. }
  199. /**
  200. * Retrieves the status of this action.
  201. *
  202. * @since 0.6.5
  203. * @return This action's current status
  204. */
  205. public ActionStatus getStatus() {
  206. return status;
  207. }
  208. /**
  209. * Retrieves a plain text description of any error that occurred while loading this action.
  210. *
  211. * @since 0.6.5
  212. * @return This action's error message, or null if no error was encountered
  213. */
  214. public String getError() {
  215. return error;
  216. }
  217. /**
  218. * Retrieves the type of the error that occurred while loading this action.
  219. *
  220. * @since 0.6.5
  221. * @return This action's error's type, or null if no error was encountered
  222. */
  223. public ActionErrorType getErrorType() {
  224. return errorType;
  225. }
  226. /**
  227. * Retrieves this action's triggers.
  228. *
  229. * @return The triggers used by this action
  230. */
  231. public ActionType[] getTriggers() {
  232. return triggers == null ? triggers : triggers.clone();
  233. }
  234. /**
  235. * Sets this action's triggers.
  236. *
  237. * @param triggers The new triggers to use
  238. */
  239. public void setTriggers(final ActionType[] triggers) {
  240. this.triggers = triggers.clone();
  241. this.modified = true;
  242. }
  243. /**
  244. * Retrieves this action's new format setting.
  245. *
  246. * @return The format that this action will use, or null if no change
  247. */
  248. public String getNewFormat() {
  249. return newFormat;
  250. }
  251. /**
  252. * Sets this action's new format setting.
  253. *
  254. * @param newFormat The new 'new format' setting
  255. */
  256. public void setNewFormat(final String newFormat) {
  257. this.newFormat = newFormat;
  258. this.modified = true;
  259. }
  260. /**
  261. * Retrieves this action's response.
  262. *
  263. * @return The commands that will be executed if this action is triggered
  264. */
  265. public String[] getResponse() {
  266. return response == null ? response : response.clone();
  267. }
  268. /**
  269. * Sets this action's response.
  270. *
  271. * @param response The new response to use
  272. */
  273. public void setResponse(final String[] response) {
  274. this.response = response.clone();
  275. this.modified = true;
  276. }
  277. /**
  278. * Retrieves this action's group name.
  279. *
  280. * @return This action's group name
  281. */
  282. public String getGroup() {
  283. return group;
  284. }
  285. /**
  286. * Sets the group of this action.
  287. *
  288. * @param newGroup The new group for this action
  289. */
  290. public void setGroup(final String newGroup) {
  291. this.group = newGroup;
  292. this.modified = true;
  293. }
  294. /**
  295. * Retrieves this action's name.
  296. *
  297. * @return This action's name
  298. */
  299. public String getName() {
  300. return name;
  301. }
  302. /**
  303. * Sets the name of this action.
  304. *
  305. * @param newName The new name for this action
  306. */
  307. public void setName(final String newName) {
  308. this.name = newName;
  309. this.modified = true;
  310. }
  311. /**
  312. * Retrieves the condition tree used for this action. Condition trees may be null, in which case
  313. * the arguments are conjoined together.
  314. *
  315. * @return This action's condition tree
  316. */
  317. public ConditionTree getConditionTree() {
  318. return conditionTree;
  319. }
  320. /**
  321. * Retrieves a concrete condition tree used for this action. If there is no condition tree
  322. * defined for this action, returns a conjunction tree for the arguments.
  323. *
  324. * @since 0.6
  325. * @return A {@link ConditionTree} object for this action
  326. */
  327. public ConditionTree getRealConditionTree() {
  328. return conditionTree == null ? ConditionTree.createConjunction(
  329. conditions.size()) : conditionTree;
  330. }
  331. /**
  332. * Sets the condition tree used for this action.
  333. *
  334. * @param conditionTree The new condition tree to be used
  335. */
  336. public void setConditionTree(final ConditionTree conditionTree) {
  337. this.conditionTree = conditionTree;
  338. this.modified = true;
  339. }
  340. /**
  341. * Retrieves the concurrency group of this action.
  342. *
  343. * @return This action's concurrency group
  344. *
  345. * @since 0.6.3
  346. */
  347. public String getConcurrencyGroup() {
  348. return concurrencyGroup;
  349. }
  350. /**
  351. * Sets the concurrency group of this action.
  352. *
  353. * @param concurrencyGroup This action's new concurrency group
  354. *
  355. * @since 0.6.3
  356. */
  357. public void setConcurrencyGroup(final String concurrencyGroup) {
  358. this.concurrencyGroup = (concurrencyGroup == null
  359. || concurrencyGroup.isEmpty()) ? null : concurrencyGroup;
  360. }
  361. /**
  362. * Determines whether or not this action will stop the event execution if it is triggered.
  363. *
  364. * @return The stopping preference of this action
  365. *
  366. * @see #setStopping(boolean)
  367. * @since 0.6.4
  368. */
  369. public boolean isStopping() {
  370. return stop;
  371. }
  372. /**
  373. * Sets the stopping preference of this action. If the stopping preference is {@code true} then
  374. * when this action is successfully triggered,it will request that execution of the event is
  375. * stopped. This will prevent the default behaviour of the callee being executed.
  376. *
  377. * @param stop The new stopping preference of this action
  378. *
  379. * @see #isStopping()
  380. * @since 0.6.4
  381. */
  382. public void setStopping(final boolean stop) {
  383. this.stop = stop;
  384. }
  385. /**
  386. * Determine if this model has been modified since it was constructed or its modified status was
  387. * reset.
  388. *
  389. * @return True if this model has been modified, false otherwise
  390. */
  391. public boolean isModified() {
  392. return modified;
  393. }
  394. /**
  395. * Resets the modified status of this model. After a call to resetModified(), this model will
  396. * report that it has not been modified, until one of the set* methods is used.
  397. */
  398. public void resetModified() {
  399. this.modified = false;
  400. }
  401. @Override
  402. public String toString() {
  403. return "[name=" + group + "/" + name + ", triggers="
  404. + Arrays.toString(triggers) + ", response="
  405. + Arrays.toString(response) + ", "
  406. + conditions + ", format='" + newFormat + "']";
  407. }
  408. }