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 12KB

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