Skip to content

Commit 89ca467

Browse files
committed
Extract LoginFunction interface, with StackExchangeLogin implementation
1 parent 097d587 commit 89ca467

File tree

3 files changed

+118
-75
lines changed

3 files changed

+118
-75
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package net.zomis.duga.chat;
2+
3+
import com.gistlabs.mechanize.impl.MechanizeAgent;
4+
5+
public interface LoginFunction {
6+
7+
MechanizeAgent constructAgent(BotConfiguration configuration);
8+
String retrieveFKey(MechanizeAgent agent, BotConfiguration configuration);
9+
10+
}

duga-core/src/main/java/net/zomis/duga/chat/StackExchangeChatBot.java

Lines changed: 5 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.*;
55
import java.util.concurrent.*;
66
import java.util.function.Consumer;
7+
import java.util.function.Function;
78
import java.util.logging.Level;
89
import java.util.logging.Logger;
910
import java.util.stream.Collectors;
@@ -41,6 +42,7 @@ public class StackExchangeChatBot implements ChatBot {
4142
private final BlockingQueue<List<ChatMessage>> messagesQueue = new LinkedBlockingQueue<>();
4243

4344
private final MechanizeAgent agent;
45+
private final LoginFunction loginFunction = new StackExchangeLogin();
4446

4547
private final BotConfiguration configuration;
4648

@@ -50,36 +52,7 @@ public class StackExchangeChatBot implements ChatBot {
5052

5153
public StackExchangeChatBot(BotConfiguration config) {
5254
this.configuration = config;
53-
this.agent = new MechanizeAgent();
54-
55-
this.agent.getClient().setRedirectStrategy(new RedirectStrategy() {
56-
@Override
57-
public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
58-
return response.getStatusLine().getStatusCode() == 302;
59-
}
60-
61-
@Override
62-
public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
63-
System.out.println(Arrays.toString(response.getAllHeaders()));
64-
String host = request.getFirstHeader("Host").getValue();
65-
String location = response.getFirstHeader("Location").getValue();
66-
String protocol = host.equals("openid.stackexchange.com") ? "https" : "http";
67-
if (location.startsWith("http://") || location.startsWith("https://")) {
68-
LOGGER.info("Redirecting to " + location);
69-
return new HttpGet(location);
70-
} else {
71-
LOGGER.info("Redirecting to " + protocol + "://" + host + location);
72-
return new HttpGet(protocol + "://" + host + location);
73-
}
74-
}
75-
});
76-
77-
this.agent.getClient().addRequestInterceptor((request, context) -> {
78-
LOGGER.info("Request to " + request.getRequestLine().getUri());
79-
if (request.getRequestLine().getUri().equals("/login/global-fallback")) {
80-
request.addHeader("Referer", configuration.getRootUrl() + "/users/chat-login");
81-
}
82-
});
55+
this.agent = loginFunction.constructAgent(config);
8356
}
8457

