Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ProcessCap.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 com.dmdirc.parser.irc.TimestampedIRCProcessor;
  26. import java.time.LocalDateTime;
  27. import java.util.ArrayList;
  28. import java.util.Collection;
  29. import javax.inject.Inject;
  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;
  45. /** List of supported capabilities. */
  46. private final Collection<String> supportedCapabilities = new ArrayList<>();
  47. /**
  48. * Create a new instance of the IRCProcessor Object.
  49. *
  50. * @param parser IRCParser That owns this IRCProcessor
  51. */
  52. @Inject
  53. public ProcessCap(final IRCParser parser) {
  54. super(parser, "CAP");
  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. supportedCapabilities.add("self-message");
  62. supportedCapabilities.add("server-time");
  63. // Freenode
  64. // supportedCapabilities.add("identify-msg");
  65. // DFBnc
  66. supportedCapabilities.add("dfbnc.com/tsirc");
  67. }
  68. /**
  69. * Process CAP responses.
  70. *
  71. * @param sParam Type of line to process ("CAP")
  72. * @param token IRCTokenised line to process
  73. */
  74. @Override
  75. public void process(final LocalDateTime date, final String sParam, final String... token) {
  76. // We will only automatically handle the first ever pre-001 CAP LS
  77. // response.
  78. // After that, the user may be sending stuff themselves so we do
  79. // nothing.
  80. if (!hasCapped && !parser.got001 && token.length > 4 && "LS".equalsIgnoreCase(token[3])) {
  81. final String[] caps = token[token.length - 1].split(" ");
  82. for (final String cap : caps) {
  83. if (cap.isEmpty()) { continue; }
  84. final String capability = cap.charAt(0) == '=' ? cap.substring(1).toLowerCase() : cap.toLowerCase();
  85. parser.addCapability(capability);
  86. if (supportedCapabilities.contains(capability)) {
  87. // Send cap requests as individual lines, as some servers
  88. // only appear to accept them one at a time.
  89. parser.sendRawMessage("CAP REQ :" + capability);
  90. }
  91. }
  92. // If this is the last of the LS responses, set hasCapped to true
  93. // so that we don't try this again, and send "CAP END"
  94. // We will accept any of the following to be the end of the list:
  95. // :DFBnc.Server CAP Dataforce LS :some caps
  96. // :DFBnc.Server CAP Dataforce LS
  97. // but not:
  98. // :DFBnc.Server CAP Dataforce LS *
  99. if (token.length == 4 || token.length == 5 && !"*".equals(token[4])) {
  100. hasCapped = true;
  101. parser.sendRawMessage("CAP END");
  102. }
  103. } else if ("ACK".equalsIgnoreCase(token[3]) || "CLEAR".equalsIgnoreCase(token[3])) {
  104. // Process the list.
  105. final String[] caps = token[token.length - 1].split(" ");
  106. for (final String cap : caps) {
  107. if (cap.isEmpty()) { continue; }
  108. final CapabilityState modifier = CapabilityState.fromModifier(cap.charAt(0));
  109. final String capability = modifier == CapabilityState.INVALID ? cap.toLowerCase() : cap.substring(1).toLowerCase();
  110. final CapabilityState state = modifier == CapabilityState.INVALID ? CapabilityState.ENABLED : modifier;
  111. if (state == CapabilityState.NEED_ACK) {
  112. parser.sendRawMessage("CAP ACK :" + capability);
  113. parser.setCapabilityState(capability, CapabilityState.ENABLED);
  114. } else {
  115. parser.setCapabilityState(capability, state);
  116. }
  117. }
  118. }
  119. }
  120. }