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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  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.common.AwayState;
  24. import com.dmdirc.parser.interfaces.LocalClientInfo;
  25. import com.dmdirc.parser.interfaces.Parser;
  26. import java.util.ArrayList;
  27. import java.util.HashMap;
  28. import java.util.LinkedList;
  29. import java.util.List;
  30. import java.util.Map;
  31. /**
  32. * Contains information about known users.
  33. *
  34. * @see IRCParser
  35. */
  36. public class IRCClientInfo implements LocalClientInfo {
  37. /** Known nickname of client. */
  38. private String nickname = "";
  39. /** Known ident of client. */
  40. private String ident = "";
  41. /** Known host of client. */
  42. private String host = "";
  43. /** Known user modes of client. */
  44. private long modes;
  45. /** Known Away Reason of client. */
  46. private String awayReason = "";
  47. /** Known Account name of client. */
  48. private String accountName = "*";
  49. /** Known RealName of client. */
  50. private String realName = "";
  51. /** Known away state for client. */
  52. private AwayState away = AwayState.UNKNOWN;
  53. /** Is this a fake client created just for a callback? */
  54. private boolean fake;
  55. /** Reference to the parser object that owns this channel, Used for modes. */
  56. private final IRCParser parser;
  57. /** A Map to allow applications to attach misc data to this object. */
  58. private final Map<Object, Object> map;
  59. /** List of ChannelClientInfos that point to this. */
  60. private final Map<String, IRCChannelClientInfo> clients = new HashMap<>();
  61. /** Modes waiting to be sent to the server. */
  62. private final List<String> modeQueue = new LinkedList<>();
  63. /**
  64. * Create a new client object from a hostmask.
  65. *
  66. * @param tParser Refernce to parser that owns this channelclient (used for modes)
  67. * @param sHostmask Hostmask parsed by parseHost to get nickname
  68. * @see IRCClientInfo#parseHost
  69. */
  70. public IRCClientInfo(final IRCParser tParser, final String sHostmask) {
  71. map = new HashMap<>();
  72. setUserBits(sHostmask, true);
  73. parser = tParser;
  74. }
  75. @Override
  76. public Map<Object, Object> getMap() {
  77. return map;
  78. }
  79. /**
  80. * Check if this is a fake client.
  81. *
  82. * @return True if this is a fake client, else false
  83. */
  84. public boolean isFake() {
  85. return fake;
  86. }
  87. /**
  88. * Check if this client is actually a server.
  89. *
  90. * @return True if this client is actually a server.
  91. */
  92. public boolean isServer() {
  93. return nickname.indexOf(':') > -1;
  94. }
  95. /**
  96. * Set if this is a fake client.
  97. * This returns "this" and thus can be used in the construction line.
  98. *
  99. * @param newValue new value for isFake - True if this is a fake client, else false
  100. * @return this Object
  101. */
  102. public IRCClientInfo setFake(final boolean newValue) {
  103. fake = newValue;
  104. return this;
  105. }
  106. /**
  107. * Get a nickname of a user from a hostmask.
  108. * Hostmask must match (?:)nick(?!ident)(?@host)
  109. *
  110. * @param sWho Hostname to parse
  111. * @return nickname of user
  112. */
  113. public static String parseHost(final String sWho) {
  114. // Get the nickname from the string.
  115. return parseHostFull(sWho)[0];
  116. }
  117. /**
  118. * Get a nick ident and host of a user from a hostmask.
  119. * Hostmask must match (?:)nick(?!ident)(?@host)
  120. *
  121. * @param hostmask Hostname to parse
  122. * @return Array containing details. (result[0] -&gt; Nick | result[1]
  123. * -&gt; Ident | result[2] - Host)
  124. */
  125. public static String[] parseHostFull(final String hostmask) {
  126. String[] sTemp;
  127. final String[] result = new String[3];
  128. if (!hostmask.isEmpty() && hostmask.charAt(0) == ':') {
  129. sTemp = hostmask.substring(1).split("@", 2);
  130. } else {
  131. sTemp = hostmask.split("@", 2);
  132. }
  133. result[2] = sTemp.length == 1 ? "" : sTemp[1];
  134. sTemp = sTemp[0].split("!", 2);
  135. result[1] = sTemp.length == 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 hostmask takes a host (?:)nick(?!ident)(?@host) and sets nick/host/ident variables
  143. * @param updateNick if this is false, only host/ident will be updated.
  144. */
  145. public void setUserBits(final String hostmask, final boolean updateNick) {
  146. setUserBits(hostmask, updateNick, false);
  147. }
  148. /**
  149. * Set the nick/ident/host of this client.
  150. *
  151. * @param hostmask takes a host (?:)nick(?!ident)(?@host) and sets nick/host/ident variables
  152. * @param updateNick 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 hostmask, final boolean updateNick, final boolean allowBlank) {
  157. final String[] hostParts = parseHostFull(hostmask);
  158. if (!hostParts[2].isEmpty() || allowBlank) {
  159. host = hostParts[2];
  160. }
  161. if (!hostParts[1].isEmpty() || allowBlank) {
  162. ident = hostParts[1];
  163. }
  164. if (updateNick) {
  165. nickname = hostParts[0];
  166. }
  167. }
  168. /**
  169. * Get a string representation of the user.
  170. *
  171. * @return String representation of the user.
  172. */
  173. @Override
  174. public String toString() {
  175. return nickname + "!" + ident + "@" + host;
  176. }
  177. /**
  178. * {@inheritDoc}
  179. *
  180. * For local clients, this method will return the nickname value supplied
  181. * by the {@link IRCParser} instead of the local nickname.
  182. */
  183. @Override
  184. public String getNickname() {
  185. // If this is the localClient then do what we are supposed to do, and ask
  186. // the parser using parser.getNickname()
  187. if (this.equals(parser.getLocalClient())) {
  188. return parser.getMyNickname();
  189. } else {
  190. return nickname;
  191. }
  192. }
  193. /**
  194. * Retrieves the locally stored nickname of this client.
  195. *
  196. * @return The client's nickname
  197. */
  198. public String getRealNickname() {
  199. return nickname;
  200. }
  201. @Override
  202. public String getUsername() {
  203. return ident;
  204. }
  205. @Override
  206. public String getHostname() {
  207. return host;
  208. }
  209. /**
  210. * Set the away state of a user.
  211. * Automatically sets away reason to "" if not set to AwayState.AWAY
  212. *
  213. * @param newState AwayState representing new away state.
  214. */
  215. protected void setAwayState(final AwayState newState) {
  216. away = newState;
  217. if (away != AwayState.AWAY) {
  218. awayReason = "";
  219. }
  220. }
  221. @Override
  222. public AwayState getAwayState() {
  223. return away;
  224. }
  225. @Override
  226. public String getAwayReason() {
  227. return awayReason;
  228. }
  229. /**
  230. * Set the Away Reason for this user.
  231. * Automatically set to "" if awaystate is set to false
  232. *
  233. * @param newValue new away reason for user.
  234. */
  235. protected void setAwayReason(final String newValue) {
  236. awayReason = newValue;
  237. }
  238. @Override
  239. public String getRealname() {
  240. return realName;
  241. }
  242. /**
  243. * Set the RealName for this user.
  244. *
  245. * @param newValue new RealName for user.
  246. */
  247. protected void setRealName(final String newValue) {
  248. realName = newValue;
  249. }
  250. @Override
  251. public String getAccountName() {
  252. return accountName;
  253. }
  254. /**
  255. * Set the account name for this user.
  256. *
  257. * @param newValue new account name for user.
  258. */
  259. protected void setAccountName(final String newValue) {
  260. accountName = newValue;
  261. }
  262. /**
  263. * Set the user modes (as an integer).
  264. *
  265. * @param newMode new long representing channel modes. (Boolean only)
  266. */
  267. protected void setUserMode(final long newMode) {
  268. modes = newMode;
  269. }
  270. /**
  271. * Get the user modes (as an integer).
  272. *
  273. * @return long representing channel modes. (Boolean only)
  274. */
  275. public long getUserMode() {
  276. return modes;
  277. }
  278. @Override
  279. public String getModes() {
  280. final StringBuilder sModes = new StringBuilder("+");
  281. final long nChanModes = this.getUserMode();
  282. for (char cTemp : parser.userModes.keySet()) {
  283. final long nTemp = parser.userModes.get(cTemp);
  284. if ((nChanModes & nTemp) == nTemp) {
  285. sModes.append(cTemp);
  286. }
  287. }
  288. return sModes.toString();
  289. }
  290. /**
  291. * Is this client an oper?
  292. * This is a guess currently based on user-modes and thus only works on the
  293. * parsers own client.
  294. *
  295. * @return True/False if this client appears to be an oper
  296. */
  297. public boolean isOper() {
  298. final String modestr = getModes();
  299. return modestr.indexOf('o') > -1 || modestr.indexOf('O') > -1;
  300. }
  301. /**
  302. * Add a ChannelClientInfo as a known reference to this client.
  303. *
  304. * @param cci ChannelClientInfo to add as a known reference
  305. */
  306. public void addChannelClientInfo(final IRCChannelClientInfo cci) {
  307. final String key = parser.getStringConverter().toLowerCase(cci.getChannel().getName());
  308. if (!clients.containsKey(key)) {
  309. clients.put(key, cci);
  310. }
  311. }
  312. /**
  313. * Remove a ChannelClientInfo as a known reference to this client.
  314. *
  315. * @param cci ChannelClientInfo to remove as a known reference
  316. */
  317. public void delChannelClientInfo(final IRCChannelClientInfo cci) {
  318. final String key = parser.getStringConverter().toLowerCase(cci.getChannel().getName());
  319. if (clients.containsKey(key)) {
  320. clients.remove(key);
  321. }
  322. }
  323. /**
  324. * Check to see if a client is still known on any of the channels we are on.
  325. *
  326. * @return Boolean to see if client is still visable.
  327. */
  328. public boolean checkVisibility() {
  329. return !clients.isEmpty();
  330. }
  331. @Override
  332. public int getChannelCount() {
  333. return clients.size();
  334. }
  335. /**
  336. * Get a list of channelClients that point to this object.
  337. *
  338. * @return int with the count of known channels
  339. */
  340. public List<IRCChannelClientInfo> getChannelClients() {
  341. return new ArrayList<>(clients.values());
  342. }
  343. @Override
  344. public void alterMode(final boolean add, final Character mode) {
  345. if (isFake() || !parser.userModes.containsKey(mode)) {
  346. return;
  347. }
  348. int modecount = 1;
  349. if (parser.h005Info.containsKey("MODES")) {
  350. try {
  351. modecount = Integer.parseInt(parser.h005Info.get("MODES"));
  352. } catch (NumberFormatException e) {
  353. modecount = 1;
  354. }
  355. }
  356. final String modestr = (add ? "+" : "-") + mode;
  357. final String teststr = (add ? "-" : "+") + mode;
  358. if (modeQueue.contains(teststr)) {
  359. modeQueue.remove(teststr);
  360. return;
  361. } else if (modeQueue.contains(modestr)) {
  362. return;
  363. }
  364. parser.callDebugInfo(IRCParser.DEBUG_INFO, "Queueing user mode: %s", modestr);
  365. modeQueue.add(modestr);
  366. if (modeQueue.size() == modecount) {
  367. flushModes();
  368. }
  369. }
  370. @Override
  371. public void flushModes() {
  372. if (modeQueue.isEmpty()) {
  373. return;
  374. }
  375. final StringBuilder positivemode = new StringBuilder();
  376. final StringBuilder negativemode = new StringBuilder();
  377. final StringBuilder sendModeStr = new StringBuilder();
  378. String modestr;
  379. boolean positive;
  380. for (String aModeQueue : modeQueue) {
  381. modestr = aModeQueue;
  382. positive = modestr.charAt(0) == '+';
  383. if (positive) {
  384. positivemode.append(modestr.charAt(1));
  385. } else {
  386. negativemode.append(modestr.charAt(1));
  387. }
  388. }
  389. if (negativemode.length() > 0) {
  390. sendModeStr.append("-").append(negativemode);
  391. }
  392. if (positivemode.length() > 0) {
  393. sendModeStr.append("+").append(positivemode);
  394. }
  395. parser.callDebugInfo(IRCParser.DEBUG_INFO, "Sending mode: %s", sendModeStr.toString());
  396. parser.sendRawMessage("MODE " + nickname + " " + sendModeStr.toString());
  397. clearModeQueue();
  398. }
  399. /**
  400. * This function will clear the mode queue (WITHOUT Sending).
  401. */
  402. public void clearModeQueue() {
  403. modeQueue.clear();
  404. }
  405. @Override
  406. public Parser getParser() {
  407. return parser;
  408. }
  409. @Override
  410. public void setNickname(final String name) {
  411. if (parser.getLocalClient().equals(this)) {
  412. parser.setNickname(name);
  413. } else {
  414. throw new UnsupportedOperationException("Cannot call setNickname on non-local client");
  415. }
  416. }
  417. @Override
  418. public void setAway(final String reason) {
  419. parser.sendRawMessage("AWAY :" + reason);
  420. }
  421. @Override
  422. public void setBack() {
  423. parser.sendRawMessage("AWAY");
  424. }
  425. }