Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

IRCClientInfo.java 14KB

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