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.

ProcessNames.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2006-2008 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;
  23. import com.dmdirc.parser.callbacks.CallbackOnChannelGotNames;
  24. /**
  25. * Process a Names reply.
  26. */
  27. public class ProcessNames extends IRCProcessor {
  28. /**
  29. * Process a Names reply.
  30. *
  31. * @param sParam Type of line to process ("366", "353")
  32. * @param token IRCTokenised line to process
  33. */
  34. @Override
  35. public void process(String sParam, String[] token) {
  36. ChannelInfo iChannel;
  37. if (sParam.equals("366")) {
  38. // End of names
  39. iChannel = getChannelInfo(token[3]);
  40. if (iChannel == null) { return; }
  41. iChannel.setAddingNames(false);
  42. callChannelGotNames(iChannel);
  43. if (!iChannel.hasAskedForListModes()) {
  44. if (myParser.getAutoListMode()) {
  45. iChannel.requestListModes();
  46. }
  47. }
  48. } else {
  49. // Names
  50. ClientInfo iClient;
  51. ChannelClientInfo iChannelClient;
  52. iChannel = getChannelInfo(token[4]);
  53. if (iChannel == null) { return; }
  54. // If we are not expecting names, clear the current known names - this is fresh stuff!
  55. if (!iChannel.isAddingNames()) { iChannel.emptyChannel(); }
  56. iChannel.setAddingNames(true);
  57. String[] sNames = token[token.length-1].split(" ");
  58. String sNameBit = "", sName = "";
  59. StringBuilder sModes = new StringBuilder();
  60. long nPrefix = 0;
  61. for (int j = 0; j < sNames.length; ++j) {
  62. sNameBit = sNames[j];
  63. // If name is empty (ie there was an extra space) ignore it.
  64. if (sNameBit.isEmpty()) { continue; }
  65. // This next bit of code allows for any ircd which decides to use @+Foo in names
  66. for (int i = 0; i < sNameBit.length(); ++i) {
  67. Character cMode = sNameBit.charAt(i);
  68. // hPrefixMap contains @, o, +, v this caused issue 107
  69. // hPrefixModes only contains o, v so if the mode is in hPrefixMap
  70. // and not in hPrefixModes, its ok to use.
  71. if (myParser.hPrefixMap.containsKey(cMode) && !myParser.hPrefixModes.containsKey(cMode)) {
  72. sModes.append(cMode);
  73. nPrefix = nPrefix + myParser.hPrefixModes.get(myParser.hPrefixMap.get(cMode));
  74. } else {
  75. sName = sNameBit.substring(i);
  76. break;
  77. }
  78. }
  79. callDebugInfo(IRCParser.DEBUG_INFO, "Name: %s Modes: \"%s\" [%d]",sName,sModes.toString(),nPrefix);
  80. iClient = getClientInfo(sName);
  81. if (iClient == null) { iClient = new ClientInfo(myParser, sName); myParser.addClient(iClient); }
  82. iChannelClient = iChannel.addClient(iClient);
  83. iChannelClient.setChanMode(nPrefix);
  84. sName = "";
  85. sModes = new StringBuilder();
  86. nPrefix = 0;
  87. }
  88. }
  89. }
  90. /**
  91. * Callback to all objects implementing the ChannelGotNames Callback.
  92. *
  93. * @see IChannelGotNames
  94. * @param cChannel Channel which the names reply is for
  95. * @return true if a method was called, false otherwise
  96. */
  97. protected boolean callChannelGotNames(ChannelInfo cChannel) {
  98. return ((CallbackOnChannelGotNames) getCallbackManager().getCallbackType("OnChannelGotNames")).call(cChannel);
  99. }
  100. /**
  101. * What does this IRCProcessor handle.
  102. *
  103. * @return String[] with the names of the tokens we handle.
  104. */
  105. @Override
  106. public String[] handles() {
  107. return new String[]{"353", "366"};
  108. }
  109. /**
  110. * Create a new instance of the IRCProcessor Object.
  111. *
  112. * @param parser IRCParser That owns this IRCProcessor
  113. * @param manager ProcessingManager that is in charge of this IRCProcessor
  114. */
  115. protected ProcessNames (IRCParser parser, ProcessingManager manager) { super(parser, manager); }
  116. }