Java IRC bot
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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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;
  23. /**
  24. * Process a Names reply.
  25. */
  26. public class ProcessNames extends IRCProcessor {
  27. /**
  28. * Process a Names reply.
  29. *
  30. * @param sParam Type of line to process ("366", "353")
  31. * @param token IRCTokenised line to process
  32. */
  33. @Override
  34. public void process(String sParam, String[] token) {
  35. ChannelInfo iChannel;
  36. if (sParam.equals("366")) {
  37. // End of names
  38. iChannel = getChannelInfo(token[3]);
  39. if (iChannel == null) { return; }
  40. iChannel.setAddingNames(false);
  41. callChannelGotNames(iChannel);
  42. if (!iChannel.hasAskedForListModes()) {
  43. if (myParser.getAutoListMode()) {
  44. iChannel.requestListModes();
  45. }
  46. }
  47. } else {
  48. // Names
  49. ClientInfo iClient;
  50. ChannelClientInfo iChannelClient;
  51. iChannel = getChannelInfo(token[4]);
  52. if (iChannel == null) { return; }
  53. // If we are not expecting names, clear the current known names - this is fresh stuff!
  54. if (!iChannel.isAddingNames()) { iChannel.emptyChannel(); }
  55. iChannel.setAddingNames(true);
  56. String[] sNames = token[token.length-1].split(" ");
  57. String sNameBit = "", sName = "";
  58. StringBuilder sModes = new StringBuilder();
  59. long nPrefix = 0;
  60. for (int j = 0; j < sNames.length; ++j) {
  61. sNameBit = sNames[j];
  62. // If name is empty (ie there was an extra space) ignore it.
  63. if (sNameBit.isEmpty()) { continue; }
  64. // This next bit of code allows for any ircd which decides to use @+Foo in names
  65. for (int i = 0; i < sNameBit.length(); ++i) {
  66. Character cMode = sNameBit.charAt(i);
  67. // hPrefixMap contains @, o, +, v this caused issue 107
  68. // hPrefixModes only contains o, v so if the mode is in hPrefixMap
  69. // and not in hPrefixModes, its ok to use.
  70. if (myParser.hPrefixMap.containsKey(cMode) && !myParser.hPrefixModes.containsKey(cMode)) {
  71. sModes.append(cMode);
  72. nPrefix = nPrefix + myParser.hPrefixModes.get(myParser.hPrefixMap.get(cMode));
  73. } else {
  74. sName = sNameBit.substring(i);
  75. break;
  76. }
  77. }
  78. callDebugInfo(IRCParser.DEBUG_INFO, "Name: %s Modes: \"%s\" [%d]",sName,sModes.toString(),nPrefix);
  79. iClient = getClientInfo(sName);
  80. if (iClient == null) { iClient = new ClientInfo(myParser, sName); myParser.addClient(iClient); }
  81. iChannelClient = iChannel.addClient(iClient);
  82. iChannelClient.setChanMode(nPrefix);
  83. sName = "";
  84. sModes = new StringBuilder();
  85. nPrefix = 0;
  86. }
  87. }
  88. }
  89. /**
  90. * Callback to all objects implementing the ChannelGotNames Callback.
  91. *
  92. * @see IChannelGotNames
  93. * @param cChannel Channel which the names reply is for
  94. * @return true if a method was called, false otherwise
  95. */
  96. protected boolean callChannelGotNames(ChannelInfo cChannel) {
  97. return getCallbackManager().getCallbackType("OnChannelGotNames").call(cChannel);
  98. }
  99. /**
  100. * What does this IRCProcessor handle.
  101. *
  102. * @return String[] with the names of the tokens we handle.
  103. */
  104. @Override
  105. public String[] handles() {
  106. return new String[]{"353", "366"};
  107. }
  108. /**
  109. * Create a new instance of the IRCProcessor Object.
  110. *
  111. * @param parser IRCParser That owns this IRCProcessor
  112. * @param manager ProcessingManager that is in charge of this IRCProcessor
  113. */
  114. protected ProcessNames (IRCParser parser, ProcessingManager manager) { super(parser, manager); }
  115. }