Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

IRCChannelClientInfo.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.parser.irc;
  23. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  24. import com.dmdirc.parser.interfaces.ChannelInfo;
  25. import java.util.Collections;
  26. import java.util.Comparator;
  27. import java.util.HashMap;
  28. import java.util.Map;
  29. /**
  30. * Contains information about a client on a channel.
  31. *
  32. * @see IRCParser
  33. */
  34. public class IRCChannelClientInfo implements ChannelClientInfo {
  35. /** Reference to ClientInfo object this represents. */
  36. private final IRCClientInfo cClient;
  37. /** The channel modes associated with this user. */
  38. private String modes = "";
  39. /** Manager to use when dealing with prefix modes. */
  40. private final PrefixModeManager modeManager;
  41. /** The parser to use to kick people. */
  42. private final IRCParser parser;
  43. /** Reference to the channel object that owns this channel client. */
  44. private final ChannelInfo myChannel;
  45. /** A Map to allow applications to attach misc data to this object. */
  46. private Map<Object, Object> myMap;
  47. /**
  48. * Create a ChannelClient instance of a CLient.
  49. *
  50. * @param tParser Refernce to parser that owns this channelclient (used for modes)
  51. * @param prefixModeManager Manager to use to access prefix mode information.
  52. * @param client Client that this channelclient represents
  53. * @param channel Channel that owns this channelclient
  54. */
  55. public IRCChannelClientInfo(final IRCParser tParser, final PrefixModeManager prefixModeManager,
  56. final IRCClientInfo client, final ChannelInfo channel) {
  57. myMap = new HashMap<>();
  58. modeManager = prefixModeManager;
  59. parser = tParser;
  60. cClient = client;
  61. myChannel = channel;
  62. cClient.addChannelClientInfo(this);
  63. }
  64. /**
  65. * Set the Map object attatched to this object.
  66. *
  67. * @param newMap New Map to attatch.
  68. * @see #getMap
  69. */
  70. public void setMap(final Map<Object, Object> newMap) {
  71. myMap = newMap;
  72. }
  73. @Override
  74. public Map<Object, Object> getMap() {
  75. return myMap;
  76. }
  77. @Override
  78. public IRCClientInfo getClient() {
  79. return cClient;
  80. }
  81. @Override
  82. public ChannelInfo getChannel() {
  83. return myChannel;
  84. }
  85. /**
  86. * Get the nickname of the client object represented by this channelclient.
  87. *
  88. * @return Nickname of the Client object represented by this channelclient
  89. */
  90. public String getNickname() {
  91. return cClient.getNickname();
  92. }
  93. /**
  94. * Set the modes this client has (Prefix modes).
  95. *
  96. * @param modes The new modes this client has, sorted most-to-least important.
  97. */
  98. public void setChanMode(final String modes) {
  99. this.modes = modes;
  100. }
  101. @Override
  102. public String getAllModes() {
  103. return modes;
  104. }
  105. @Override
  106. public String getAllModesPrefix() {
  107. return modeManager.getPrefixesFor(modes);
  108. }
  109. @Override
  110. public String getImportantMode() {
  111. return modes.isEmpty() ? "" : modes.substring(0, 1);
  112. }
  113. @Override
  114. public String getImportantModePrefix() {
  115. return modes.isEmpty() ? "" : getAllModesPrefix().substring(0, 1);
  116. }
  117. /**
  118. * Get the String Value of ChannelClientInfo (e.g. @Nickname).
  119. *
  120. * @return String Value of user (inc prefix) (e.g. @Nickname)
  121. */
  122. @Override
  123. public String toString() {
  124. return getImportantModePrefix() + getNickname();
  125. }
  126. @Override
  127. public void kick(final String message) {
  128. parser.sendString("KICK " + myChannel + ' ' + getNickname(), message);
  129. }
  130. /**
  131. * Get the "Complete" String Value of ChannelClientInfo (ie @+Nickname).
  132. *
  133. * @return String Value of user (inc prefix) (ie @+Nickname)
  134. */
  135. public String toFullString() {
  136. return getAllModesPrefix() + getNickname();
  137. }
  138. @Override
  139. public int compareTo(final ChannelClientInfo arg0) {
  140. return modeManager.compareImportantModes(getAllModes(), arg0.getAllModes());
  141. }
  142. @Override
  143. public Comparator<String> getImportantModeComparator() {
  144. return Collections.reverseOrder(modeManager::compareImportantModes);
  145. }
  146. /**
  147. * Determines if this client is opped or not.
  148. *
  149. * @return True if the client is opped, false otherwise.
  150. */
  151. public boolean isOpped() {
  152. return modeManager.isOpped(getAllModes());
  153. }
  154. /**
  155. * Adds the specified mode to this client model.
  156. *
  157. * @param mode The mode to be added.
  158. */
  159. public void addMode(final char mode) {
  160. modes = modeManager.insertMode(modes, mode);
  161. }
  162. /**
  163. * Removes the specified mode from this client model.
  164. *
  165. * @param mode The mode to be removed.
  166. */
  167. public void removeMode(final char mode) {
  168. modes = modeManager.removeMode(modes, mode);
  169. }
  170. }