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

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