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

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