Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Server.java 57KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  1. /*
  2. * Copyright (c) 2006-2014 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.ClientModule.UserConfig;
  24. import com.dmdirc.actions.ActionManager;
  25. import com.dmdirc.actions.CoreActionType;
  26. import com.dmdirc.commandparser.CommandType;
  27. import com.dmdirc.commandparser.parsers.CommandParser;
  28. import com.dmdirc.interfaces.AwayStateListener;
  29. import com.dmdirc.interfaces.Connection;
  30. import com.dmdirc.interfaces.InviteListener;
  31. import com.dmdirc.interfaces.config.ConfigChangeListener;
  32. import com.dmdirc.interfaces.config.ConfigProvider;
  33. import com.dmdirc.interfaces.config.ConfigProviderMigrator;
  34. import com.dmdirc.interfaces.config.IdentityFactory;
  35. import com.dmdirc.logger.ErrorLevel;
  36. import com.dmdirc.logger.Logger;
  37. import com.dmdirc.messages.MessageSinkManager;
  38. import com.dmdirc.parser.common.ChannelJoinRequest;
  39. import com.dmdirc.parser.common.DefaultStringConverter;
  40. import com.dmdirc.parser.common.IgnoreList;
  41. import com.dmdirc.parser.common.MyInfo;
  42. import com.dmdirc.parser.common.ParserError;
  43. import com.dmdirc.parser.common.ThreadedParser;
  44. import com.dmdirc.parser.interfaces.ChannelInfo;
  45. import com.dmdirc.parser.interfaces.ClientInfo;
  46. import com.dmdirc.parser.interfaces.EncodingParser;
  47. import com.dmdirc.parser.interfaces.Parser;
  48. import com.dmdirc.parser.interfaces.ProtocolDescription;
  49. import com.dmdirc.parser.interfaces.SecureParser;
  50. import com.dmdirc.parser.interfaces.StringConverter;
  51. import com.dmdirc.tls.CertificateManager;
  52. import com.dmdirc.tls.CertificateProblemListener;
  53. import com.dmdirc.ui.StatusMessage;
  54. import com.dmdirc.ui.WindowManager;
  55. import com.dmdirc.ui.core.components.StatusBarManager;
  56. import com.dmdirc.ui.core.components.WindowComponent;
  57. import com.dmdirc.ui.input.TabCompleter;
  58. import com.dmdirc.ui.input.TabCompleterFactory;
  59. import com.dmdirc.ui.input.TabCompletionType;
  60. import com.dmdirc.ui.messages.Formatter;
  61. import com.dmdirc.util.annotations.factory.Factory;
  62. import com.dmdirc.util.annotations.factory.Unbound;
  63. import java.net.URI;
  64. import java.net.URISyntaxException;
  65. import java.security.cert.CertificateException;
  66. import java.security.cert.X509Certificate;
  67. import java.util.ArrayList;
  68. import java.util.Arrays;
  69. import java.util.Collection;
  70. import java.util.Collections;
  71. import java.util.Date;
  72. import java.util.HashSet;
  73. import java.util.List;
  74. import java.util.Map;
  75. import java.util.Random;
  76. import java.util.Set;
  77. import java.util.Timer;
  78. import java.util.TimerTask;
  79. import java.util.concurrent.ConcurrentSkipListMap;
  80. import java.util.concurrent.locks.ReadWriteLock;
  81. import java.util.concurrent.locks.ReentrantReadWriteLock;
  82. import javax.net.ssl.TrustManager;
  83. import org.slf4j.LoggerFactory;
  84. import static com.google.common.base.Preconditions.checkArgument;
  85. import static com.google.common.base.Preconditions.checkNotNull;
  86. /**
  87. * The Server class represents the client's view of a server. It maintains
  88. * a list of all channels, queries, etc, and handles parser callbacks pertaining
  89. * to the server.
  90. */
  91. @Factory(inject = true, singleton = true, providers = true, name = "ServerFactoryImpl")
  92. public class Server extends WritableFrameContainer implements ConfigChangeListener,
  93. CertificateProblemListener, Connection {
  94. private static final org.slf4j.Logger log = LoggerFactory.getLogger(Server.class);
  95. /** The name of the general domain. */
  96. private static final String DOMAIN_GENERAL = "general";
  97. /** The name of the profile domain. */
  98. private static final String DOMAIN_PROFILE = "profile";
  99. /** The name of the server domain. */
  100. private static final String DOMAIN_SERVER = "server";
  101. /** Open channels that currently exist on the server. */
  102. private final Map<String, Channel> channels = new ConcurrentSkipListMap<>();
  103. /** Open query windows on the server. */
  104. private final Map<String, Query> queries = new ConcurrentSkipListMap<>();
  105. /** The Parser instance handling this server. */
  106. private Parser parser;
  107. /** The Parser instance that used to be handling this server. */
  108. private Parser oldParser;
  109. /** The parser-supplied protocol description object. */
  110. private ProtocolDescription protocolDescription;
  111. /**
  112. * Object used to synchronise access to parser. This object should be
  113. * locked by anything requiring that the parser reference remains the same
  114. * for a duration of time, or by anything which is updating the parser
  115. * reference.
  116. *
  117. * If used in conjunction with myStateLock, the parserLock must always be
  118. * locked INSIDE the myStateLock to prevent deadlocks.
  119. */
  120. private final ReadWriteLock parserLock = new ReentrantReadWriteLock();
  121. /** The raw frame used for this server instance. */
  122. private Raw raw;
  123. /** The address of the server we're connecting to. */
  124. private URI address;
  125. /** The profile we're using. */
  126. private ConfigProvider profile;
  127. /** Object used to synchronise access to myState. */
  128. private final Object myStateLock = new Object();
  129. /** The current state of this server. */
  130. private final ServerStatus myState = new ServerStatus(this, myStateLock);
  131. /** The timer we're using to delay reconnects. */
  132. private Timer reconnectTimer;
  133. /** The timer we're using to send WHO requests. */
  134. private final Timer whoTimer;
  135. /** The tabcompleter used for this server. */
  136. private final TabCompleter tabCompleter;
  137. /** Our reason for being away, if any. */
  138. private String awayMessage;
  139. /** Our event handler. */
  140. private final ServerEventHandler eventHandler = new ServerEventHandler(this);
  141. /** A list of outstanding invites. */
  142. private final List<Invite> invites = new ArrayList<>();
  143. /** A set of channels we want to join without focusing. */
  144. private final Set<String> backgroundChannels = new HashSet<>();
  145. /** Our ignore list. */
  146. private final IgnoreList ignoreList = new IgnoreList();
  147. /** Our string convertor. */
  148. private StringConverter converter = new DefaultStringConverter();
  149. /** The certificate manager in use, if any. */
  150. private CertificateManager certificateManager;
  151. /** ParserFactory we use for creating parsers. */
  152. private final ParserFactory parserFactory;
  153. /** ServerManager that created us. */
  154. private final ServerManager manager;
  155. /** Factory to use to create new identities. */
  156. private final IdentityFactory identityFactory;
  157. /** Window manager to pas to children. */
  158. private final WindowManager windowManager;
  159. /** The migrator to use to change our config provider. */
  160. private final ConfigProviderMigrator configMigrator;
  161. /** Factory to use for creating channels. */
  162. private final ChannelFactory channelFactory;
  163. /** Factory to use for creating queries. */
  164. private final QueryFactory queryFactory;
  165. /** Factory to use for creating raw windows. */
  166. private final RawFactory rawFactory;
  167. /** The config provider to write user settings to. */
  168. private final ConfigProvider userSettings;
  169. /** The manager to use to add status bar messages. */
  170. private final StatusBarManager statusBarManager;
  171. /**
  172. * Creates a new server which will connect to the specified URL with
  173. * the specified profile.
  174. *
  175. * @since 0.6.3
  176. * @param manager The server manager that owns this server.
  177. * @param configMigrator The migrateable configuration manager to read config settings from.
  178. * @param commandParser The parser to use for commands in this server's window.
  179. * @param parserFactory The factory to use to generate parsers.
  180. * @param tabCompleterFactory The factory to use for tab completers.
  181. * @param identityFactory The factory to use to create identities.
  182. * @param messageSinkManager The sink manager to use to despatch messages.
  183. * @param statusBarManager The manager to use to add status bar messages.
  184. * @param windowManager Window Manager
  185. * @param channelFactory The factory to use to create channels.
  186. * @param queryFactory The factory to use to create queries.
  187. * @param rawFactory The factory to use to create raw windows.
  188. * @param userSettings The config provider to write user settings to.
  189. * @param uri The address of the server to connect to
  190. * @param profile The profile to use
  191. */
  192. public Server(
  193. final ServerManager manager,
  194. @Unbound final ConfigProviderMigrator configMigrator,
  195. @Unbound final CommandParser commandParser,
  196. final ParserFactory parserFactory,
  197. final TabCompleterFactory tabCompleterFactory,
  198. final IdentityFactory identityFactory,
  199. final MessageSinkManager messageSinkManager,
  200. final StatusBarManager statusBarManager,
  201. final WindowManager windowManager,
  202. final ChannelFactory channelFactory,
  203. final QueryFactory queryFactory,
  204. final RawFactory rawFactory,
  205. @SuppressWarnings("qualifiers") @UserConfig final ConfigProvider userSettings,
  206. @Unbound final URI uri,
  207. @Unbound final ConfigProvider profile) {
  208. super("server-disconnected",
  209. getHost(uri),
  210. getHost(uri),
  211. configMigrator.getConfigProvider(),
  212. commandParser,
  213. messageSinkManager,
  214. Arrays.asList(
  215. WindowComponent.TEXTAREA.getIdentifier(),
  216. WindowComponent.INPUTFIELD.getIdentifier(),
  217. WindowComponent.CERTIFICATE_VIEWER.getIdentifier()));
  218. this.manager = manager;
  219. this.parserFactory = parserFactory;
  220. this.identityFactory = identityFactory;
  221. this.windowManager = windowManager;
  222. this.configMigrator = configMigrator;
  223. this.channelFactory = channelFactory;
  224. this.queryFactory = queryFactory;
  225. this.rawFactory = rawFactory;
  226. this.userSettings = userSettings;
  227. this.statusBarManager = statusBarManager;
  228. setConnectionDetails(uri, profile);
  229. tabCompleter = tabCompleterFactory.getTabCompleter(getConfigManager(),
  230. CommandType.TYPE_SERVER, CommandType.TYPE_GLOBAL);
  231. updateIcon();
  232. // TODO: Don't start timers in the constructor!
  233. whoTimer = new Timer("Server Who Timer");
  234. whoTimer.schedule(new TimerTask() {
  235. @Override
  236. public void run() {
  237. for (Channel channel : channels.values()) {
  238. channel.checkWho();
  239. }
  240. }
  241. }, 0, getConfigManager().getOptionInt(DOMAIN_GENERAL, "whotime"));
  242. getConfigManager().addChangeListener("formatter", "serverName", this);
  243. getConfigManager().addChangeListener("formatter", "serverTitle", this);
  244. }
  245. /**
  246. * Updates the connection details for this server. If the specified URI
  247. * does not define a port, the default port from the protocol description
  248. * will be used.
  249. *
  250. * @param uri The new URI that this server should connect to
  251. * @param profile The profile that this server should use
  252. */
  253. private void setConnectionDetails(final URI uri, final ConfigProvider profile) {
  254. this.address = uri;
  255. this.protocolDescription = parserFactory.getDescription(uri);
  256. this.profile = profile;
  257. if (uri.getPort() == -1 && protocolDescription != null) {
  258. try {
  259. this.address = new URI(uri.getScheme(), uri.getUserInfo(),
  260. uri.getHost(), protocolDescription.getDefaultPort(),
  261. uri.getPath(), uri.getQuery(), uri.getFragment());
  262. } catch (URISyntaxException ex) {
  263. Logger.appError(ErrorLevel.MEDIUM, "Unable to construct URI", ex);
  264. }
  265. }
  266. }
  267. /** {@inheritDoc} */
  268. @Override
  269. public void connect() {
  270. connect(address, profile);
  271. }
  272. /** {@inheritDoc} */
  273. @Override
  274. @Precondition({
  275. "The current parser is null or not connected",
  276. "The specified profile is not null"
  277. })
  278. @SuppressWarnings("fallthrough")
  279. public void connect(final URI address, final ConfigProvider profile) {
  280. assert profile != null;
  281. synchronized (myStateLock) {
  282. log.info("Connecting to {}, current state is {}", address,
  283. myState.getState());
  284. switch (myState.getState()) {
  285. case RECONNECT_WAIT:
  286. log.debug("Cancelling reconnection timer");
  287. reconnectTimer.cancel();
  288. break;
  289. case CLOSING:
  290. // Ignore the connection attempt
  291. return;
  292. case CONNECTED:
  293. case CONNECTING:
  294. disconnect(getConfigManager().getOption(DOMAIN_GENERAL, "quitmessage"));
  295. case DISCONNECTING:
  296. while (!myState.getState().isDisconnected()) {
  297. try {
  298. myStateLock.wait();
  299. } catch (InterruptedException ex) {
  300. return;
  301. }
  302. }
  303. break;
  304. default:
  305. // Do nothing
  306. break;
  307. }
  308. final URI connectAddress;
  309. try {
  310. parserLock.writeLock().lock();
  311. if (parser != null) {
  312. throw new IllegalArgumentException("Connection attempt while parser "
  313. + "is still connected.\n\nMy state:" + getState());
  314. }
  315. configMigrator.migrate(address.getScheme(), "", "", address.getHost());
  316. setConnectionDetails(address, profile);
  317. updateTitle();
  318. updateIcon();
  319. parser = buildParser();
  320. if (parser == null) {
  321. addLine("serverUnknownProtocol", address.getScheme());
  322. return;
  323. }
  324. connectAddress = parser.getURI();
  325. } finally {
  326. parserLock.writeLock().unlock();
  327. }
  328. addLine("serverConnecting", connectAddress.getHost(), connectAddress.getPort());
  329. myState.transition(ServerState.CONNECTING);
  330. doCallbacks();
  331. updateAwayState(null);
  332. removeInvites();
  333. parser.connect();
  334. if (parser instanceof ThreadedParser) {
  335. ((ThreadedParser)parser).getControlThread().setName("Parser - " + connectAddress.getHost());
  336. }
  337. }
  338. ActionManager.getActionManager().triggerEvent(
  339. CoreActionType.SERVER_CONNECTING, null, this);
  340. }
  341. /** {@inheritDoc} */
  342. @Override
  343. public void reconnect(final String reason) {
  344. synchronized (myStateLock) {
  345. if (myState.getState() == ServerState.CLOSING) {
  346. return;
  347. }
  348. disconnect(reason);
  349. connect(address, profile);
  350. }
  351. }
  352. /** {@inheritDoc} */
  353. @Override
  354. public void reconnect() {
  355. reconnect(getConfigManager().getOption(DOMAIN_GENERAL, "reconnectmessage"));
  356. }
  357. /** {@inheritDoc} */
  358. @Override
  359. public void disconnect() {
  360. disconnect(getConfigManager().getOption(DOMAIN_GENERAL, "quitmessage"));
  361. }
  362. /** {@inheritDoc} */
  363. @Override
  364. public void disconnect(final String reason) {
  365. synchronized (myStateLock) {
  366. log.info("Disconnecting. Current state: {}", myState.getState());
  367. switch (myState.getState()) {
  368. case CLOSING:
  369. case DISCONNECTING:
  370. case DISCONNECTED:
  371. case TRANSIENTLY_DISCONNECTED:
  372. return;
  373. case RECONNECT_WAIT:
  374. log.debug("Cancelling reconnection timer");
  375. reconnectTimer.cancel();
  376. break;
  377. default:
  378. break;
  379. }
  380. clearChannels();
  381. backgroundChannels.clear();
  382. try {
  383. parserLock.readLock().lock();
  384. if (parser == null) {
  385. myState.transition(ServerState.DISCONNECTED);
  386. } else {
  387. myState.transition(ServerState.DISCONNECTING);
  388. removeInvites();
  389. updateIcon();
  390. parser.disconnect(reason);
  391. }
  392. } finally {
  393. parserLock.readLock().unlock();
  394. }
  395. if (getConfigManager().getOptionBool(DOMAIN_GENERAL,
  396. "closechannelsonquit")) {
  397. closeChannels();
  398. }
  399. if (getConfigManager().getOptionBool(DOMAIN_GENERAL,
  400. "closequeriesonquit")) {
  401. closeQueries();
  402. }
  403. }
  404. }
  405. /**
  406. * Schedules a reconnect attempt to be performed after a user-defiend delay.
  407. */
  408. @Precondition("The server state is transiently disconnected")
  409. private void doDelayedReconnect() {
  410. synchronized (myStateLock) {
  411. log.info("Performing delayed reconnect. State: {}", myState.getState());
  412. if (myState.getState() != ServerState.TRANSIENTLY_DISCONNECTED) {
  413. throw new IllegalStateException("doDelayedReconnect when not "
  414. + "transiently disconnected\n\nState: " + myState);
  415. }
  416. final int delay = Math.max(1000,
  417. getConfigManager().getOptionInt(DOMAIN_GENERAL, "reconnectdelay"));
  418. handleNotification("connectRetry", getAddress(), delay / 1000);
  419. reconnectTimer = new Timer("Server Reconnect Timer");
  420. reconnectTimer.schedule(new TimerTask() {
  421. @Override
  422. public void run() {
  423. reconnectTimer.cancel();
  424. synchronized (myStateLock) {
  425. log.debug("Reconnect task executing, state: {}",
  426. myState.getState());
  427. if (myState.getState() == ServerState.RECONNECT_WAIT) {
  428. myState.transition(ServerState.TRANSIENTLY_DISCONNECTED);
  429. reconnect();
  430. }
  431. }
  432. }
  433. }, delay);
  434. log.info("Scheduling reconnect task for delay of {}", delay);
  435. myState.transition(ServerState.RECONNECT_WAIT);
  436. updateIcon();
  437. }
  438. }
  439. /** {@inheritDoc} */
  440. @Override
  441. public boolean hasChannel(final String channel) {
  442. return channels.containsKey(converter.toLowerCase(channel));
  443. }
  444. /** {@inheritDoc} */
  445. @Override
  446. public Channel getChannel(final String channel) {
  447. return channels.get(converter.toLowerCase(channel));
  448. }
  449. /** {@inheritDoc} */
  450. @Override
  451. public List<String> getChannels() {
  452. return new ArrayList<>(channels.keySet());
  453. }
  454. /** {@inheritDoc} */
  455. @Override
  456. public boolean hasQuery(final String host) {
  457. return queries.containsKey(converter.toLowerCase(parseHostmask(host)[0]));
  458. }
  459. /** {@inheritDoc} */
  460. @Override
  461. public Query getQuery(final String host) {
  462. return getQuery(host, false);
  463. }
  464. /** {@inheritDoc} */
  465. @Override
  466. public Query getQuery(final String host, final boolean focus) {
  467. synchronized (myStateLock) {
  468. if (myState.getState() == ServerState.CLOSING) {
  469. // Can't open queries while the server is closing
  470. return null;
  471. }
  472. }
  473. final String nick = parseHostmask(host)[0];
  474. final String lnick = converter.toLowerCase(nick);
  475. if (!queries.containsKey(lnick)) {
  476. final Query newQuery = queryFactory.getQuery(this, host);
  477. windowManager.addWindow(this, newQuery, focus);
  478. ActionManager.getActionManager().triggerEvent(
  479. CoreActionType.QUERY_OPENED, null, newQuery);
  480. tabCompleter.addEntry(TabCompletionType.QUERY_NICK, nick);
  481. queries.put(lnick, newQuery);
  482. }
  483. return queries.get(lnick);
  484. }
  485. /** {@inheritDoc} */
  486. @Override
  487. public void updateQuery(final Query query, final String oldNick, final String newNick) {
  488. tabCompleter.removeEntry(TabCompletionType.QUERY_NICK, oldNick);
  489. tabCompleter.addEntry(TabCompletionType.QUERY_NICK, newNick);
  490. queries.put(converter.toLowerCase(newNick), query);
  491. queries.remove(converter.toLowerCase(oldNick));
  492. }
  493. /** {@inheritDoc} */
  494. @Override
  495. public Collection<Query> getQueries() {
  496. return Collections.unmodifiableCollection(queries.values());
  497. }
  498. /** {@inheritDoc} */
  499. @Override
  500. public void delQuery(final Query query) {
  501. tabCompleter.removeEntry(TabCompletionType.QUERY_NICK, query.getNickname());
  502. queries.remove(converter.toLowerCase(query.getNickname()));
  503. }
  504. /** {@inheritDoc} */
  505. @Override
  506. public void addRaw() {
  507. if (raw == null) {
  508. raw = rawFactory.getRaw(this);
  509. windowManager.addWindow(this, raw);
  510. try {
  511. parserLock.readLock().lock();
  512. if (parser != null) {
  513. raw.registerCallbacks();
  514. }
  515. } finally {
  516. parserLock.readLock().unlock();
  517. }
  518. }
  519. }
  520. /** {@inheritDoc} */
  521. @Override
  522. public void delRaw() {
  523. raw = null; //NOPMD
  524. }
  525. /** {@inheritDoc} */
  526. @Override
  527. public void delChannel(final String chan) {
  528. tabCompleter.removeEntry(TabCompletionType.CHANNEL, chan);
  529. channels.remove(converter.toLowerCase(chan));
  530. }
  531. /** {@inheritDoc} */
  532. @Override
  533. public Channel addChannel(final ChannelInfo chan) {
  534. return addChannel(chan, !backgroundChannels.contains(chan.getName())
  535. || getConfigManager().getOptionBool(DOMAIN_GENERAL,
  536. "hidechannels"));
  537. }
  538. /** {@inheritDoc} */
  539. @Override
  540. public Channel addChannel(final ChannelInfo chan, final boolean focus) {
  541. synchronized (myStateLock) {
  542. if (myState.getState() == ServerState.CLOSING) {
  543. // Can't join channels while the server is closing
  544. return null;
  545. }
  546. }
  547. backgroundChannels.remove(chan.getName());
  548. if (hasChannel(chan.getName())) {
  549. getChannel(chan.getName()).setChannelInfo(chan);
  550. getChannel(chan.getName()).selfJoin();
  551. } else {
  552. final ConfigProviderMigrator channelConfig = identityFactory.createMigratableConfig(
  553. getProtocol(), getIrcd(), getNetwork(), getAddress(), chan.getName());
  554. final Channel newChan = channelFactory.getChannel(this, chan, channelConfig);
  555. windowManager.addWindow(this, newChan, focus);
  556. ActionManager.getActionManager().triggerEvent(CoreActionType.CHANNEL_OPENED, null,
  557. newChan);
  558. tabCompleter.addEntry(TabCompletionType.CHANNEL, chan.getName());
  559. channels.put(converter.toLowerCase(chan.getName()), newChan);
  560. }
  561. return getChannel(chan.getName());
  562. }
  563. /**
  564. * Closes all open channel windows associated with this server.
  565. */
  566. private void closeChannels() {
  567. for (Channel channel : new ArrayList<>(channels.values())) {
  568. channel.close();
  569. }
  570. }
  571. /**
  572. * Clears the nicklist of all open channels.
  573. */
  574. private void clearChannels() {
  575. for (Channel channel : channels.values()) {
  576. channel.resetWindow();
  577. }
  578. }
  579. /**
  580. * Closes all open query windows associated with this server.
  581. */
  582. private void closeQueries() {
  583. for (Query query : new ArrayList<>(queries.values())) {
  584. query.close();
  585. }
  586. }
  587. /**
  588. * Retrieves the host component of the specified URI, or throws a relevant
  589. * exception if this is not possible.
  590. *
  591. * @param uri The URI to be processed
  592. * @return The URI's host component, as returned by {@link URI#getHost()}.
  593. * @throws NullPointerException If <code>uri</code> is null
  594. * @throws IllegalArgumentException If the specified URI has no host
  595. * @since 0.6.4
  596. */
  597. private static String getHost(final URI uri) {
  598. if (uri.getHost() == null) {
  599. throw new IllegalArgumentException("URIs must have hosts");
  600. }
  601. return uri.getHost();
  602. }
  603. /**
  604. * Builds an appropriately configured {@link Parser} for this server.
  605. *
  606. * @return A configured parser.
  607. */
  608. private Parser buildParser() {
  609. final MyInfo myInfo = buildMyInfo();
  610. final Parser myParser = parserFactory.getParser(myInfo, address);
  611. if (myParser instanceof SecureParser) {
  612. certificateManager = new CertificateManager(
  613. address.getHost(), getConfigManager(), userSettings);
  614. final SecureParser secureParser = (SecureParser) myParser;
  615. secureParser.setTrustManagers(new TrustManager[]{certificateManager});
  616. secureParser.setKeyManagers(certificateManager.getKeyManager());
  617. certificateManager.addCertificateProblemListener(this);
  618. }
  619. if (myParser instanceof EncodingParser) {
  620. final EncodingParser encodingParser = (EncodingParser) myParser;
  621. encodingParser.setEncoder(new MessageEncoder(this, myParser));
  622. }
  623. if (myParser != null) {
  624. myParser.setIgnoreList(ignoreList);
  625. myParser.setPingTimerInterval(getConfigManager().getOptionInt(DOMAIN_SERVER,
  626. "pingtimer"));
  627. myParser.setPingTimerFraction((int) (getConfigManager().getOptionInt(DOMAIN_SERVER,
  628. "pingfrequency") / myParser.getPingTimerInterval()));
  629. if (getConfigManager().hasOptionString(DOMAIN_GENERAL, "bindip")) {
  630. myParser.setBindIP(getConfigManager().getOption(DOMAIN_GENERAL, "bindip"));
  631. }
  632. myParser.setProxy(buildProxyURI());
  633. }
  634. return myParser;
  635. }
  636. /**
  637. * Constructs a URI for the configured proxy for this server, if any.
  638. *
  639. * @return An appropriate URI or null if no proxy is configured
  640. */
  641. private URI buildProxyURI() {
  642. if (getConfigManager().hasOptionString(DOMAIN_SERVER, "proxy.address")) {
  643. final String type;
  644. if (getConfigManager().hasOptionString(DOMAIN_SERVER, "proxy.type")) {
  645. type = getConfigManager().getOption(DOMAIN_SERVER, "proxy.type");
  646. } else {
  647. type = "socks";
  648. }
  649. final int port;
  650. if (getConfigManager().hasOptionInt(DOMAIN_SERVER, "proxy.port")) {
  651. port = getConfigManager().getOptionInt(DOMAIN_SERVER, "proxy.port");
  652. } else {
  653. port = 8080;
  654. }
  655. final String host = getConfigManager().getOptionString(DOMAIN_SERVER, "proxy.address");
  656. final String userInfo;
  657. if (getConfigManager().hasOptionString(DOMAIN_SERVER, "proxy.username")
  658. && getConfigManager().hasOptionString(DOMAIN_SERVER, "proxy.password")) {
  659. userInfo = getConfigManager().getOption(DOMAIN_SERVER, "proxy.username")
  660. + getConfigManager().getOption(DOMAIN_SERVER, "proxy.password");
  661. } else {
  662. userInfo = "";
  663. }
  664. try {
  665. return new URI(type, userInfo, host, port, "", "", "");
  666. } catch (URISyntaxException ex) {
  667. Logger.appError(ErrorLevel.MEDIUM, "Unable to create proxy URI", ex);
  668. }
  669. }
  670. return null;
  671. }
  672. /** {@inheritDoc} */
  673. @Override
  674. public boolean compareURI(final URI uri) {
  675. if (parser != null) {
  676. return parser.compareURI(uri);
  677. }
  678. if (oldParser != null) {
  679. return oldParser.compareURI(uri);
  680. }
  681. return false;
  682. }
  683. /** {@inheritDoc} */
  684. @Override
  685. public String[] parseHostmask(final String hostmask) {
  686. return protocolDescription.parseHostmask(hostmask);
  687. }
  688. /**
  689. * Retrieves the MyInfo object used for the Parser.
  690. *
  691. * @return The MyInfo object for our profile
  692. */
  693. @Precondition({
  694. "The current profile is not null",
  695. "The current profile specifies at least one nickname"
  696. })
  697. private MyInfo buildMyInfo() {
  698. checkNotNull(profile);
  699. checkArgument(!profile.getOptionList(DOMAIN_PROFILE, "nicknames").isEmpty());
  700. final MyInfo myInfo = new MyInfo();
  701. myInfo.setNickname(profile.getOptionList(DOMAIN_PROFILE, "nicknames").get(0));
  702. myInfo.setRealname(profile.getOption(DOMAIN_PROFILE, "realname"));
  703. if (profile.hasOptionString(DOMAIN_PROFILE, "ident")) {
  704. myInfo.setUsername(profile.getOption(DOMAIN_PROFILE, "ident"));
  705. }
  706. return myInfo;
  707. }
  708. /**
  709. * Updates this server's icon.
  710. */
  711. private void updateIcon() {
  712. final String icon = myState.getState() == ServerState.CONNECTED
  713. ? protocolDescription.isSecure(address)
  714. ? "secure-server" : "server" : "server-disconnected";
  715. setIcon(icon);
  716. }
  717. /**
  718. * Registers callbacks.
  719. */
  720. private void doCallbacks() {
  721. if (raw != null) {
  722. raw.registerCallbacks();
  723. }
  724. eventHandler.registerCallbacks();
  725. for (Query query : queries.values()) {
  726. query.reregister();
  727. }
  728. }
  729. /** {@inheritDoc} */
  730. @Override
  731. public void join(final ChannelJoinRequest ... requests) {
  732. join(true, requests);
  733. }
  734. /** {@inheritDoc} */
  735. @Override
  736. public void join(final boolean focus, final ChannelJoinRequest ... requests) {
  737. synchronized (myStateLock) {
  738. if (myState.getState() == ServerState.CONNECTED) {
  739. final List<ChannelJoinRequest> pending = new ArrayList<>();
  740. for (ChannelJoinRequest request : requests) {
  741. removeInvites(request.getName());
  742. final String name;
  743. if (parser.isValidChannelName(request.getName())) {
  744. name = request.getName();
  745. } else {
  746. name = parser.getChannelPrefixes().substring(0, 1)
  747. + request.getName();
  748. }
  749. if (!hasChannel(name) || !getChannel(name).isOnChannel()) {
  750. if (!focus) {
  751. backgroundChannels.add(name);
  752. }
  753. pending.add(request);
  754. }
  755. }
  756. parser.joinChannels(pending.toArray(new ChannelJoinRequest[pending.size()]));
  757. }
  758. // TODO: otherwise: address.getChannels().add(channel);
  759. }
  760. }
  761. /** {@inheritDoc} */
  762. @Override
  763. public void sendLine(final String line) {
  764. synchronized (myStateLock) {
  765. try {
  766. parserLock.readLock().lock();
  767. if (parser != null && !line.isEmpty()
  768. && myState.getState() == ServerState.CONNECTED) {
  769. parser.sendRawMessage(line);
  770. }
  771. } finally {
  772. parserLock.readLock().unlock();
  773. }
  774. }
  775. }
  776. /** {@inheritDoc} */
  777. @Override
  778. public int getMaxLineLength() {
  779. try {
  780. parserLock.readLock().lock();
  781. return parser == null ? -1 : parser.getMaxLength();
  782. } finally {
  783. parserLock.readLock().unlock();
  784. }
  785. }
  786. /** {@inheritDoc} */
  787. @Override
  788. public Parser getParser() {
  789. return parser;
  790. }
  791. /** {@inheritDoc} */
  792. @Override
  793. public ConfigProvider getProfile() {
  794. return profile;
  795. }
  796. /** {@inheritDoc} */
  797. @Override
  798. public String getChannelPrefixes() {
  799. try {
  800. parserLock.readLock().lock();
  801. return parser == null ? "#&" : parser.getChannelPrefixes();
  802. } finally {
  803. parserLock.readLock().unlock();
  804. }
  805. }
  806. /** {@inheritDoc} */
  807. @Override
  808. public String getAddress() {
  809. try {
  810. parserLock.readLock().lock();
  811. return parser == null ? address.getHost() : parser.getServerName();
  812. } finally {
  813. parserLock.readLock().unlock();
  814. }
  815. }
  816. /** {@inheritDoc} */
  817. @Override
  818. public String getNetwork() {
  819. try {
  820. parserLock.readLock().lock();
  821. if (parser == null) {
  822. throw new IllegalStateException("getNetwork called when "
  823. + "parser is null (state: " + getState() + ")");
  824. } else if (parser.getNetworkName().isEmpty()) {
  825. return getNetworkFromServerName(parser.getServerName());
  826. } else {
  827. return parser.getNetworkName();
  828. }
  829. } finally {
  830. parserLock.readLock().unlock();
  831. }
  832. }
  833. /** {@inheritDoc} */
  834. @Override
  835. public boolean isNetwork(final String target) {
  836. synchronized (myStateLock) {
  837. try {
  838. parserLock.readLock().lock();
  839. if (parser == null) {
  840. return false;
  841. } else {
  842. return getNetwork().equalsIgnoreCase(target);
  843. }
  844. } finally {
  845. parserLock.readLock().unlock();
  846. }
  847. }
  848. }
  849. /**
  850. * Calculates a network name from the specified server name. This method
  851. * implements parts 2-4 of the procedure documented at getNetwork().
  852. *
  853. * @param serverName The server name to parse
  854. * @return A network name for the specified server
  855. */
  856. protected static String getNetworkFromServerName(final String serverName) {
  857. final String[] parts = serverName.split("\\.");
  858. final String[] tlds = {"biz", "com", "info", "net", "org"};
  859. boolean isTLD = false;
  860. for (String tld : tlds) {
  861. if (serverName.endsWith("." + tld)) {
  862. isTLD = true;
  863. break;
  864. }
  865. }
  866. if (isTLD && parts.length > 2) {
  867. return parts[parts.length - 2] + "." + parts[parts.length - 1];
  868. } else if (parts.length > 2) {
  869. final StringBuilder network = new StringBuilder();
  870. for (int i = 1; i < parts.length; i++) {
  871. if (network.length() > 0) {
  872. network.append('.');
  873. }
  874. network.append(parts[i]);
  875. }
  876. return network.toString();
  877. } else {
  878. return serverName;
  879. }
  880. }
  881. /** {@inheritDoc} */
  882. @Override
  883. public String getIrcd() {
  884. return parser.getServerSoftwareType();
  885. }
  886. /** {@inheritDoc} */
  887. @Override
  888. public String getProtocol() {
  889. return address.getScheme();
  890. }
  891. /** {@inheritDoc} */
  892. @Override
  893. public boolean isAway() {
  894. return awayMessage != null;
  895. }
  896. /** {@inheritDoc} */
  897. @Override
  898. public String getAwayMessage() {
  899. return awayMessage;
  900. }
  901. /** {@inheritDoc} */
  902. @Override
  903. public TabCompleter getTabCompleter() {
  904. return tabCompleter;
  905. }
  906. /** {@inheritDoc} */
  907. @Override
  908. public ServerState getState() {
  909. return myState.getState();
  910. }
  911. /** {@inheritDoc} */
  912. @Override
  913. public ServerStatus getStatus() {
  914. return myState;
  915. }
  916. /** {@inheritDoc} */
  917. @Override
  918. public void windowClosing() {
  919. synchronized (myStateLock) {
  920. // 2: Remove any callbacks or listeners
  921. eventHandler.unregisterCallbacks();
  922. getConfigManager().removeListener(this);
  923. whoTimer.cancel();
  924. // 3: Trigger any actions neccessary
  925. disconnect();
  926. myState.transition(ServerState.CLOSING);
  927. }
  928. closeChannels();
  929. closeQueries();
  930. removeInvites();
  931. if (raw != null) {
  932. raw.close();
  933. }
  934. // 4: Trigger action for the window closing
  935. // 5: Inform any parents that the window is closing
  936. manager.unregisterServer(this);
  937. }
  938. /** {@inheritDoc} */
  939. @Override
  940. public void windowClosed() {
  941. // 7: Remove any references to the window and parents
  942. oldParser = null; //NOPMD
  943. parser = null; //NOPMD
  944. }
  945. /** {@inheritDoc} */
  946. @Override
  947. public void addLineToAll(final String messageType, final Date date,
  948. final Object... args) {
  949. for (Channel channel : channels.values()) {
  950. channel.addLine(messageType, date, args);
  951. }
  952. for (Query query : queries.values()) {
  953. query.addLine(messageType, date, args);
  954. }
  955. addLine(messageType, date, args);
  956. }
  957. /** {@inheritDoc} */
  958. @Override
  959. public void sendCTCPReply(final String source, final String type, final String args) {
  960. if (type.equalsIgnoreCase("VERSION")) {
  961. parser.sendCTCPReply(source, "VERSION", "DMDirc "
  962. + getConfigManager().getOption("version", "version")
  963. + " - http://www.dmdirc.com/");
  964. } else if (type.equalsIgnoreCase("PING")) {
  965. parser.sendCTCPReply(source, "PING", args);
  966. } else if (type.equalsIgnoreCase("CLIENTINFO")) {
  967. parser.sendCTCPReply(source, "CLIENTINFO", "VERSION PING CLIENTINFO");
  968. }
  969. }
  970. /** {@inheritDoc} */
  971. @Override
  972. public boolean isValidChannelName(final String channelName) {
  973. try {
  974. parserLock.readLock().lock();
  975. return hasChannel(channelName)
  976. || (parser != null && parser.isValidChannelName(channelName));
  977. } finally {
  978. parserLock.readLock().unlock();
  979. }
  980. }
  981. /** {@inheritDoc} */
  982. @Override
  983. public Connection getConnection() {
  984. return this;
  985. }
  986. /** {@inheritDoc} */
  987. @Override
  988. protected boolean processNotificationArg(final Object arg, final List<Object> args) {
  989. if (arg instanceof ClientInfo) {
  990. final ClientInfo clientInfo = (ClientInfo) arg;
  991. args.add(clientInfo.getNickname());
  992. args.add(clientInfo.getUsername());
  993. args.add(clientInfo.getHostname());
  994. return true;
  995. } else {
  996. return super.processNotificationArg(arg, args);
  997. }
  998. }
  999. /** {@inheritDoc} */
  1000. @Override
  1001. public void updateTitle() {
  1002. synchronized (myStateLock) {
  1003. if (myState.getState() == ServerState.CLOSING) {
  1004. return;
  1005. }
  1006. try {
  1007. parserLock.readLock().lock();
  1008. final Object[] arguments = new Object[]{
  1009. address.getHost(), parser == null ? "Unknown" : parser.getServerName(),
  1010. address.getPort(), parser == null ? "Unknown" : getNetwork(),
  1011. parser == null ? "Unknown" : parser.getLocalClient().getNickname(),
  1012. };
  1013. setName(Formatter.formatMessage(getConfigManager(),
  1014. "serverName", arguments));
  1015. setTitle(Formatter.formatMessage(getConfigManager(),
  1016. "serverTitle", arguments));
  1017. } finally {
  1018. parserLock.readLock().unlock();
  1019. }
  1020. }
  1021. }
  1022. /** {@inheritDoc} */
  1023. @Override
  1024. public void configChanged(final String domain, final String key) {
  1025. if ("formatter".equals(domain)) {
  1026. updateTitle();
  1027. }
  1028. }
  1029. /**
  1030. * Called when the server says that the nickname we're trying to use is
  1031. * already in use.
  1032. *
  1033. * @param nickname The nickname that we were trying to use
  1034. */
  1035. public void onNickInUse(final String nickname) {
  1036. final String lastNick = parser.getLocalClient().getNickname();
  1037. // If our last nick is still valid, ignore the in use message
  1038. if (!converter.equalsIgnoreCase(lastNick, nickname)) {
  1039. return;
  1040. }
  1041. String newNick = lastNick + new Random().nextInt(10);
  1042. final List<String> alts = profile.getOptionList(DOMAIN_PROFILE, "nicknames");
  1043. int offset = 0;
  1044. // Loop so we can check case sensitivity
  1045. for (String alt : alts) {
  1046. offset++;
  1047. if (converter.equalsIgnoreCase(alt, lastNick)) {
  1048. break;
  1049. }
  1050. }
  1051. if (offset < alts.size() && !alts.get(offset).isEmpty()) {
  1052. newNick = alts.get(offset);
  1053. }
  1054. parser.getLocalClient().setNickname(newNick);
  1055. }
  1056. /**
  1057. * Called when the server sends a numeric event.
  1058. *
  1059. * @param numeric The numeric code for the event
  1060. * @param tokens The (tokenised) arguments of the event
  1061. */
  1062. public void onNumeric(final int numeric, final String[] tokens) {
  1063. String snumeric = String.valueOf(numeric);
  1064. if (numeric < 10) {
  1065. snumeric = "00" + snumeric;
  1066. } else if (numeric < 100) {
  1067. snumeric = "0" + snumeric;
  1068. }
  1069. final String sansIrcd = "numeric_" + snumeric;
  1070. StringBuffer target = new StringBuffer("");
  1071. if (getConfigManager().hasOptionString("formatter", sansIrcd)) {
  1072. target = new StringBuffer(sansIrcd);
  1073. } else if (getConfigManager().hasOptionString("formatter", "numeric_unknown")) {
  1074. target = new StringBuffer("numeric_unknown");
  1075. }
  1076. ActionManager.getActionManager().triggerEvent(
  1077. CoreActionType.SERVER_NUMERIC, target, this,
  1078. Integer.valueOf(numeric), tokens);
  1079. handleNotification(target.toString(), (Object[]) tokens);
  1080. }
  1081. /**
  1082. * Called when the socket has been closed.
  1083. */
  1084. public void onSocketClosed() {
  1085. log.info("Received socket closed event, state: {}", myState.getState());
  1086. if (Thread.holdsLock(myStateLock)) {
  1087. log.info("State lock contended: rerunning on a new thread");
  1088. new Thread(new Runnable() {
  1089. /** {@inheritDoc} */
  1090. @Override
  1091. public void run() {
  1092. onSocketClosed();
  1093. }
  1094. }, "Socket closed deferred thread").start();
  1095. return;
  1096. }
  1097. handleNotification("socketClosed", getAddress());
  1098. ActionManager.getActionManager().triggerEvent(
  1099. CoreActionType.SERVER_DISCONNECTED, null, this);
  1100. eventHandler.unregisterCallbacks();
  1101. synchronized (myStateLock) {
  1102. if (myState.getState() == ServerState.CLOSING
  1103. || myState.getState() == ServerState.DISCONNECTED) {
  1104. // This has been triggered via .disconnect()
  1105. return;
  1106. }
  1107. if (myState.getState() == ServerState.DISCONNECTING) {
  1108. myState.transition(ServerState.DISCONNECTED);
  1109. } else {
  1110. myState.transition(ServerState.TRANSIENTLY_DISCONNECTED);
  1111. }
  1112. clearChannels();
  1113. try {
  1114. parserLock.writeLock().lock();
  1115. oldParser = parser;
  1116. parser = null;
  1117. } finally {
  1118. parserLock.writeLock().unlock();
  1119. }
  1120. updateIcon();
  1121. if (getConfigManager().getOptionBool(DOMAIN_GENERAL,
  1122. "closechannelsondisconnect")) {
  1123. closeChannels();
  1124. }
  1125. if (getConfigManager().getOptionBool(DOMAIN_GENERAL,
  1126. "closequeriesondisconnect")) {
  1127. closeQueries();
  1128. }
  1129. removeInvites();
  1130. updateAwayState(null);
  1131. if (getConfigManager().getOptionBool(DOMAIN_GENERAL,
  1132. "reconnectondisconnect")
  1133. && myState.getState() == ServerState.TRANSIENTLY_DISCONNECTED) {
  1134. doDelayedReconnect();
  1135. }
  1136. }
  1137. }
  1138. /**
  1139. * Called when an error was encountered while connecting.
  1140. *
  1141. * @param errorInfo The parser's error information
  1142. */
  1143. @Precondition("The current server state is CONNECTING")
  1144. public void onConnectError(final ParserError errorInfo) {
  1145. synchronized (myStateLock) {
  1146. log.info("Received connect error event, state: {}; error: {}",
  1147. myState.getState(), errorInfo);
  1148. if (myState.getState() == ServerState.CLOSING
  1149. || myState.getState() == ServerState.DISCONNECTING) {
  1150. // Do nothing
  1151. return;
  1152. } else if (myState.getState() != ServerState.CONNECTING) {
  1153. // Shouldn't happen
  1154. throw new IllegalStateException("Connect error when not "
  1155. + "connecting\n\n" + getStatus().getTransitionHistory());
  1156. }
  1157. myState.transition(ServerState.TRANSIENTLY_DISCONNECTED);
  1158. try {
  1159. parserLock.writeLock().lock();
  1160. oldParser = parser;
  1161. parser = null;
  1162. } finally {
  1163. parserLock.writeLock().unlock();
  1164. }
  1165. updateIcon();
  1166. String description;
  1167. if (errorInfo.getException() == null) {
  1168. description = errorInfo.getData();
  1169. } else {
  1170. final Exception exception = errorInfo.getException();
  1171. if (exception instanceof java.net.UnknownHostException) {
  1172. description = "Unknown host (unable to resolve)";
  1173. } else if (exception instanceof java.net.NoRouteToHostException) {
  1174. description = "No route to host";
  1175. } else if (exception instanceof java.net.SocketTimeoutException) {
  1176. description = "Connection attempt timed out";
  1177. } else if (exception instanceof java.net.SocketException
  1178. || exception instanceof javax.net.ssl.SSLException) {
  1179. description = exception.getMessage();
  1180. } else {
  1181. Logger.appError(ErrorLevel.LOW, "Unknown socket error: "
  1182. + exception.getClass().getCanonicalName(),
  1183. new IllegalArgumentException(exception));
  1184. description = "Unknown error: " + exception.getMessage();
  1185. }
  1186. }
  1187. ActionManager.getActionManager().triggerEvent(
  1188. CoreActionType.SERVER_CONNECTERROR, null, this,
  1189. description);
  1190. handleNotification("connectError", getAddress(), description);
  1191. if (getConfigManager().getOptionBool(DOMAIN_GENERAL,
  1192. "reconnectonconnectfailure")) {
  1193. doDelayedReconnect();
  1194. }
  1195. }
  1196. }
  1197. /**
  1198. * Called when we fail to receive a ping reply within a set period of time.
  1199. */
  1200. public void onPingFailed() {
  1201. statusBarManager.setMessage(new StatusMessage(
  1202. "No ping reply from " + getName() + " for over "
  1203. + ((int) (Math.floor(parser.getPingTime() / 1000.0)))
  1204. + " seconds.", getConfigManager()));
  1205. ActionManager.getActionManager().triggerEvent(
  1206. CoreActionType.SERVER_NOPING, null, this,
  1207. Long.valueOf(parser.getPingTime()));
  1208. if (parser.getPingTime()
  1209. >= getConfigManager().getOptionInt(DOMAIN_SERVER, "pingtimeout")) {
  1210. log.warn("Server appears to be stoned, reconnecting");
  1211. handleNotification("stonedServer", getAddress());
  1212. reconnect();
  1213. }
  1214. }
  1215. /**
  1216. * Called after the parser receives the 005 headers from the server.
  1217. */
  1218. @Precondition("State is CONNECTING")
  1219. public void onPost005() {
  1220. synchronized (myStateLock) {
  1221. if (myState.getState() != ServerState.CONNECTING) {
  1222. // Shouldn't happen
  1223. throw new IllegalStateException("Received onPost005 while not "
  1224. + "connecting\n\n" + myState.getTransitionHistory());
  1225. }
  1226. if (myState.getState() != ServerState.CONNECTING) {
  1227. // We've transitioned while waiting for the lock. Just abort.
  1228. return;
  1229. }
  1230. myState.transition(ServerState.CONNECTED);
  1231. configMigrator.migrate(address.getScheme(),
  1232. parser.getServerSoftwareType(), getNetwork(), parser.getServerName());
  1233. updateIcon();
  1234. updateTitle();
  1235. updateIgnoreList();
  1236. converter = parser.getStringConverter();
  1237. final List<ChannelJoinRequest> requests = new ArrayList<>();
  1238. if (getConfigManager().getOptionBool(DOMAIN_GENERAL, "rejoinchannels")) {
  1239. for (Channel chan : channels.values()) {
  1240. requests.add(new ChannelJoinRequest(chan.getName()));
  1241. }
  1242. }
  1243. join(requests.toArray(new ChannelJoinRequest[requests.size()]));
  1244. checkModeAliases();
  1245. }
  1246. ActionManager.getActionManager().triggerEvent(
  1247. CoreActionType.SERVER_CONNECTED, null, this);
  1248. }
  1249. /**
  1250. * Checks that we have the necessary mode aliases for this server.
  1251. */
  1252. private void checkModeAliases() {
  1253. // Check we have mode aliases
  1254. final String modes = parser.getBooleanChannelModes() + parser.getListChannelModes()
  1255. + parser.getParameterChannelModes() + parser.getDoubleParameterChannelModes();
  1256. final String umodes = parser.getUserModes();
  1257. final StringBuffer missingModes = new StringBuffer();
  1258. final StringBuffer missingUmodes = new StringBuffer();
  1259. for (char mode : modes.toCharArray()) {
  1260. if (!getConfigManager().hasOptionString(DOMAIN_SERVER, "mode" + mode)) {
  1261. missingModes.append(mode);
  1262. }
  1263. }
  1264. for (char mode : umodes.toCharArray()) {
  1265. if (!getConfigManager().hasOptionString(DOMAIN_SERVER, "umode" + mode)) {
  1266. missingUmodes.append(mode);
  1267. }
  1268. }
  1269. if (missingModes.length() + missingUmodes.length() > 0) {
  1270. final StringBuffer missing = new StringBuffer("Missing mode aliases: ");
  1271. if (missingModes.length() > 0) {
  1272. missing.append("channel: +");
  1273. missing.append(missingModes);
  1274. }
  1275. if (missingUmodes.length() > 0) {
  1276. if (missingModes.length() > 0) {
  1277. missing.append(' ');
  1278. }
  1279. missing.append("user: +");
  1280. missing.append(missingUmodes);
  1281. }
  1282. Logger.appError(ErrorLevel.LOW, missing.toString() + " ["
  1283. + parser.getServerSoftwareType() + "]",
  1284. new MissingModeAliasException(getNetwork(), parser,
  1285. getConfigManager().getOption("identity",
  1286. "modealiasversion"), missing.toString()));
  1287. }
  1288. }
  1289. /** {@inheritDoc} */
  1290. @Override
  1291. public IgnoreList getIgnoreList() {
  1292. return ignoreList;
  1293. }
  1294. /** {@inheritDoc} */
  1295. @Override
  1296. public void updateIgnoreList() {
  1297. ignoreList.clear();
  1298. ignoreList.addAll(getConfigManager().getOptionList("network", "ignorelist"));
  1299. }
  1300. /** {@inheritDoc} */
  1301. @Override
  1302. public void saveIgnoreList() {
  1303. getNetworkIdentity().setOption("network", "ignorelist", ignoreList.getRegexList());
  1304. }
  1305. /** {@inheritDoc} */
  1306. @Override
  1307. public ConfigProvider getServerIdentity() {
  1308. return identityFactory.createServerConfig(parser.getServerName());
  1309. }
  1310. /** {@inheritDoc} */
  1311. @Override
  1312. public ConfigProvider getNetworkIdentity() {
  1313. return identityFactory.createNetworkConfig(getNetwork());
  1314. }
  1315. /** {@inheritDoc} */
  1316. @Override
  1317. public void addInviteListener(final InviteListener listener) {
  1318. synchronized (listeners) {
  1319. listeners.add(InviteListener.class, listener);
  1320. }
  1321. }
  1322. /** {@inheritDoc} */
  1323. @Override
  1324. public void removeInviteListener(final InviteListener listener) {
  1325. synchronized (listeners) {
  1326. listeners.remove(InviteListener.class, listener);
  1327. }
  1328. }
  1329. /** {@inheritDoc} */
  1330. @Override
  1331. public void addInvite(final Invite invite) {
  1332. synchronized (invites) {
  1333. for (Invite oldInvite : new ArrayList<>(invites)) {
  1334. if (oldInvite.getChannel().equals(invite.getChannel())) {
  1335. removeInvite(oldInvite);
  1336. }
  1337. }
  1338. invites.add(invite);
  1339. synchronized (listeners) {
  1340. for (InviteListener listener : listeners.get(InviteListener.class)) {
  1341. listener.inviteReceived(this, invite);
  1342. }
  1343. }
  1344. }
  1345. }
  1346. /** {@inheritDoc} */
  1347. @Override
  1348. public void acceptInvites(final Invite ... invites) {
  1349. final ChannelJoinRequest[] requests = new ChannelJoinRequest[invites.length];
  1350. for (int i = 0; i < invites.length; i++) {
  1351. requests[i] = new ChannelJoinRequest(invites[i].getChannel());
  1352. }
  1353. join(requests);
  1354. }
  1355. /** {@inheritDoc} */
  1356. @Override
  1357. public void acceptInvites() {
  1358. synchronized (invites) {
  1359. acceptInvites(invites.toArray(new Invite[invites.size()]));
  1360. }
  1361. }
  1362. /** {@inheritDoc} */
  1363. @Override
  1364. public void removeInvites(final String channel) {
  1365. for (Invite invite : new ArrayList<>(invites)) {
  1366. if (invite.getChannel().equals(channel)) {
  1367. removeInvite(invite);
  1368. }
  1369. }
  1370. }
  1371. /** {@inheritDoc} */
  1372. @Override
  1373. public void removeInvites() {
  1374. for (Invite invite : new ArrayList<>(invites)) {
  1375. removeInvite(invite);
  1376. }
  1377. }
  1378. /** {@inheritDoc} */
  1379. @Override
  1380. public void removeInvite(final Invite invite) {
  1381. synchronized (invites) {
  1382. invites.remove(invite);
  1383. synchronized (listeners) {
  1384. for (InviteListener listener : listeners.get(InviteListener.class)) {
  1385. listener.inviteExpired(this, invite);
  1386. }
  1387. }
  1388. }
  1389. }
  1390. /** {@inheritDoc} */
  1391. @Override
  1392. public List<Invite> getInvites() {
  1393. return invites;
  1394. }
  1395. /** {@inheritDoc} */
  1396. @Override
  1397. public void addAwayStateListener(final AwayStateListener listener) {
  1398. synchronized (listeners) {
  1399. listeners.add(AwayStateListener.class, listener);
  1400. }
  1401. }
  1402. /** {@inheritDoc} */
  1403. @Override
  1404. public void removeAwayStateListener(final AwayStateListener listener) {
  1405. synchronized (listeners) {
  1406. listeners.remove(AwayStateListener.class, listener);
  1407. }
  1408. }
  1409. /** {@inheritDoc} */
  1410. @Override
  1411. public void updateAwayState(final String message) {
  1412. if ((awayMessage != null && awayMessage.equals(message))
  1413. || (awayMessage == null && message == null)) {
  1414. return;
  1415. }
  1416. awayMessage = message;
  1417. new Thread(new Runnable() {
  1418. /** {@inheritDoc} */
  1419. @Override
  1420. public void run() {
  1421. synchronized (listeners) {
  1422. if (message == null) {
  1423. for (AwayStateListener listener : listeners.get(AwayStateListener.class)) {
  1424. listener.onBack();
  1425. }
  1426. } else {
  1427. for (AwayStateListener listener : listeners.get(AwayStateListener.class)) {
  1428. listener.onAway(message);
  1429. }
  1430. }
  1431. }
  1432. }
  1433. }, "Away state listener runner").start();
  1434. }
  1435. /** {@inheritDoc} */
  1436. @Override
  1437. public void addCertificateProblemListener(final CertificateProblemListener listener) {
  1438. listeners.add(CertificateProblemListener.class, listener);
  1439. if (certificateManager != null && !certificateManager.getProblems().isEmpty()) {
  1440. listener.certificateProblemEncountered(certificateManager.getChain(),
  1441. certificateManager.getProblems(), certificateManager);
  1442. }
  1443. }
  1444. /** {@inheritDoc} */
  1445. @Override
  1446. public void removeCertificateProblemListener(final CertificateProblemListener listener) {
  1447. listeners.remove(CertificateProblemListener.class, listener);
  1448. }
  1449. /** {@inheritDoc} */
  1450. @Override
  1451. public void certificateProblemEncountered(final X509Certificate[] chain,
  1452. final Collection<CertificateException> problems,
  1453. final CertificateManager certificateManager) {
  1454. for (CertificateProblemListener listener : listeners.get(CertificateProblemListener.class)) {
  1455. listener.certificateProblemEncountered(chain, problems, certificateManager);
  1456. }
  1457. }
  1458. /** {@inheritDoc} */
  1459. @Override
  1460. public void certificateProblemResolved(final CertificateManager manager) {
  1461. for (CertificateProblemListener listener : listeners.get(CertificateProblemListener.class)) {
  1462. listener.certificateProblemResolved(manager);
  1463. }
  1464. }
  1465. }