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.2KB

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