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.

ProcessJoin.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. import com.dmdirc.parser.common.ParserError;
  24. import com.dmdirc.parser.common.QueuePriority;
  25. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  26. import com.dmdirc.parser.interfaces.ChannelInfo;
  27. import com.dmdirc.parser.interfaces.callbacks.ChannelJoinListener;
  28. import com.dmdirc.parser.interfaces.callbacks.ChannelSelfJoinListener;
  29. /**
  30. * Process a channel join.
  31. */
  32. public class ProcessJoin extends IRCProcessor {
  33. /**
  34. * Process a channel join.
  35. *
  36. * @param sParam Type of line to process ("JOIN")
  37. * @param token IRCTokenised line to process
  38. */
  39. @Override
  40. public void process(final String sParam, final String[] token) {
  41. if (sParam.equals("329")) {
  42. if (token.length < 5) { return; }
  43. IRCChannelInfo iChannel = myParser.getChannel(token[3]);
  44. if (iChannel != null) {
  45. try {
  46. iChannel.setCreateTime(Integer.parseInt(token[4]));
  47. } catch (NumberFormatException nfe) { /* Oh well, not a normal ircd I guess */ }
  48. }
  49. } else {
  50. // :nick!ident@host JOIN (:)#Channel
  51. Byte nTemp;
  52. if (token.length < 3) { return; }
  53. IRCClientInfo iClient;
  54. IRCChannelInfo iChannel;
  55. IRCChannelClientInfo iChannelClient;
  56. iClient = getClientInfo(token[0]);
  57. iChannel = myParser.getChannel(token[token.length-1]);
  58. if (iClient == null) {
  59. iClient = new IRCClientInfo(myParser, token[0]);
  60. myParser.addClient(iClient);
  61. }
  62. // Check to see if we know the host/ident for this client to facilitate dmdirc Formatter
  63. if (iClient.getHostname().isEmpty()) { iClient.setUserBits(token[0],false); }
  64. if (iChannel != null) {
  65. if (iClient == myParser.getLocalClient()) {
  66. try {
  67. if (iChannel.getChannelClient(iClient) != null) {
  68. // If we are joining a channel we are already on, fake a part from
  69. // the channel internally, and rejoin.
  70. myParser.getProcessingManager().process("PART", token);
  71. } else {
  72. // Otherwise we have a channel known, that we are not in?
  73. myParser.callErrorInfo(new ParserError(ParserError.ERROR_FATAL, "Joined known channel that we wern't already on..", myParser.getLastLine()));
  74. }
  75. } catch (ProcessorNotFoundException e) { }
  76. } else if (iChannel.getChannelClient(iClient) != null) {
  77. // Client joined channel that we already know of.
  78. return;
  79. } else {
  80. // This is only done if we are already the channel, and it isn't us that
  81. // joined.
  82. iChannelClient = iChannel.addClient(iClient);
  83. callChannelJoin(iChannel, iChannelClient);
  84. return;
  85. }
  86. }
  87. //if (iClient != myParser.getMyself()) {
  88. // callErrorInfo(new ParserError(ParserError.ERROR_WARNING, "Got join for channel ("+token[token.length-1]+") that I am not on. [Me: "+myParser.getMyself()+"]", myParser.getLastLine()));
  89. //}
  90. iChannel = new IRCChannelInfo(myParser, token[token.length-1]);
  91. // Add ourself to the channel, this will be overridden by the NAMES reply
  92. iChannel.addClient(iClient);
  93. myParser.addChannel(iChannel);
  94. sendString("MODE "+iChannel.getName(), QueuePriority.LOW);
  95. callChannelSelfJoin(iChannel);
  96. }
  97. }
  98. /**
  99. * Callback to all objects implementing the ChannelJoin Callback.
  100. *
  101. * @see IChannelJoin
  102. * @param cChannel Channel Object
  103. * @param cChannelClient ChannelClient object for new person
  104. * @return true if a method was called, false otherwise
  105. */
  106. protected boolean callChannelJoin(final ChannelInfo cChannel, final ChannelClientInfo cChannelClient) {
  107. return getCallbackManager().getCallbackType(ChannelJoinListener.class).call(cChannel, cChannelClient);
  108. }
  109. /**
  110. * Callback to all objects implementing the ChannelSelfJoin Callback.
  111. *
  112. * @see IChannelSelfJoin
  113. * @param cChannel Channel Object
  114. * @return true if a method was called, false otherwise
  115. */
  116. protected boolean callChannelSelfJoin(final ChannelInfo cChannel) {
  117. return getCallbackManager().getCallbackType(ChannelSelfJoinListener.class).call(cChannel);
  118. }
  119. /**
  120. * What does this IRCProcessor handle.
  121. *
  122. * @return String[] with the names of the tokens we handle.
  123. */
  124. @Override
  125. public String[] handles() {
  126. return new String[]{"JOIN", "329"};
  127. }
  128. /**
  129. * Create a new instance of the IRCProcessor Object.
  130. *
  131. * @param parser IRCParser That owns this IRCProcessor
  132. * @param manager ProcessingManager that is in charge of this IRCProcessor
  133. */
  134. protected ProcessJoin (IRCParser parser, ProcessingManager manager) { super(parser, manager); }
  135. }