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.

ProcessJoin.java 5.1KB

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