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

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