Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ProcessCap.java 5.5KB

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