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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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.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. If the specified perform is
  94. * empty - that is, any non-null elements are empty Strings - then the perform is removed. If a
  95. * profile is specified, the perform will only be executed for that profile.
  96. *
  97. * @param perform The perform to be set
  98. * @param content The new content of that perform
  99. *
  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, a zero-length array
  131. * is returned.
  132. *
  133. * @param perform The perform to be retrieved
  134. *
  135. * @return The corresponding perform, or a zero-length array if none set
  136. *
  137. * @since 0.6.4
  138. */
  139. public String[] getPerform(final PerformDescription perform) {
  140. final Action action = getAction(perform.getType() == PerformType.NETWORK
  141. ? CoreActionComponent.SERVER_NETWORK : CoreActionComponent.SERVER_NAME,
  142. perform.getTarget(), perform.getProfile());
  143. if (action == null || action.getResponse() == null) {
  144. return new String[0];
  145. } else {
  146. return action.getResponse();
  147. }
  148. }
  149. /**
  150. * Determines if the specified perform is "empty". An empty perform is one that does not contain
  151. * any non-empty Strings.
  152. *
  153. * @param perform The perform to test
  154. *
  155. * @return True if the perform is empty, false otherwise
  156. *
  157. * @since 0.6.4
  158. */
  159. private static boolean isEmpty(final String[] perform) {
  160. for (String part : perform) {
  161. if (part != null && !part.isEmpty()) {
  162. return false;
  163. }
  164. }
  165. return true;
  166. }
  167. /**
  168. * Checks that the specified conditions are valid for a perform action.
  169. *
  170. * @param conditions The conditions to be checked
  171. *
  172. * @since 0.6.3m2
  173. * @return True if the conditions are valid, false otherwise
  174. */
  175. protected boolean checkConditions(final List<ActionCondition> conditions) {
  176. boolean target = false;
  177. boolean profile = false;
  178. for (ActionCondition condition : conditions) {
  179. if ((condition.getComponent() == CoreActionComponent.SERVER_NETWORK
  180. || condition.getComponent() == CoreActionComponent.SERVER_NAME)
  181. && !target) {
  182. target = true;
  183. } else if (condition.getComponent() instanceof ActionComponentChain
  184. && PP_COMP_NAME.equals(condition.getComponent().toString())
  185. && !profile) {
  186. profile = true;
  187. } else {
  188. return false;
  189. }
  190. }
  191. return target || profile;
  192. }
  193. /**
  194. * Creates a new, empty, perform wrapper for the specified server or network. Note that both
  195. * server and network must be specified, and exactly one of them must be empty.
  196. *
  197. * @param server The server to create the action for
  198. * @param network The network to create the action for
  199. * @param profile The profile the action is for (or null if "global")
  200. *
  201. * @since 0.6.3
  202. * @return The new perform wrapper action
  203. */
  204. private Action createAction(final String server, final String network,
  205. final String profile) {
  206. final List<ActionCondition> conditions = new ArrayList<>();
  207. final CoreActionComponent component =
  208. server.isEmpty() ? CoreActionComponent.SERVER_NETWORK
  209. : CoreActionComponent.SERVER_NAME;
  210. conditions.add(new ActionCondition(0, component,
  211. CoreActionComparison.STRING_EQUALS, server + network));
  212. if (profile != null) {
  213. conditions.add(new ActionCondition(0,
  214. new ActionComponentChain(Connection.class, PP_COMP_NAME, ActionManager.
  215. getActionManager()),
  216. CoreActionComparison.STRING_EQUALS, profile));
  217. }
  218. return actionFactory.getAction(getName(), server + network
  219. + (profile == null ? "" : " - " + profile),
  220. new ActionType[]{CoreActionType.SERVER_CONNECTED},
  221. new String[0], conditions,
  222. ConditionTree.createConjunction(conditions.size()), null);
  223. }
  224. /**
  225. * Retrieve an action with a condition that checks the specified component, and matches it
  226. * against the specified target.
  227. *
  228. * @param component The action component to look for
  229. * @param target The string the component is matched against
  230. * @param profile The name of the profile that the action must target, or null for a
  231. * non-profile specific action
  232. *
  233. * @since 0.6.3
  234. * @return The matching action if one exists, or null
  235. */
  236. private Action getAction(final ActionComponent component, final String target,
  237. final String profile) {
  238. for (Action action : this) {
  239. int matches = profile == null ? 1 : 2;
  240. for (ActionCondition condition : action.getConditions()) {
  241. if (condition.getComponent() == component
  242. && condition.getTarget().equalsIgnoreCase(target)) {
  243. matches--;
  244. } else if (profile != null
  245. && PP_COMP_NAME.equals(condition.getComponent().toString())
  246. && condition.getTarget().equalsIgnoreCase(profile)) {
  247. matches--;
  248. }
  249. }
  250. if (matches == 0) {
  251. return action;
  252. }
  253. }
  254. return null;
  255. }
  256. /** {@inheritDoc} */
  257. @Override
  258. public boolean isDelible() {
  259. return false;
  260. }
  261. /** {@inheritDoc} */
  262. @Override
  263. public String getDescription() {
  264. return "Performs allow you to automatically execute commands when"
  265. + " you connect to a specific server or network. You can edit"
  266. + " the perform for the current server or network in the "
  267. + "\"Server Settings\" dialog, which can be accessed through "
  268. + "the Settings menu.";
  269. }
  270. /**
  271. * Describes one specific perform.
  272. *
  273. * @since 0.6.4
  274. */
  275. public static class PerformDescription {
  276. /** The type of the perform being described. */
  277. private final PerformType type;
  278. /** The target of the perform. */
  279. private final String target;
  280. /** The profile (if any) of the perform. */
  281. private final String profile;
  282. /**
  283. * Creates a new perform description with the specified arguments.
  284. *
  285. * @param type The type of the perform in question
  286. * @param target The target of the perform
  287. * @param profile The profile of the perform (or null)
  288. */
  289. public PerformDescription(final PerformType type, final String target,
  290. final String profile) {
  291. this.type = type;
  292. this.target = target;
  293. this.profile = profile;
  294. if (target == null) {
  295. throw new NullPointerException("Target may not be null");
  296. }
  297. }
  298. /**
  299. * Creates a new perform description with the specified arguments.
  300. *
  301. * @param type The type of the perform in question
  302. * @param target The target of the perform
  303. */
  304. public PerformDescription(final PerformType type, final String target) {
  305. this.type = type;
  306. this.target = target;
  307. this.profile = null;
  308. if (target == null) {
  309. throw new NullPointerException("Target may not be null");
  310. }
  311. }
  312. /**
  313. * Retrieves the profile of this perform.
  314. *
  315. * @return This perform's profile
  316. */
  317. public String getProfile() {
  318. return profile;
  319. }
  320. /**
  321. * Retrieves the target of this perform.
  322. *
  323. * @return This perform's target
  324. */
  325. public String getTarget() {
  326. return target;
  327. }
  328. /**
  329. * Retrieves the type of this perform.
  330. *
  331. * @return This perform's type
  332. */
  333. public PerformType getType() {
  334. return type;
  335. }
  336. /** {@inheritDoc} */
  337. @Override
  338. public boolean equals(final Object obj) {
  339. if (obj == null || getClass() != obj.getClass()) {
  340. return false;
  341. }
  342. final PerformDescription other = (PerformDescription) obj;
  343. if (this.type != other.type || !this.target.equals(other.target)) {
  344. return false;
  345. }
  346. if ((this.profile == null) ? (other.profile != null)
  347. : !this.profile.equals(other.profile)) {
  348. return false;
  349. }
  350. return true;
  351. }
  352. /** {@inheritDoc} */
  353. @Override
  354. public int hashCode() {
  355. int hash = 7;
  356. hash = 89 * hash + (this.type != null ? this.type.hashCode() : 0);
  357. hash = 89 * hash + (this.target != null ? this.target.hashCode() : 0);
  358. hash = 89 * hash + (this.profile != null ? this.profile.hashCode() : 0);
  359. return hash;
  360. }
  361. }
  362. }