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

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