Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Process004005.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2006-2012 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;
  23. import com.dmdirc.parser.common.ParserError;
  24. import com.dmdirc.parser.common.QueuePriority;
  25. import com.dmdirc.parser.interfaces.callbacks.NetworkDetectedListener;
  26. import java.util.regex.Matcher;
  27. import java.util.regex.Pattern;
  28. /**
  29. * Process ISUPPORT lines.
  30. */
  31. public class Process004005 extends IRCProcessor {
  32. /**
  33. * Create a new instance of the IRCProcessor Object.
  34. *
  35. * @param parser IRCParser That owns this IRCProcessor
  36. * @param manager ProcessingManager that is in charge of this IRCProcessor
  37. */
  38. protected Process004005(final IRCParser parser, final ProcessingManager manager) {
  39. super(parser, manager);
  40. }
  41. /**
  42. * Process ISUPPORT lines.
  43. *
  44. * @param sParam Type of line to process ("005", "004")
  45. * @param token IRCTokenised line to process
  46. */
  47. @Override
  48. public void process(final String sParam, final String[] token) {
  49. if ("002".equals(sParam)) {
  50. final Pattern pattern = Pattern.compile("running(?: version)? (.*)$");
  51. final Matcher matcher = pattern.matcher(parser.getLastLine());
  52. if (matcher.find()) {
  53. parser.h005Info.put("002IRCD", matcher.group(1));
  54. }
  55. } else if ("003".equals(sParam)) {
  56. parser.h005Info.put("003IRCD", token[token.length - 1]);
  57. } else if ("004".equals(sParam)) {
  58. // 004
  59. final boolean multiParam = token.length > 4;
  60. int i = multiParam ? 4 : 1;
  61. final String[] bits = multiParam ? token : token[3].split(" ");
  62. parser.h005Info.put("004IRCD", bits[i++]);
  63. if (bits[i].matches("^\\d+$")) {
  64. // some IRCDs put a timestamp where the usermodes should be
  65. // (issues 4140. 4181 and 4183) so check to see if this is
  66. // numeric only, and if so, skip it.
  67. i++;
  68. }
  69. parser.h005Info.put("USERMODES", bits[i++]);
  70. parser.h005Info.put("USERCHANMODES", bits[i++]);
  71. if (bits.length > i) {
  72. // INSPIRCD includes an extra param
  73. parser.h005Info.put("USERCHANPARAMMODES", bits[i++]);
  74. }
  75. parser.parseUserModes();
  76. } else if ("005".equals(sParam)) {
  77. for (int i = 3; i < token.length; i++) {
  78. final String[] bits = token[i].split("=", 2);
  79. if (bits[0].isEmpty()) {
  80. continue;
  81. }
  82. final boolean isNegation = bits[0].charAt(0) == '-';
  83. final String key = (isNegation ? bits[0].substring(1) : bits[0]).toUpperCase();
  84. final String value = bits.length == 2 ? bits[1] : "";
  85. callDebugInfo(IRCParser.DEBUG_INFO, "%s => %s", key, value);
  86. if (isNegation) {
  87. parser.h005Info.remove(key);
  88. } else {
  89. parser.h005Info.put(key, value);
  90. }
  91. if ("NETWORK".equals(key) && !isNegation) {
  92. parser.networkName = value;
  93. callGotNetwork();
  94. } else if ("CASEMAPPING".equals(key) && !isNegation) {
  95. IRCEncoding encoding = IRCEncoding.RFC1459;
  96. try {
  97. encoding = IRCEncoding.valueOf(value.toUpperCase().replace('-', '_'));
  98. } catch (IllegalArgumentException ex) {
  99. parser.callErrorInfo(new ParserError(ParserError.ERROR_WARNING, "Unknown casemapping: '" + value + "' - assuming rfc1459", parser.getLastLine()));
  100. }
  101. final boolean encodingChanged = parser.getStringConverter().getEncoding() != encoding;
  102. parser.setEncoding(encoding);
  103. if (encodingChanged && parser.knownClients() == 1) {
  104. // This means that the casemapping is not rfc1459
  105. // We have only added ourselves so far (from 001)
  106. // We can fix the hashtable easily.
  107. parser.removeClient(parser.getLocalClient());
  108. parser.addClient(parser.getLocalClient());
  109. }
  110. } else if ("CHANTYPES".equals(key)) {
  111. parser.parseChanPrefix();
  112. } else if ("PREFIX".equals(key)) {
  113. parser.parsePrefixModes();
  114. } else if ("CHANMODES".equals(key)) {
  115. parser.parseChanModes();
  116. } else if ("NAMESX".equals(key) && parser.getCapabilityState("multi-prefix") != CapabilityState.ENABLED) {
  117. parser.sendString("PROTOCTL " + key);
  118. } else if ("UHNAMES".equals(key) && parser.getCapabilityState("userhost-in-names") != CapabilityState.ENABLED) {
  119. parser.sendString("PROTOCTL " + key);
  120. } else if ("LISTMODE".equals(key)) {
  121. // Support for potential future decent mode listing in the protocol
  122. //
  123. // See my proposal: http://shanemcc.co.uk/irc/#listmode
  124. // Add listmode handler
  125. String[] handles = new String[2];
  126. handles[0] = value; // List mode item
  127. final String endValue = Integer.toString(Integer.parseInt(value) + 1);
  128. parser.h005Info.put("LISTMODEEND", endValue);
  129. handles[1] = endValue; // List mode end
  130. // Add listmode handlers
  131. try {
  132. parser.getProcessingManager().addProcessor(handles, parser.getProcessingManager().getProcessor("__LISTMODE__"));
  133. } catch (ProcessorNotFoundException e) {
  134. }
  135. } else if ("TIMESTAMPEDIRC".equals(key) && parser.getCapabilityState("dfbnc.com/tsirc") != CapabilityState.ENABLED) {
  136. // Let the server know we also understand timestamped irc.
  137. // See my other proposal: http://shanemcc.co.uk/irc/#timestamping
  138. parser.sendString("TIMESTAMPEDIRC ON", QueuePriority.HIGH);
  139. }
  140. }
  141. }
  142. }
  143. /**
  144. * What does this IRCProcessor handle.
  145. *
  146. * @return String[] with the names of the tokens we handle.
  147. */
  148. @Override
  149. public String[] handles() {
  150. return new String[]{"002", "003", "004", "005"};
  151. }
  152. /**
  153. * Callback to all objects implementing the GotNetwork Callback.
  154. * This takes no params of its own, but works them out itself.
  155. *
  156. * @see IGotNetwork
  157. * @return true if a method was called, false otherwise
  158. */
  159. protected boolean callGotNetwork() {
  160. final String networkName = parser.networkName;
  161. final String ircdVersion = parser.getServerSoftware();
  162. final String ircdType = parser.getServerSoftwareType();
  163. return getCallbackManager().getCallbackType(NetworkDetectedListener.class).call(networkName, ircdVersion, ircdType);
  164. }
  165. }