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.

PerformWrapper.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Copyright (c) 2006-2013 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.wrappers;
  23. import com.dmdirc.actions.Action;
  24. import com.dmdirc.actions.ActionComponentChain;
  25. import com.dmdirc.actions.ActionCondition;
  26. import com.dmdirc.actions.ActionFactory;
  27. import com.dmdirc.actions.ActionGroup;
  28. import com.dmdirc.actions.ActionManager;
  29. import com.dmdirc.actions.ConditionTree;
  30. import com.dmdirc.actions.CoreActionComparison;
  31. import com.dmdirc.actions.CoreActionComponent;
  32. import com.dmdirc.actions.CoreActionType;
  33. import com.dmdirc.interfaces.Connection;
  34. import com.dmdirc.interfaces.actions.ActionComponent;
  35. import com.dmdirc.interfaces.actions.ActionType;
  36. import com.dmdirc.logger.ErrorLevel;
  37. import com.dmdirc.logger.Logger;
  38. import java.util.ArrayList;
  39. import java.util.List;
  40. import javax.inject.Inject;
  41. import javax.inject.Singleton;
  42. /**
  43. * An action wrapper for performs.
  44. */
  45. @Singleton
  46. public class PerformWrapper extends ActionGroup {
  47. /** The component name for per-profile perform conditions. */
  48. private static final String PP_COMP_NAME = "SERVER_PROFILE.IDENTITY_NAME";
  49. /** Factory to use for actions. */
  50. private final ActionFactory actionFactory;
  51. /**
  52. * Creates a new instance of PerformWrapper.
  53. *
  54. * @param actionFactory Factory to use to create actions.
  55. */
  56. @Inject
  57. public PerformWrapper(final ActionFactory actionFactory) {
  58. super("performs");
  59. this.actionFactory = actionFactory;
  60. }
  61. /** {@inheritDoc} */
  62. @Override
  63. public void add(final Action action) {
  64. if (action.getTriggers().length != 1) {
  65. Logger.userError(ErrorLevel.MEDIUM,
  66. "Invalid perform action: " + action.getName(),
  67. "Perform actions may only have one trigger");
  68. } else if (action.getTriggers()[0] != CoreActionType.SERVER_CONNECTED) {
  69. Logger.userError(ErrorLevel.MEDIUM,
  70. "Invalid perform action: " + action.getName(),
  71. "Perform actions must be triggered when a server connects");
  72. } else if (!checkConditions(action.getConditions())) {
  73. Logger.userError(ErrorLevel.MEDIUM,
  74. "Invalid perform action: " + action.getName(),
  75. "Perform actions must have exactly one or two conditions, "
  76. + "one may target the server's name or network, and one may "
  77. + "target the server's profile name. No other targets are "
  78. + "allowed.");
  79. } else {
  80. synchronized (this) {
  81. super.add(action);
  82. }
  83. }
  84. }
  85. /** {@inheritDoc} */
  86. @Override
  87. public void deleteAction(final Action action) {
  88. synchronized (this) {
  89. super.deleteAction(action);
  90. }
  91. }
  92. /**
  93. * Sets the perform for the specified target of the specified type.
  94. * If the specified perform is empty - that is, any non-null elements are
  95. * empty Strings - then the perform is removed. If a profile is specified,
  96. * the perform will only be executed for that profile.
  97. *
  98. * @param perform The perform to be set
  99. * @param content The new content of that perform
  100. * @since 0.6.4
  101. */
  102. public void setPerform(final PerformDescription perform, final String[] content) {
  103. synchronized (this) {
  104. Action action = getAction(perform.getType() == PerformType.NETWORK
  105. ? CoreActionComponent.SERVER_NETWORK : CoreActionComponent.SERVER_NAME,
  106. perform.getTarget(), perform.getProfile());
  107. final boolean empty = isEmpty(content);
  108. if (action == null && !empty) {
  109. // They want to set a perform but we don't have an action
  110. action = createAction(
  111. perform.getType() == PerformType.SERVER ? perform.getTarget() : "",
  112. perform.getType() == PerformType.NETWORK ? perform.getTarget() : "",
  113. perform.getProfile());
  114. action.setResponse(content);
  115. action.save();
  116. }
  117. if (action != null) {
  118. if (empty) {
  119. // They want to clear the perform but we have an action
  120. deleteAction(action);
  121. } else {
  122. // They want to set a perform and we have an action
  123. action.setResponse(content);
  124. action.save();
  125. }
  126. }
  127. }
  128. }
  129. /**
  130. * Retrieves the perform for the relevant target. If no such perform exists,
  131. * a zero-length array is returned.
  132. *
  133. * @param perform The perform to be retrieved
  134. * @return The corresponding perform, or a zero-length array if none set
  135. * @since 0.6.4
  136. */
  137. public String[] getPerform(final PerformDescription perform) {
  138. final Action action = getAction(perform.getType() == PerformType.NETWORK
  139. ? CoreActionComponent.SERVER_NETWORK : CoreActionComponent.SERVER_NAME,
  140. perform.getTarget(), perform.getProfile());
  141. if (action == null || action.getResponse() == null) {
  142. return new String[0];
  143. } else {
  144. return action.getResponse();
  145. }
  146. }
  147. /**
  148. * Determines if the specified perform is "empty". An empty perform is one
  149. * that does not contain any non-empty Strings.
  150. *
  151. * @param perform The perform to test
  152. * @return True if the perform is empty, false otherwise
  153. * @since 0.6.4
  154. */
  155. private static boolean isEmpty(final String[] perform) {
  156. for (String part : perform) {
  157. if (part != null && !part.isEmpty()) {
  158. return false;
  159. }
  160. }
  161. return true;
  162. }
  163. /**
  164. * Checks that the specified conditions are valid for a perform action.
  165. *
  166. * @param conditions The conditions to be checked
  167. * @since 0.6.3m2
  168. * @return True if the conditions are valid, false otherwise
  169. */
  170. protected boolean checkConditions(final List<ActionCondition> conditions) {
  171. boolean target = false, profile = false;
  172. for (ActionCondition condition : conditions) {
  173. if ((condition.getComponent() == CoreActionComponent.SERVER_NETWORK
  174. || condition.getComponent() == CoreActionComponent.SERVER_NAME)
  175. && !target) {
  176. target = true;
  177. } else if (condition.getComponent() instanceof ActionComponentChain
  178. && PP_COMP_NAME.equals(condition.getComponent().toString())
  179. && !profile) {
  180. profile = true;
  181. } else {
  182. return false;
  183. }
  184. }
  185. return target || profile;
  186. }
  187. /**
  188. * Creates a new, empty, perform wrapper for the specified server or
  189. * network. Note that both server and network must be specified, and
  190. * exactly one of them must be empty.
  191. *
  192. * @param server The server to create the action for
  193. * @param network The network to create the action for
  194. * @param profile The profile the action is for (or null if "global")
  195. * @since 0.6.3
  196. * @return The new perform wrapper action
  197. */
  198. private Action createAction(final String server, final String network,
  199. final String profile) {
  200. final List<ActionCondition> conditions = new ArrayList<>();
  201. final CoreActionComponent component =
  202. server.isEmpty() ? CoreActionComponent.SERVER_NETWORK
  203. : CoreActionComponent.SERVER_NAME;
  204. conditions.add(new ActionCondition(0, component,
  205. CoreActionComparison.STRING_EQUALS, server + network));
  206. if (profile != null) {
  207. conditions.add(new ActionCondition(0,
  208. new ActionComponentChain(Connection.class, PP_COMP_NAME, ActionManager.getActionManager()),
  209. CoreActionComparison.STRING_EQUALS, profile));
  210. }
  211. return actionFactory.getAction(getName(), server + network
  212. + (profile == null ? "" : " - " + profile),
  213. new ActionType[]{CoreActionType.SERVER_CONNECTED},
  214. new String[0], conditions,
  215. ConditionTree.createConjunction(conditions.size()), null);
  216. }
  217. /**
  218. * Retrieve an action with a condition that checks the specified component,
  219. * and matches it against the specified target.
  220. *
  221. * @param component The action component to look for
  222. * @param target The string the component is matched against
  223. * @param profile The name of the profile that the action must target, or
  224. * null for a non-profile specific action
  225. * @since 0.6.3
  226. * @return The matching action if one exists, or null
  227. */
  228. private Action getAction(final ActionComponent component, final String target,
  229. final String profile) {
  230. for (Action action : this) {
  231. int matches = profile == null ? 1 : 2;
  232. for (ActionCondition condition : action.getConditions()) {
  233. if (condition.getComponent() == component
  234. && condition.getTarget().equalsIgnoreCase(target)) {
  235. matches--;
  236. } else if (profile != null
  237. && PP_COMP_NAME.equals(condition.getComponent().toString())
  238. && condition.getTarget().equalsIgnoreCase(profile)) {
  239. matches--;
  240. }
  241. }
  242. if (matches == 0) {
  243. return action;
  244. }
  245. }
  246. return null;
  247. }
  248. /** {@inheritDoc} */
  249. @Override
  250. public boolean isDelible() {
  251. return false;
  252. }
  253. /** {@inheritDoc} */
  254. @Override
  255. public String getDescription() {
  256. return "Performs allow you to automatically execute commands when"
  257. + " you connect to a specific server or network. You can edit"
  258. + " the perform for the current server or network in the "
  259. + "\"Server Settings\" dialog, which can be accessed through "
  260. + "the Settings menu.";
  261. }
  262. /**
  263. * Describes one specific perform.
  264. *
  265. * @since 0.6.4
  266. */
  267. public static class PerformDescription {
  268. /** The type of the perform being described. */
  269. private final PerformType type;
  270. /** The target of the perform. */
  271. private final String target;
  272. /** The profile (if any) of the perform. */
  273. private final String profile;
  274. /**
  275. * Creates a new perform description with the specified arguments.
  276. *
  277. * @param type The type of the perform in question
  278. * @param target The target of the perform
  279. * @param profile The profile of the perform (or null)
  280. */
  281. public PerformDescription(final PerformType type, final String target,
  282. final String profile) {
  283. this.type = type;
  284. this.target = target;
  285. this.profile = profile;
  286. if (target == null) {
  287. throw new NullPointerException("Target may not be null");
  288. }
  289. }
  290. /**
  291. * Creates a new perform description with the specified arguments.
  292. *
  293. * @param type The type of the perform in question
  294. * @param target The target of the perform
  295. */
  296. public PerformDescription(final PerformType type, final String target) {
  297. this.type = type;
  298. this.target = target;
  299. this.profile = null;
  300. if (target == null) {
  301. throw new NullPointerException("Target may not be null");
  302. }
  303. }
  304. /**
  305. * Retrieves the profile of this perform.
  306. *
  307. * @return This perform's profile
  308. */
  309. public String getProfile() {
  310. return profile;
  311. }
  312. /**
  313. * Retrieves the target of this perform.
  314. *
  315. * @return This perform's target
  316. */
  317. public String getTarget() {
  318. return target;
  319. }
  320. /**
  321. * Retrieves the type of this perform.
  322. *
  323. * @return This perform's type
  324. */
  325. public PerformType getType() {
  326. return type;
  327. }
  328. /** {@inheritDoc} */
  329. @Override
  330. public boolean equals(final Object obj) {
  331. if (obj == null || getClass() != obj.getClass()) {
  332. return false;
  333. }
  334. final PerformDescription other = (PerformDescription) obj;
  335. if (this.type != other.type || !this.target.equals(other.target)) {
  336. return false;
  337. }
  338. if ((this.profile == null) ? (other.profile != null)
  339. : !this.profile.equals(other.profile)) {
  340. return false;
  341. }
  342. return true;
  343. }
  344. /** {@inheritDoc} */
  345. @Override
  346. public int hashCode() {
  347. int hash = 7;
  348. hash = 89 * hash + (this.type != null ? this.type.hashCode() : 0);
  349. hash = 89 * hash + (this.target != null ? this.target.hashCode() : 0);
  350. hash = 89 * hash + (this.profile != null ? this.profile.hashCode() : 0);
  351. return hash;
  352. }
  353. }
  354. }