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.

IRCChannelClientInfo.java 5.8KB

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