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.

ParserFactory.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc;
  18. import com.dmdirc.config.profiles.Profile;
  19. import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
  20. import com.dmdirc.parser.common.MyInfo;
  21. import com.dmdirc.parser.interfaces.Parser;
  22. import com.dmdirc.parser.interfaces.ProtocolDescription;
  23. import com.dmdirc.plugins.NoSuchProviderException;
  24. import com.dmdirc.plugins.Service;
  25. import com.dmdirc.plugins.ServiceManager;
  26. import com.dmdirc.plugins.ServiceProvider;
  27. import java.net.URI;
  28. import java.net.URISyntaxException;
  29. import java.util.Optional;
  30. import javax.annotation.Nullable;
  31. import javax.inject.Inject;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. import static com.dmdirc.util.LogUtils.APP_ERROR;
  35. import static com.dmdirc.util.LogUtils.USER_ERROR;
  36. import static com.google.common.base.Preconditions.checkArgument;
  37. import static com.google.common.base.Preconditions.checkNotNull;
  38. /**
  39. * Provides a method to retrieve a parser.
  40. *
  41. * @since 0.6
  42. */
  43. public class ParserFactory {
  44. private static final Logger LOG = LoggerFactory.getLogger(ParserFactory.class);
  45. /** The name of the general domain. */
  46. private static final String DOMAIN_GENERAL = "general";
  47. /** The name of the server domain. */
  48. private static final String DOMAIN_SERVER = "server";
  49. /** ServiceManager used by this ParserFactory */
  50. private final ServiceManager serviceManager;
  51. /**
  52. * Creates a new instance of {@link ParserFactory}.
  53. */
  54. @Inject
  55. public ParserFactory(final ServiceManager serviceManager) {
  56. this.serviceManager = serviceManager;
  57. }
  58. /**
  59. * Creates a new parser instance.
  60. *
  61. * @param profile The profile to use
  62. * @param address The address of the server to connect to
  63. * @param configManager The config manager to read settings from
  64. *
  65. * @return An appropriately configured parser
  66. *
  67. * @since 0.6.3
  68. */
  69. public Optional<Parser> getParser(final Profile profile, final URI address,
  70. final ReadOnlyConfigProvider configManager) {
  71. final Object obj = getExportResult(address, "getParser", buildMyInfo(profile), address);
  72. if (obj instanceof Parser) {
  73. final Parser parser = (Parser) obj;
  74. parser.setPingTimerInterval(configManager.getOptionInt(DOMAIN_SERVER, "pingtimer"));
  75. parser.setPingTimerFraction((int) (configManager.getOptionInt(DOMAIN_SERVER,
  76. "pingfrequency") / parser.getPingTimerInterval()));
  77. if (configManager.hasOptionString(DOMAIN_GENERAL, "bindip")) {
  78. parser.setBindIP(configManager.getOption(DOMAIN_GENERAL, "bindip"));
  79. }
  80. parser.setProxy(buildProxyURI(configManager));
  81. return Optional.of(parser);
  82. }
  83. return Optional.empty();
  84. }
  85. /**
  86. * Retrieves the MyInfo object used for the Parser.
  87. *
  88. * @return The MyInfo object for our profile
  89. */
  90. private MyInfo buildMyInfo(final Profile profile) {
  91. checkNotNull(profile);
  92. checkArgument(!profile.getNicknames().isEmpty());
  93. final MyInfo myInfo = new MyInfo();
  94. myInfo.setNickname(profile.getNicknames().get(0));
  95. myInfo.setRealname(profile.getRealname());
  96. profile.getIdent().ifPresent(myInfo::setUsername);
  97. return myInfo;
  98. }
  99. /**
  100. * Constructs a URI for the configured proxy for this server, if any.
  101. *
  102. * @return An appropriate URI or null if no proxy is configured
  103. */
  104. @Nullable
  105. private URI buildProxyURI(final ReadOnlyConfigProvider configManager) {
  106. if (configManager.hasOptionString(DOMAIN_SERVER, "proxy.address")) {
  107. final String type;
  108. if (configManager.hasOptionString(DOMAIN_SERVER, "proxy.type")) {
  109. type = configManager.getOption(DOMAIN_SERVER, "proxy.type");
  110. } else {
  111. type = "socks";
  112. }
  113. final int port;
  114. if (configManager.hasOptionInt(DOMAIN_SERVER, "proxy.port")) {
  115. port = configManager.getOptionInt(DOMAIN_SERVER, "proxy.port");
  116. } else {
  117. port = 8080;
  118. }
  119. final String host = configManager.getOptionString(DOMAIN_SERVER, "proxy.address");
  120. final String userInfo;
  121. if (configManager.hasOptionString(DOMAIN_SERVER, "proxy.username")
  122. && configManager.hasOptionString(DOMAIN_SERVER, "proxy.password")) {
  123. userInfo = configManager.getOption(DOMAIN_SERVER, "proxy.username")
  124. + configManager.getOption(DOMAIN_SERVER, "proxy.password");
  125. } else {
  126. userInfo = "";
  127. }
  128. try {
  129. return new URI(type, userInfo, host, port, "", "", "");
  130. } catch (URISyntaxException ex) {
  131. LOG.warn(APP_ERROR, "Unable to create proxy URI", ex);
  132. }
  133. }
  134. return null;
  135. }
  136. /**
  137. * Retrieves a protocol description.
  138. *
  139. * @param address The address to retrieve a description for
  140. *
  141. * @return A corresponding protocol description
  142. *
  143. * @since 0.6.4
  144. */
  145. public ProtocolDescription getDescription(final URI address) {
  146. final Object obj = getExportResult(address, "getProtocolDescription");
  147. if (obj instanceof ProtocolDescription) {
  148. return (ProtocolDescription) obj;
  149. }
  150. return null;
  151. }
  152. /**
  153. * Retrieves and executes an exported service with the specified name from a
  154. * {@link ServiceProvider} which can provide a parser for the specified address. If no such
  155. * provider or service is found, null is returned.
  156. *
  157. * @param address The address a provider is required for
  158. * @param serviceName The name of the service to be retrieved
  159. * @param args The arguments to be passed to the exported service
  160. *
  161. * @return The result from a relevant exported service, or null
  162. *
  163. * @since 0.6.4
  164. */
  165. protected Object getExportResult(final URI address,
  166. final String serviceName, final Object... args) {
  167. // TODO: Move default scheme to a setting
  168. final String scheme = address.getScheme() == null ? "irc" : address.getScheme();
  169. try {
  170. final Service service = serviceManager.getService("parser", scheme);
  171. if (service != null && !service.getProviders().isEmpty()) {
  172. final ServiceProvider provider = service.getProviders().get(0);
  173. if (provider != null) {
  174. provider.activateServices();
  175. return provider.getExportedService(serviceName)
  176. .execute(args);
  177. }
  178. }
  179. } catch (NoSuchProviderException nspe) {
  180. LOG.warn(USER_ERROR, "No parser found for {}", address.getScheme(), nspe);
  181. }
  182. return null;
  183. }
  184. }