Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PrefixModeManager.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2006-2017 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.parser.irc;
  23. /**
  24. * Handles prefix modes (those that can be applied to a user in a channel, such as +ohv).
  25. */
  26. public class PrefixModeManager {
  27. /** All known modes, in increasing order of importance. */
  28. private final ModeManager modes = new ModeManager();
  29. /** All known prefixes, in increasing order of importance. */
  30. private String prefixes = "";
  31. /**
  32. * Resets the state of this manager, clearing all known modes.
  33. */
  34. public void clear() {
  35. setModes("", "");
  36. }
  37. /**
  38. * Replaces all existing modes with the specified ones.
  39. *
  40. * @param modes The new modes, in increasing order of importance.
  41. * @param prefixes The corresponding new prefixes, in increasing order of importance.
  42. */
  43. public void setModes(final String modes, final String prefixes) {
  44. this.modes.set(modes);
  45. this.prefixes = prefixes;
  46. }
  47. /**
  48. * Determines if the specified character is a prefix mode (e.g. 'o', 'v').
  49. *
  50. * @param mode The mode to be tested
  51. * @return True if the mode is a prefix mode, false otherwise.
  52. */
  53. public boolean isPrefixMode(final char mode) {
  54. return modes.isMode(mode);
  55. }
  56. /**
  57. * Determines if the specified character is a prefix for a mode (e.g. '@', '+').
  58. *
  59. * @param prefix The prefix to be tested
  60. * @return True if the character is a prefix, false otherwise.
  61. */
  62. public boolean isPrefix(final char prefix) {
  63. return prefixes.indexOf(prefix) > -1;
  64. }
  65. /**
  66. * Returns the prefix corresponding to the specified mode (e.g. '@' given 'o').
  67. *
  68. * @param mode The mode to retrieve the prefix for.
  69. * @return The prefix corresponding to the mode.
  70. */
  71. public char getPrefixFor(final char mode) {
  72. return prefixes.charAt(modes.getModes().indexOf(mode));
  73. }
  74. /**
  75. * Converts a string containing prefix modes into a string containing the corresponding
  76. * prefixes (e.g. 'ov' becomes '@+').
  77. *
  78. * @param modeString The modes to retrieve prefixes for.
  79. * @return The prefixes corresponding to the modes.
  80. */
  81. public String getPrefixesFor(final String modeString) {
  82. final StringBuilder builder = new StringBuilder(modeString.length());
  83. for (char mode : modeString.toCharArray()) {
  84. builder.append(getPrefixFor(mode));
  85. }
  86. return builder.toString();
  87. }
  88. /**
  89. * Returns the mode corresponding to the specified prefix (e.g. 'o' given '@').
  90. *
  91. * @param prefix The prefix to retrieve the mode for.
  92. * @return The mode corresponding to the prefix.
  93. */
  94. public char getModeFor(final char prefix) {
  95. return modes.getModes().charAt(prefixes.indexOf(prefix));
  96. }
  97. /**
  98. * Gets the set of all known prefix modes.
  99. *
  100. * @return Set of known modes, in increasing order of importance.
  101. */
  102. public String getModes() {
  103. return modes.getModes();
  104. }
  105. /**
  106. * Gets the set of all known prefixes.
  107. *
  108. * @return Set of known prefixes, in increasing order of importance.
  109. */
  110. public String getPrefixes() {
  111. return prefixes;
  112. }
  113. /**
  114. * Adds a new mode. Modes must be added in increasing order of importance.
  115. *
  116. * @param mode The mode that appears in mode strings (e.g. 'o').
  117. * @param prefix The prefix that is used to show a user has the mode (e.g. '@')
  118. */
  119. public void add(final char mode, final char prefix) {
  120. modes.add(mode);
  121. prefixes += prefix;
  122. }
  123. /**
  124. * Compares the most important mode of the given mode lists.
  125. *
  126. * @param modes1 The first set of modes to compare. Must be ordered by importance.
  127. * @param modes2 The second set of modes to compare. Must be ordered by importance.
  128. * @return A negative number of modes2 is more important than modes1; a positive number if
  129. * modes1 is more important than modes2; zero if the two are equivalent.
  130. */
  131. public int compareImportantModes(final String modes1, final String modes2) {
  132. return modes.compareImportantModes(modes1, modes2);
  133. }
  134. /**
  135. * Determines if the specified mode string indicates a user is opped. An opped user is
  136. * considered one who has any mode greater than 'v' (voice), or if voice doesn't exist then
  137. * any mode at all.
  138. *
  139. * @param modeString The modes to test
  140. * @return True if the modes indicate the client is "opped", false otherwise.
  141. */
  142. public boolean isOpped(final String modeString) {
  143. return !modeString.isEmpty()
  144. && modes.getModes().indexOf(modeString.charAt(0)) > modes.getModes().indexOf('v');
  145. }
  146. /**
  147. * Inserts the specified mode into the correct place in the mode string, maintaining importance
  148. * order.
  149. *
  150. * @param modeString The existing modes to add the new one to.
  151. * @param mode The new mode to be added.
  152. * @return A mode string containing all the modes.
  153. */
  154. public String insertMode(final String modeString, final char mode) {
  155. return modes.insertMode(modeString, mode);
  156. }
  157. /**
  158. * Removes the specified mode from the mode string.
  159. *
  160. * @param modeString The mode string to modify.
  161. * @param mode The mode to be removed.
  162. * @return A copy of the mode string with the mode removed.
  163. */
  164. public String removeMode(final String modeString, final char mode) {
  165. return modes.removeMode(modeString, mode);
  166. }
  167. }