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.

IRCClientInfo.java 13KB

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