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

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