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.

ProcessCap.java 5.6KB

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