Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

IRCClientInfo.java 14KB

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