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 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.ChannelClientInfo;
  24. import com.dmdirc.parser.interfaces.ChannelInfo;
  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. * @see IRCParser
  33. */
  34. public class IRCChannelClientInfo implements ChannelClientInfo {
  35. /** Reference to ClientInfo object this represents. */
  36. private final IRCClientInfo cClient;
  37. /** Integer representation of the channel modes assocated with this user. */
  38. private long nModes;
  39. /** Reference to the parser object that owns this channelclient, Used for modes. */
  40. private final IRCParser myParser;
  41. /** Reference to the channel object that owns this channelclient. */
  42. private final ChannelInfo myChannel;
  43. /** A Map to allow applications to attach misc data to this object */
  44. private Map<Object, Object> myMap;
  45. /**
  46. * Create a ChannelClient instance of a CLient.
  47. *
  48. * @param tParser Refernce to parser that owns this channelclient (used for modes)
  49. * @param client Client that this channelclient represents
  50. * @param channel Channel that owns this channelclient
  51. */
  52. public IRCChannelClientInfo(final IRCParser tParser, final IRCClientInfo client, final ChannelInfo channel) {
  53. myMap = new HashMap<Object, Object>();
  54. myParser = tParser;
  55. cClient = client;
  56. myChannel = channel;
  57. cClient.addChannelClientInfo(this);
  58. }
  59. /**
  60. * Set the Map object attatched to this object.
  61. *
  62. * @param newMap New Map to attatch.
  63. * @see #getMap
  64. */
  65. public void setMap(final Map<Object, Object> newMap) {
  66. myMap = newMap;
  67. }
  68. /** {@inheritDoc} */
  69. @Override
  70. public Map<Object, Object> getMap() { return myMap; }
  71. /** {@inheritDoc} */
  72. @Override
  73. public IRCClientInfo getClient() { return cClient; }
  74. /** {@inheritDoc} */
  75. @Override
  76. public ChannelInfo getChannel() { return myChannel; }
  77. /**
  78. * Get the nickname of the client object represented by this channelclient.
  79. *
  80. * @return Nickname of the Client object represented by this channelclient
  81. */
  82. public String getNickname() { return cClient.getNickname(); }
  83. /**
  84. * Set the modes this client has (Prefix modes).
  85. *
  86. * @param nNewMode integer representing the modes this client has.
  87. */
  88. public void setChanMode(final long nNewMode) { nModes = nNewMode; }
  89. /**
  90. * Get the modes this client has (Prefix modes).
  91. *
  92. * @return integer representing the modes this client has.
  93. */
  94. public long getChanMode() { return nModes; }
  95. /**
  96. * Get the modes this client has (Prefix modes) as a string.
  97. * Returns all known modes that the client has.
  98. * getChanModeStr(false).charAt(0) can be used to get the highest mode (o)
  99. * getChanModeStr(true).charAt(0) can be used to get the highest prefix (@)
  100. *
  101. * @param bPrefix if this is true, prefixes will be returned (@+) not modes (ov)
  102. * @return String representing the modes this client has.
  103. */
  104. public String getChanModeStr(final boolean bPrefix) {
  105. StringBuilder sModes = new StringBuilder();
  106. long nTemp = 0;
  107. final long nCurrentModes = this.getChanMode();
  108. for (long i = myParser.nextKeyPrefix; i > 0; i = i / 2) {
  109. if ((nCurrentModes & i) == i) {
  110. for (char cTemp : myParser.prefixModes.keySet()) {
  111. nTemp = myParser.prefixModes.get(cTemp);
  112. if (nTemp == i) {
  113. if (bPrefix) { cTemp = myParser.prefixMap.get(cTemp); }
  114. sModes = sModes.append(cTemp);
  115. break;
  116. }
  117. }
  118. }
  119. }
  120. return sModes.toString();
  121. }
  122. /** {@inheritDoc} */
  123. @Override
  124. public String getAllModes() {
  125. return getChanModeStr(false);
  126. }
  127. /**
  128. * Get the value of the most important mode this client has (Prefix modes).
  129. * A higher value, is a more important mode, 0 = no modes.
  130. *
  131. * @return integer representing the value of the most important mode.
  132. */
  133. public long getImportantModeValue() {
  134. for (long i = myParser.nextKeyPrefix; i > 0; i = i / 2) {
  135. if ((nModes & i) == i) { return i; }
  136. }
  137. return 0;
  138. }
  139. /** {@inheritDoc} */
  140. @Override
  141. public String getImportantMode() {
  142. String sModes = this.getChanModeStr(false);
  143. if (!sModes.isEmpty()) { sModes = "" + sModes.charAt(0); }
  144. return sModes;
  145. }
  146. /** {@inheritDoc} */
  147. @Override
  148. public String getImportantModePrefix() {
  149. String sModes = this.getChanModeStr(true);
  150. if (!sModes.isEmpty()) { sModes = "" + sModes.charAt(0); }
  151. return sModes;
  152. }
  153. /**
  154. * Get the String Value of ChannelClientInfo (ie @Nickname).
  155. *
  156. * @return String Value of user (inc prefix) (ie @Nickname)
  157. */
  158. @Override
  159. public String toString() {
  160. return this.getImportantModePrefix() + this.getNickname();
  161. }
  162. /** {@inheritDoc} */
  163. @Override
  164. public void kick(final String sReason) {
  165. myParser.sendString("KICK " + myChannel + " " + this.getNickname() + (sReason.isEmpty() ? sReason : " :" + sReason));
  166. }
  167. /**
  168. * Get the "Complete" String Value of ChannelClientInfo (ie @+Nickname).
  169. *
  170. * @return String Value of user (inc prefix) (ie @+Nickname)
  171. */
  172. public String toFullString() { return this.getChanModeStr(true) + this.getNickname(); }
  173. /** {@inheritDoc} */
  174. @Override
  175. public int compareTo(final ChannelClientInfo arg0) {
  176. if (arg0 instanceof IRCChannelClientInfo) {
  177. final IRCChannelClientInfo other = (IRCChannelClientInfo) arg0;
  178. return (int) (getImportantModeValue() - other.getImportantModeValue());
  179. }
  180. return 0;
  181. }
  182. }