Skip to content

Commit

Permalink
Add compatibility with ergochat (#3038)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheikah45 authored Oct 15, 2023
1 parent 1fc4c7b commit 8374d7b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 60 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/faforever/client/chat/IrcChatToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.faforever.client.chat;

public record IrcChatToken(String value) {
}
62 changes: 18 additions & 44 deletions src/main/java/com/faforever/client/chat/KittehChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
import com.faforever.client.remote.FafServerAccessor;
import com.faforever.client.ui.tray.event.UpdateApplicationBadgeEvent;
import com.faforever.client.user.LoginService;
import com.faforever.commons.lobby.IrcPasswordInfo;
import com.faforever.commons.lobby.Player.LeaderboardStats;
import com.faforever.commons.lobby.SocialInfo;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.google.common.hash.Hashing;
import javafx.beans.InvalidationListener;
import javafx.beans.WeakInvalidationListener;
import javafx.beans.property.ObjectProperty;
Expand Down Expand Up @@ -54,18 +52,18 @@
import org.kitteh.irc.client.library.event.connection.ClientConnectionEndedEvent;
import org.kitteh.irc.client.library.event.connection.ClientConnectionFailedEvent;
import org.kitteh.irc.client.library.event.user.PrivateMessageEvent;
import org.kitteh.irc.client.library.event.user.PrivateNoticeEvent;
import org.kitteh.irc.client.library.event.user.UserQuitEvent;
import org.kitteh.irc.client.library.event.user.WhoisEvent;
import org.kitteh.irc.client.library.feature.auth.NickServ;
import org.kitteh.irc.client.library.feature.auth.SaslPlain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.ArrayDeque;
import java.util.ArrayList;
Expand Down Expand Up @@ -101,7 +99,8 @@ public class KittehChatService implements ChatService, InitializingBean, Disposa
private final PlayerService playerService;
private final ChatPrefs chatPrefs;
private final FxApplicationThreadExecutor fxApplicationThreadExecutor;

@Qualifier("userWebClient")
private final ObjectFactory<WebClient> userWebClientFactory;

/**
* Maps channels by name.
Expand All @@ -114,9 +113,7 @@ public class KittehChatService implements ChatService, InitializingBean, Disposa
String defaultChannelName;
@VisibleForTesting
DefaultClient client;
private NickServ nickServ;
private String username;
private String password;
/**
* A list of channels the server wants us to join.
*/
Expand Down Expand Up @@ -192,11 +189,6 @@ private ChatChannelUser initializeUserForChannel(String username, ChatChannel ch
return chatChannelUser;
}

@Subscribe
public void onIrcPassword(IrcPasswordInfo event) {
password = Hashing.md5().hashString(event.getPassword(), StandardCharsets.UTF_8).toString();
}

@Subscribe
public void onPlayerOnline(PlayerOnlineEvent event) {
PlayerBean player = event.player();
Expand Down Expand Up @@ -323,31 +315,8 @@ private void onPrivateMessage(PrivateMessageEvent event) {
getOrCreateChannel(user.getNick()).addMessage(new ChatMessage(Instant.now(), user.getNick(), event.getMessage()));
}

@Handler
private void onWhoIs(WhoisEvent event) {
if (event.getWhoisData().getRealName().map(realName -> username.equals(realName)).orElse(false)) {
nickServ.startAuthentication();
}
}

@Handler
private void onNotice(PrivateNoticeEvent event) {
String message = event.getMessage();

if (message.contains("isn't registered")) {
client.sendMessage("NickServ", String.format("register %s %s@users.faforever.com", password, client.getNick()));
} else if (message.contains("you are now recognized")) {
client.sendMessage("NickServ", String.format("recover %s", username));
} else if (message.contains("You have regained control")) {
client.setNick(username);
}
}

private void joinAutoChannels() {
log.trace("Joining auto channels: {}", autoChannels);
if (autoChannels == null) {
return;
}
autoChannels.forEach(this::joinChannel);
}

Expand Down Expand Up @@ -383,7 +352,6 @@ private void onChatUserLeftChannel(String channelName, String username) {
}

private void onMessage(String message) {
message = message.replace(password, "*****");
ircLog.debug(message);
}

Expand Down Expand Up @@ -437,7 +405,6 @@ public void connect() {
username = loginService.getUsername();

client = (DefaultClient) Client.builder()
.user(String.valueOf(loginService.getUserId()))
.realName(username)
.nick(username)
.server()
Expand All @@ -451,11 +418,18 @@ public void connect() {
.then()
.build();

nickServ = NickServ.builder(client).account(username).password(password).build();

client.getEventManager().registerEventListener(this);
client.getActorTracker().setQueryChannelInformation(false);
client.connect();
userWebClientFactory.getObject()
.get()
.uri("irc/ergochat/token")
.retrieve()
.bodyToMono(IrcChatToken.class)
.map(IrcChatToken::value)
.subscribe(token -> {
client.getAuthManager().addProtocol(new SaslPlain(client, username, "token:%s".formatted(token)));
client.getEventManager().registerEventListener(this);
client.getActorTracker().setQueryChannelInformation(false);
client.connect();
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.faforever.commons.lobby.GameLaunchResponse;
import com.faforever.commons.lobby.GameVisibility;
import com.faforever.commons.lobby.GpgGameOutboundMessage;
import com.faforever.commons.lobby.IrcPasswordInfo;
import com.faforever.commons.lobby.MatchmakerState;
import com.faforever.commons.lobby.MessageTarget;
import com.faforever.commons.lobby.NoticeInfo;
Expand Down Expand Up @@ -79,9 +78,6 @@ public class FafServerAccessor implements InitializingBean, DisposableBean {
@Override
public void afterPropertiesSet() throws Exception {
eventBus.register(this);
getEvents(IrcPasswordInfo.class).doOnError(throwable -> log.error("Error processing irc password", throwable))
.retry()
.subscribe(this::onIrcPassword);
getEvents(NoticeInfo.class).doOnError(throwable -> log.error("Error processing notice", throwable))
.retry()
.subscribe(this::onNotice);
Expand Down Expand Up @@ -250,10 +246,6 @@ private void onNotice(NoticeInfo noticeMessage) {
notificationService.addServerNotification(new ImmediateNotification(i18n.get("messageFromServer"), noticeMessage.getText(), severity, Collections.singletonList(new DismissAction(i18n))));
}

private void onIrcPassword(IrcPasswordInfo ircPasswordInfo) {
eventBus.post(ircPasswordInfo);
}

public void restoreGameSession(int id) {
lobbyClient.restoreGameSession(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.faforever.client.remote.FafServerAccessor;
import com.faforever.client.test.ServiceTest;
import com.faforever.client.user.LoginService;
import com.faforever.commons.lobby.IrcPasswordInfo;
import com.faforever.commons.lobby.SocialInfo;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.eventbus.EventBus;
Expand Down Expand Up @@ -675,13 +674,6 @@ public void testAddChannelUserListListener() {
verify(listener, times(2)).onChanged(any());
}

@Test
public void testLeaveChannel() {
IrcPasswordInfo event = new IrcPasswordInfo("abc");
instance.onIrcPassword(event);
instance.leaveChannel(new ChatChannel(DEFAULT_CHANNEL_NAME));
}

@Test
public void testSendActionInBackground() throws Exception {
connect();
Expand Down

0 comments on commit 8374d7b

Please sign in to comment.