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.

ChannelClientInfo.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2006-2008 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. * SVN: $Id$
  23. */
  24. package com.dmdirc.parser;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. /**
  28. * Contains information about a client on a channel.
  29. *
  30. * @author Shane Mc Cormack
  31. * @author Chris Smith
  32. * @version $Id$
  33. * @see IRCParser
  34. */
  35. public final class ChannelClientInfo {
  36. /** Reference to ClientInfo object this represents. */
  37. private final ClientInfo cClient;
  38. /** Integer representation of the channel modes assocated with this user. */
  39. private long nModes;
  40. /** Reference to the parser object that owns this channelclient, Used for modes. */
  41. private final IRCParser myParser;
  42. /** Reference to the channel object that owns this channelclient. */
  43. private final ChannelInfo myChannel;
  44. /** A Map to allow applications to attach misc data to this object */
  45. private Map 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 client Client that this channelclient represents
  51. * @param channel Channel that owns this channelclient
  52. */
  53. public ChannelClientInfo(final IRCParser tParser, final ClientInfo client, final ChannelInfo channel) {
  54. myMap = new HashMap<Object, Object>();
  55. myParser = tParser;
  56. cClient = client;
  57. myChannel = channel;
  58. cClient.addChannelClientInfo(this);
  59. }
  60. /**
  61. * Set the Map object attatched to this object.
  62. *
  63. * @param newMap New Map to attatch.
  64. * @see #getMap
  65. */
  66. public void setMap(final Map newMap) {
  67. myMap = newMap;
  68. }
  69. /**
  70. * Get the Map object attatched to this object.
  71. *
  72. * @return Map to attatched to this.
  73. * @see #setMap
  74. */
  75. public Map getMap() {
  76. return myMap;
  77. }
  78. /**
  79. * Check if this ChannelClient is linked to a fake client.
  80. *
  81. * @return True if this ChannelClient is linked to a fake client, else false
  82. */
  83. public boolean isFake() { return cClient.isFake(); }
  84. /**
  85. * Get the client object represented by this channelclient.
  86. *
  87. * @return Client object represented by this channelclient
  88. */
  89. public ClientInfo getClient() { return cClient; }
  90. /**
  91. * Get the Channel object that owns this ChannelClient.
  92. *
  93. * @return Channel object that owns this ChannelClient
  94. */
  95. public ChannelInfo getChannel() { return myChannel; }
  96. /**
  97. * Get the nickname of the client object represented by this channelclient.
  98. *
  99. * @return Nickname of the Client object represented by this channelclient
  100. */
  101. public String getNickname() { return cClient.getNickname(); }
  102. /**
  103. * Set the modes this client has (Prefix modes).
  104. *
  105. * @param nNewMode integer representing the modes this client has.
  106. */
  107. public void setChanMode(final long nNewMode) { nModes = nNewMode; }
  108. /**
  109. * Get the modes this client has (Prefix modes).
  110. *
  111. * @return integer representing the modes this client has.
  112. */
  113. public long getChanMode() { return nModes; }
  114. /**
  115. * Get the modes this client has (Prefix modes) as a string.
  116. * Returns all known modes that the client has.
  117. * getChanModeStr(false).charAt(0) can be used to get the highest mode (o)
  118. * getChanModeStr(true).charAt(0) can be used to get the highest prefix (@)
  119. *
  120. * @param bPrefix if this is true, prefixes will be returned (@+) not modes (ov)
  121. * @return String representing the modes this client has.
  122. */
  123. public String getChanModeStr(final boolean bPrefix) {
  124. StringBuilder sModes = new StringBuilder();
  125. long nTemp = 0;
  126. final long nCurrentModes = this.getChanMode();
  127. for (long i = myParser.nNextKeyPrefix; i > 0; i = i / 2) {
  128. if ((nCurrentModes & i) == i) {
  129. for (char cTemp : myParser.hPrefixModes.keySet()) {
  130. nTemp = myParser.hPrefixModes.get(cTemp);
  131. if (nTemp == i) {
  132. if (bPrefix) { cTemp = myParser.hPrefixMap.get(cTemp); }
  133. sModes = sModes.append(cTemp);
  134. break;
  135. }
  136. }
  137. }
  138. }
  139. return sModes.toString();
  140. }
  141. /**
  142. * Get the value of the most important mode this client has (Prefix modes).
  143. * A higher value, is a more important mode, 0 = no modes.
  144. *
  145. * @return integer representing the value of the most important mode.
  146. */
  147. public long getImportantModeValue() {
  148. for (long i = myParser.nNextKeyPrefix; i > 0; i = i / 2) {
  149. if ((nModes & i) == i) { return i; }
  150. }
  151. return 0;
  152. }
  153. /**
  154. * Get the most important mode this client has (o, v etc).
  155. *
  156. * @return String representing the most important mode.
  157. */
  158. public String getImportantMode() {
  159. String sModes = this.getChanModeStr(false);
  160. if (!sModes.isEmpty()) { sModes = "" + sModes.charAt(0); }
  161. return sModes;
  162. }
  163. /**
  164. * Get the most important prefix this client has (@, + etc).
  165. *
  166. * @return String representing the most important mode.
  167. */
  168. public String getImportantModePrefix() {
  169. String sModes = this.getChanModeStr(true);
  170. if (!sModes.isEmpty()) { sModes = "" + sModes.charAt(0); }
  171. return sModes;
  172. }
  173. /**
  174. * Get the String Value of ChannelClientInfo (ie @Nickname).
  175. *
  176. * @return String Value of user (inc prefix) (ie @Nickname)
  177. */
  178. @Override
  179. public String toString() {
  180. return this.getImportantModePrefix() + this.getNickname();
  181. }
  182. /**
  183. * Attempt to kick this user from the channel.
  184. *
  185. * @param sReason Why are they being kicked? "" for no reason
  186. */
  187. public void kick(final String sReason) {
  188. myParser.sendString("KICK " + myChannel + " " + this.getNickname()
  189. + (sReason.isEmpty() ? sReason : " :" + sReason));
  190. }
  191. /**
  192. * Get the "Complete" String Value of ChannelClientInfo (ie @+Nickname).
  193. *
  194. * @return String Value of user (inc prefix) (ie @+Nickname)
  195. */
  196. public String toFullString() { return this.getChanModeStr(true) + this.getNickname(); }
  197. }