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

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