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

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