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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Copyright (c) 2006-2008 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. * SVN: $Id$
  23. */
  24. package com.dmdirc.parser;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import java.util.LinkedList;
  28. import java.util.Hashtable;
  29. import java.util.HashMap;
  30. import java.util.Map;
  31. /**
  32. * Contains information about known users.
  33. *
  34. * @author Shane Mc Cormack
  35. * @author Chris Smith
  36. * @version $Id$
  37. * @see IRCParser
  38. */
  39. public final class ClientInfo {
  40. /** Known nickname of client. */
  41. private String sNickname = "";
  42. /** Known ident of client. */
  43. private String sIdent = "";
  44. /** Known host of client. */
  45. private String sHost = "";
  46. /** Known user modes of client. */
  47. private long nModes;
  48. /** Known Away Reason of client. */
  49. private String myAwayReason = "";
  50. /** Known RealName of client. */
  51. private String sRealName = "";
  52. /** Known away state for client. */
  53. private boolean bIsAway;
  54. /** Is this a fake client created just for a callback? */
  55. private boolean bIsFake;
  56. /** Reference to the parser object that owns this channel, Used for modes. */
  57. private IRCParser myParser;
  58. /** A Map to allow applications to attach misc data to this object */
  59. private Map myMap;
  60. /** List of ChannelClientInfos that point to this */
  61. private Hashtable<String, ChannelClientInfo> myChannelClientInfos = new Hashtable<String, ChannelClientInfo>();
  62. /** Modes waiting to be sent to the server. */
  63. private LinkedList<String> lModeQueue = new LinkedList<String>();
  64. /**
  65. * Create a new client object from a hostmask.
  66. *
  67. * @param tParser Refernce to parser that owns this channelclient (used for modes)
  68. * @param sHostmask Hostmask parsed by parseHost to get nickname
  69. * @see ClientInfo#parseHost
  70. */
  71. public ClientInfo(final IRCParser tParser, final String sHostmask) {
  72. myMap = new HashMap<Object, Object>();
  73. setUserBits(sHostmask, true);
  74. myParser = tParser;
  75. }
  76. /**
  77. * Set the Map object attatched to this object.
  78. *
  79. * @param newMap New Map to attatch.
  80. */
  81. public void setMap(Map newMap) {
  82. myMap = newMap;
  83. }
  84. /**
  85. * Get the Map object attatched to this object.
  86. *
  87. * @return Map to attatched to this.
  88. */
  89. public Map getMap() {
  90. return myMap;
  91. }
  92. /**
  93. * Check if this is a fake client.
  94. *
  95. * @return True if this is a fake client, else false
  96. */
  97. public boolean isFake() { return bIsFake; }
  98. /**
  99. * Check if this client is actually a server.
  100. *
  101. * @return True if this client is actually a server.
  102. */
  103. public boolean isServer() { return !(sNickname.indexOf(":") == -1); }
  104. /**
  105. * Set if this is a fake client.
  106. * This returns "this" and thus can be used in the construction line.
  107. *
  108. * @param newValue new value for isFake - True if this is a fake client, else false
  109. * @return this Object
  110. */
  111. public ClientInfo setFake(boolean newValue) { bIsFake = newValue; return this; }
  112. /**
  113. * Get a nickname of a user from a hostmask.
  114. * Hostmask must match (?:)nick(?!ident)(?@host)
  115. *
  116. * @param sWho Hostname to parse
  117. * @return nickname of user
  118. */
  119. public static String parseHost(final String sWho) {
  120. // Get the nickname from the string.
  121. return parseHostFull(sWho)[0];
  122. }
  123. /**
  124. * Get a nick ident and host of a user from a hostmask.
  125. * Hostmask must match (?:)nick(?!ident)(?@host)
  126. *
  127. * @param sWho Hostname to parse
  128. * @return Array containing details. (result[0] -> Nick | result[1] -> Ident | result[2] -> Host)
  129. */
  130. public static String[] parseHostFull(String sWho) {
  131. String[] sTemp = null;
  132. final String[] result = new String[3];
  133. if (!sWho.isEmpty()) {
  134. if (sWho.charAt(0) == ':') { sWho = sWho.substring(1); }
  135. }
  136. sTemp = sWho.split("@", 2);
  137. if (sTemp.length != 1) { result[2] = sTemp[1]; } else { result[2] = ""; }
  138. sTemp = sTemp[0].split("!", 2);
  139. if (sTemp.length != 1) { result[1] = sTemp[1]; } else { result[1] = ""; }
  140. result[0] = sTemp[0];
  141. return result;
  142. }
  143. /**
  144. * Set the nick/ident/host of this client
  145. *
  146. * @param sHostmask takes a host (?:)nick(?!ident)(?@host) and sets nick/host/ident variables
  147. * @param bUpdateNick if this is false, only host/ident will be updated.
  148. */
  149. public void setUserBits(final String sHostmask, final boolean bUpdateNick) {
  150. setUserBits(sHostmask, bUpdateNick, false);
  151. }
  152. /**
  153. * Set the nick/ident/host of this client
  154. *
  155. * @param sHostmask takes a host (?:)nick(?!ident)(?@host) and sets nick/host/ident variables
  156. * @param bUpdateNick if this is false, only host/ident will be updated.
  157. * @param allowBlank if this is true, ident/host will be set even if
  158. * parseHostFull returns empty values for them
  159. */
  160. public void setUserBits(final String sHostmask, final boolean bUpdateNick, final boolean allowBlank) {
  161. final String[] sTemp = parseHostFull(sHostmask);
  162. if (!sTemp[2].isEmpty() || allowBlank) { sHost = sTemp[2]; }
  163. if (!sTemp[1].isEmpty() || allowBlank) { sIdent = sTemp[1]; }
  164. if (bUpdateNick) { sNickname = sTemp[0]; }
  165. }
  166. /**
  167. * Get a string representation of the user.
  168. *
  169. * @return String representation of the user.
  170. */
  171. public String toString() { return sNickname + "!" + sIdent + "@" + sHost; }
  172. /**
  173. * Get the nickname for this user.
  174. *
  175. * @return Known nickname for user.
  176. */
  177. public String getNickname() { return sNickname; }
  178. /**
  179. * Get the ident for this user.
  180. *
  181. * @return Known ident for user. (May be "")
  182. */
  183. public String getIdent() { return sIdent; }
  184. /**
  185. * Get the hostname for this user.
  186. *
  187. * @return Known host for user. (May be "")
  188. */
  189. public String getHost() { return sHost; }
  190. /**
  191. * Set the away state of a user.
  192. * Automatically sets away reason to "" if set to false
  193. *
  194. * @param bNewState Boolean representing state. true = away, false = here
  195. */
  196. protected void setAwayState(final boolean bNewState) {
  197. bIsAway = bNewState;
  198. if (!bIsAway) { myAwayReason = ""; }
  199. }
  200. /**
  201. * Get the away state of a user.
  202. *
  203. * @return Boolean representing state. true = away, false = here
  204. */
  205. public boolean getAwayState() { return bIsAway; }
  206. /**
  207. * Get the Away Reason for this user.
  208. *
  209. * @return Known away reason for user.
  210. */
  211. public String getAwayReason() { return myAwayReason; }
  212. /**
  213. * Set the Away Reason for this user.
  214. * Automatically set to "" if awaystate is set to false
  215. *
  216. * @param newValue new away reason for user.
  217. */
  218. protected void setAwayReason(final String newValue) { myAwayReason = newValue; }
  219. /**
  220. * Get the RealName for this user.
  221. *
  222. * @return Known RealName for user.
  223. */
  224. public String getRealName() { return sRealName; }
  225. /**
  226. * Set the RealName for this user.
  227. *
  228. * @param newValue new RealName for user.
  229. */
  230. protected void setRealName(final String newValue) { sRealName = newValue; }
  231. /**
  232. * Set the user modes (as an integer).
  233. *
  234. * @param nNewMode new long representing channel modes. (Boolean only)
  235. */
  236. protected void setUserMode(final long nNewMode) { nModes = nNewMode; }
  237. /**
  238. * Get the user modes (as an integer).
  239. *
  240. * @return long representing channel modes. (Boolean only)
  241. */
  242. public long getUserMode() { return nModes; }
  243. /**
  244. * Get the user modes (as a string representation).
  245. *
  246. * @return string representing modes. (boolean and non-list)
  247. */
  248. public String getUserModeStr() {
  249. final StringBuilder sModes = new StringBuilder("+");
  250. long nTemp = 0;
  251. final long nChanModes = this.getUserMode();
  252. for (char cTemp : myParser.hUserModes.keySet()) {
  253. nTemp = myParser.hUserModes.get(cTemp);
  254. if ((nChanModes & nTemp) == nTemp) { sModes.append(cTemp); }
  255. }
  256. return sModes.toString();
  257. }
  258. /**
  259. * Is this client an oper?
  260. * This is a guess currently based on user-modes and thus only works on the
  261. * parsers own client.
  262. *
  263. * @return True/False if this client appears to be an oper
  264. */
  265. public boolean isOper() {
  266. final String modestr = getUserModeStr();
  267. return (modestr.indexOf('o') > -1) || (modestr.indexOf('O') > -1);
  268. }
  269. /**
  270. * Add a ChannelClientInfo as a known reference to this client.
  271. *
  272. * @param cci ChannelClientInfo to add as a known reference
  273. */
  274. public void addChannelClientInfo(final ChannelClientInfo cci) {
  275. final String key = myParser.toLowerCase(cci.getChannel().getName());
  276. if (!myChannelClientInfos.containsKey(key)) {
  277. myChannelClientInfos.put(key, cci);
  278. }
  279. }
  280. /**
  281. * Remove a ChannelClientInfo as a known reference to this client.
  282. *
  283. * @param cci ChannelClientInfo to remove as a known reference
  284. */
  285. public void delChannelClientInfo(final ChannelClientInfo cci) {
  286. final String key = myParser.toLowerCase(cci.getChannel().getName());
  287. if (myChannelClientInfos.containsKey(key)) {
  288. myChannelClientInfos.remove(key);
  289. }
  290. }
  291. /**
  292. * Check to see if a client is still known on any of the channels we are on.
  293. *
  294. * @return Boolean to see if client is still visable.
  295. */
  296. public boolean checkVisibility() {
  297. return !myChannelClientInfos.isEmpty();
  298. }
  299. /**
  300. * Check how many channels this client is known on.
  301. *
  302. * @return int with the count of known channels
  303. */
  304. public int channelCount() {
  305. return myChannelClientInfos.size();
  306. }
  307. /**
  308. * Get a list of channelClients that point to this object.
  309. *
  310. * @return int with the count of known channels
  311. */
  312. public List<ChannelClientInfo> getChannelClients() {
  313. ArrayList<ChannelClientInfo> result = new ArrayList<ChannelClientInfo>();
  314. for (ChannelClientInfo cci : myChannelClientInfos.values()) {
  315. result.add(cci);
  316. }
  317. return result;
  318. }
  319. /**
  320. * Adjust the channel modes on a channel.
  321. * This function will queue modes up to be sent in one go, according to 005 params.
  322. * If less modes are altered than the queue accepts, sendModes() must be called.<br><br>
  323. * sendModes is automatically called if you attempt to add more modes than is allowed
  324. * to be queued
  325. *
  326. * @param positive Is this a positive mode change, or a negative mode change
  327. * @param mode Character representing the mode to change
  328. */
  329. public void alterMode(final boolean positive, final Character mode) {
  330. if (isFake()) { return; }
  331. int modecount = 1;
  332. String modestr = "";
  333. if (myParser.h005Info.containsKey("MODES")) {
  334. try {
  335. modecount = Integer.parseInt(myParser.h005Info.get("MODES"));
  336. } catch (NumberFormatException e) {
  337. modecount = 1;
  338. }
  339. }
  340. if (lModeQueue.size() == modecount) { sendModes(); }
  341. if (positive) { modestr = "+"; } else { modestr = "-"; }
  342. modestr = modestr + mode;
  343. if (!myParser.hUserModes.containsKey(mode)) { return; }
  344. myParser.callDebugInfo(myParser.DEBUG_INFO, "Queueing user mode: %s", modestr);
  345. lModeQueue.add(modestr);
  346. }
  347. /**
  348. * This function will send modes that are currently queued up to send.
  349. * This assumes that the queue only contains the amount that are alowed to be sent
  350. * and thus will try to send the entire queue in one go.<br><br>
  351. * Modes are always sent negative then positive and not mixed.
  352. */
  353. public void sendModes() {
  354. if (lModeQueue.size() == 0) { return; }
  355. final StringBuilder positivemode = new StringBuilder();
  356. final StringBuilder negativemode = new StringBuilder();
  357. final StringBuilder sendModeStr = new StringBuilder();
  358. String modestr;
  359. boolean positive;
  360. for (int i = 0; i < lModeQueue.size(); ++i) {
  361. modestr = lModeQueue.get(i);
  362. positive = modestr.charAt(0) == '+';
  363. if (positive) {
  364. positivemode.append(modestr.charAt(1));
  365. } else {
  366. negativemode.append(modestr.charAt(1));
  367. }
  368. }
  369. if (negativemode.length() > 0) { sendModeStr.append("-").append(negativemode); }
  370. if (positivemode.length() > 0) { sendModeStr.append("+").append(positivemode); }
  371. myParser.callDebugInfo(myParser.DEBUG_INFO, "Sending mode: %s", sendModeStr.toString());
  372. myParser.sendLine("MODE " + sNickname + " " + sendModeStr.toString());
  373. clearModeQueue();
  374. }
  375. /**
  376. * This function will clear the mode queue (WITHOUT Sending).
  377. */
  378. public void clearModeQueue() {
  379. lModeQueue.clear();
  380. }
  381. /**
  382. * Get the parser object that owns this client.
  383. *
  384. * @return The parser object that owns this client
  385. */
  386. public IRCParser getParser() { return myParser; }
  387. }