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.

ProcessCap.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2006-2014 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.irc.CapabilityState;
  24. import com.dmdirc.parser.irc.IRCParser;
  25. import com.dmdirc.parser.irc.ProcessingManager;
  26. import com.dmdirc.parser.irc.TimestampedIRCProcessor;
  27. import java.util.ArrayList;
  28. import java.util.Date;
  29. import java.util.List;
  30. /**
  31. * Process CAP extension.
  32. * There are currently no callbacks related to this, as it is just to tell
  33. * the server what we support.
  34. *
  35. * For all the capabilities we support, we are always able to handle lines#
  36. * either with or without the capability enabled, so we don't actually need to
  37. * keep much/any state here, which makes this easy.
  38. *
  39. * See: http://www.leeh.co.uk/draft-mitchell-irc-capabilities-02.html
  40. * See: http://ircv3.atheme.org/specification/capability-negotiation-3.1
  41. */
  42. public class ProcessCap extends TimestampedIRCProcessor {
  43. /** Have we handled the pre-connect cap request? */
  44. private boolean hasCapped = false;
  45. /** List of supported capabilities. */
  46. private final List<String> supportedCapabilities = new ArrayList<>();
  47. /**
  48. * Create a new instance of the IRCProcessor Object.
  49. *
  50. * @param parser IRCParser That owns this IRCProcessor
  51. * @param manager ProcessingManager that is in charge of this IRCProcessor
  52. */
  53. public ProcessCap(final IRCParser parser, final ProcessingManager manager) {
  54. super(parser, manager);
  55. // IRCv3.1 Standard
  56. supportedCapabilities.add("multi-prefix");
  57. supportedCapabilities.add("userhost-in-names");
  58. supportedCapabilities.add("away-notify");
  59. supportedCapabilities.add("account-notify");
  60. supportedCapabilities.add("extended-join");
  61. // Freenode
  62. // supportedCapabilities.add("identify-msg");
  63. // DFBnc
  64. supportedCapabilities.add("dfbnc.com/tsirc");
  65. }
  66. /**
  67. * Process CAP responses.
  68. *
  69. * @param sParam Type of line to process ("CAP")
  70. * @param token IRCTokenised line to process
  71. */
  72. @Override
  73. public void process(final Date date, final String sParam, final String[] token) {
  74. // We will only automatically handle the first ever pre-001 CAP LS
  75. // response.
  76. // After that, the user may be sending stuff themselves so we do
  77. // nothing.
  78. if (!hasCapped && !parser.got001 && token.length > 4 && token[3].equalsIgnoreCase("LS")) {
  79. final String[] caps = token[token.length - 1].split(" ");
  80. for (final String cap : caps) {
  81. if (cap.isEmpty()) { continue; }
  82. final String capability = (cap.charAt(0) == '=') ? cap.substring(1).toLowerCase() : cap.toLowerCase();
  83. parser.addCapability(capability);
  84. if (supportedCapabilities.contains(capability)) {
  85. // Send cap requests as individual lines, as some servers
  86. // only appear to accept them one at a time.
  87. parser.sendRawMessage("CAP REQ :" + capability);
  88. }
  89. }
  90. // If this is the last of the LS responses, set hasCapped to true
  91. // so that we don't try this again, and send "CAP END"
  92. // We will accept any of the following to be the end of the list:
  93. // :DFBnc.Server CAP Dataforce LS :some caps
  94. // :DFBnc.Server CAP Dataforce LS
  95. // but not:
  96. // :DFBnc.Server CAP Dataforce LS *
  97. if (token.length == 4 || (token.length == 5 && !token[4].equals("*"))) {
  98. hasCapped = true;
  99. parser.sendRawMessage("CAP END");
  100. }
  101. } else if (token[3].equalsIgnoreCase("ACK") || token[3].equalsIgnoreCase("CLEAR")) {
  102. // Process the list.
  103. final String[] caps = token[token.length - 1].split(" ");
  104. for (final String cap : caps) {
  105. if (cap.isEmpty()) { continue; }
  106. final CapabilityState modifier = CapabilityState.fromModifier(cap.charAt(0));
  107. final String capability = (modifier == CapabilityState.INVALID) ? cap.toLowerCase() : cap.substring(1).toLowerCase();
  108. final CapabilityState state = (modifier == CapabilityState.INVALID) ? CapabilityState.ENABLED : modifier;
  109. if (state == CapabilityState.NEED_ACK) {
  110. parser.sendRawMessage("CAP ACK :" + capability);
  111. parser.setCapabilityState(capability, CapabilityState.ENABLED);
  112. } else {
  113. parser.setCapabilityState(capability, state);
  114. }
  115. }
  116. }
  117. }
  118. /**
  119. * What does this IRCProcessor handle.
  120. *
  121. * @return String[] with the names of the tokens we handle.
  122. */
  123. @Override
  124. public String[] handles() {
  125. return new String[]{"CAP"};
  126. }
  127. }