Java IRC bot
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.

ClientInfo.java 13KB

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