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.

IRCChannelInfo.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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.ChannelListModeItem;
  24. import com.dmdirc.parser.common.ParserError;
  25. import com.dmdirc.parser.common.QueuePriority;
  26. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  27. import com.dmdirc.parser.interfaces.ChannelInfo;
  28. import com.dmdirc.parser.interfaces.ClientInfo;
  29. import com.dmdirc.parser.interfaces.Parser;
  30. import java.util.ArrayList;
  31. import java.util.Collection;
  32. import java.util.Collections;
  33. import java.util.HashMap;
  34. import java.util.LinkedList;
  35. import java.util.List;
  36. import java.util.Map;
  37. import java.util.Queue;
  38. /**
  39. * Contains Channel information.
  40. *
  41. * @see IRCParser
  42. */
  43. public class IRCChannelInfo implements ChannelInfo {
  44. /**
  45. * Boolean repreenting the status of names requests.
  46. * When this is false, any new names reply will cause current known channelclients to be removed.
  47. */
  48. private boolean addingNames = true;
  49. /** Unixtimestamp representing time when the channel was created. */
  50. private long creationTime;
  51. /** Current known topic in the channel. */
  52. private String topic = "";
  53. /** Last known user to set the topic (Full host where possible). */
  54. private String topicUser = "";
  55. /** Unixtimestamp representing time when the topic was set. */
  56. private long topicTime;
  57. /** Has this channel ever had a topic? */
  58. private boolean hadTopic;
  59. /** Known boolean-modes for channel. */
  60. private String modes = "";
  61. /** Reference to the parser object that owns this channel, Used for modes. */
  62. private final IRCParser parser;
  63. /** Mode manager to use for user modes. */
  64. private final ModeManager userModeManager;
  65. /** Mode manager to use for channel modes. */
  66. private final ModeManager chanModeManager;
  67. /** Mode manager to use for prefix mode information. */
  68. private final PrefixModeManager prefixModeManager;
  69. /** Channel Name. */
  70. private final String name;
  71. /** Channel Key. */
  72. private String password = "";
  73. /** Hashtable containing references to ChannelClients. */
  74. private final Map<String, IRCChannelClientInfo> clients = Collections.synchronizedMap(new HashMap<>());
  75. /** Hashtable storing values for modes set in the channel that use parameters. */
  76. private final Map<Character, String> paramModes = new HashMap<>();
  77. /** Hashtable storing list modes. */
  78. private final Map<Character, ArrayList<ChannelListModeItem>> listModes = new HashMap<>();
  79. /**
  80. * LinkedList storing status of mode adding.
  81. * if an item is in this list for a mode, we are expecting new items for the list
  82. */
  83. private final Collection<Character> addingModes = new LinkedList<>();
  84. /** Modes waiting to be sent to the server. */
  85. private final Collection<String> modeQueue = new LinkedList<>();
  86. /** A Map to allow applications to attach misc data to this object. */
  87. private final Map<Object, Object> map;
  88. /** Queue of requested list modes. */
  89. private final Queue<Character> listModeQueue = new LinkedList<>();
  90. /** Listmode Queue Time. */
  91. private long listModeQueueTime = System.currentTimeMillis();
  92. /** Have we asked the server for the list modes for this channel yet? */
  93. private boolean askedForListModes;
  94. /** Has OnChannelGotListModes ever been called for this channel? */
  95. private boolean hasGotListModes;
  96. /**
  97. * Create a new channel object.
  98. *
  99. * @param parser Reference to parser that owns this channelclient (used for modes)
  100. * @param prefixModeManager The manager to use for prefix modes.
  101. * @param userModeManager Mode manager to use for user modes.
  102. * @param chanModeManager Mode manager to use for channel modes.
  103. * @param name Channel name.
  104. */
  105. public IRCChannelInfo(final IRCParser parser, final PrefixModeManager prefixModeManager,
  106. final ModeManager userModeManager, final ModeManager chanModeManager,
  107. final String name) {
  108. map = new HashMap<>();
  109. this.parser = parser;
  110. this.prefixModeManager = prefixModeManager;
  111. this.userModeManager = userModeManager;
  112. this.chanModeManager = chanModeManager;
  113. this.name = name;
  114. }
  115. /**
  116. * Get the listModeQueue.
  117. *
  118. * @return The listModeQueue
  119. */
  120. public Queue<Character> getListModeQueue() {
  121. Queue<Character> result = listModeQueue;
  122. final long now = System.currentTimeMillis();
  123. // Incase of breakage, if getListModeQueue() was last called greater than
  124. // 60 seconds ago, we reset the list.
  125. if (now - 30 * 1000 > listModeQueueTime) {
  126. result = new LinkedList<>();
  127. parser.callDebugInfo(IRCParser.DEBUG_LMQ, "Resetting LMQ");
  128. }
  129. listModeQueueTime = now;
  130. return result;
  131. }
  132. /**
  133. * Ask the server for all the list modes for this channel.
  134. */
  135. @Override
  136. public void requestListModes() {
  137. final IRCChannelClientInfo me = getChannelClient(parser.getLocalClient());
  138. if (me == null) {
  139. // In a normal situation of non bouncer-brokenness this won't happen
  140. return;
  141. }
  142. askedForListModes = true;
  143. final ServerType serverType = parser.getServerType();
  144. final boolean isOpped = me.isOpped();
  145. int modecount = 1;
  146. if (!ServerTypeGroup.SINGLE_LISTMODE.isMember(serverType) && parser.h005Info.containsKey("MODES")) {
  147. try {
  148. modecount = Integer.parseInt(parser.h005Info.get("MODES"));
  149. } catch (NumberFormatException e) {
  150. modecount = 1;
  151. }
  152. }
  153. // Support for potential future decent mode listing in the protocol
  154. //
  155. // See my proposal: http://shane.dmdirc.com/listmodes.php
  156. // Add listmode handler
  157. final boolean supportLISTMODE = parser.h005Info.containsKey("LISTMODE");
  158. String listmodes = "";
  159. int i = 0;
  160. for (Character cTemp : parser.chanModesOther.keySet()) {
  161. final int nTemp = parser.chanModesOther.get(cTemp);
  162. if (nTemp == IRCParser.MODE_LIST) {
  163. if (!isOpped && serverType.isOpOnly(cTemp)) {
  164. // IRCD doesn't allow non-ops to ask for these modes.
  165. continue;
  166. } else if (serverType == ServerType.STARCHAT && cTemp == 'H') {
  167. // IRCD Denies the mode exists
  168. continue;
  169. }
  170. i++;
  171. listmodes = listmodes + cTemp;
  172. if (i >= modecount && !supportLISTMODE) {
  173. parser.sendString("MODE " + getName() + " " + listmodes, QueuePriority.LOW);
  174. i = 0;
  175. listmodes = "";
  176. }
  177. }
  178. }
  179. if (i > 0) {
  180. if (supportLISTMODE) {
  181. parser.sendString("LISTMODE " + getName() + " " + listmodes, QueuePriority.LOW);
  182. } else {
  183. parser.sendString("MODE " + getName() + " " + listmodes, QueuePriority.LOW);
  184. }
  185. }
  186. }
  187. /**
  188. * Has this channel ever had a topic? (even an empty one!)
  189. *
  190. * @return True if a topic has ever been known for this channel.
  191. */
  192. public synchronized boolean hadTopic() {
  193. return hadTopic;
  194. }
  195. /**
  196. * Change the value of hadTopic to true.
  197. */
  198. public synchronized void setHadTopic() {
  199. this.hadTopic = true;
  200. }
  201. /**
  202. * Have we ever asked the server for this channels listmodes?
  203. *
  204. * @return True if requestListModes() has ever been used, else false
  205. */
  206. public boolean hasAskedForListModes() {
  207. return askedForListModes;
  208. }
  209. /**
  210. * Returns true if OnChannelGotListModes ever been called for this channel.
  211. *
  212. * @return True if OnChannelGotListModes ever been called for this channel.
  213. */
  214. public boolean hasGotListModes() {
  215. return hasGotListModes;
  216. }
  217. /**
  218. * Set if OnChannelGotListModes ever been called for this channel.
  219. *
  220. * @param newValue new value for if OnChannelGotListModes ever been called for this channel.
  221. */
  222. public void setHasGotListModes(final boolean newValue) {
  223. hasGotListModes = newValue;
  224. }
  225. @Override
  226. public Map<Object, Object> getMap() {
  227. return map;
  228. }
  229. /**
  230. * Set if we are getting a names request or not.
  231. *
  232. * @param newValue if false, any new names reply will cause current known channelclients to be removed.
  233. */
  234. public void setAddingNames(final boolean newValue) {
  235. addingNames = newValue;
  236. }
  237. /**
  238. * Get if we are getting a names request or not.
  239. *
  240. * @return if false, any new names reply will cause current known channelclients to be removed.
  241. */
  242. public boolean isAddingNames() {
  243. return addingNames;
  244. }
  245. @Override
  246. public String getName() {
  247. return name;
  248. }
  249. @Override
  250. public int getChannelClientCount() {
  251. return clients.size();
  252. }
  253. @Override
  254. public Collection<ChannelClientInfo> getChannelClients() {
  255. synchronized (clients) {
  256. return new ArrayList<>(clients.values());
  257. }
  258. }
  259. /**
  260. * Empty the channel (Remove all known channelclients).
  261. */
  262. public void emptyChannel() {
  263. IRCClientInfo cTemp;
  264. synchronized (clients) {
  265. for (IRCChannelClientInfo client : clients.values()) {
  266. cTemp = client.getClient();
  267. cTemp.delChannelClientInfo(client);
  268. if (cTemp != parser.getLocalClient() && !cTemp.checkVisibility()) {
  269. parser.removeClient(cTemp);
  270. }
  271. }
  272. }
  273. clients.clear();
  274. }
  275. @Override
  276. public IRCChannelClientInfo getChannelClient(final String client) {
  277. return getChannelClient(client, false);
  278. }
  279. @Override
  280. public IRCChannelClientInfo getChannelClient(final String client, final boolean create) {
  281. final String who = parser.getStringConverter().toLowerCase(IRCClientInfo.parseHost(client));
  282. if (clients.containsKey(who)) {
  283. return clients.get(who);
  284. }
  285. if (create) {
  286. return new IRCChannelClientInfo(parser, prefixModeManager,
  287. new IRCClientInfo(parser, userModeManager, client).setFake(true), this);
  288. } else {
  289. return null;
  290. }
  291. }
  292. @Override
  293. public IRCChannelClientInfo getChannelClient(final ClientInfo client) {
  294. synchronized (clients) {
  295. for (IRCChannelClientInfo target : clients.values()) {
  296. if (target.getClient() == client) {
  297. return target;
  298. }
  299. }
  300. }
  301. return null;
  302. }
  303. /**
  304. * Get the ChannelClientInfo object associated with a ClientInfo object.
  305. *
  306. * @param cClient Client object to be added to channel
  307. * @return ChannelClientInfo object added, or an existing object if already known on channel
  308. */
  309. public IRCChannelClientInfo addClient(final IRCClientInfo cClient) {
  310. IRCChannelClientInfo cTemp = getChannelClient(cClient);
  311. if (cTemp == null) {
  312. cTemp = new IRCChannelClientInfo(parser, prefixModeManager, cClient, this);
  313. clients.put(parser.getStringConverter().toLowerCase(cTemp.getClient().getNickname()), cTemp);
  314. }
  315. return cTemp;
  316. }
  317. /**
  318. * Remove ChannelClientInfo object associated with a ClientInfo object.
  319. *
  320. * @param cClient Client object to be removed from channel
  321. */
  322. public void delClient(final IRCClientInfo cClient) {
  323. final IRCChannelClientInfo cTemp = getChannelClient(cClient);
  324. if (cTemp != null) {
  325. final IRCClientInfo clTemp = cTemp.getClient();
  326. clTemp.delChannelClientInfo(cTemp);
  327. if (clTemp != parser.getLocalClient() && !clTemp.checkVisibility()) {
  328. parser.removeClient(clTemp);
  329. }
  330. clients.remove(parser.getStringConverter().toLowerCase(cTemp.getClient().getNickname()));
  331. }
  332. }
  333. /**
  334. * Rename a channelClient.
  335. *
  336. * @param oldNickname Nickname client used to be known as
  337. * @param cChannelClient ChannelClient object with updated client object
  338. */
  339. public void renameClient(final String oldNickname, final IRCChannelClientInfo cChannelClient) {
  340. if (clients.containsKey(oldNickname)) {
  341. final IRCChannelClientInfo cTemp = clients.get(oldNickname);
  342. if (cTemp == cChannelClient) {
  343. // Remove the old key
  344. clients.remove(oldNickname);
  345. // Add with the new key. (getNickname will return the new name not the
  346. // old one)
  347. clients.put(parser.getStringConverter().toLowerCase(cTemp.getClient().getNickname()), cTemp);
  348. }
  349. }
  350. }
  351. /**
  352. * Set the create time.
  353. *
  354. * @param nNewTime New unixtimestamp time for the channel creation (Seconds since epoch, not milliseconds)
  355. */
  356. public void setCreateTime(final long nNewTime) {
  357. creationTime = nNewTime;
  358. }
  359. /**
  360. * Get the Create time.
  361. *
  362. * @return Unixtimestamp time for the channel creation (Seconds since epoch, not milliseconds)
  363. */
  364. public long getCreateTime() {
  365. return creationTime;
  366. }
  367. /**
  368. * Set the topic time.
  369. *
  370. * @param nNewTime New unixtimestamp time for the topic (Seconds since epoch, not milliseconds)
  371. */
  372. public void setTopicTime(final long nNewTime) {
  373. topicTime = nNewTime;
  374. }
  375. @Override
  376. public long getTopicTime() {
  377. return topicTime;
  378. }
  379. /**
  380. * Set the topic.
  381. *
  382. * @param sNewTopic New contents of topic
  383. */
  384. public void setInternalTopic(final String sNewTopic) {
  385. topic = sNewTopic;
  386. }
  387. @Override
  388. public String getTopic() {
  389. return topic;
  390. }
  391. /**
  392. * Set the topic creator.
  393. *
  394. * @param sNewUser New user who set the topic (nickname if gotten on connect, full host if seen by parser)
  395. */
  396. public void setTopicUser(final String sNewUser) {
  397. topicUser = sNewUser;
  398. }
  399. @Override
  400. public String getTopicSetter() {
  401. return topicUser;
  402. }
  403. @Override
  404. public String getPassword() {
  405. return password;
  406. }
  407. /**
  408. * Set the internal value of the channel password,
  409. *
  410. * @param newValue New Value to set
  411. */
  412. public void setInternalPassword(final String newValue) {
  413. password = newValue;
  414. }
  415. /**
  416. * Set the channel modes.
  417. *
  418. * @param nNewMode new boolean channel modes
  419. */
  420. public void setMode(final String nNewMode) {
  421. modes = nNewMode;
  422. }
  423. /**
  424. * Get the channel modes.
  425. *
  426. * @return the boolean channel modes.
  427. */
  428. public String getMode() {
  429. return modes;
  430. }
  431. @Override
  432. public String getModes() {
  433. final StringBuilder sModes = new StringBuilder("+");
  434. final StringBuilder sModeParams = new StringBuilder();
  435. sModes.append(modes);
  436. for (char cTemp : paramModes.keySet()) {
  437. final String sTemp = paramModes.get(cTemp);
  438. if (!sTemp.isEmpty()) {
  439. sModes.append(cTemp);
  440. sModeParams.append(' ').append(this.getMode(cTemp));
  441. }
  442. }
  443. return sModes.append(sModeParams).toString();
  444. }
  445. /**
  446. * Set a channel mode that requires a parameter.
  447. *
  448. * @param cMode Character representing mode
  449. * @param sValue String repreenting value (if "" mode is unset)
  450. */
  451. public void setModeParam(final Character cMode, final String sValue) {
  452. if (sValue.isEmpty()) {
  453. if (paramModes.containsKey(cMode)) {
  454. paramModes.remove(cMode);
  455. }
  456. } else {
  457. paramModes.put(cMode, sValue);
  458. if (cMode == 'k') {
  459. if (sValue.equalsIgnoreCase("*") && !getPassword().equalsIgnoreCase("*")) {
  460. // Don't overwrite a guessed password with a hidden one.
  461. return;
  462. }
  463. setInternalPassword(sValue);
  464. }
  465. }
  466. }
  467. @Override
  468. public String getMode(final char mode) {
  469. if (paramModes.containsKey(mode)) {
  470. return paramModes.get(mode);
  471. }
  472. return "";
  473. }
  474. /**
  475. * Add/Remove a value to a channel list.
  476. *
  477. * @param givenMode Character representing mode
  478. * @param givenItem ChannelListModeItem representing the item
  479. * @param bAdd Add or remove the value. (true for add, false for remove)
  480. */
  481. public void setListModeParam(final Character givenMode, final ChannelListModeItem givenItem,
  482. final boolean bAdd) {
  483. Character cMode = givenMode;
  484. ChannelListModeItem newItem = givenItem;
  485. if (!parser.chanModesOther.containsKey(cMode) || parser.chanModesOther.get(cMode) != IRCParser.MODE_LIST) {
  486. return;
  487. }
  488. // Hyperion sucks.
  489. if (cMode == 'b' || cMode == 'q') {
  490. final ServerType serverType = parser.getServerType();
  491. if (ServerTypeGroup.FREENODE.isMember(serverType)) {
  492. if (cMode == 'b' && givenItem.getItem().charAt(0) == '%') {
  493. cMode = 'q';
  494. } else if (cMode == 'q' && givenItem.getItem().charAt(0) != '%') {
  495. cMode = 'b';
  496. }
  497. if (givenItem.getItem().charAt(0) == '%') {
  498. newItem = new ChannelListModeItem(givenItem.getItem().substring(1), givenItem.getOwner(), givenItem.getTime());
  499. }
  500. }
  501. }
  502. if (!listModes.containsKey(cMode)) {
  503. listModes.put(cMode, new ArrayList<>());
  504. }
  505. final List<ChannelListModeItem> lModes = listModes.get(cMode);
  506. for (int i = 0; i < lModes.size(); i++) {
  507. if (parser.getStringConverter().equalsIgnoreCase(lModes.get(i).getItem(), newItem.getItem())) {
  508. if (bAdd) {
  509. return;
  510. } else {
  511. lModes.remove(i);
  512. break;
  513. }
  514. }
  515. }
  516. if (bAdd) {
  517. lModes.add(newItem);
  518. }
  519. }
  520. @Override
  521. public Collection<ChannelListModeItem> getListMode(final char mode) {
  522. if (!parser.chanModesOther.containsKey(mode) || parser.chanModesOther.get(mode) != IRCParser.MODE_LIST) {
  523. return null;
  524. }
  525. if (!listModes.containsKey(mode)) {
  526. listModes.put(mode, new ArrayList<>());
  527. }
  528. return listModes.get(mode);
  529. }
  530. /**
  531. * Get the "adding state" of a list mode.
  532. *
  533. * @param cMode Character representing mode
  534. * @return false if we are not expecting a 367 etc, else true.
  535. */
  536. public boolean getAddState(final Character cMode) {
  537. synchronized (addingModes) {
  538. return addingModes.contains(cMode);
  539. }
  540. }
  541. /**
  542. * Get the "adding state" of a list mode.
  543. *
  544. * @param cMode Character representing mode
  545. * @param newState change the value returned by getAddState
  546. */
  547. public void setAddState(final Character cMode, final boolean newState) {
  548. synchronized (addingModes) {
  549. if (newState) {
  550. addingModes.add(cMode);
  551. } else {
  552. if (addingModes.contains(cMode)) {
  553. addingModes.remove(cMode);
  554. }
  555. }
  556. }
  557. }
  558. /**
  559. * Reset the "adding state" of *all* list modes.
  560. */
  561. public void resetAddState() {
  562. synchronized (addingModes) {
  563. addingModes.clear();
  564. }
  565. }
  566. @Override
  567. public void alterMode(final boolean add, final Character mode, final String parameter) {
  568. int modecount = 1;
  569. final int modeint;
  570. String modestr;
  571. if (parser.h005Info.containsKey("MODES")) {
  572. try {
  573. modecount = Integer.parseInt(parser.h005Info.get("MODES"));
  574. } catch (NumberFormatException e) {
  575. if (parser.getServerType() == ServerType.OTHERNET) {
  576. modecount = 6;
  577. } else {
  578. modecount = 1;
  579. }
  580. }
  581. }
  582. if (!parser.isUserSettable(mode)) {
  583. return;
  584. }
  585. modestr = (add ? "+" : "-") + mode;
  586. if (chanModeManager.isMode(mode)) {
  587. final String teststr = (add ? "-" : "+") + mode;
  588. if (modeQueue.contains(teststr)) {
  589. modeQueue.remove(teststr);
  590. return;
  591. } else if (modeQueue.contains(modestr)) {
  592. return;
  593. }
  594. } else {
  595. // May need a param
  596. if (prefixModeManager.isPrefixMode(mode)) {
  597. modestr = modestr + ' ' + parameter;
  598. } else if (parser.chanModesOther.containsKey(mode)) {
  599. modeint = parser.chanModesOther.get(mode);
  600. if ((modeint & IRCParser.MODE_LIST) == IRCParser.MODE_LIST) {
  601. modestr = modestr + " " + parameter;
  602. } else if (!add && (modeint & IRCParser.MODE_UNSET) == IRCParser.MODE_UNSET) {
  603. modestr = modestr + " " + parameter;
  604. } else if (add && (modeint & IRCParser.MODE_SET) == IRCParser.MODE_SET) {
  605. // Does mode require a param to unset aswell?
  606. // We might need to queue an unset first
  607. if ((modeint & IRCParser.MODE_UNSET) == IRCParser.MODE_UNSET) {
  608. final String existingParam = getMode(mode);
  609. if (!existingParam.isEmpty()) {
  610. final String reverseModeStr = "-" + mode + " " + existingParam;
  611. parser.callDebugInfo(IRCParser.DEBUG_INFO, "Queueing mode: %s", reverseModeStr);
  612. modeQueue.add(reverseModeStr);
  613. if (modeQueue.size() == modecount) {
  614. flushModes();
  615. }
  616. }
  617. }
  618. modestr = modestr + " " + parameter;
  619. }
  620. } else {
  621. parser.callErrorInfo(new ParserError(ParserError.ERROR_WARNING, "Trying to alter unknown mode. positive: '" + add + "' | mode: '" + mode + "' | parameter: '" + parameter + "' ", ""));
  622. }
  623. }
  624. parser.callDebugInfo(IRCParser.DEBUG_INFO, "Queueing mode: %s", modestr);
  625. modeQueue.add(modestr);
  626. if (modeQueue.size() == modecount) {
  627. flushModes();
  628. }
  629. }
  630. @Override
  631. public void flushModes() {
  632. if (modeQueue.isEmpty()) {
  633. return;
  634. }
  635. final StringBuilder positivemode = new StringBuilder();
  636. final StringBuilder positiveparam = new StringBuilder();
  637. final StringBuilder negativemode = new StringBuilder();
  638. final StringBuilder negativeparam = new StringBuilder();
  639. final StringBuilder sendModeStr = new StringBuilder();
  640. String modestr;
  641. String[] modeparam;
  642. boolean positive;
  643. for (String aModeQueue : modeQueue) {
  644. modeparam = aModeQueue.split(" ");
  645. modestr = modeparam[0];
  646. positive = modestr.charAt(0) == '+';
  647. if (positive) {
  648. positivemode.append(modestr.charAt(1));
  649. if (modeparam.length > 1) {
  650. positiveparam.append(' ').append(modeparam[1]);
  651. }
  652. } else {
  653. negativemode.append(modestr.charAt(1));
  654. if (modeparam.length > 1) {
  655. negativeparam.append(' ').append(modeparam[1]);
  656. }
  657. }
  658. }
  659. if (negativemode.length() > 0) {
  660. sendModeStr.append('-').append(negativemode);
  661. }
  662. if (positivemode.length() > 0) {
  663. sendModeStr.append('+').append(positivemode);
  664. }
  665. if (negativeparam.length() > 0) {
  666. sendModeStr.append(negativeparam);
  667. }
  668. if (positiveparam.length() > 0) {
  669. sendModeStr.append(positiveparam);
  670. }
  671. parser.callDebugInfo(IRCParser.DEBUG_INFO, "Sending mode: %s", sendModeStr.toString());
  672. parser.sendRawMessage("MODE " + name + ' ' + sendModeStr);
  673. clearModeQueue();
  674. }
  675. /**
  676. * This function will clear the mode queue (WITHOUT Sending).
  677. */
  678. public void clearModeQueue() {
  679. modeQueue.clear();
  680. }
  681. @Override
  682. public void sendMessage(final String message) {
  683. if (message.isEmpty()) {
  684. return;
  685. }
  686. parser.sendString("PRIVMSG " + name, message);
  687. }
  688. /**
  689. * Send a notice message to a target.
  690. *
  691. * @param sMessage Message to send
  692. */
  693. public void sendNotice(final String sMessage) {
  694. if (sMessage.isEmpty()) {
  695. return;
  696. }
  697. parser.sendString("NOTICE " + name, sMessage);
  698. }
  699. @Override
  700. public void sendAction(final String action) {
  701. if (action.isEmpty()) {
  702. return;
  703. }
  704. sendCTCP("ACTION", action);
  705. }
  706. /**
  707. * Send a CTCP to a target.
  708. *
  709. * @param sType Type of CTCP
  710. * @param sMessage Optional Additional Parameters
  711. */
  712. public void sendCTCP(final String sType, final String sMessage) {
  713. if (sType.isEmpty()) {
  714. return;
  715. }
  716. final char char1 = (char) 1;
  717. if (sMessage.isEmpty()) {
  718. sendMessage(char1 + sType.toUpperCase() + sMessage + char1);
  719. } else {
  720. sendMessage(char1 + sType.toUpperCase() + ' ' + sMessage + char1);
  721. }
  722. }
  723. /**
  724. * Send a CTCPReply to a target.
  725. *
  726. * @param sType Type of CTCP
  727. * @param sMessage Optional Additional Parameters
  728. */
  729. public void sendCTCPReply(final String sType, final String sMessage) {
  730. if (sType.isEmpty()) {
  731. return;
  732. }
  733. final char char1 = (char) 1;
  734. if (sMessage.isEmpty()) {
  735. sendNotice(char1 + sType.toUpperCase() + sMessage + char1);
  736. } else {
  737. sendNotice(char1 + sType.toUpperCase() + ' ' + sMessage + char1);
  738. }
  739. }
  740. /**
  741. * Get a string representation of the Channel.
  742. *
  743. * @return String representation of the Channel.
  744. */
  745. @Override
  746. public String toString() {
  747. return name;
  748. }
  749. @Override
  750. public Parser getParser() {
  751. return parser;
  752. }
  753. @Override
  754. public void part(final String reason) {
  755. parser.partChannel(name, reason);
  756. }
  757. @Override
  758. public void setTopic(final String topic) {
  759. parser.sendRawMessage("TOPIC " + name + " :" + topic);
  760. }
  761. @Override
  762. public void sendWho() {
  763. parser.sendRawMessage("WHO " + name);
  764. }
  765. }