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.

ClientInfo.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack
  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 known users.
  29. *
  30. * @author Shane Mc Cormack
  31. * @author Chris Smith
  32. * @version $Id$
  33. * @see IRCParser
  34. */
  35. public final class ClientInfo {
  36. /** Known nickname of client. */
  37. private String sNickname = "";
  38. /** Known ident of client. */
  39. private String sIdent = "";
  40. /** Known host of client. */
  41. private String sHost = "";
  42. /** Known user modes of client. */
  43. private int nModes;
  44. /** Known Away Reason of client. */
  45. private String myAwayReason = "";
  46. /** Known away state for client. */
  47. private boolean bIsAway;
  48. /** Is this a fake client created just for a callback? */
  49. private boolean bIsFake;
  50. /** Reference to the parser object that owns this channel, Used for modes. */
  51. private IRCParser myParser;
  52. /** A Map to allow applications to attach misc data to this object */
  53. private Map myMap;
  54. /**
  55. * Create a new client object from a hostmask.
  56. *
  57. * @param tParser Refernce to parser that owns this channelclient (used for modes)
  58. * @param sHostmask Hostmask parsed by parseHost to get nickname
  59. * @see ClientInfo#parseHost
  60. */
  61. public ClientInfo(final IRCParser tParser, final String sHostmask) {
  62. myMap = new HashMap<Object, Object>();
  63. setUserBits(sHostmask, true);
  64. myParser = tParser;
  65. }
  66. /**
  67. * Set the Map object attatched to this object.
  68. *
  69. * @param newMap New Map to attatch.
  70. */
  71. public void setMap(Map newMap) {
  72. myMap = newMap;
  73. }
  74. /**
  75. * Get the Map object attatched to this object.
  76. *
  77. * @return Map to attatched to this.
  78. */
  79. public Map getMap() {
  80. return myMap;
  81. }
  82. /**
  83. * Check if this is a fake client.
  84. *
  85. * @return True if this is a fake client, else false
  86. */
  87. public boolean isFake() { return bIsFake; }
  88. /**
  89. * Check if this client is actually a server.
  90. *
  91. * @return True if this client is actually a server.
  92. */
  93. public boolean isServer() { return !(sNickname.indexOf(":") == -1); }
  94. /**
  95. * Set if this is a fake client.
  96. * This returns "this" and thus can be used in the construction line.
  97. *
  98. * @param newValue new value for isFake - True if this is a fake client, else false
  99. */
  100. public ClientInfo setFake(boolean newValue) { bIsFake = newValue; return this; }
  101. /**
  102. * Get a nickname of a user from a hostmask.
  103. * Hostmask must match (?:)nick(?!ident)(?@host)
  104. *
  105. * @param sWho Hostname to parse
  106. * @return nickname of user
  107. */
  108. public static String parseHost(final String sWho) {
  109. // Get the nickname from the string.
  110. return parseHostFull(sWho)[0];
  111. }
  112. /**
  113. * Get a nick ident and host of a user from a hostmask.
  114. * Hostmask must match (?:)nick(?!ident)(?@host)
  115. *
  116. * @param sWho Hostname to parse
  117. * @return Array containing details. (result[0] -> Nick | result[1] -> Ident | result[2] -> Host)
  118. */
  119. public static String[] parseHostFull(String sWho) {
  120. String[] sTemp = null;
  121. final String[] result = new String[3];
  122. if (!sWho.equals("")) {
  123. if (sWho.charAt(0) == ':') { sWho = sWho.substring(1); }
  124. }
  125. sTemp = sWho.split("@", 2);
  126. if (sTemp.length != 1) { result[2] = sTemp[1]; } else { result[2] = ""; }
  127. sTemp = sTemp[0].split("!", 2);
  128. if (sTemp.length != 1) { result[1] = sTemp[1]; } else { result[1] = ""; }
  129. result[0] = sTemp[0];
  130. return result;
  131. }
  132. /**
  133. * Get a string representation of the user.
  134. *
  135. * @param sHostmask takes a host (?:)nick(?!ident)(?@host) and sets nick/host/ident variables
  136. * @param bUpdateNick if this is false, only host/ident will be updated.
  137. */
  138. public void setUserBits(final String sHostmask, final boolean bUpdateNick) {
  139. final String[] sTemp = parseHostFull(sHostmask);
  140. if (!sTemp[2].equals("")) { sHost = sTemp[2]; }
  141. if (!sTemp[1].equals("")) { sIdent = sTemp[1]; }
  142. if (bUpdateNick) { sNickname = sTemp[0]; }
  143. }
  144. /**
  145. * Get a string representation of the user.
  146. *
  147. * @return String representation of the user.
  148. */
  149. public String toString() { return sNickname + "!" + sIdent + "@" + sHost; }
  150. /**
  151. * Get the nickname for this user.
  152. *
  153. * @return Known nickname for user.
  154. */
  155. public String getNickname() { return sNickname; }
  156. /**
  157. * Get the ident for this user.
  158. *
  159. * @return Known ident for user. (May be "")
  160. */
  161. public String getIdent() { return sIdent; }
  162. /**
  163. * Get the hostname for this user.
  164. *
  165. * @return Known host for user. (May be "")
  166. */
  167. public String getHost() { return sHost; }
  168. /**
  169. * Set the away state of a user.
  170. * Automatically sets away reason to "" if set to false
  171. *
  172. * @param bNewState Boolean representing state. true = away, false = here
  173. */
  174. protected void setAwayState(final boolean bNewState) {
  175. bIsAway = bNewState;
  176. if (!bIsAway) { myAwayReason = ""; }
  177. }
  178. /**
  179. * Get the away state of a user.
  180. *
  181. * @return Boolean representing state. true = away, false = here
  182. */
  183. public boolean getAwayState() { return bIsAway; }
  184. /**
  185. * Get the Away Reason for this user.
  186. *
  187. * @return Known away reason for user.
  188. */
  189. public String getAwayReason() { return myAwayReason; }
  190. /**
  191. * Set the Away Reason for this user.
  192. * Automatically set to "" if awaystate is set to false
  193. *
  194. * @param newValue new away reason for user.
  195. */
  196. protected void setAwayReason(final String newValue) { myAwayReason = newValue; }
  197. /**
  198. * Set the user modes (as an integer).
  199. *
  200. * @param nNewMode new integer representing channel modes. (Boolean only)
  201. */
  202. protected void setUserMode(final int nNewMode) { nModes = nNewMode; }
  203. /**
  204. * Get the user modes (as an integer).
  205. *
  206. * @return integer representing channel modes. (Boolean only)
  207. */
  208. public int getUserMode() { return nModes; }
  209. /**
  210. * Get the user modes (as a string representation).
  211. *
  212. * @return string representing modes. (boolean and non-list)
  213. */
  214. public String getUserModeStr() {
  215. final StringBuilder sModes = new StringBuilder("+");
  216. int nTemp = 0;
  217. final int nChanModes = this.getUserMode();
  218. for (char cTemp : myParser.hUserModes.keySet()) {
  219. nTemp = myParser.hUserModes.get(cTemp);
  220. if ((nChanModes & nTemp) == nTemp) { sModes.append(cTemp); }
  221. }
  222. return sModes.toString();
  223. }
  224. /**
  225. * Check to see if a client is still known on any of the channels we are on.
  226. *
  227. * @return Boolean to see if client is still visable.
  228. */
  229. public boolean checkVisibility() {
  230. return checkVisibility(null);
  231. }
  232. /**
  233. * Check to see if a client is still known on any of the channels we are on.
  234. *
  235. * @param cChannel Channel to ignore when checking.
  236. * @return Boolean to see if client is still visable.
  237. */
  238. public boolean checkVisibility(ChannelInfo cChannel) {
  239. boolean bCanSee = false;
  240. ChannelClientInfo iChannelClient;
  241. for (ChannelInfo iChannel : myParser.hChannelList.values()) {
  242. if (iChannel == cChannel) { continue; }
  243. iChannelClient = iChannel.getUser(this);
  244. if (iChannelClient != null) { bCanSee = true; break; }
  245. }
  246. return bCanSee;
  247. }
  248. /**
  249. * Get the parser object that owns this client
  250. *
  251. * @return The parser object that owns this client
  252. */
  253. public IRCParser getParser() { return myParser; }
  254. /**
  255. * Get SVN Version information.
  256. *
  257. * @return SVN Version String
  258. */
  259. public static String getSvnInfo() { return "$Id$"; }
  260. }