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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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.processors;
  23. import com.dmdirc.parser.common.ChannelListModeItem;
  24. import com.dmdirc.parser.common.ParserError;
  25. import com.dmdirc.parser.events.ChannelListModeEvent;
  26. import com.dmdirc.parser.interfaces.ChannelInfo;
  27. import com.dmdirc.parser.irc.IRCChannelInfo;
  28. import com.dmdirc.parser.irc.IRCParser;
  29. import com.dmdirc.parser.irc.ServerType;
  30. import com.dmdirc.parser.irc.ServerTypeGroup;
  31. import java.util.Arrays;
  32. import java.util.Collection;
  33. import java.util.Date;
  34. import java.util.Queue;
  35. /**
  36. * Process a List Modes.
  37. */
  38. public class ProcessListModes extends IRCProcessor {
  39. /**
  40. * Create a new instance of the IRCProcessor Object.
  41. *
  42. * @param parser IRCParser That owns this IRCProcessor
  43. */
  44. public ProcessListModes(final IRCParser parser) {
  45. super(parser, "367", "368", /* Bans */
  46. "344", "345", /* Reop list (ircnet) or bad words (euirc) */
  47. "346", "347", /* Invite List */
  48. "348", "349", /* Except/Exempt List */
  49. "386", "387", /* Channel Owner List (swiftirc ) */
  50. "388", "389", /* Protected User List (swiftirc) */
  51. "940", "941", /* Censored words list */
  52. "910", "911", /* INSPIRCD Channel Access List. */
  53. "954", "953", /* INSPIRCD exemptchanops List. */
  54. "482", /* Permission Denied */
  55. "__LISTMODE__" /* Sensible List Modes */
  56. );
  57. }
  58. /**
  59. * Process a ListModes.
  60. *
  61. * @param sParam Type of line to process
  62. * @param token IRCTokenised line to process
  63. */
  64. @Override
  65. public void process(final String sParam, final String... token) {
  66. final IRCChannelInfo channel = getChannel(token[3]);
  67. final ServerType serverType = parser.getServerType();
  68. if (channel == null) {
  69. return;
  70. }
  71. boolean isItem = true; // true if item listing, false if "end of .." item
  72. char mode = ' ';
  73. boolean isCleverMode = false;
  74. byte tokenStart = 4; // Where do the relevent tokens start?
  75. if ("367".equals(sParam) || "368".equals(sParam)) {
  76. // Ban List/Item.
  77. // (Also used for +d and +q on dancer/hyperion... -_-)
  78. // (Also used for +q on ircd-seven... -_-)
  79. mode = 'b';
  80. isItem = "367".equals(sParam);
  81. } else if ("348".equals(sParam) || "349".equals(sParam)) {
  82. // Except / Exempt List etc
  83. mode = 'e';
  84. isItem = "348".equals(sParam);
  85. } else if ("346".equals(sParam) || "347".equals(sParam)) {
  86. // Invite List
  87. mode = 'I';
  88. isItem = "346".equals(sParam);
  89. } else if ("940".equals(sParam) || "941".equals(sParam)) {
  90. // Censored words List
  91. mode = 'g';
  92. isItem = "941".equals(sParam);
  93. } else if (serverType == ServerType.INSPIRCD && ("910".equals(sParam) ||
  94. "911".equals(sParam))) {
  95. // Channel Access List
  96. mode = 'w';
  97. isItem = "910".equals(sParam);
  98. } else if (serverType == ServerType.INSPIRCD && ("954".equals(sParam) ||
  99. "953".equals(sParam))) {
  100. // Channel exemptchanops List
  101. mode = 'X';
  102. isItem = "954".equals(sParam);
  103. } else if ("344".equals(sParam) || "345".equals(sParam)) {
  104. // Reop List, or bad words list, or quiet list. god damn.
  105. if (serverType == ServerType.EUIRCD) {
  106. mode = 'w';
  107. } else if (serverType == ServerType.OFTC_HYBRID) {
  108. mode = 'q';
  109. } else {
  110. mode = 'R';
  111. }
  112. isItem = "344".equals(sParam);
  113. } else if (ServerTypeGroup.OWNER_386.isMember(serverType) && ("386".equals(sParam) ||
  114. "387".equals(sParam))) {
  115. // Channel Owner list
  116. mode = 'q';
  117. isItem = "387".equals(sParam);
  118. } else if (ServerTypeGroup.PROTECTED_388.isMember(serverType) && ("388".equals(sParam) ||
  119. "389".equals(sParam))) {
  120. // Protected User list
  121. mode = 'a';
  122. isItem = "389".equals(sParam);
  123. } else if (sParam.equals(parser.h005Info.get("LISTMODE")) || sParam.equals(parser.h005Info.get("LISTMODEEND"))) {
  124. // Support for potential future decent mode listing in the protocol
  125. //
  126. // See my proposal: http://shane.dmdirc.com/listmodes.php
  127. mode = token[4].charAt(0);
  128. isItem = sParam.equals(parser.h005Info.get("LISTMODE"));
  129. tokenStart = 5;
  130. isCleverMode = true;
  131. }
  132. // Unknown mode.
  133. if (mode == ' ') {
  134. parser.callDebugInfo(IRCParser.DEBUG_LMQ, "Unknown mode line: " + Arrays.toString(token));
  135. return;
  136. }
  137. final Queue<Character> listModeQueue = channel.getListModeQueue();
  138. if (!isCleverMode && listModeQueue != null) {
  139. if ("482".equals(sParam)) {
  140. parser.callDebugInfo(IRCParser.DEBUG_LMQ, "Dropped LMQ mode " + listModeQueue.poll());
  141. return;
  142. } else {
  143. if (listModeQueue.peek() != null) {
  144. final Character oldMode = mode;
  145. mode = listModeQueue.peek();
  146. parser.callDebugInfo(IRCParser.DEBUG_LMQ, "LMQ says this is " + mode);
  147. boolean error = true;
  148. // b or q are given in the same list, d is separate.
  149. // b and q remove each other from the LMQ.
  150. //
  151. // Only raise an LMQ error if the lmqmode isn't one of bdq if the
  152. // guess is one of bdq
  153. if (ServerTypeGroup.FREENODE.isMember(serverType) && (mode == 'b' || mode == 'q' || mode == 'd')) {
  154. if (mode == 'b') {
  155. error = oldMode != 'q' && oldMode != 'd';
  156. listModeQueue.remove('q');
  157. parser.callDebugInfo(IRCParser.DEBUG_LMQ, "Dropping q from list");
  158. } else if (mode == 'q') {
  159. error = oldMode != 'b' && oldMode != 'd';
  160. listModeQueue.remove('b');
  161. parser.callDebugInfo(IRCParser.DEBUG_LMQ, "Dropping b from list");
  162. } else if (mode == 'd') {
  163. error = oldMode != 'b' && oldMode != 'q';
  164. }
  165. } else if (ServerTypeGroup.CHARYBDIS.isMember(serverType) && (mode == 'b' || mode == 'q')) {
  166. // Finally freenode appear to have an ircd which isn't completely annoying.
  167. // Only error if the LMQ thinks the mode should be
  168. // something thats not the other one of these 2 modes.
  169. error = oldMode != (mode == 'b' ? 'q' : 'b');
  170. }
  171. // If the lmq and the actual mode are not the same or the
  172. // freenode-specific hacks above think the mode should be
  173. // something else, error.
  174. if (oldMode != mode && error) {
  175. parser.callDebugInfo(IRCParser.DEBUG_LMQ, "LMQ disagrees with guess. LMQ: " + mode + " Guess: " + oldMode);
  176. }
  177. if (!isItem) {
  178. listModeQueue.poll();
  179. }
  180. }
  181. }
  182. }
  183. if (isItem) {
  184. if (!isCleverMode && listModeQueue == null && ServerTypeGroup.FREENODE.isMember(serverType) && token.length > 4 && mode == 'b') {
  185. // Assume mode is a 'd' mode
  186. mode = 'd';
  187. // Now work out if its not (or attempt to.)
  188. final int identstart = token[tokenStart].indexOf('!');
  189. final int hoststart = token[tokenStart].indexOf('@');
  190. // Check that ! and @ are both in the string - as required by +b and +q
  191. if (identstart >= 0 && identstart < hoststart) {
  192. if (serverType == ServerType.HYPERION && token[tokenStart].charAt(0) == '%') {
  193. mode = 'q';
  194. } else {
  195. mode = 'b';
  196. }
  197. }
  198. } // End Hyperian stupidness of using the same numeric for 3 different things..
  199. if (!channel.getAddState(mode)) {
  200. callDebugInfo(IRCParser.DEBUG_INFO, "New List Mode Batch (" + mode + "): Clearing!");
  201. final Collection<ChannelListModeItem> list = channel.getListMode(mode);
  202. if (list == null) {
  203. parser.callErrorInfo(new ParserError(ParserError.ERROR_WARNING, "Got list mode: '" + mode + "' - but channel object doesn't agree.", parser.getLastLine()));
  204. } else {
  205. list.clear();
  206. if (ServerTypeGroup.FREENODE.isMember(serverType) && (mode == 'b' || mode == 'q')) {
  207. // Also clear the other list if b or q.
  208. final Character otherMode = mode == 'b' ? 'q' : 'b';
  209. if (!channel.getAddState(otherMode)) {
  210. callDebugInfo(IRCParser.DEBUG_INFO, "New List Mode Batch (" + mode + "): Clearing!");
  211. final Collection<ChannelListModeItem> otherList = channel.getListMode(otherMode);
  212. if (otherList == null) {
  213. parser.callErrorInfo(new ParserError(ParserError.ERROR_WARNING, "Got list mode: '" + otherMode + "' - but channel object doesn't agree.", parser.getLastLine()));
  214. } else {
  215. otherList.clear();
  216. }
  217. }
  218. }
  219. }
  220. channel.setAddState(mode, true);
  221. }
  222. long time = 0;
  223. if (token.length > tokenStart + 2) {
  224. try {
  225. time = Long.parseLong(token[tokenStart + 2]);
  226. } catch (NumberFormatException e) {
  227. time = 0;
  228. }
  229. }
  230. String owner = "";
  231. if (token.length > tokenStart + 1) {
  232. owner = token[tokenStart + 1];
  233. }
  234. String item = "";
  235. if (token.length > tokenStart) {
  236. item = token[tokenStart];
  237. }
  238. if (!item.isEmpty()) {
  239. final ChannelListModeItem clmi = new ChannelListModeItem(item, owner, time);
  240. callDebugInfo(IRCParser.DEBUG_INFO, "List Mode: %c [%s/%s/%d]", mode, item, owner, time);
  241. channel.setListModeParam(mode, clmi, true);
  242. }
  243. } else {
  244. callDebugInfo(IRCParser.DEBUG_INFO, "List Mode Batch over");
  245. channel.resetAddState();
  246. if (isCleverMode || listModeQueue == null || listModeQueue.isEmpty()) {
  247. callDebugInfo(IRCParser.DEBUG_INFO, "Calling GotListModes");
  248. channel.setHasGotListModes(true);
  249. if (isCleverMode) {
  250. parser.chanModesOther.keySet()
  251. .stream()
  252. .filter(thisMode -> parser.chanModesOther
  253. .get(thisMode) == IRCParser.MODE_LIST)
  254. .forEach(thisMode -> callChannelGotListModes(channel, thisMode));
  255. } else {
  256. callChannelGotListModes(channel, mode);
  257. }
  258. }
  259. }
  260. }
  261. /**
  262. * Callback to all objects implementing the ChannelGotListModes Callback.
  263. *
  264. * @param cChannel Channel which the ListModes reply is for
  265. * @param mode the mode that we got list modes for.
  266. */
  267. protected void callChannelGotListModes(final ChannelInfo cChannel, final char mode) {
  268. getCallbackManager().publish(new ChannelListModeEvent(parser, new Date(), cChannel, mode));
  269. }
  270. }