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.

BaseParser.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.common;
  23. import com.dmdirc.parser.interfaces.ChannelInfo;
  24. import com.dmdirc.parser.interfaces.ClientInfo;
  25. import java.net.InetSocketAddress;
  26. import java.net.Proxy;
  27. import java.net.ProxySelector;
  28. import java.net.SocketAddress;
  29. import java.net.URI;
  30. import java.net.URISyntaxException;
  31. import java.util.List;
  32. /**
  33. * Implements common base functionality for parsers.
  34. * <p>
  35. * Implementations of this class must be annotated with {@link ChildImplementations} to define
  36. * the implementations to use for instances of {@link ClientInfo}, {@link ChannelInfo}, etc.
  37. */
  38. public abstract class BaseParser extends ThreadedParser {
  39. /** The URI that this parser was constructed for. */
  40. private final URI uri;
  41. /** The ignore list for this parser. */
  42. private IgnoreList ignoreList;
  43. /** The ping timer interval for this parser. */
  44. private long pingTimerInterval;
  45. /** The ping timer fraction for this parser. */
  46. private int pingTimerFraction;
  47. /** The cached name of the server this parser is connected to. */
  48. private String serverName;
  49. /** The IP that this parser should bind to. */
  50. private String bindIp;
  51. /** The IPv6 IP that this parser should bind to. */
  52. private String bindIpv6;
  53. /** The URI of the proxy to use when connecting, if any. */
  54. private URI proxy;
  55. /** The callback manager to use for this parser. */
  56. private final CallbackManager callbackManager;
  57. /**
  58. * Creates a new base parser for the specified URI.
  59. *
  60. * @param uri The URI this parser will connect to.
  61. */
  62. public BaseParser(final URI uri) {
  63. this.uri = uri;
  64. callbackManager = new CallbackManager(this);
  65. }
  66. @Override
  67. public void quit(final String reason) {
  68. disconnect(reason);
  69. }
  70. @Override
  71. public URI getURI() {
  72. return uri;
  73. }
  74. @Override
  75. public URI getProxy() {
  76. if (proxy == null && ProxySelector.getDefault() != null) {
  77. final List<Proxy> proxies = ProxySelector.getDefault().select(getURI());
  78. if (!proxies.isEmpty()) {
  79. final SocketAddress sock = proxies.get(0).address();
  80. if (sock instanceof InetSocketAddress) {
  81. final InetSocketAddress isa = (InetSocketAddress)sock;
  82. try {
  83. return new URI("proxy://", "", isa.getAddress().getHostAddress(), isa.getPort(), "", "", "");
  84. } catch (final URISyntaxException use) { /* Oh well... */ }
  85. }
  86. }
  87. } else {
  88. return proxy;
  89. }
  90. return null;
  91. }
  92. @Override
  93. public void setProxy(final URI proxy) {
  94. this.proxy = proxy;
  95. }
  96. @Override
  97. public IgnoreList getIgnoreList() {
  98. return ignoreList;
  99. }
  100. @Override
  101. public void setIgnoreList(final IgnoreList ignoreList) {
  102. this.ignoreList = ignoreList;
  103. }
  104. @Override
  105. public long getPingTimerInterval() {
  106. return pingTimerInterval;
  107. }
  108. @Override
  109. public void setPingTimerInterval(final long newValue) {
  110. pingTimerInterval = newValue;
  111. }
  112. @Override
  113. public int getPingTimerFraction() {
  114. return pingTimerFraction;
  115. }
  116. @Override
  117. public void setPingTimerFraction(final int newValue) {
  118. pingTimerFraction = newValue;
  119. }
  120. @Override
  121. public CallbackManager getCallbackManager() {
  122. return callbackManager;
  123. }
  124. @Override
  125. public void joinChannel(final String channel) {
  126. joinChannels(new ChannelJoinRequest(channel));
  127. }
  128. @Override
  129. public void joinChannel(final String channel, final String key) {
  130. joinChannels(new ChannelJoinRequest(channel, key));
  131. }
  132. @Override
  133. public String getServerName() {
  134. return serverName;
  135. }
  136. /**
  137. * Sets the name of this parser's server.
  138. *
  139. * @param serverName The new name for this parser's server
  140. */
  141. protected void setServerName(final String serverName) {
  142. this.serverName = serverName;
  143. }
  144. @Override
  145. public String getBindIP() {
  146. return bindIp;
  147. }
  148. @Override
  149. public void setBindIP(final String ip) {
  150. this.bindIp = ip;
  151. }
  152. @Override
  153. public String getBindIPv6() {
  154. return bindIpv6;
  155. }
  156. @Override
  157. public void setBindIPv6(final String ip) {
  158. this.bindIpv6 = ip;
  159. }
  160. }