Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ProcessListModes.java 13KB

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