12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292 |
- /*
- * Copyright (c) 2006-2015 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc;
-
- import com.dmdirc.commandparser.CommandType;
- import com.dmdirc.commandparser.parsers.CommandParser;
- import com.dmdirc.config.profiles.Profile;
- import com.dmdirc.events.AppErrorEvent;
- import com.dmdirc.events.ChannelOpenedEvent;
- import com.dmdirc.events.QueryOpenedEvent;
- import com.dmdirc.events.ServerConnectErrorEvent;
- import com.dmdirc.events.ServerConnectedEvent;
- import com.dmdirc.events.ServerConnectingEvent;
- import com.dmdirc.events.ServerDisconnectedEvent;
- import com.dmdirc.events.ServerInviteExpiredEvent;
- import com.dmdirc.interfaces.Connection;
- import com.dmdirc.interfaces.GroupChat;
- import com.dmdirc.interfaces.User;
- import com.dmdirc.interfaces.config.ConfigChangeListener;
- import com.dmdirc.interfaces.config.ConfigProvider;
- import com.dmdirc.interfaces.config.ConfigProviderMigrator;
- import com.dmdirc.interfaces.config.IdentityFactory;
- import com.dmdirc.logger.ErrorLevel;
- import com.dmdirc.parser.common.ChannelJoinRequest;
- import com.dmdirc.parser.common.DefaultStringConverter;
- import com.dmdirc.parser.common.IgnoreList;
- import com.dmdirc.parser.common.ParserError;
- import com.dmdirc.parser.common.ThreadedParser;
- import com.dmdirc.parser.interfaces.ChannelInfo;
- import com.dmdirc.parser.interfaces.ClientInfo;
- import com.dmdirc.parser.interfaces.EncodingParser;
- import com.dmdirc.parser.interfaces.LocalClientInfo;
- import com.dmdirc.parser.interfaces.Parser;
- import com.dmdirc.parser.interfaces.ProtocolDescription;
- import com.dmdirc.parser.interfaces.SecureParser;
- import com.dmdirc.parser.interfaces.StringConverter;
- import com.dmdirc.tls.CertificateManager;
- import com.dmdirc.ui.WindowManager;
- import com.dmdirc.ui.core.components.WindowComponent;
- import com.dmdirc.ui.input.TabCompleterFactory;
- import com.dmdirc.ui.input.TabCompletionType;
- import com.dmdirc.ui.messages.BackBufferFactory;
- import com.dmdirc.ui.messages.Formatter;
- import com.dmdirc.ui.messages.HighlightManager;
- import com.dmdirc.ui.messages.sink.MessageSinkManager;
- import com.dmdirc.util.URLBuilder;
-
- import java.net.NoRouteToHostException;
- import java.net.SocketException;
- import java.net.SocketTimeoutException;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.net.UnknownHostException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.Date;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Optional;
- import java.util.concurrent.ConcurrentSkipListMap;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.ScheduledFuture;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.locks.ReadWriteLock;
- import java.util.concurrent.locks.ReentrantReadWriteLock;
- import java.util.function.Function;
- import java.util.stream.Collectors;
-
- import javax.annotation.Nonnull;
- import javax.annotation.Nullable;
- import javax.net.ssl.SSLException;
- import javax.net.ssl.TrustManager;
-
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- import static com.google.common.base.Preconditions.checkNotNull;
-
- /**
- * The Server class represents the client's view of a server. It maintains a list of all channels,
- * queries, etc, and handles parser callbacks pertaining to the server.
- */
- public class Server extends FrameContainer implements Connection {
-
- private static final Logger LOG = LoggerFactory.getLogger(Server.class);
- /** The name of the general domain. */
- private static final String DOMAIN_GENERAL = "general";
-
- /** Open channels that currently exist on the server. */
- private final ChannelMap channels = new ChannelMap();
- /** Open query windows on the server. */
- private final Map<String, Query> queries = new ConcurrentSkipListMap<>();
- /** The user manager to retrieve users from. */
- private final UserManager userManager;
-
- /** The Parser instance handling this server. */
- @Nonnull
- private Optional<Parser> parser = Optional.empty();
- /** The Parser instance that used to be handling this server. */
- @Nonnull
- private Optional<Parser> oldParser = Optional.empty();
- /** The parser-supplied protocol description object. */
- @Nonnull
- private Optional<ProtocolDescription> protocolDescription = Optional.empty();
-
- /**
- * Object used to synchronise access to parser. This object should be locked by anything
- * requiring that the parser reference remains the same for a duration of time, or by anything
- * which is updating the parser reference.
- *
- * If used in conjunction with myStateLock, the parserLock must always be locked INSIDE the
- * myStateLock to prevent deadlocks.
- */
- private final ReadWriteLock parserLock = new ReentrantReadWriteLock();
-
- /** Object used to synchronise access to myState. */
- private final Object myStateLock = new Object();
- /** The current state of this server. */
- private final ServerStatus myState = new ServerStatus(this, myStateLock);
-
- /** The address of the server we're connecting to. */
- @Nonnull
- private URI address;
- /** The profile we're using. */
- @Nonnull
- private Profile profile;
-
- /** Our reason for being away, if any. */
- private Optional<String> awayMessage;
- /** Our event handler. */
- private final ServerEventHandler eventHandler;
- /** A list of outstanding invites. */
- private final List<Invite> invites = new ArrayList<>();
- /** A set of channels we want to join without focusing. */
- private final Collection<String> backgroundChannels = new HashSet<>();
- /** Our ignore list. */
- private final IgnoreList ignoreList = new IgnoreList();
- /** Our string convertor. */
- private StringConverter converter = new DefaultStringConverter();
- /** ParserFactory we use for creating parsers. */
- private final ParserFactory parserFactory;
- /** ServerManager that created us. */
- private final ServerManager manager;
- /** Factory to use to create new identities. */
- private final IdentityFactory identityFactory;
- /** Window manager to pas to children. */
- private final WindowManager windowManager;
- /** The migrator to use to change our config provider. */
- private final ConfigProviderMigrator configMigrator;
- /** Factory to use for creating channels. */
- private final ChannelFactory channelFactory;
- /** Factory to use for creating queries. */
- private final QueryFactory queryFactory;
- /** The config provider to write user settings to. */
- private final ConfigProvider userSettings;
- /** Executor service to use to schedule repeated events. */
- private final ScheduledExecutorService executorService;
- /** The message encoder factory to create a message encoder with. */
- private final MessageEncoderFactory messageEncoderFactory;
- /** The manager to use for highlighting. */
- private final HighlightManager highlightManager;
- /** Listener to use for config changes. */
- private final ConfigChangeListener configListener = (domain, key) -> updateTitle();
- /** The future used when a who timer is scheduled. */
- private ScheduledFuture<?> whoTimerFuture;
- /** The future used when a reconnect timer is scheduled. */
- private ScheduledFuture<?> reconnectTimerFuture;
-
- /**
- * Creates a new server which will connect to the specified URL with the specified profile.
- */
- public Server(
- final ServerManager manager,
- final ConfigProviderMigrator configMigrator,
- final CommandParser commandParser,
- final ParserFactory parserFactory,
- final TabCompleterFactory tabCompleterFactory,
- final IdentityFactory identityFactory,
- final MessageSinkManager messageSinkManager,
- final WindowManager windowManager,
- final ChannelFactory channelFactory,
- final QueryFactory queryFactory,
- final URLBuilder urlBuilder,
- final DMDircMBassador eventBus,
- final MessageEncoderFactory messageEncoderFactory,
- final ConfigProvider userSettings,
- final ScheduledExecutorService executorService,
- @Nonnull final URI uri,
- @Nonnull final Profile profile,
- final BackBufferFactory backBufferFactory,
- final UserManager userManager) {
- super(null, "server-disconnected",
- getHost(uri),
- getHost(uri),
- configMigrator.getConfigProvider(),
- backBufferFactory,
- urlBuilder,
- commandParser,
- tabCompleterFactory.getTabCompleter(configMigrator.getConfigProvider(),
- CommandType.TYPE_SERVER, CommandType.TYPE_GLOBAL),
- messageSinkManager,
- eventBus,
- Arrays.asList(
- WindowComponent.TEXTAREA.getIdentifier(),
- WindowComponent.INPUTFIELD.getIdentifier(),
- WindowComponent.CERTIFICATE_VIEWER.getIdentifier()));
-
- this.manager = manager;
- this.parserFactory = parserFactory;
- this.identityFactory = identityFactory;
- this.windowManager = windowManager;
- this.configMigrator = configMigrator;
- this.channelFactory = channelFactory;
- this.queryFactory = queryFactory;
- this.executorService = executorService;
- this.userSettings = userSettings;
- this.messageEncoderFactory = messageEncoderFactory;
- this.userManager = userManager;
-
- awayMessage = Optional.empty();
- eventHandler = new ServerEventHandler(this, eventBus);
-
- this.address = uri;
- this.profile = profile;
- setConnectionDetails(uri, profile);
-
- updateIcon();
-
- getConfigManager().addChangeListener("formatter", "serverName", configListener);
- getConfigManager().addChangeListener("formatter", "serverTitle", configListener);
-
- this.highlightManager = new HighlightManager();
- getEventBus().subscribe(highlightManager);
- }
-
- /**
- * Updates the connection details for this server. If the specified URI does not define a port,
- * the default port from the protocol description will be used.
- *
- * @param uri The new URI that this server should connect to
- * @param profile The profile that this server should use
- */
- private void setConnectionDetails(final URI uri, final Profile profile) {
- this.address = checkNotNull(uri);
- this.protocolDescription = Optional.ofNullable(parserFactory.getDescription(uri));
- this.profile = profile;
-
- if (uri.getPort() == -1) {
- protocolDescription.ifPresent(pd -> {
- try {
- this.address = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(),
- pd.getDefaultPort(), uri.getPath(), uri.getQuery(), uri.getFragment());
- } catch (URISyntaxException ex) {
- getEventBus().publish(
- new AppErrorEvent(ErrorLevel.MEDIUM, ex, "Unable to construct URI",
- ""));
- }
- });
- }
- }
-
- @Override
- public void connect() {
- connect(address, profile);
- }
-
- @Override
- @Precondition({
- "The current parser is null or not connected",
- "The specified profile is not null"
- })
- @SuppressWarnings("fallthrough")
- public void connect(final URI address, final Profile profile) {
- checkNotNull(address);
- checkNotNull(profile);
-
- synchronized (myStateLock) {
- LOG.info("Connecting to {}, current state is {}", address, myState.getState());
-
- switch (myState.getState()) {
- case RECONNECT_WAIT:
- LOG.debug("Cancelling reconnection timer");
- if (reconnectTimerFuture != null) {
- reconnectTimerFuture.cancel(false);
- }
- break;
- case CLOSING:
- // Ignore the connection attempt
- return;
- case CONNECTED:
- case CONNECTING:
- disconnect(getConfigManager().getOption(DOMAIN_GENERAL, "quitmessage"));
- case DISCONNECTING:
- while (!myState.getState().isDisconnected()) {
- try {
- myStateLock.wait();
- } catch (InterruptedException ex) {
- return;
- }
- }
- break;
- default:
- // Do nothing
- break;
- }
-
- final URI connectAddress;
- final Parser newParser;
-
- try {
- parserLock.writeLock().lock();
- if (parser.isPresent()) {
- throw new IllegalArgumentException("Connection attempt while parser "
- + "is still connected.\n\nMy state:" + getState());
- }
-
- configMigrator.migrate(address.getScheme(), "", "", address.getHost());
-
- setConnectionDetails(address, profile);
-
- updateTitle();
- updateIcon();
-
- parser = Optional.ofNullable(buildParser());
-
- if (!parser.isPresent()) {
- addLine("serverUnknownProtocol", address.getScheme());
- return;
- }
-
- newParser = parser.get();
- connectAddress = newParser.getURI();
- } finally {
- parserLock.writeLock().unlock();
- }
-
- addLine("serverConnecting", connectAddress.getHost(), connectAddress.getPort());
-
- myState.transition(ServerState.CONNECTING);
-
- doCallbacks();
-
- updateAwayState(Optional.empty());
- removeInvites();
-
- newParser.connect();
- if (newParser instanceof ThreadedParser) {
- ((ThreadedParser) newParser).getControlThread()
- .setName("Parser - " + connectAddress.getHost());
- }
- }
-
- getEventBus().publish(new ServerConnectingEvent(this));
- }
-
- @Override
- public void reconnect(final String reason) {
- synchronized (myStateLock) {
- if (myState.getState() == ServerState.CLOSING) {
- return;
- }
-
- disconnect(reason);
-
- connect(address, profile);
- }
- }
-
- @Override
- public void reconnect() {
- reconnect(getConfigManager().getOption(DOMAIN_GENERAL, "reconnectmessage"));
- }
-
- @Override
- public void disconnect() {
- disconnect(getConfigManager().getOption(DOMAIN_GENERAL, "quitmessage"));
- }
-
- @Override
- public void disconnect(final String reason) {
- synchronized (myStateLock) {
- LOG.info("Disconnecting. Current state: {}", myState.getState());
-
- switch (myState.getState()) {
- case CLOSING:
- case DISCONNECTING:
- case DISCONNECTED:
- case TRANSIENTLY_DISCONNECTED:
- return;
- case RECONNECT_WAIT:
- LOG.debug("Cancelling reconnection timer");
- if (reconnectTimerFuture != null) {
- reconnectTimerFuture.cancel(false);
- }
- break;
- default:
- break;
- }
-
- channels.resetAll();
- backgroundChannels.clear();
-
- try {
- parserLock.readLock().lock();
- if (parser.isPresent()) {
- myState.transition(ServerState.DISCONNECTING);
-
- removeInvites();
- updateIcon();
-
- parser.get().disconnect(reason);
- } else {
- myState.transition(ServerState.DISCONNECTED);
- }
- } finally {
- parserLock.readLock().unlock();
- }
-
- if (getConfigManager().getOptionBool(DOMAIN_GENERAL, "closechannelsonquit")) {
- channels.closeAll();
- }
-
- if (getConfigManager().getOptionBool(DOMAIN_GENERAL, "closequeriesonquit")) {
- closeQueries();
- }
- }
- }
-
- /**
- * Schedules a reconnect attempt to be performed after a user-defined delay.
- */
- @Precondition("The server state is transiently disconnected")
- private void doDelayedReconnect() {
- synchronized (myStateLock) {
- LOG.info("Performing delayed reconnect. State: {}", myState.getState());
-
- if (myState.getState() != ServerState.TRANSIENTLY_DISCONNECTED) {
- throw new IllegalStateException("doDelayedReconnect when not "
- + "transiently disconnected\n\nState: " + myState);
- }
-
- final int delay = Math.max(1000,
- getConfigManager().getOptionInt(DOMAIN_GENERAL, "reconnectdelay"));
-
- handleNotification("connectRetry", getAddress(), delay / 1000);
-
- reconnectTimerFuture = executorService.schedule(() -> {
- synchronized (myStateLock) {
- LOG.debug("Reconnect task executing, state: {}", myState.getState());
- if (myState.getState() == ServerState.RECONNECT_WAIT) {
- myState.transition(ServerState.TRANSIENTLY_DISCONNECTED);
- reconnect();
- }
- }
- }, delay, TimeUnit.MILLISECONDS);
-
- LOG.info("Scheduling reconnect task for delay of {}", delay);
-
- myState.transition(ServerState.RECONNECT_WAIT);
- updateIcon();
- }
- }
-
- @Override
- public Optional<GroupChat> getChannel(final String channel) {
- return channels.get(channel).map(c -> (GroupChat) c);
- }
-
- @Override
- public Collection<GroupChat> getChannels() {
- return channels.getAll().parallelStream()
- .map(c -> (GroupChat) c)
- .collect(Collectors.toList());
- }
-
- @Override
- public boolean hasQuery(final String host) {
- return queries.containsKey(converter.toLowerCase(parseHostmask(host)[0]));
- }
-
- @Override
- public User getLocalUser() {
- return parser.map(Parser::getLocalClient)
- .map(client -> userManager.getUserFromClientInfo(client, this)).get();
- }
-
- @Override
- public User getUser(final String details) {
- return parser.map(p -> p.getClient(details))
- .map(client -> userManager.getUserFromClientInfo(client, this)).get();
- }
-
- @Override
- public Query getQuery(final String host) {
- return getQuery(host, false);
- }
-
- @Override
- public Query getQuery(final String host, final boolean focus) {
- synchronized (myStateLock) {
- if (myState.getState() == ServerState.CLOSING) {
- // Can't open queries while the server is closing
- return null;
- }
- }
-
- final String nick = parseHostmask(host)[0];
- final String lnick = converter.toLowerCase(nick);
-
- if (!queries.containsKey(lnick)) {
- final Query newQuery = queryFactory.getQuery(this, getUser(host));
- if (!getState().isDisconnected()) {
- newQuery.reregister();
- }
-
- windowManager.addWindow(this, newQuery, focus);
- getEventBus().publish(new QueryOpenedEvent(newQuery));
-
- getTabCompleter().addEntry(TabCompletionType.QUERY_NICK, nick);
- queries.put(lnick, newQuery);
- }
-
- return queries.get(lnick);
- }
-
- @Override
- public void updateQuery(final Query query, final String oldNick, final String newNick) {
- getTabCompleter().removeEntry(TabCompletionType.QUERY_NICK, oldNick);
- getTabCompleter().addEntry(TabCompletionType.QUERY_NICK, newNick);
-
- queries.put(converter.toLowerCase(newNick), query);
- queries.remove(converter.toLowerCase(oldNick));
- }
-
- @Override
- public Collection<Query> getQueries() {
- return Collections.unmodifiableCollection(queries.values());
- }
-
- @Override
- public void delQuery(final Query query) {
- getTabCompleter().removeEntry(TabCompletionType.QUERY_NICK, query.getNickname());
- queries.remove(converter.toLowerCase(query.getNickname()));
- }
-
- @Override
- public void delChannel(final String chan) {
- getTabCompleter().removeEntry(TabCompletionType.CHANNEL, chan);
- channels.remove(chan);
- }
-
- public Channel addChannel(final ChannelInfo chan) {
- return addChannel(chan, !backgroundChannels.contains(chan.getName())
- || getConfigManager().getOptionBool(DOMAIN_GENERAL, "hidechannels"));
- }
-
- public Channel addChannel(final ChannelInfo chan, final boolean focus) {
- synchronized (myStateLock) {
- if (myState.getState() == ServerState.CLOSING) {
- // Can't join channels while the server is closing
- return null;
- }
- }
-
- backgroundChannels.remove(chan.getName());
-
- final Optional<Channel> channel = channels.get(chan.getName());
- if (channel.isPresent()) {
- channel.get().setChannelInfo(chan);
- channel.get().selfJoin();
- return channel.get();
- } else {
- final ConfigProviderMigrator channelConfig = identityFactory.createMigratableConfig(
- getProtocol(), getIrcd(), getNetwork(), getAddress(), chan.getName());
- final Channel newChan = channelFactory.getChannel(this, chan, channelConfig);
-
- windowManager.addWindow(this, newChan, focus);
- getEventBus().publish(new ChannelOpenedEvent(newChan));
-
- getTabCompleter().addEntry(TabCompletionType.CHANNEL, chan.getName());
- channels.add(newChan);
- return newChan;
- }
- }
-
- /**
- * Closes all open query windows associated with this server.
- */
- private void closeQueries() {
- new ArrayList<>(queries.values()).forEach(Query::close);
- }
-
- /**
- * Retrieves the host component of the specified URI, or throws a relevant exception if this is
- * not possible.
- *
- * @param uri The URI to be processed
- *
- * @return The URI's host component, as returned by {@link URI#getHost()}.
- *
- * @throws NullPointerException If <code>uri</code> is null
- * @throws IllegalArgumentException If the specified URI has no host
- * @since 0.6.4
- */
- private static String getHost(final URI uri) {
- if (uri.getHost() == null) {
- throw new IllegalArgumentException("URIs must have hosts");
- }
-
- return uri.getHost();
- }
-
- /**
- * Builds an appropriately configured {@link Parser} for this server.
- *
- * @return A configured parser.
- */
- @Nullable
- private Parser buildParser() {
- final Parser myParser = parserFactory.getParser(profile, address, getConfigManager())
- .orElse(null);
-
- if (myParser != null) {
- myParser.setIgnoreList(ignoreList);
- }
-
- if (myParser instanceof SecureParser) {
- final CertificateManager certificateManager =
- new CertificateManager(this, address.getHost(), getConfigManager(),
- userSettings, getEventBus());
- final SecureParser secureParser = (SecureParser) myParser;
- secureParser.setTrustManagers(new TrustManager[]{certificateManager});
- secureParser.setKeyManagers(certificateManager.getKeyManager());
- }
-
- if (myParser instanceof EncodingParser) {
- final EncodingParser encodingParser = (EncodingParser) myParser;
- encodingParser.setEncoder(messageEncoderFactory.getMessageEncoder(this, myParser));
- }
-
- return myParser;
- }
-
- @Override
- public boolean compareURI(final URI uri) {
- return parser.map(p -> p.compareURI(uri)).orElse(
- oldParser.map(op -> op.compareURI(uri)).orElse(false));
- }
-
- /**
- * Parses a hostmask into nickname, username and hostname. Should probably use
- * {@link #getUser} instead.
- *
- * @param hostmask The mask to parse.
- */
- public String[] parseHostmask(final String hostmask) {
- return protocolDescription.get().parseHostmask(hostmask);
- }
-
- /**
- * Updates this server's icon.
- */
- private void updateIcon() {
- final String icon = myState.getState() == ServerState.CONNECTED
- ? protocolDescription.get().isSecure(address)
- ? "secure-server" : "server" : "server-disconnected";
- setIcon(icon);
- }
-
- /**
- * Registers callbacks.
- */
- private void doCallbacks() {
- eventHandler.registerCallbacks();
- queries.values().forEach(Query::reregister);
- }
-
- @Override
- public void join(final ChannelJoinRequest... requests) {
- join(true, requests);
- }
-
- @Override
- public void join(final boolean focus, final ChannelJoinRequest... requests) {
- synchronized (myStateLock) {
- if (myState.getState() == ServerState.CONNECTED) {
- final Collection<ChannelJoinRequest> pending = new ArrayList<>();
-
- for (ChannelJoinRequest request : requests) {
- removeInvites(request.getName());
-
- final String name;
- if (parser.get().isValidChannelName(request.getName())) {
- name = request.getName();
- } else {
- name = parser.get().getChannelPrefixes().substring(0, 1)
- + request.getName();
- }
-
- if (getChannel(name).map(GroupChat::isOnChannel).orElse(false)) {
- if (!focus) {
- backgroundChannels.add(name);
- }
-
- pending.add(request);
- }
- }
-
- parser.get().joinChannels(pending.toArray(new ChannelJoinRequest[pending.size()]));
- }
- // TODO: otherwise: address.getChannels().add(channel);
- }
- }
-
- @Override
- public void sendLine(final String line) {
- synchronized (myStateLock) {
- try {
- parserLock.readLock().lock();
- parser.ifPresent(p -> {
- if (!line.isEmpty() && myState.getState() == ServerState.CONNECTED) {
- p.sendRawMessage(line);
- }
- });
- } finally {
- parserLock.readLock().unlock();
- }
- }
- }
-
- @Override
- public int getMaxLineLength() {
- return withParserReadLock(Parser::getMaxLength, -1);
- }
-
- @Override
- @Nonnull
- public Optional<Parser> getParser() {
- return parser;
- }
-
- @Nonnull
- @Override
- public Profile getProfile() {
- return profile;
- }
-
- @Override
- public String getChannelPrefixes() {
- return withParserReadLock(Parser::getChannelPrefixes, "#&");
- }
-
- @Override
- public String getAddress() {
- return withParserReadLock(Parser::getServerName, address.getHost());
- }
-
- @Override
- public String getNetwork() {
- try {
- parserLock.readLock().lock();
- return parser.map(p -> p.getNetworkName().isEmpty()
- ? getNetworkFromServerName(p.getServerName()) : p.getNetworkName())
- .orElseThrow(() -> new IllegalStateException(
- "getNetwork called when " + "parser is null (state: " + getState() +
- ')'));
- } finally {
- parserLock.readLock().unlock();
- }
- }
-
- @Override
- public boolean isNetwork(final String target) {
- synchronized (myStateLock) {
- return withParserReadLock(p -> getNetwork().equalsIgnoreCase(target), false);
- }
- }
-
- /**
- * Calculates a network name from the specified server name. This method implements parts 2-4 of
- * the procedure documented at getNetwork().
- *
- * @param serverName The server name to parse
- *
- * @return A network name for the specified server
- */
- protected static String getNetworkFromServerName(final String serverName) {
- final String[] parts = serverName.split("\\.");
- final String[] tlds = {"biz", "com", "info", "net", "org"};
- boolean isTLD = false;
-
- for (String tld : tlds) {
- if (serverName.endsWith('.' + tld)) {
- isTLD = true;
- break;
- }
- }
-
- if (isTLD && parts.length > 2) {
- return parts[parts.length - 2] + '.' + parts[parts.length - 1];
- } else if (parts.length > 2) {
- final StringBuilder network = new StringBuilder();
-
- for (int i = 1; i < parts.length; i++) {
- if (network.length() > 0) {
- network.append('.');
- }
-
- network.append(parts[i]);
- }
-
- return network.toString();
- } else {
- return serverName;
- }
- }
-
- @Override
- public String getIrcd() {
- return parser.get().getServerSoftwareType();
- }
-
- @Override
- public String getProtocol() {
- return address.getScheme();
- }
-
- @Override
- public boolean isAway() {
- return awayMessage.isPresent();
- }
-
- @Override
- public String getAwayMessage() {
- return awayMessage.orElse(null);
- }
-
- @Override
- public ServerState getState() {
- return myState.getState();
- }
-
- @Override
- public ServerStatus getStatus() {
- return myState;
- }
-
- @Override
- public void close() {
- synchronized (myStateLock) {
- eventHandler.unregisterCallbacks();
- getConfigManager().removeListener(configListener);
- getEventBus().unsubscribe(highlightManager);
- executorService.shutdown();
-
- disconnect();
-
- myState.transition(ServerState.CLOSING);
- }
-
- channels.closeAll();
- closeQueries();
- removeInvites();
-
- manager.unregisterServer(this);
-
- super.close();
- }
-
- @Override
- public FrameContainer getWindowModel() {
- return this;
- }
-
- @Override
- public void addLineToAll(final String messageType, final Date date,
- final Object... args) {
- channels.addLineToAll(messageType, date, args);
-
- for (Query query : queries.values()) {
- query.addLine(messageType, date, args);
- }
-
- addLine(messageType, date, args);
- }
-
- @Override
- public void sendCTCPReply(final String source, final String type, final String args) {
- if ("VERSION".equalsIgnoreCase(type)) {
- parser.get().sendCTCPReply(source, "VERSION",
- "DMDirc " + getConfigManager().getOption("version", "version") +
- " - https://www.dmdirc.com/");
- } else if ("PING".equalsIgnoreCase(type)) {
- parser.get().sendCTCPReply(source, "PING", args);
- } else if ("CLIENTINFO".equalsIgnoreCase(type)) {
- parser.get().sendCTCPReply(source, "CLIENTINFO", "VERSION PING CLIENTINFO");
- }
- }
-
- @Override
- public boolean isValidChannelName(final String channelName) {
- return getChannel(channelName).isPresent()
- || withParserReadLock(p -> p.isValidChannelName(channelName), false);
- }
-
- @Override
- public Optional<Connection> getConnection() {
- return Optional.of(this);
- }
-
- @Override
- protected boolean processNotificationArg(final Object arg, final List<Object> args) {
- if (arg instanceof User) {
- final User clientInfo = (User) arg;
- args.add(clientInfo.getNickname());
- args.add(clientInfo.getUsername());
- args.add(clientInfo.getHostname());
- return true;
- } else if (arg instanceof ClientInfo) {
- final ClientInfo clientInfo = (ClientInfo) arg;
- args.add(clientInfo.getNickname());
- args.add(clientInfo.getUsername());
- args.add(clientInfo.getHostname());
- return true;
- } else {
- return super.processNotificationArg(arg, args);
- }
- }
-
- @Override
- public void updateTitle() {
- synchronized (myStateLock) {
- if (myState.getState() == ServerState.CLOSING) {
- return;
- }
-
- try {
- parserLock.readLock().lock();
- final Object[] arguments = {
- address.getHost(), parser.map(Parser::getServerName).orElse("Unknown"),
- address.getPort(), parser.map(p -> getNetwork()).orElse("Unknown"),
- parser.map(Parser::getLocalClient).map(LocalClientInfo::getNickname).orElse(
- "Unknown")
- };
-
- setName(Formatter.formatMessage(getConfigManager(),
- "serverName", arguments));
- setTitle(Formatter.formatMessage(getConfigManager(),
- "serverTitle", arguments));
- } finally {
- parserLock.readLock().unlock();
- }
- }
- }
-
- /**
- * Called when the socket has been closed.
- */
- public void onSocketClosed() {
- LOG.info("Received socket closed event, state: {}", myState.getState());
-
- if (whoTimerFuture != null) {
- whoTimerFuture.cancel(false);
- }
-
- if (Thread.holdsLock(myStateLock)) {
- LOG.info("State lock contended: rerunning on a new thread");
-
- executorService.schedule(this::onSocketClosed, 0, TimeUnit.SECONDS);
- return;
- }
-
- handleNotification("socketClosed", getAddress());
-
- getEventBus().publish(new ServerDisconnectedEvent(this));
-
- eventHandler.unregisterCallbacks();
-
- synchronized (myStateLock) {
- if (myState.getState() == ServerState.CLOSING
- || myState.getState() == ServerState.DISCONNECTED) {
- // This has been triggered via .disconnect()
- return;
- }
-
- if (myState.getState() == ServerState.DISCONNECTING) {
- myState.transition(ServerState.DISCONNECTED);
- } else {
- myState.transition(ServerState.TRANSIENTLY_DISCONNECTED);
- }
-
- channels.resetAll();
-
- try {
- parserLock.writeLock().lock();
- oldParser = parser;
- parser = Optional.empty();
- } finally {
- parserLock.writeLock().unlock();
- }
-
- updateIcon();
-
- if (getConfigManager().getOptionBool(DOMAIN_GENERAL, "closechannelsondisconnect")) {
- channels.closeAll();
- }
-
- if (getConfigManager().getOptionBool(DOMAIN_GENERAL, "closequeriesondisconnect")) {
- closeQueries();
- }
-
- removeInvites();
- updateAwayState(Optional.empty());
-
- if (getConfigManager().getOptionBool(DOMAIN_GENERAL,
- "reconnectondisconnect")
- && myState.getState() == ServerState.TRANSIENTLY_DISCONNECTED) {
- doDelayedReconnect();
- }
- }
- }
-
- /**
- * Called when an error was encountered while connecting.
- *
- * @param errorInfo The parser's error information
- */
- @Precondition("The current server state is CONNECTING")
- public void onConnectError(final ParserError errorInfo) {
- synchronized (myStateLock) {
- LOG.info("Received connect error event, state: {}; error: {}", myState.getState(),
- errorInfo);
-
- if (myState.getState() == ServerState.CLOSING
- || myState.getState() == ServerState.DISCONNECTING) {
- // Do nothing
- return;
- } else if (myState.getState() != ServerState.CONNECTING) {
- // Shouldn't happen
- throw new IllegalStateException("Connect error when not "
- + "connecting\n\n" + getStatus().getTransitionHistory());
- }
-
- myState.transition(ServerState.TRANSIENTLY_DISCONNECTED);
-
- try {
- parserLock.writeLock().lock();
- oldParser = parser;
- parser = Optional.empty();
- } finally {
- parserLock.writeLock().unlock();
- }
-
- updateIcon();
-
- final String description;
-
- if (errorInfo.getException() == null) {
- description = errorInfo.getData();
- } else {
- final Exception exception = errorInfo.getException();
-
- if (exception instanceof UnknownHostException) {
- description = "Unknown host (unable to resolve)";
- } else if (exception instanceof NoRouteToHostException) {
- description = "No route to host";
- } else if (exception instanceof SocketTimeoutException) {
- description = "Connection attempt timed out";
- } else if (exception instanceof SocketException
- || exception instanceof SSLException) {
- description = exception.getMessage();
- } else {
- getEventBus().publish(new AppErrorEvent(ErrorLevel.LOW,
- new IllegalArgumentException(exception),
- "Unknown socket error: " + exception.getClass().getCanonicalName(),
- ""));
- description = "Unknown error: " + exception.getMessage();
- }
- }
-
- getEventBus().publish(new ServerConnectErrorEvent(this, description));
-
- handleNotification("connectError", getAddress(), description);
-
- if (getConfigManager().getOptionBool(DOMAIN_GENERAL, "reconnectonconnectfailure")) {
- doDelayedReconnect();
- }
- }
- }
-
- /**
- * Called after the parser receives the 005 headers from the server.
- */
- @Precondition("State is CONNECTING")
- public void onPost005() {
- synchronized (myStateLock) {
- if (myState.getState() != ServerState.CONNECTING) {
- // Shouldn't happen
- throw new IllegalStateException("Received onPost005 while not "
- + "connecting\n\n" + myState.getTransitionHistory());
- }
-
- myState.transition(ServerState.CONNECTED);
-
- configMigrator.migrate(address.getScheme(),
- parser.get().getServerSoftwareType(), getNetwork(), parser.get().getServerName());
-
- updateIcon();
- updateTitle();
- updateIgnoreList();
-
- converter = parser.get().getStringConverter();
- channels.setStringConverter(converter);
-
- final List<ChannelJoinRequest> requests = new ArrayList<>();
- if (getConfigManager().getOptionBool(DOMAIN_GENERAL, "rejoinchannels")) {
- requests.addAll(channels.asJoinRequests());
- }
- join(requests.toArray(new ChannelJoinRequest[requests.size()]));
-
- final int whoTime = getConfigManager().getOptionInt(DOMAIN_GENERAL, "whotime");
- whoTimerFuture = executorService.scheduleAtFixedRate(
- channels.getWhoRunnable(), whoTime, whoTime, TimeUnit.MILLISECONDS);
- }
-
- getEventBus().publish(new ServerConnectedEvent(this));
- }
-
- @Override
- public IgnoreList getIgnoreList() {
- return ignoreList;
- }
-
- @Override
- public void updateIgnoreList() {
- ignoreList.clear();
- ignoreList.addAll(getConfigManager().getOptionList("network", "ignorelist"));
- }
-
- @Override
- public void saveIgnoreList() {
- getNetworkIdentity().setOption("network", "ignorelist", ignoreList.getRegexList());
- }
-
- @Override
- public ConfigProvider getServerIdentity() {
- return identityFactory.createServerConfig(parser.get().getServerName());
- }
-
- @Override
- public ConfigProvider getNetworkIdentity() {
- return identityFactory.createNetworkConfig(getNetwork());
- }
-
- @Override
- public void addInvite(final Invite invite) {
- synchronized (invites) {
- new ArrayList<>(invites).stream()
- .filter(oldInvite -> oldInvite.getChannel().equals(invite.getChannel()))
- .forEach(this::removeInvite);
-
- invites.add(invite);
- }
- }
-
- @Override
- public void acceptInvites(final Invite... invites) {
- final ChannelJoinRequest[] requests = new ChannelJoinRequest[invites.length];
-
- for (int i = 0; i < invites.length; i++) {
- requests[i] = new ChannelJoinRequest(invites[i].getChannel());
- }
-
- join(requests);
- }
-
- @Override
- public void acceptInvites() {
- synchronized (invites) {
- acceptInvites(invites.toArray(new Invite[invites.size()]));
- }
- }
-
- @Override
- public void removeInvites(final String channel) {
- new ArrayList<>(invites).stream().filter(invite -> invite.getChannel().equals(channel))
- .forEach(this::removeInvite);
- }
-
- @Override
- public void removeInvites() {
- new ArrayList<>(invites).forEach(this::removeInvite);
- }
-
- @Override
- public void removeInvite(final Invite invite) {
- synchronized (invites) {
- invites.remove(invite);
- getEventBus().publishAsync(new ServerInviteExpiredEvent(this, invite));
- }
- }
-
- @Override
- public List<Invite> getInvites() {
- return Collections.unmodifiableList(invites);
- }
-
- @Override
- public void updateAwayState(final Optional<String> message) {
- checkNotNull(message);
- if (awayMessage.equals(message)) {
- return;
- }
-
- awayMessage = message;
- }
-
- @Override
- public String getUserModes() {
- return getParser().map(Parser::getChannelUserModes).orElse("");
- }
-
- @Override
- public String getBooleanModes() {
- return getParser().map(Parser::getBooleanChannelModes).orElse("");
- }
-
- @Override
- public String getListModes() {
- return getParser().map(Parser::getListChannelModes).orElse("");
- }
-
- @Override
- public String getParameterModes() {
- return getParser().map(Parser::getParameterChannelModes).orElse("");
- }
-
- @Override
- public String getDoubleParameterModes() {
- return getParser().map(Parser::getDoubleParameterChannelModes).orElse("");
- }
-
- @Override
- public int getMaxListModes(final char mode) {
- return getParser().map(p -> p.getMaxListModes(mode)).orElse(-1);
- }
-
- /**
- * Utility method to get a result from the parser while holding the {@link #parserLock}
- * read lock.
- *
- * @param func The function to use to retrieve information from the parser.
- * @param orElse The value to return if the parser is otherwise not present.
- * @param <T> The type of result returned.
- * @return The value returned by {@code func}, if the parser is present, otherwise the
- * {@code orElse} value.
- */
- private <T> T withParserReadLock(
- final Function<? super Parser, ? extends T> func,
- final T orElse) {
- try {
- parserLock.readLock().lock();
- return parser.map(func).orElse(orElse);
- } finally {
- parserLock.readLock().unlock();
- }
- }
-
- }
|