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

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