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.

Process004005.java 7.5KB

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