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

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