Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

IRCClientInfo.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * Copyright (c) 2006-2012 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<String, IRCChannelClientInfo>();
  61. /** Modes waiting to be sent to the server. */
  62. private final List<String> modeQueue = new LinkedList<String>();
  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 ClientInfo#parseHost
  69. */
  70. public IRCClientInfo(final IRCParser tParser, final String sHostmask) {
  71. map = new HashMap<Object, Object>();
  72. setUserBits(sHostmask, true);
  73. parser = tParser;
  74. }
  75. /** {@inheritDoc} */
  76. @Override
  77. public Map<Object, Object> getMap() {
  78. return map;
  79. }
  80. /**
  81. * Check if this is a fake client.
  82. *
  83. * @return True if this is a fake client, else false
  84. */
  85. public boolean isFake() {
  86. return fake;
  87. }
  88. /**
  89. * Check if this client is actually a server.
  90. *
  91. * @return True if this client is actually a server.
  92. */
  93. public boolean isServer() {
  94. return nickname.indexOf(':') > -1;
  95. }
  96. /**
  97. * Set if this is a fake client.
  98. * This returns "this" and thus can be used in the construction line.
  99. *
  100. * @param newValue new value for isFake - True if this is a fake client, else false
  101. * @return this Object
  102. */
  103. public IRCClientInfo setFake(final boolean newValue) {
  104. fake = newValue;
  105. return this;
  106. }
  107. /**
  108. * Get a nickname of a user from a hostmask.
  109. * Hostmask must match (?:)nick(?!ident)(?@host)
  110. *
  111. * @param sWho Hostname to parse
  112. * @return nickname of user
  113. */
  114. public static String parseHost(final String sWho) {
  115. // Get the nickname from the string.
  116. return parseHostFull(sWho)[0];
  117. }
  118. /**
  119. * Get a nick ident and host of a user from a hostmask.
  120. * Hostmask must match (?:)nick(?!ident)(?@host)
  121. *
  122. * @param hostmask Hostname to parse
  123. * @return Array containing details. (result[0] -> Nick | result[1] -> 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. /** {@inheritDoc} */
  202. @Override
  203. public String getUsername() {
  204. return ident;
  205. }
  206. /** {@inheritDoc} */
  207. @Override
  208. public String getHostname() {
  209. return host;
  210. }
  211. /**
  212. * Set the away state of a user.
  213. * Automatically sets away reason to "" if not set to AwayState.AWAY
  214. *
  215. * @param newState AwayState representing new away state.
  216. */
  217. protected void setAwayState(final AwayState newState) {
  218. away = newState;
  219. if (away != AwayState.AWAY) {
  220. awayReason = "";
  221. }
  222. }
  223. /** {@inheritDoc} */
  224. @Override
  225. public AwayState getAwayState() {
  226. return away;
  227. }
  228. /** {@inheritDoc} */
  229. @Override
  230. public String getAwayReason() {
  231. return awayReason;
  232. }
  233. /**
  234. * Set the Away Reason for this user.
  235. * Automatically set to "" if awaystate is set to false
  236. *
  237. * @param newValue new away reason for user.
  238. */
  239. protected void setAwayReason(final String newValue) {
  240. awayReason = newValue;
  241. }
  242. /** {@inheritDoc} */
  243. @Override
  244. public String getRealname() {
  245. return realName;
  246. }
  247. /**
  248. * Set the RealName for this user.
  249. *
  250. * @param newValue new RealName for user.
  251. */
  252. protected void setRealName(final String newValue) {
  253. realName = newValue;
  254. }
  255. /** {@inheritDoc} */
  256. @Override
  257. public String getAccountName() {
  258. return accountName;
  259. }
  260. /**
  261. * Set the account name for this user.
  262. *
  263. * @param newValue new account name for user.
  264. */
  265. protected void setAccountName(final String newValue) {
  266. accountName = newValue;
  267. }
  268. /**
  269. * Set the user modes (as an integer).
  270. *
  271. * @param newMode new long representing channel modes. (Boolean only)
  272. */
  273. protected void setUserMode(final long newMode) {
  274. modes = newMode;
  275. }
  276. /**
  277. * Get the user modes (as an integer).
  278. *
  279. * @return long representing channel modes. (Boolean only)
  280. */
  281. public long getUserMode() {
  282. return modes;
  283. }
  284. /** {@inheritDoc} */
  285. @Override
  286. public String getModes() {
  287. final StringBuilder sModes = new StringBuilder("+");
  288. final long nChanModes = this.getUserMode();
  289. for (char cTemp : parser.userModes.keySet()) {
  290. final long nTemp = parser.userModes.get(cTemp);
  291. if ((nChanModes & nTemp) == nTemp) {
  292. sModes.append(cTemp);
  293. }
  294. }
  295. return sModes.toString();
  296. }
  297. /**
  298. * Is this client an oper?
  299. * This is a guess currently based on user-modes and thus only works on the
  300. * parsers own client.
  301. *
  302. * @return True/False if this client appears to be an oper
  303. */
  304. public boolean isOper() {
  305. final String modestr = getModes();
  306. return modestr.indexOf('o') > -1 || modestr.indexOf('O') > -1;
  307. }
  308. /**
  309. * Add a ChannelClientInfo as a known reference to this client.
  310. *
  311. * @param cci ChannelClientInfo to add as a known reference
  312. */
  313. public void addChannelClientInfo(final IRCChannelClientInfo cci) {
  314. final String key = parser.getStringConverter().toLowerCase(cci.getChannel().getName());
  315. if (!clients.containsKey(key)) {
  316. clients.put(key, cci);
  317. }
  318. }
  319. /**
  320. * Remove a ChannelClientInfo as a known reference to this client.
  321. *
  322. * @param cci ChannelClientInfo to remove as a known reference
  323. */
  324. public void delChannelClientInfo(final IRCChannelClientInfo cci) {
  325. final String key = parser.getStringConverter().toLowerCase(cci.getChannel().getName());
  326. if (clients.containsKey(key)) {
  327. clients.remove(key);
  328. }
  329. }
  330. /**
  331. * Check to see if a client is still known on any of the channels we are on.
  332. *
  333. * @return Boolean to see if client is still visable.
  334. */
  335. public boolean checkVisibility() {
  336. return !clients.isEmpty();
  337. }
  338. /** {@inheritDoc} */
  339. @Override
  340. public int getChannelCount() {
  341. return clients.size();
  342. }
  343. /**
  344. * Get a list of channelClients that point to this object.
  345. *
  346. * @return int with the count of known channels
  347. */
  348. public List<IRCChannelClientInfo> getChannelClients() {
  349. return new ArrayList<IRCChannelClientInfo>(clients.values());
  350. }
  351. /** {@inheritDoc} */
  352. @Override
  353. public void alterMode(final boolean add, final Character mode) {
  354. if (isFake() || !parser.userModes.containsKey(mode)) {
  355. return;
  356. }
  357. int modecount = 1;
  358. if (parser.h005Info.containsKey("MODES")) {
  359. try {
  360. modecount = Integer.parseInt(parser.h005Info.get("MODES"));
  361. } catch (NumberFormatException e) {
  362. modecount = 1;
  363. }
  364. }
  365. final String modestr = (add ? "+" : "-") + mode;
  366. final String teststr = (add ? "-" : "+") + mode;
  367. if (modeQueue.contains(teststr)) {
  368. modeQueue.remove(teststr);
  369. return;
  370. } else if (modeQueue.contains(modestr)) {
  371. return;
  372. }
  373. parser.callDebugInfo(IRCParser.DEBUG_INFO, "Queueing user mode: %s", modestr);
  374. modeQueue.add(modestr);
  375. if (modeQueue.size() == modecount) {
  376. flushModes();
  377. }
  378. }
  379. /** {@inheritDoc} */
  380. @Override
  381. public void flushModes() {
  382. if (modeQueue.isEmpty()) {
  383. return;
  384. }
  385. final StringBuilder positivemode = new StringBuilder();
  386. final StringBuilder negativemode = new StringBuilder();
  387. final StringBuilder sendModeStr = new StringBuilder();
  388. String modestr;
  389. boolean positive;
  390. for (int i = 0; i < modeQueue.size(); ++i) {
  391. modestr = modeQueue.get(i);
  392. positive = modestr.charAt(0) == '+';
  393. if (positive) {
  394. positivemode.append(modestr.charAt(1));
  395. } else {
  396. negativemode.append(modestr.charAt(1));
  397. }
  398. }
  399. if (negativemode.length() > 0) {
  400. sendModeStr.append("-").append(negativemode);
  401. }
  402. if (positivemode.length() > 0) {
  403. sendModeStr.append("+").append(positivemode);
  404. }
  405. parser.callDebugInfo(IRCParser.DEBUG_INFO, "Sending mode: %s", sendModeStr.toString());
  406. parser.sendRawMessage("MODE " + nickname + " " + sendModeStr.toString());
  407. clearModeQueue();
  408. }
  409. /**
  410. * This function will clear the mode queue (WITHOUT Sending).
  411. */
  412. public void clearModeQueue() {
  413. modeQueue.clear();
  414. }
  415. /** {@inheritDoc} */
  416. @Override
  417. public Parser getParser() {
  418. return parser;
  419. }
  420. /** {@inheritDoc} */
  421. @Override
  422. public void setNickname(final String name) {
  423. if (parser.getLocalClient().equals(this)) {
  424. parser.setNickname(name);
  425. } else {
  426. throw new UnsupportedOperationException("Cannot call setNickname on non-local client");
  427. }
  428. }
  429. /** {@inheritDoc} */
  430. @Override
  431. public void setAway(final String reason) {
  432. parser.sendRawMessage("AWAY :" + reason);
  433. }
  434. /** {@inheritDoc} */
  435. @Override
  436. public void setBack() {
  437. parser.sendRawMessage("AWAY");
  438. }
  439. }