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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2006-2014 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.interfaces.ChannelInfo;
  24. import com.dmdirc.parser.interfaces.callbacks.ChannelNamesListener;
  25. import com.dmdirc.parser.interfaces.callbacks.ChannelTopicListener;
  26. import com.dmdirc.parser.irc.IRCChannelClientInfo;
  27. import com.dmdirc.parser.irc.IRCChannelInfo;
  28. import com.dmdirc.parser.irc.IRCClientInfo;
  29. import com.dmdirc.parser.irc.IRCParser;
  30. import com.dmdirc.parser.irc.ModeManager;
  31. import com.dmdirc.parser.irc.PrefixModeManager;
  32. import com.dmdirc.parser.irc.ProcessingManager;
  33. /**
  34. * Process a Names reply.
  35. */
  36. public class ProcessNames extends IRCProcessor {
  37. /** The manager to use to access prefix modes. */
  38. private final PrefixModeManager prefixModeManager;
  39. /** Mode manager to use for user modes. */
  40. private final ModeManager userModeManager;
  41. /**
  42. * Create a new instance of the IRCProcessor Object.
  43. *
  44. * @param parser IRCParser That owns this IRCProcessor
  45. * @param prefixModeManager The manager to use to access prefix modes.
  46. * @param userModeManager Mode manager to use for user modes.
  47. * @param manager ProcessingManager that is in charge of this IRCProcessor
  48. */
  49. public ProcessNames(final IRCParser parser, final PrefixModeManager prefixModeManager,
  50. final ModeManager userModeManager, final ProcessingManager manager) {
  51. super(parser, manager);
  52. this.prefixModeManager = prefixModeManager;
  53. this.userModeManager = userModeManager;
  54. }
  55. /**
  56. * Process a Names reply.
  57. *
  58. * @param sParam Type of line to process ("366", "353")
  59. * @param token IRCTokenised line to process
  60. */
  61. @Override
  62. public void process(final String sParam, final String... token) {
  63. final IRCChannelInfo iChannel;
  64. if ("366".equals(sParam)) {
  65. // End of names
  66. iChannel = getChannel(token[3]);
  67. if (iChannel == null) {
  68. return;
  69. }
  70. if (!iChannel.hadTopic()) {
  71. callChannelTopic(iChannel, true);
  72. }
  73. iChannel.setAddingNames(false);
  74. callChannelGotNames(iChannel);
  75. if (!iChannel.hasAskedForListModes()
  76. && parser.getAutoListMode()) {
  77. iChannel.requestListModes();
  78. }
  79. } else {
  80. // Names
  81. iChannel = getChannel(token[4]);
  82. if (iChannel == null) {
  83. return;
  84. }
  85. // If we are not expecting names, clear the current known names - this is fresh stuff!
  86. if (!iChannel.isAddingNames()) {
  87. iChannel.emptyChannel();
  88. }
  89. iChannel.setAddingNames(true);
  90. final String[] sNames = token[token.length - 1].split(" ");
  91. String sName = "";
  92. StringBuilder sModes = new StringBuilder();
  93. for (String sName1 : sNames) {
  94. // If name is empty (ie there was an extra space) ignore it.
  95. if (sName1.isEmpty()) {
  96. continue;
  97. }
  98. // This next bit of code allows for any ircd which decides to use @+Foo in names
  99. for (int i = 0; i < sName1.length(); i++) {
  100. final char cMode = sName1.charAt(i);
  101. if (prefixModeManager.isPrefix(cMode)) {
  102. sModes.append(prefixModeManager.getModeFor(cMode));
  103. } else {
  104. sName = sName1.substring(i);
  105. break;
  106. }
  107. }
  108. callDebugInfo(IRCParser.DEBUG_INFO, "Name: %s Modes: \"%s\"", sName,
  109. sModes.toString());
  110. IRCClientInfo iClient = getClientInfo(sName);
  111. if (iClient == null) {
  112. iClient = new IRCClientInfo(parser, userModeManager, sName);
  113. parser.addClient(iClient);
  114. }
  115. iClient.setUserBits(sName, false); // Will do nothing if this isn't UHNAMES
  116. final IRCChannelClientInfo iChannelClient = iChannel.addClient(iClient);
  117. iChannelClient.setChanMode(sModes.toString());
  118. sName = "";
  119. sModes = new StringBuilder();
  120. }
  121. }
  122. }
  123. /**
  124. * Callback to all objects implementing the ChannelTopic Callback.
  125. *
  126. * @see ChannelTopicListener
  127. * @param cChannel Channel that topic was set on
  128. * @param bIsJoinTopic True when getting topic on join, false if set by user/server
  129. */
  130. protected void callChannelTopic(final ChannelInfo cChannel, final boolean bIsJoinTopic) {
  131. ((IRCChannelInfo) cChannel).setHadTopic();
  132. getCallbackManager().getCallback(ChannelTopicListener.class)
  133. .onChannelTopic(null, null, cChannel, bIsJoinTopic);
  134. }
  135. /**
  136. * Callback to all objects implementing the ChannelGotNames Callback.
  137. *
  138. * @see ChannelNamesListener
  139. * @param cChannel Channel which the names reply is for
  140. */
  141. protected void callChannelGotNames(final ChannelInfo cChannel) {
  142. getCallbackManager().getCallback(ChannelNamesListener.class)
  143. .onChannelGotNames(null, null, cChannel);
  144. }
  145. /**
  146. * What does this IRCProcessor handle.
  147. *
  148. * @return String[] with the names of the tokens we handle.
  149. */
  150. @Override
  151. public String[] handles() {
  152. return new String[]{"353", "366"};
  153. }
  154. }