Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ProcessWho.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.common.AwayState;
  24. import com.dmdirc.parser.events.AwayStateEvent;
  25. import com.dmdirc.parser.events.ChannelOtherAwayStateEvent;
  26. import com.dmdirc.parser.events.OtherAwayStateEvent;
  27. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  28. import com.dmdirc.parser.interfaces.ChannelInfo;
  29. import com.dmdirc.parser.interfaces.ClientInfo;
  30. import com.dmdirc.parser.interfaces.callbacks.AwayStateListener;
  31. import com.dmdirc.parser.interfaces.callbacks.ChannelOtherAwayStateListener;
  32. import com.dmdirc.parser.interfaces.callbacks.OtherAwayStateListener;
  33. import com.dmdirc.parser.irc.IRCClientInfo;
  34. import com.dmdirc.parser.irc.IRCParser;
  35. import com.dmdirc.parser.irc.ProcessingManager;
  36. import java.util.Date;
  37. /**
  38. * Process a /who reply.
  39. */
  40. public class ProcessWho extends IRCProcessor {
  41. /**
  42. * Create a new instance of the IRCProcessor Object.
  43. *
  44. * @param parser IRCParser That owns this IRCProcessor
  45. * @param manager ProcessingManager that is in charge of this IRCProcessor
  46. */
  47. public ProcessWho(final IRCParser parser, final ProcessingManager manager) {
  48. super(parser, manager, "352");
  49. }
  50. /**
  51. * Process a /who reply.
  52. *
  53. * @param sParam Type of line to process ("352")
  54. * @param token IRCTokenised line to process
  55. */
  56. @Override
  57. public void process(final String sParam, final String... token) {
  58. // :blueyonder2.uk.quakenet.org 352 Dataforce #mdbot shane Tobavaj.users.quakenet.org *.quakenet.org Tobavaj G+x :3 Tobavaj - http://shane.dmdirc.com/scriptbot.php
  59. // 0 1 2 3 4 5 6 7 8 9
  60. // :blueyonder2.uk.quakenet.org 352 Dataforce #mdbot ~Dataforce ResNetUser-BrynDinas-147.143.246.102.bangor.ac.uk *.quakenet.org Dataforce H@ :0 Dataforce
  61. // 0 1 2 3 4 5 6 7 8 9
  62. // :blueyonder2.uk.quakenet.org 352 Dataforce #mdbot shane soren.dataforce.org.uk *.quakenet.org DF|Soren H :3 Unknown
  63. // 0 1 2 3 4 5 6 7 8 9
  64. // :server 352 mynickname channel username address server nick flags :hops info
  65. // 0 1 2 3 4 5 6 7 8 9
  66. final IRCClientInfo client = getClientInfo(token[7]);
  67. if (client != null) {
  68. // Update ident/host
  69. client.setUserBits(token[7] + '!' + token[4] + '@' + token[5], false);
  70. // Update real name
  71. if (client.getRealname().isEmpty()) {
  72. final String name = token[9].split(" ", 2)[1];
  73. client.setRealName(name);
  74. }
  75. // Update away state
  76. final String mode = token[8];
  77. final AwayState isAway = mode.indexOf('G') == -1 ? AwayState.HERE : AwayState.AWAY;
  78. if (client.getAwayState() != isAway) {
  79. final AwayState oldState = client.getAwayState();
  80. client.setAwayState(isAway);
  81. if (client == parser.getLocalClient()) {
  82. callAwayState(oldState, client.getAwayState(), client.getAwayReason());
  83. } else {
  84. callAwayStateOther(client, oldState, isAway);
  85. for (ChannelInfo iChannel : parser.getChannels()) {
  86. final ChannelClientInfo iChannelClient = iChannel.getChannelClient(client);
  87. if (iChannelClient != null) {
  88. callChannelAwayStateOther(iChannel, iChannelClient, oldState, isAway);
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. /**
  96. * Callback to all objects implementing the onAwayState Callback.
  97. *
  98. * @see AwayStateListener
  99. * @param oldState Old Away State
  100. * @param currentState Current Away State
  101. * @param reason Best guess at away reason
  102. */
  103. protected void callAwayState(final AwayState oldState, final AwayState currentState,
  104. final String reason) {
  105. getCallbackManager().publish(
  106. new AwayStateEvent(parser, new Date(), oldState, currentState, reason));
  107. }
  108. /**
  109. * Callback to all objects implementing the onAwayStateOther Callback.
  110. *
  111. * @see OtherAwayStateListener
  112. * @param client Client this is for
  113. * @param oldState Old Away State
  114. * @param state Current Away State
  115. */
  116. protected void callAwayStateOther(final ClientInfo client, final AwayState oldState,
  117. final AwayState state) {
  118. getCallbackManager().publish(
  119. new OtherAwayStateEvent(parser, new Date(), client, oldState, state));
  120. }
  121. /**
  122. * Callback to all objects implementing the onChannelAwayStateOther Callback.
  123. *
  124. * @see ChannelOtherAwayStateListener
  125. * @param channel Channel this is for
  126. * @param channelClient ChannelClient this is for
  127. * @param oldState Old Away State
  128. * @param state Current Away State
  129. */
  130. protected void callChannelAwayStateOther(final ChannelInfo channel,
  131. final ChannelClientInfo channelClient, final AwayState oldState, final AwayState state) {
  132. getCallbackManager().publish(
  133. new ChannelOtherAwayStateEvent(parser, new Date(), channel, channelClient, oldState,
  134. state));
  135. }
  136. }