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

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