Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2006-2013 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.addons.parser_msn;
  23. import com.dmdirc.parser.common.AwayState;
  24. import com.dmdirc.parser.common.ParserError;
  25. import com.dmdirc.parser.interfaces.ChannelInfo;
  26. import com.dmdirc.parser.interfaces.ClientInfo;
  27. import com.dmdirc.parser.interfaces.callbacks.AwayStateListener;
  28. import com.dmdirc.parser.interfaces.callbacks.ChannelSelfJoinListener;
  29. import com.dmdirc.parser.interfaces.callbacks.ConnectErrorListener;
  30. import com.dmdirc.parser.interfaces.callbacks.ErrorInfoListener;
  31. import com.dmdirc.parser.interfaces.callbacks.OtherAwayStateListener;
  32. import com.dmdirc.parser.interfaces.callbacks.PasswordRequiredListener;
  33. import com.dmdirc.parser.interfaces.callbacks.PrivateMessageListener;
  34. import com.dmdirc.parser.interfaces.callbacks.ServerNoticeListener;
  35. import com.dmdirc.parser.interfaces.callbacks.ServerReadyListener;
  36. import com.dmdirc.parser.interfaces.callbacks.SocketCloseListener;
  37. import java.net.ConnectException;
  38. import java.util.ArrayList;
  39. import java.util.Arrays;
  40. import java.util.Collection;
  41. import java.util.List;
  42. import net.sf.jml.MsnContact;
  43. import net.sf.jml.MsnList;
  44. import net.sf.jml.MsnMessenger;
  45. import net.sf.jml.MsnSwitchboard;
  46. import net.sf.jml.MsnUserStatus;
  47. import net.sf.jml.event.MsnAdapter;
  48. import net.sf.jml.exception.IncorrectPasswordException;
  49. import net.sf.jml.exception.LoginException;
  50. import net.sf.jml.message.MsnInstantMessage;
  51. import net.sf.jml.message.MsnSystemMessage;
  52. import net.sf.jml.message.MsnUnknownMessage;
  53. /**
  54. * Listener object acting on events from the MSN connection.
  55. */
  56. public class MSNListener extends MsnAdapter {
  57. /** MSN Parser. */
  58. private final MSNParser parser;
  59. /** Switchboard ID. */
  60. private final Object object = new Object();
  61. /**
  62. * Creates a new MSN listener.
  63. *
  64. * @param parser Parent parser
  65. */
  66. public MSNListener(final MSNParser parser) {
  67. this.parser = parser;
  68. }
  69. /** {@inheritDoc} */
  70. @Override
  71. public void contactAddCompleted(final MsnMessenger mm, final MsnContact mc,
  72. final MsnList ml) {
  73. parser.addClient(mc);
  74. parser.addClients(parser.getFakeChannel(), Arrays.asList(
  75. new ClientInfo[]{ parser.getClient(mc), }));
  76. }
  77. /** {@inheritDoc} */
  78. @Override
  79. public void contactListSyncCompleted(final MsnMessenger mm) {
  80. final Collection<MsnContact> contacts = Arrays.asList(
  81. mm.getContactList().getContacts());
  82. final List<ClientInfo> clients = new ArrayList<ClientInfo>();
  83. for (MsnContact contact : contacts) {
  84. parser.addClient(contact);
  85. clients.add(parser.getClient(contact));
  86. }
  87. }
  88. /** {@inheritDoc} */
  89. @Override
  90. public void contactStatusChanged(final MsnMessenger mm,
  91. final MsnContact mc) {
  92. if (mc.getStatus() != MsnUserStatus.OFFLINE) {
  93. parser.addClients(parser.getFakeChannel(), Arrays.asList(
  94. new ClientInfo[]{ parser.getClient(mc), }));
  95. } else if (mc.getStatus() == MsnUserStatus.OFFLINE) {
  96. parser.removeClients(parser.getFakeChannel(), Arrays.asList(
  97. new ClientInfo[]{ parser.getClient(mc), }));
  98. }
  99. final boolean isBack = mc.getStatus().equals(MsnUserStatus.ONLINE);
  100. parser.getCallbackManager().getCallbackType(
  101. OtherAwayStateListener.class).call(parser.getClient(mc),
  102. isBack ? AwayState.AWAY : AwayState.HERE,
  103. isBack ? AwayState.HERE : AwayState.AWAY);
  104. }
  105. /** {@inheritDoc} */
  106. @Override
  107. public void exceptionCaught(final MsnMessenger mm, final Throwable thrwbl) {
  108. final ParserError error = new ParserError(ParserError.ERROR_ERROR,
  109. thrwbl.getMessage(), "");
  110. error.setException(new Exception(thrwbl)); //NOPMD
  111. if (thrwbl instanceof LoginException) {
  112. parser.getCallbackManager().getCallbackType(
  113. ConnectErrorListener.class).call(error);
  114. } else if (thrwbl instanceof IncorrectPasswordException) {
  115. parser.getCallbackManager().getCallbackType(
  116. PasswordRequiredListener.class).call(error);
  117. } else if (thrwbl instanceof ConnectException) {
  118. parser.getCallbackManager().getCallbackType(
  119. SocketCloseListener.class).call(error);
  120. parser.removeMSNParser();
  121. } else {
  122. parser.getCallbackManager().getCallbackType(
  123. ErrorInfoListener.class).call(error);
  124. }
  125. }
  126. /** {@inheritDoc} */
  127. @Override
  128. public void instantMessageReceived(final MsnSwitchboard ms,
  129. final MsnInstantMessage mim, final MsnContact mc) {
  130. parser.getCallbackManager().getCallbackType(
  131. PrivateMessageListener.class).call(mim.getContent(),
  132. mc.getEmail().getEmailAddress());
  133. }
  134. /** {@inheritDoc} */
  135. @Override
  136. public void loginCompleted(final MsnMessenger mm) {
  137. mm.newSwitchboard(object);
  138. parser.getCallbackManager().getCallbackType(ServerReadyListener.class)
  139. .call();
  140. final ChannelInfo channel = parser.getFakeChannel();
  141. if (channel != null) {
  142. parser.getCallbackManager().getCallbackType(
  143. ChannelSelfJoinListener.class).call(channel);
  144. }
  145. }
  146. /** {@inheritDoc} */
  147. @Override
  148. public void logout(final MsnMessenger mm) {
  149. parser.getCallbackManager().getCallbackType(SocketCloseListener.class)
  150. .call();
  151. }
  152. /** {@inheritDoc} */
  153. @Override
  154. public void offlineMessageReceived(final String body,
  155. final String contentType, final String encoding,
  156. final MsnContact mc) {
  157. parser.getCallbackManager().getCallbackType(
  158. PrivateMessageListener.class).call(body,
  159. mc.getEmail().getEmailAddress());
  160. }
  161. /** {@inheritDoc} */
  162. @Override
  163. public void ownerStatusChanged(final MsnMessenger mm) {
  164. if (mm.getOwner().getStatus() == MsnUserStatus.ONLINE) {
  165. parser.getCallbackManager().getCallbackType(AwayStateListener.class)
  166. .call(AwayState.AWAY, AwayState.HERE, null);
  167. } else {
  168. parser.getCallbackManager().getCallbackType(AwayStateListener.class)
  169. .call(AwayState.HERE, AwayState.AWAY, mm.getOwner()
  170. .getStatus().getDisplayStatus());
  171. }
  172. }
  173. /** {@inheritDoc} */
  174. @Override
  175. public void systemMessageReceived(final MsnMessenger mm,
  176. final MsnSystemMessage msm) {
  177. if (msm.getContent() != null) {
  178. parser.getCallbackManager().getCallbackType(
  179. ServerNoticeListener.class).call(msm.getContent(), "MSN");
  180. }
  181. }
  182. /** {@inheritDoc} */
  183. @Override
  184. public void unknownMessageReceived(final MsnSwitchboard ms,
  185. final MsnUnknownMessage mum, final MsnContact mc) {
  186. parser.getCallbackManager().getCallbackType(
  187. ServerNoticeListener.class).call("Unknown message: "
  188. + mum.getContent(), "MSN");
  189. }
  190. }