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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. final boolean isNegation = (Bits[0].charAt(0) == '-');
  67. final String sKey = (isNegation) ? Bits[0].substring(1).toUpperCase() : Bits[0].toUpperCase();
  68. final String sValue = (Bits.length == 2) ? Bits[1] : "";
  69. callDebugInfo(IRCParser.DEBUG_INFO, "%s => %s",sKey,sValue);
  70. if (isNegation) {
  71. myParser.h005Info.remove(sKey);
  72. } else {
  73. myParser.h005Info.put(sKey,sValue);
  74. }
  75. if (sKey.equals("NETWORK") && !isNegation) {
  76. myParser.networkName = sValue;
  77. callGotNetwork();
  78. } else if (sKey.equals("CASEMAPPING") && !isNegation) {
  79. byte limit = (byte)4;
  80. if (sValue.equalsIgnoreCase("strict-rfc1459")) {
  81. limit = (byte)3;
  82. } else if (sValue.equalsIgnoreCase("ascii")) {
  83. limit = (byte)0;
  84. } else if (!sValue.equalsIgnoreCase("rfc1459")) {
  85. myParser.callErrorInfo(new ParserError(ParserError.ERROR_WARNING, "Unknown casemapping: '"+sValue+"' - assuming rfc1459", myParser.getLastLine()));
  86. }
  87. final boolean limitChanged = (myParser.getStringConverter().getLimit() != limit);
  88. myParser.updateCharArrays(limit);
  89. if (limitChanged && myParser.knownClients() == 1) {
  90. // This means that the casemapping is not rfc1459
  91. // We have only added ourselves so far (from 001)
  92. // We can fix the hashtable easily.
  93. myParser.removeClient(myParser.getLocalClient());
  94. myParser.addClient(myParser.getLocalClient());
  95. }
  96. } else if (sKey.equals("CHANTYPES")) {
  97. myParser.parseChanPrefix();
  98. } else if (sKey.equals("PREFIX")) {
  99. myParser.parsePrefixModes();
  100. } else if (sKey.equals("CHANMODES")) {
  101. myParser.parseChanModes();
  102. } else if (sKey.equals("NAMESX") || sKey.equals("UHNAMES")) {
  103. myParser.sendString("PROTOCTL "+sKey);
  104. } else if (sKey.equals("LISTMODE")) {
  105. // Support for potential future decent mode listing in the protocol
  106. //
  107. // See my proposal: http://shanemcc.co.uk/irc/#listmode
  108. // Add listmode handler
  109. String[] handles = new String[2];
  110. handles[0] = sValue; // List mode item
  111. final String endValue = ""+(Integer.parseInt(sValue) + 1);
  112. myParser.h005Info.put("LISTMODEEND", endValue);
  113. handles[1] = endValue; // List mode end
  114. // Add listmode handlers
  115. try {
  116. myParser.getProcessingManager().addProcessor(handles, myParser.getProcessingManager().getProcessor("__LISTMODE__"));
  117. } catch (ProcessorNotFoundException e) { }
  118. }
  119. }
  120. }
  121. }
  122. /**
  123. * What does this IRCProcessor handle.
  124. *
  125. * @return String[] with the names of the tokens we handle.
  126. */
  127. @Override
  128. public String[] handles() {
  129. return new String[]{"002", "003", "004", "005"};
  130. }
  131. /**
  132. * Callback to all objects implementing the GotNetwork Callback.
  133. * This takes no params of its own, but works them out itself.
  134. *
  135. * @see IGotNetwork
  136. * @return true if a method was called, false otherwise
  137. */
  138. protected boolean callGotNetwork() {
  139. final String networkName = myParser.networkName;
  140. final String ircdVersion = myParser.getIRCD(false);
  141. final String ircdType = myParser.getIRCD(true);
  142. return getCallbackManager().getCallbackType(NetworkDetectedListener.class).call(networkName, ircdVersion, ircdType);
  143. }
  144. /**
  145. * Create a new instance of the IRCProcessor Object.
  146. *
  147. * @param parser IRCParser That owns this IRCProcessor
  148. * @param manager ProcessingManager that is in charge of this IRCProcessor
  149. */
  150. protected Process004005 (final IRCParser parser, final ProcessingManager manager) { super(parser, manager); }
  151. }