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

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