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 6.1KB

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