Java IRC bot
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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 java.util.HashMap;
  24. import java.util.Map;
  25. /**
  26. * Contains information about a client on a channel.
  27. *
  28. * @author Shane Mc Cormack
  29. * @author Chris Smith
  30. * @see IRCParser
  31. */
  32. public class ChannelClientInfo {
  33. /** Reference to ClientInfo object this represents. */
  34. private final ClientInfo 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 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 ChannelClientInfo(final IRCParser tParser, final ClientInfo client, final ChannelInfo channel) {
  51. myMap = new HashMap<Object, Object>();
  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 newMap) {
  64. myMap = newMap;
  65. }
  66. /**
  67. * Get the Map object attatched to this object.
  68. *
  69. * @return Map to attatched to this.
  70. * @see #setMap
  71. */
  72. public Map getMap() {
  73. return myMap;
  74. }
  75. /**
  76. * Get the client object represented by this channelclient.
  77. *
  78. * @return Client object represented by this channelclient
  79. */
  80. public ClientInfo getClient() { return cClient; }
  81. /**
  82. * Get the Channel object that owns this ChannelClient.
  83. *
  84. * @return Channel object that owns this ChannelClient
  85. */
  86. public ChannelInfo getChannel() { return myChannel; }
  87. /**
  88. * Get the nickname of the client object represented by this channelclient.
  89. *
  90. * @return Nickname of the Client object represented by this channelclient
  91. */
  92. public String getNickname() { return cClient.getNickname(); }
  93. /**
  94. * Set the modes this client has (Prefix modes).
  95. *
  96. * @param nNewMode integer representing the modes this client has.
  97. */
  98. public void setChanMode(final long nNewMode) { nModes = nNewMode; }
  99. /**
  100. * Get the modes this client has (Prefix modes).
  101. *
  102. * @return integer representing the modes this client has.
  103. */
  104. public long getChanMode() { return nModes; }
  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.nNextKeyPrefix; i > 0; i = i / 2) {
  119. if ((nCurrentModes & i) == i) {
  120. for (char cTemp : myParser.hPrefixModes.keySet()) {
  121. nTemp = myParser.hPrefixModes.get(cTemp);
  122. if (nTemp == i) {
  123. if (bPrefix) { cTemp = myParser.hPrefixMap.get(cTemp); }
  124. sModes = sModes.append(cTemp);
  125. break;
  126. }
  127. }
  128. }
  129. }
  130. return sModes.toString();
  131. }
  132. /**
  133. * Get the value of the most important mode this client has (Prefix modes).
  134. * A higher value, is a more important mode, 0 = no modes.
  135. *
  136. * @return integer representing the value of the most important mode.
  137. */
  138. public long getImportantModeValue() {
  139. for (long i = myParser.nNextKeyPrefix; i > 0; i = i / 2) {
  140. if ((nModes & i) == i) { return i; }
  141. }
  142. return 0;
  143. }
  144. /**
  145. * Get the most important mode this client has (o, v etc), or an empty
  146. * string if the client has no modes.
  147. *
  148. * @return String representing the most important mode.
  149. */
  150. public String getImportantMode() {
  151. String sModes = this.getChanModeStr(false);
  152. if (!sModes.isEmpty()) { sModes = "" + sModes.charAt(0); }
  153. return sModes;
  154. }
  155. /**
  156. * Get the most important prefix this client has (@, + etc), or an empty
  157. * string if the client has no modes.
  158. *
  159. * @return String representing the most important mode.
  160. */
  161. public String getImportantModePrefix() {
  162. String sModes = this.getChanModeStr(true);
  163. if (!sModes.isEmpty()) { sModes = "" + sModes.charAt(0); }
  164. return sModes;
  165. }
  166. /**
  167. * Get the String Value of ChannelClientInfo (ie @Nickname).
  168. *
  169. * @return String Value of user (inc prefix) (ie @Nickname)
  170. */
  171. @Override
  172. public String toString() {
  173. return this.getImportantModePrefix() + this.getNickname();
  174. }
  175. /**
  176. * Attempt to kick this user from the channel.
  177. *
  178. * @param sReason Why are they being kicked? "" for no reason
  179. */
  180. public void kick(final String sReason) {
  181. myParser.sendString("KICK " + myChannel + " " + this.getNickname() + (sReason.isEmpty() ? sReason : " :" + sReason));
  182. }
  183. /**
  184. * Get the "Complete" String Value of ChannelClientInfo (ie @+Nickname).
  185. *
  186. * @return String Value of user (inc prefix) (ie @+Nickname)
  187. */
  188. public String toFullString() { return this.getChanModeStr(true) + this.getNickname(); }
  189. }