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.

ProcessListModes.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.common.ParserError;
  25. import com.dmdirc.parser.interfaces.ChannelInfo;
  26. import com.dmdirc.parser.interfaces.callbacks.ChannelListModeListener;
  27. import java.util.Collection;
  28. import java.util.LinkedList;
  29. import java.util.Queue;
  30. /**
  31. * Process a List Modes.
  32. */
  33. public class ProcessListModes extends IRCProcessor {
  34. /**
  35. * Process a ListModes.
  36. *
  37. * @param sParam Type of line to process
  38. * @param token IRCTokenised line to process
  39. */
  40. @SuppressWarnings("unchecked")
  41. @Override
  42. public void process(String sParam, String[] token) {
  43. IRCChannelInfo channel = getChannel(token[3]);
  44. String thisIRCD = myParser.getIRCD(true).toLowerCase();
  45. String item = "";
  46. String owner = "";
  47. byte tokenStart = 4; // Where do the relevent tokens start?
  48. boolean isCleverMode = false;
  49. long time = 0;
  50. char mode = 'b';
  51. boolean isItem = true; // true if item listing, false if "end of .." item
  52. if (channel == null) { return; }
  53. if (sParam.equals("367") || sParam.equals("368")) {
  54. // Ban List/Item.
  55. // (Also used for +d and +q on hyperion... -_-)
  56. mode = 'b';
  57. isItem = sParam.equals("367");
  58. } else if (sParam.equals("348") || sParam.equals("349")) {
  59. // Except / Exempt List etc
  60. mode = 'e';
  61. isItem = sParam.equals("348");
  62. } else if (sParam.equals("346") || sParam.equals("347")) {
  63. // Invite List
  64. mode = 'I';
  65. isItem = sParam.equals("346");
  66. } else if (sParam.equals("940") || sParam.equals("941")) {
  67. // Censored words List
  68. mode = 'g';
  69. isItem = sParam.equals("941");
  70. } else if (sParam.equals("344") || sParam.equals("345")) {
  71. // Reop List, or bad words list, or quiet list. god damn.
  72. if (thisIRCD.equals("euircd")) {
  73. mode = 'w';
  74. } else if (thisIRCD.equals("oftc-hybrid")) {
  75. mode = 'q';
  76. } else {
  77. mode = 'R';
  78. }
  79. isItem = sParam.equals("344");
  80. } else if (thisIRCD.equals("swiftirc") && (sParam.equals("386") || sParam.equals("387"))) {
  81. // Channel Owner list
  82. mode = 'q';
  83. isItem = sParam.equals("387");
  84. } else if (thisIRCD.equals("swiftirc") && (sParam.equals("388") || sParam.equals("389"))) {
  85. // Protected User list
  86. mode = 'a';
  87. isItem = sParam.equals("389");
  88. } else if (sParam.equals(myParser.h005Info.get("LISTMODE")) || sParam.equals(myParser.h005Info.get("LISTMODEEND"))) {
  89. // Support for potential future decent mode listing in the protocol
  90. //
  91. // See my proposal: http://shane.dmdirc.com/listmodes.php
  92. mode = token[4].charAt(0);
  93. isItem = sParam.equals(myParser.h005Info.get("LISTMODE"));
  94. tokenStart = 5;
  95. isCleverMode = true;
  96. }
  97. final Queue<Character> listModeQueue = channel.getListModeQueue();
  98. if (!isCleverMode && listModeQueue != null) {
  99. if (sParam.equals("482")) {
  100. myParser.callDebugInfo(IRCParser.DEBUG_LMQ, "Dropped LMQ mode "+listModeQueue.poll());
  101. return;
  102. } else {
  103. if (listModeQueue.peek() != null) {
  104. Character oldMode = mode;
  105. mode = listModeQueue.peek();
  106. myParser.callDebugInfo(IRCParser.DEBUG_LMQ, "LMQ says this is "+mode);
  107. boolean error = true;
  108. // b or q are given in the same list, d is separate.
  109. // b and q remove each other from the LMQ.
  110. //
  111. // Only raise an LMQ error if the lmqmode isn't one of bdq if the
  112. // guess is one of bdq
  113. if ((thisIRCD.equals("hyperion") || thisIRCD.equals("dancer")) && (mode == 'b' || mode == 'q' || mode == 'd')) {
  114. LinkedList<Character> lmq = (LinkedList<Character>)listModeQueue;
  115. if (mode == 'b') {
  116. error = !(oldMode == 'q' || oldMode == 'd');
  117. lmq.remove((Character)'q');
  118. myParser.callDebugInfo(IRCParser.DEBUG_LMQ, "Dropping q from list");
  119. } else if (mode == 'q') {
  120. error = !(oldMode == 'b' || oldMode == 'd');
  121. lmq.remove((Character)'b');
  122. myParser.callDebugInfo(IRCParser.DEBUG_LMQ, "Dropping b from list");
  123. } else if (mode == 'd') {
  124. error = !(oldMode == 'b' || oldMode == 'q');
  125. }
  126. }
  127. if (oldMode != mode && error) {
  128. myParser.callDebugInfo(IRCParser.DEBUG_LMQ, "LMQ disagrees with guess. LMQ: "+mode+" Guess: "+oldMode);
  129. // myParser.callErrorInfo(new ParserError(ParserError.ERROR_WARNING, "LMQ disagrees with guess. LMQ: "+mode+" Guess: "+oldMode, myParser.getLastLine()));
  130. }
  131. if (!isItem) {
  132. listModeQueue.poll();
  133. }
  134. }
  135. }
  136. }
  137. if (isItem) {
  138. if ((!isCleverMode) && listModeQueue == null && (thisIRCD.equals("hyperion") || thisIRCD.equals("dancer")) && token.length > 4 && mode == 'b') {
  139. // Assume mode is a 'd' mode
  140. mode = 'd';
  141. // Now work out if its not (or attempt to.)
  142. int identstart = token[tokenStart].indexOf('!');
  143. int hoststart = token[tokenStart].indexOf('@');
  144. // Check that ! and @ are both in the string - as required by +b and +q
  145. if ((identstart >= 0) && (identstart < hoststart)) {
  146. if (thisIRCD.equals("hyperion") && token[tokenStart].charAt(0) == '%') { mode = 'q'; }
  147. else { mode = 'b'; }
  148. }
  149. } // End Hyperian stupidness of using the same numeric for 3 different things..
  150. if (!channel.getAddState(mode)) {
  151. callDebugInfo(IRCParser.DEBUG_INFO, "New List Mode Batch ("+mode+"): Clearing!");
  152. final Collection<ChannelListModeItem> list = channel.getListMode(mode);
  153. if (list == null) {
  154. myParser.callErrorInfo(new ParserError(ParserError.ERROR_WARNING, "Got list mode: '"+mode+"' - but channel object doesn't agree.", myParser.getLastLine()));
  155. } else {
  156. list.clear();
  157. if ((thisIRCD.equals("hyperion") || thisIRCD.equals("dancer")) && (mode == 'b' || mode == 'q')) {
  158. // Also clear the other list if b or q.
  159. final Character otherMode = (mode == 'b') ? 'q' : 'b';
  160. if (!channel.getAddState(otherMode)) {
  161. callDebugInfo(IRCParser.DEBUG_INFO, "New List Mode Batch ("+mode+"): Clearing!");
  162. final Collection<ChannelListModeItem> otherList = channel.getListMode(otherMode);
  163. if (otherList == null) {
  164. myParser.callErrorInfo(new ParserError(ParserError.ERROR_WARNING, "Got list mode: '"+otherMode+"' - but channel object doesn't agree.", myParser.getLastLine()));
  165. } else {
  166. otherList.clear();
  167. }
  168. }
  169. }
  170. }
  171. channel.setAddState(mode, true);
  172. }
  173. if (token.length > (tokenStart+2)) {
  174. try { time = Long.parseLong(token[tokenStart+2]); } catch (NumberFormatException e) { time = 0; }
  175. }
  176. if (token.length > (tokenStart+1)) { owner = token[tokenStart+1]; }
  177. if (token.length > tokenStart) { item = token[tokenStart]; }
  178. if (!item.isEmpty()) {
  179. ChannelListModeItem clmi = new ChannelListModeItem(item, owner, time);
  180. callDebugInfo(IRCParser.DEBUG_INFO, "List Mode: %c [%s/%s/%d]",mode, item, owner, time);
  181. channel.setListModeParam(mode, clmi, true);
  182. }
  183. } else {
  184. callDebugInfo(IRCParser.DEBUG_INFO, "List Mode Batch over");
  185. channel.resetAddState();
  186. if (isCleverMode || listModeQueue == null || ((LinkedList<Character>)listModeQueue).size() == 0) {
  187. callDebugInfo(IRCParser.DEBUG_INFO, "Calling GotListModes");
  188. channel.setHasGotListModes(true);
  189. callChannelGotListModes(channel);
  190. }
  191. }
  192. }
  193. /**
  194. * What does this IRCProcessor handle.
  195. *
  196. * @return String[] with the names of the tokens we handle.
  197. */
  198. @Override
  199. public String[] handles() {
  200. return new String[]{"367", "368", /* Bans */
  201. "344", "345", /* Reop list (ircnet) or bad words (euirc) */
  202. "346", "347", /* Invite List */
  203. "348", "349", /* Except/Exempt List */
  204. "386", "387", /* Channel Owner List (swiftirc ) */
  205. "388", "389", /* Protected User List (swiftirc) */
  206. "940", "941", /* Censored words list */
  207. "482", /* Permission Denied */
  208. "__LISTMODE__" /* Sensible List Modes */
  209. };
  210. }
  211. /**
  212. * Callback to all objects implementing the ChannelGotListModes Callback.
  213. *
  214. * @see IChannelGotListModes
  215. * @param cChannel Channel which the ListModes reply is for
  216. * @return true if a method was called, false otherwise
  217. */
  218. protected boolean callChannelGotListModes(ChannelInfo cChannel) {
  219. return getCallbackManager().getCallbackType(ChannelListModeListener.class).call(cChannel);
  220. }
  221. /**
  222. * Create a new instance of the IRCProcessor Object.
  223. *
  224. * @param parser IRCParser That owns this IRCProcessor
  225. * @param manager ProcessingManager that is in charge of this IRCProcessor
  226. */
  227. protected ProcessListModes (IRCParser parser, ProcessingManager manager) { super(parser, manager); }
  228. }