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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2006-2009 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.parser.irc;
  23. import com.dmdirc.parser.interfaces.ChannelInfo;
  24. import com.dmdirc.parser.interfaces.ClientInfo;
  25. import com.dmdirc.parser.interfaces.Parser;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. /**
  29. * Contains information about a client on a channel.
  30. *
  31. * @author Shane Mc Cormack
  32. * @author Chris Smith
  33. * @see IRCParser
  34. */
  35. public 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 Parser 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 Parser 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. * Get the client object represented by this channelclient.
  80. *
  81. * @return Client object represented by this channelclient
  82. */
  83. public ClientInfo getClient() { return cClient; }
  84. /**
  85. * Get the Channel object that owns this ChannelClient.
  86. *
  87. * @return Channel object that owns this ChannelClient
  88. */
  89. public ChannelInfo getChannel() { return myChannel; }
  90. /**
  91. * Get the nickname of the client object represented by this channelclient.
  92. *
  93. * @return Nickname of the Client object represented by this channelclient
  94. */
  95. public String getNickname() { return cClient.getNickname(); }
  96. /**
  97. * Set the modes this client has (Prefix modes).
  98. *
  99. * @param nNewMode integer representing the modes this client has.
  100. */
  101. public void setChanMode(final long nNewMode) { nModes = nNewMode; }
  102. /**
  103. * Get the modes this client has (Prefix modes).
  104. *
  105. * @return integer representing the modes this client has.
  106. */
  107. public long getChanMode() { return nModes; }
  108. /**
  109. * Get the modes this client has (Prefix modes) as a string.
  110. * Returns all known modes that the client has.
  111. * getChanModeStr(false).charAt(0) can be used to get the highest mode (o)
  112. * getChanModeStr(true).charAt(0) can be used to get the highest prefix (@)
  113. *
  114. * @param bPrefix if this is true, prefixes will be returned (@+) not modes (ov)
  115. * @return String representing the modes this client has.
  116. */
  117. public String getChanModeStr(final boolean bPrefix) {
  118. StringBuilder sModes = new StringBuilder();
  119. long nTemp = 0;
  120. final long nCurrentModes = this.getChanMode();
  121. for (long i = myParser.nNextKeyPrefix; i > 0; i = i / 2) {
  122. if ((nCurrentModes & i) == i) {
  123. for (char cTemp : myParser.hPrefixModes.keySet()) {
  124. nTemp = myParser.hPrefixModes.get(cTemp);
  125. if (nTemp == i) {
  126. if (bPrefix) { cTemp = myParser.hPrefixMap.get(cTemp); }
  127. sModes = sModes.append(cTemp);
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. return sModes.toString();
  134. }
  135. /**
  136. * Get the value of the most important mode this client has (Prefix modes).
  137. * A higher value, is a more important mode, 0 = no modes.
  138. *
  139. * @return integer representing the value of the most important mode.
  140. */
  141. public long getImportantModeValue() {
  142. for (long i = myParser.nNextKeyPrefix; i > 0; i = i / 2) {
  143. if ((nModes & i) == i) { return i; }
  144. }
  145. return 0;
  146. }
  147. /**
  148. * Get the most important mode this client has (o, v etc), or an empty
  149. * string if the client has no modes.
  150. *
  151. * @return String representing the most important mode.
  152. */
  153. public String getImportantMode() {
  154. String sModes = this.getChanModeStr(false);
  155. if (!sModes.isEmpty()) { sModes = "" + sModes.charAt(0); }
  156. return sModes;
  157. }
  158. /**
  159. * Get the most important prefix this client has (@, + etc), or an empty
  160. * string if the client has no modes.
  161. *
  162. * @return String representing the most important mode.
  163. */
  164. public String getImportantModePrefix() {
  165. String sModes = this.getChanModeStr(true);
  166. if (!sModes.isEmpty()) { sModes = "" + sModes.charAt(0); }
  167. return sModes;
  168. }
  169. /**
  170. * Get the String Value of ChannelClientInfo (ie @Nickname).
  171. *
  172. * @return String Value of user (inc prefix) (ie @Nickname)
  173. */
  174. @Override
  175. public String toString() {
  176. return this.getImportantModePrefix() + this.getNickname();
  177. }
  178. /**
  179. * Attempt to kick this user from the channel.
  180. *
  181. * @param sReason Why are they being kicked? "" for no reason
  182. */
  183. public void kick(final String sReason) {
  184. myParser.sendString("KICK " + myChannel + " " + this.getNickname() + (sReason.isEmpty() ? sReason : " :" + sReason));
  185. }
  186. /**
  187. * Get the "Complete" String Value of ChannelClientInfo (ie @+Nickname).
  188. *
  189. * @return String Value of user (inc prefix) (ie @+Nickname)
  190. */
  191. public String toFullString() { return this.getChanModeStr(true) + this.getNickname(); }
  192. }