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 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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;
  23. import com.dmdirc.config.profiles.Profile;
  24. import com.dmdirc.events.AppErrorEvent;
  25. import com.dmdirc.events.UserErrorEvent;
  26. import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
  27. import com.dmdirc.logger.ErrorLevel;
  28. import com.dmdirc.parser.common.MyInfo;
  29. import com.dmdirc.parser.interfaces.Parser;
  30. import com.dmdirc.parser.interfaces.ProtocolDescription;
  31. import com.dmdirc.plugins.NoSuchProviderException;
  32. import com.dmdirc.plugins.PluginManager;
  33. import com.dmdirc.plugins.Service;
  34. import com.dmdirc.plugins.ServiceProvider;
  35. import java.net.URI;
  36. import java.net.URISyntaxException;
  37. import java.util.Optional;
  38. import javax.annotation.Nullable;
  39. import javax.inject.Inject;
  40. import static com.google.common.base.Preconditions.checkArgument;
  41. import static com.google.common.base.Preconditions.checkNotNull;
  42. /**
  43. * Provides a method to retrieve a parser.
  44. *
  45. * @since 0.6
  46. */
  47. public class ParserFactory {
  48. /** The name of the general domain. */
  49. private static final String DOMAIN_GENERAL = "general";
  50. /** The name of the server domain. */
  51. private static final String DOMAIN_SERVER = "server";
  52. /** PluginManager used by this ParserFactory */
  53. private final PluginManager pluginManager;
  54. /** The event bus to post events to. */
  55. private final DMDircMBassador eventBus;
  56. /**
  57. * Creates a new instance of {@link ParserFactory}.
  58. *
  59. * @param pluginManager The plugin manager used by this factory.
  60. * @param eventBus The event bus to post events to.
  61. */
  62. @Inject
  63. public ParserFactory(
  64. final PluginManager pluginManager,
  65. final DMDircMBassador eventBus) {
  66. this.pluginManager = pluginManager;
  67. this.eventBus = eventBus;
  68. }
  69. /**
  70. * Creates a new parser instance.
  71. *
  72. * @param profile The profile to use
  73. * @param address The address of the server to connect to
  74. * @param configManager The config manager to read settings from
  75. *
  76. * @return An appropriately configured parser
  77. *
  78. * @since 0.6.3
  79. */
  80. public Optional<Parser> getParser(final Profile profile, final URI address,
  81. final ReadOnlyConfigProvider configManager) {
  82. final Object obj = getExportResult(address, "getParser", buildMyInfo(profile), address);
  83. if (obj instanceof Parser) {
  84. final Parser parser = (Parser) obj;
  85. parser.setPingTimerInterval(configManager.getOptionInt(DOMAIN_SERVER, "pingtimer"));
  86. parser.setPingTimerFraction((int) (configManager.getOptionInt(DOMAIN_SERVER,
  87. "pingfrequency") / parser.getPingTimerInterval()));
  88. if (configManager.hasOptionString(DOMAIN_GENERAL, "bindip")) {
  89. parser.setBindIP(configManager.getOption(DOMAIN_GENERAL, "bindip"));
  90. }
  91. parser.setProxy(buildProxyURI(configManager));
  92. return Optional.of(parser);
  93. }
  94. return Optional.empty();
  95. }
  96. /**
  97. * Retrieves the MyInfo object used for the Parser.
  98. *
  99. * @return The MyInfo object for our profile
  100. */
  101. private MyInfo buildMyInfo(final Profile profile) {
  102. checkNotNull(profile);
  103. checkArgument(!profile.getNicknames().isEmpty());
  104. final MyInfo myInfo = new MyInfo();
  105. myInfo.setNickname(profile.getNicknames().get(0));
  106. myInfo.setRealname(profile.getRealname());
  107. profile.getIdent().ifPresent(myInfo::setUsername);
  108. return myInfo;
  109. }
  110. /**
  111. * Constructs a URI for the configured proxy for this server, if any.
  112. *
  113. * @return An appropriate URI or null if no proxy is configured
  114. */
  115. @Nullable
  116. private URI buildProxyURI(final ReadOnlyConfigProvider configManager) {
  117. if (configManager.hasOptionString(DOMAIN_SERVER, "proxy.address")) {
  118. final String type;
  119. if (configManager.hasOptionString(DOMAIN_SERVER, "proxy.type")) {
  120. type = configManager.getOption(DOMAIN_SERVER, "proxy.type");
  121. } else {
  122. type = "socks";
  123. }
  124. final int port;
  125. if (configManager.hasOptionInt(DOMAIN_SERVER, "proxy.port")) {
  126. port = configManager.getOptionInt(DOMAIN_SERVER, "proxy.port");
  127. } else {
  128. port = 8080;
  129. }
  130. final String host = configManager.getOptionString(DOMAIN_SERVER, "proxy.address");
  131. final String userInfo;
  132. if (configManager.hasOptionString(DOMAIN_SERVER, "proxy.username")
  133. && configManager.hasOptionString(DOMAIN_SERVER, "proxy.password")) {
  134. userInfo = configManager.getOption(DOMAIN_SERVER, "proxy.username")
  135. + configManager.getOption(DOMAIN_SERVER, "proxy.password");
  136. } else {
  137. userInfo = "";
  138. }
  139. try {
  140. return new URI(type, userInfo, host, port, "", "", "");
  141. } catch (URISyntaxException ex) {
  142. eventBus.publish(
  143. new AppErrorEvent(ErrorLevel.MEDIUM, ex, "Unable to create proxy URI", ""));
  144. }
  145. }
  146. return null;
  147. }
  148. /**
  149. * Retrieves a protocol description.
  150. *
  151. * @param address The address to retrieve a description for
  152. *
  153. * @return A corresponding protocol description
  154. *
  155. * @since 0.6.4
  156. */
  157. public ProtocolDescription getDescription(final URI address) {
  158. final Object obj = getExportResult(address, "getProtocolDescription");
  159. if (obj instanceof ProtocolDescription) {
  160. return (ProtocolDescription) obj;
  161. }
  162. return null;
  163. }
  164. /**
  165. * Retrieves and executes an exported service with the specified name from a
  166. * {@link ServiceProvider} which can provide a parser for the specified address. If no such
  167. * provider or service is found, null is returned.
  168. *
  169. * @param address The address a provider is required for
  170. * @param serviceName The name of the service to be retrieved
  171. * @param args The arguments to be passed to the exported service
  172. *
  173. * @return The result from a relevant exported service, or null
  174. *
  175. * @since 0.6.4
  176. */
  177. protected Object getExportResult(final URI address,
  178. final String serviceName, final Object... args) {
  179. // TODO: Move default scheme to a setting
  180. final String scheme = address.getScheme() == null ? "irc" : address.getScheme();
  181. try {
  182. final Service service = pluginManager.getService("parser", scheme);
  183. if (service != null && !service.getProviders().isEmpty()) {
  184. final ServiceProvider provider = service.getProviders().get(0);
  185. if (provider != null) {
  186. provider.activateServices();
  187. return provider.getExportedService(serviceName)
  188. .execute(args);
  189. }
  190. }
  191. } catch (NoSuchProviderException nspe) {
  192. eventBus.publishAsync(new UserErrorEvent(ErrorLevel.MEDIUM, nspe,
  193. "No parser found for: " + address.getScheme(), ""));
  194. }
  195. return null;
  196. }
  197. }