8558
public void start() {
@@ -97,53 +70,10 @@ public void start() {
9770
}
9871

9972
private void login() {
100-
loginOpenId();
101-
loginRoot();
102-
loginChat();
103-
104-
String fkey = retrieveFKey();
105-
this.chatFKey = fkey;
106-
LOGGER.info("Found fkey: " + fkey);
73+
this.chatFKey = loginFunction.retrieveFKey(agent, configuration);
74+
LOGGER.info("Found fkey: " + chatFKey);
10775
}
10876

109-
private void loginOpenId() {
110-
HtmlDocument openIdLoginPage = agent.get("https://openid.stackexchange.com/account/login");
111-
System.out.println(openIdLoginPage);
112-
System.out.println(openIdLoginPage.getRoot());
113-
Form loginForm = openIdLoginPage.forms().getAll().get(0);
114-
loginForm.get("email").setValue(configuration.getBotEmail());
115-
loginForm.get("password").setValue(configuration.getBotPassword());
116-
List<SubmitButton> submitButtons = loginForm.findAll("input[type=submit]", SubmitButton.class);
117-
HtmlDocument response = loginForm.submit(submitButtons.get(0));
118-
LOGGER.info(response.getTitle());
119-
LOGGER.info("OpenID login attempted.");
120-
}
121-
122-
private void loginRoot() {
123-
HtmlDocument rootLoginPage = agent.get(configuration.getRootUrl() + "/users/login");
124-
Form loginForm = rootLoginPage.forms().getAll().get(rootLoginPage.forms().getAll().size() - 1);
125-
loginForm.get("openid_identifier").setValue("https://openid.stackexchange.com/");
126-
List<SubmitButton> submitButtons = loginForm.findAll("input[type=submit]", SubmitButton.class);
127-
HtmlDocument response = loginForm.submit(submitButtons.get(submitButtons.size() - 1));
128-
LOGGER.info(response.getTitle());
129-
LOGGER.info("Root login attempted.");
130-
}
131-
132-
private void loginChat() {
133-
HtmlDocument chatLoginPage = agent.get(configuration.getRootUrl() + "/users/chat-login");
134-
Form loginForm = chatLoginPage.forms().getAll().get(chatLoginPage.forms().getAll().size() - 1);
135-
List<SubmitButton> submitButtons = loginForm.findAll("input[type=submit]", SubmitButton.class);
136-
HtmlDocument response = loginForm.submit(submitButtons.get(submitButtons.size() - 1));
137-
LOGGER.info(response.getTitle());
138-
LOGGER.info("Chat login attempted.");
139-
}
140-
141-
private String retrieveFKey() {
142-
HtmlDocument joinFavoritesPage = agent.get(configuration.getChatUrl() + "/chats/join/favorite");
143-
Form joinForm = joinFavoritesPage.forms().getAll().get(joinFavoritesPage.forms().getAll().size() - 1);
144-
return joinForm.get("fkey").getValue();
145-
}
146-
14777
public Future<List<ChatMessageResponse>> postMessages(WebhookParameters params, final List<String> messages) {
14878
if (params == null) {
14979
throw new NullPointerException("Params cannot be null, unable to post " + messages);
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package net.zomis.duga.chat;
2+
3+
import com.gistlabs.mechanize.document.html.HtmlDocument;
4+
import com.gistlabs.mechanize.document.html.form.Form;
5+
import com.gistlabs.mechanize.document.html.form.SubmitButton;
6+
import com.gistlabs.mechanize.impl.MechanizeAgent;
7+
import org.apache.http.HttpRequest;
8+
import org.apache.http.HttpResponse;
9+
import org.apache.http.ProtocolException;
10+
import org.apache.http.client.RedirectStrategy;
11+
import org.apache.http.client.methods.HttpGet;
12+
import org.apache.http.client.methods.HttpUriRequest;
13+
import org.apache.http.protocol.HttpContext;
14+
15+
import java.util.Arrays;
16+
import java.util.List;
17+
import java.util.logging.Logger;
18+
19+
public class StackExchangeLogin implements LoginFunction {
20+
private static final Logger logger = Logger.getLogger(StackExchangeLogin.class.getName());
21+
22+
@Override
23+
public MechanizeAgent constructAgent(BotConfiguration configuration) {
24+
MechanizeAgent agent = new MechanizeAgent();
25+
agent.getClient().setRedirectStrategy(new RedirectStrategy() {
26+
@Override
27+
public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
28+
return response.getStatusLine().getStatusCode() == 302;
29+
}
30+
31+
@Override
32+
public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
33+
System.out.println(Arrays.toString(response.getAllHeaders()));
34+
String host = request.getFirstHeader("Host").getValue();
35+
String location = response.getFirstHeader("Location").getValue();
36+
String protocol = host.equals("openid.stackexchange.com") ? "https" : "http";
37+
if (location.startsWith("http://") || location.startsWith("https://")) {
38+
logger.info("Redirecting to " + location);
39+
return new HttpGet(location);
40+
} else {
41+
logger.info("Redirecting to " + protocol + "://" + host + location);
42+
return new HttpGet(protocol + "://" + host + location);
43+
}
44+
}
45+
});
46+
47+
agent.getClient().addRequestInterceptor((request, context) -> {
48+
logger.info("Request to " + request.getRequestLine().getUri());
49+
if (request.getRequestLine().getUri().equals("/login/global-fallback")) {
50+
request.addHeader("Referer", configuration.getRootUrl() + "/users/chat-login");
51+
}
52+
});
53+
54+
return agent;
55+
}
56+
57+
@Override
58+
public String retrieveFKey(MechanizeAgent agent, BotConfiguration configuration) {
59+
loginOpenId(agent, configuration);
60+
loginRoot(agent, configuration);
61+
loginChat(agent, configuration);
62+
return retrieveFKeyReal(agent, configuration);
63+
}
64+
65+
private void loginOpenId(MechanizeAgent agent, BotConfiguration configuration) {
66+
HtmlDocument openIdLoginPage = agent.get("https://openid.stackexchange.com/account/login");
67+
System.out.println(openIdLoginPage);
68+
System.out.println(openIdLoginPage.getRoot());
69+
Form loginForm = openIdLoginPage.forms().getAll().get(0);
70+
loginForm.get("email").setValue(configuration.getBotEmail());
71+
loginForm.get("password").setValue(configuration.getBotPassword());
72+
List<SubmitButton> submitButtons = loginForm.findAll("input[type=submit]", SubmitButton.class);
73+
HtmlDocument response = loginForm.submit(submitButtons.get(0));
74+
logger.info(response.getTitle());
75+
logger.info("OpenID login attempted.");
76+
}
77+
78+
private void loginRoot(MechanizeAgent agent, BotConfiguration configuration) {
79+
HtmlDocument rootLoginPage = agent.get(configuration.getRootUrl() + "/users/login");
80+
Form loginForm = rootLoginPage.forms().getAll().get(rootLoginPage.forms().getAll().size() - 1);
81+
loginForm.get("openid_identifier").setValue("https://openid.stackexchange.com/");
82+
List<SubmitButton> submitButtons = loginForm.findAll("input[type=submit]", SubmitButton.class);
83+
HtmlDocument response = loginForm.submit(submitButtons.get(submitButtons.size() - 1));
84+
logger.info(response.getTitle());
85+
logger.info("Root login attempted.");
86+
}
87+
88+
private void loginChat(MechanizeAgent agent, BotConfiguration configuration) {
89+
HtmlDocument chatLoginPage = agent.get(configuration.getRootUrl() + "/users/chat-login");
90+
Form loginForm = chatLoginPage.forms().getAll().get(chatLoginPage.forms().getAll().size() - 1);
91+
List<SubmitButton> submitButtons = loginForm.findAll("input[type=submit]", SubmitButton.class);
92+
HtmlDocument response = loginForm.submit(submitButtons.get(submitButtons.size() - 1));
93+
logger.info(response.getTitle());
94+
logger.info("Chat login attempted.");
95+
}
96+
97+
private String retrieveFKeyReal(MechanizeAgent agent, BotConfiguration configuration) {
98+
HtmlDocument joinFavoritesPage = agent.get(configuration.getChatUrl() + "/chats/join/favorite");
99+
Form joinForm = joinFavoritesPage.forms().getAll().get(joinFavoritesPage.forms().getAll().size() - 1);
100+
return joinForm.get("fkey").getValue();
101+
}
102+
103+
}

0 commit comments

Comments
 (0)