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.

ProcessWho.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2006-2008 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.irc.callbacks.CallbackOnAwayState;
  24. import com.dmdirc.parser.irc.callbacks.CallbackOnAwayStateOther;
  25. import com.dmdirc.parser.irc.callbacks.CallbackOnChannelAwayStateOther;
  26. /**
  27. * Process a /who reply.
  28. */
  29. public class ProcessWho extends IRCProcessor {
  30. /**
  31. * Process a /who reply.
  32. *
  33. * @param sParam Type of line to process ("352")
  34. * @param token IRCTokenised line to process
  35. */
  36. @Override
  37. public void process(final String sParam, final String[] token) {
  38. // :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
  39. // 0 1 2 3 4 5 6 7 8 9
  40. // :blueyonder2.uk.quakenet.org 352 Dataforce #mdbot ~Dataforce ResNetUser-BrynDinas-147.143.246.102.bangor.ac.uk *.quakenet.org Dataforce H@ :0 Dataforce
  41. // 0 1 2 3 4 5 6 7 8 9
  42. // :blueyonder2.uk.quakenet.org 352 Dataforce #mdbot shane soren.dataforce.org.uk *.quakenet.org DF|Soren H :3 Unknown
  43. // 0 1 2 3 4 5 6 7 8 9
  44. // :server 352 mynickname channel username address server nick flags :hops info
  45. // 0 1 2 3 4 5 6 7 8 9
  46. // ChannelInfo channel = myParser.getChannelInfo(token[3]);
  47. // ChannelClientInfo channelClient = channel.getUser(token[7]);
  48. // ClientInfo client = channelClient.getClient();
  49. final ClientInfo client = myParser.getClientInfo(token[7]);
  50. if (client != null) {
  51. // Update ident/host
  52. client.setUserBits(token[7]+"!"+token[4]+"@"+token[5], false);
  53. // Update real name
  54. if (client.getRealName().isEmpty()) {
  55. final String name = token[9].split(" ", 2)[1];
  56. client.setRealName(name);
  57. }
  58. // Update away state
  59. final String mode = token[8];
  60. final boolean isAway = mode.indexOf('G') != -1;
  61. if (client.getAwayState() != isAway) {
  62. // System.out.println("Away state for '"+client+"' changed to: "+isAway);
  63. client.setAwayState(isAway);
  64. if (!isAway) { client.setAwayReason(""); }
  65. if (client == myParser.getMyself()) {
  66. callAwayState(client.getAwayState(), client.getAwayReason());
  67. } else {
  68. callAwayStateOther(client, isAway);
  69. ChannelClientInfo iChannelClient;
  70. for (ChannelInfo iChannel : myParser.getChannels()) {
  71. iChannelClient = iChannel.getUser(client);
  72. if (iChannelClient != null) {
  73. callChannelAwayStateOther(iChannel,iChannelClient,isAway);
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * Callback to all objects implementing the onAwayState Callback.
  82. *
  83. * @see IAwayState
  84. * @param currentState Set to true if we are now away, else false.
  85. * @param reason Best guess at away reason
  86. * @return true if a method was called, false otherwise
  87. */
  88. protected boolean callAwayState(boolean currentState, String reason) {
  89. return ((CallbackOnAwayState) myParser.getCallbackManager().getCallbackType("OnAwayState")).call(currentState, reason);
  90. }
  91. /**
  92. * Callback to all objects implementing the onAwayStateOther Callback.
  93. *
  94. * @see IAwayStateOther
  95. * @param client Client this is for
  96. * @param state Away State (true if away, false if here)
  97. * @return true if a method was called, false otherwise
  98. */
  99. protected boolean callAwayStateOther(final ClientInfo client, final boolean state) {
  100. return ((CallbackOnAwayStateOther) myParser.getCallbackManager().getCallbackType("OnAwayStateOther")).call(client, state);
  101. }
  102. /**
  103. * Callback to all objects implementing the onChannelAwayStateOther Callback.
  104. *
  105. * @see IAwayStateOther
  106. * @param channel Channel this is for
  107. * @param channelClient ChannelClient this is for
  108. * @param state Away State (true if away, false if here)
  109. * @return true if a method was called, false otherwise
  110. */
  111. protected boolean callChannelAwayStateOther(final ChannelInfo channel, final ChannelClientInfo channelClient, final boolean state) {
  112. return ((CallbackOnChannelAwayStateOther) myParser.getCallbackManager().getCallbackType("OnChannelAwayStateOther")).call(channel, channelClient, state);
  113. }
  114. /**
  115. * What does this IRCProcessor handle.
  116. *
  117. * @return String[] with the names of the tokens we handle.
  118. */
  119. @Override
  120. public String[] handles() {
  121. return new String[]{"352"};
  122. }
  123. /**
  124. * Create a new instance of the IRCProcessor Object.
  125. *
  126. * @param parser IRCParser That owns this IRCProcessor
  127. * @param manager ProcessingManager that is in charge of this IRCProcessor
  128. */
  129. protected ProcessWho (IRCParser parser, ProcessingManager manager) { super(parser, manager); }
  130. }