Java IRC bot
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ProcessListModes.java 8.3KB

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