Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.skiwi.githubhooksechatservice.chatbot;
- import com.gistlabs.mechanize.document.html.HtmlDocument;
- import com.gistlabs.mechanize.document.html.form.Form;
- import com.gistlabs.mechanize.document.html.form.SubmitButton;
- import com.gistlabs.mechanize.document.json.JsonDocument;
- import com.gistlabs.mechanize.impl.DefaultResource;
- import com.gistlabs.mechanize.impl.MechanizeAgent;
- import java.io.IOException;
- import java.io.UncheckedIOException;
- import java.io.UnsupportedEncodingException;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Objects;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import java.util.stream.Stream;
- import org.apache.http.HttpException;
- import org.apache.http.HttpRequest;
- import org.apache.http.HttpRequestInterceptor;
- import org.apache.http.HttpResponse;
- import org.apache.http.ProtocolException;
- import org.apache.http.client.RedirectStrategy;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpUriRequest;
- import org.apache.http.protocol.HttpContext;
- /**
- *
- * @author Frank van Heeswijk
- */
- public class StackExchangeChatBot implements ChatBot {
- private final MechanizeAgent agent;
- private String chatFKey;
- public StackExchangeChatBot() {
- this.agent = new MechanizeAgent();
- //TODO $agent.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
- this.agent.getClient().setRedirectStrategy(new RedirectStrategy() {
- @Override
- public boolean isRedirected(final HttpRequest httpRequest, final HttpResponse httpResponse, final HttpContext httpContext) throws ProtocolException {
- return (httpResponse.getStatusLine().getStatusCode() == 302);
- }
- @Override
- public HttpUriRequest getRedirect(final HttpRequest httpRequest, final HttpResponse httpResponse, final HttpContext httpContext) throws ProtocolException {
- httpRequest.getRequestLine().getProtocolVersion().getProtocol();
- String host = httpRequest.getFirstHeader("Host").getValue();
- String location = httpResponse.getFirstHeader("Location").getValue();
- String protocol = (httpRequest.getFirstHeader("Host").getValue().equals("openid.stackexchange.com")) ? "https" : "http";
- if (location.startsWith("http://") || location.startsWith("https://")) {
- System.out.println("Redirecting to " + location);
- return new HttpGet(location);
- }
- else {
- System.out.println("Redirecting to " + protocol + "://" + host + location);
- return new HttpGet(protocol + "://" + host + location);
- }
- }
- });
- this.agent.getClient().addRequestInterceptor((request, context) -> {
- System.out.println("Request to " + request.getRequestLine().getUri());
- if (request.getRequestLine().getUri().equals("/login/global-fallback")) {
- request.addHeader("Referer", "http://stackexchange.com/users/chat-login");
- }
- });
- }
- @Override
- public void start() {
- loginOpenId();
- System.out.println();
- loginRoot();
- System.out.println();
- loginChat();
- System.out.println();
- String fkey = getFKey();
- this.chatFKey = fkey;
- System.out.println("Found fkey: " + fkey);
- System.out.println();
- }
- private void loginOpenId() {
- HtmlDocument openIdLoginPage = agent.get("https://openid.stackexchange.com/account/login");
- Form loginForm = openIdLoginPage.forms().getAll().get(0);
- loginForm.get("email").setValue("*secret*");
- loginForm.get("password").setValue("*secret*");
- List<SubmitButton> submitButtons = loginForm.findAll("input[type=submit]", SubmitButton.class);
- HtmlDocument response = loginForm.submit(submitButtons.get(0));
- System.out.println(response.getTitle());
- System.out.println("OpenID login attempted.");
- }
- private void loginRoot() {
- HtmlDocument rootLoginPage = agent.get("http://stackexchange.com/users/login");
- Form loginForm = rootLoginPage.forms().getAll().get(rootLoginPage.forms().getAll().size() - 1);
- loginForm.get("openid_identifier").setValue("https://openid.stackexchange.com/");
- List<SubmitButton> submitButtons = loginForm.findAll("input[type=submit]", SubmitButton.class);
- HtmlDocument response = loginForm.submit(submitButtons.get(submitButtons.size() - 1));
- System.out.println(response.getTitle());
- System.out.println("Root login attempted.");
- }
- private void loginChat() {
- HtmlDocument chatLoginPage = agent.get("http://stackexchange.com/users/chat-login");
- System.out.println("Request headers");
- Stream.of(chatLoginPage.getRequest().getAllHeaders()).forEach(System.out::println);
- System.out.println();
- System.out.println("Response headers");
- Stream.of(chatLoginPage.getResponse().getAllHeaders()).forEach(System.out::println);
- Form loginForm = chatLoginPage.forms().getAll().get(chatLoginPage.forms().getAll().size() - 1);
- List<SubmitButton> submitButtons = loginForm.findAll("input[type=submit]", SubmitButton.class);
- HtmlDocument response = loginForm.submit(submitButtons.get(submitButtons.size() - 1));
- System.out.println(response.getTitle());
- System.out.println("Chat login attempted.");
- }
- private String getFKey() {
- HtmlDocument joinFavoritesPage = agent.get("http://chat.stackexchange.com/chats/join/favorite");
- Form joinForm = joinFavoritesPage.forms().getAll().get(joinFavoritesPage.forms().getAll().size() - 1);
- return joinForm.get("fkey").getValue();
- }
- @Override
- public void postMessage(final String text) {
- Objects.requireNonNull(text, "text");
- Map<String, String> parameters = new HashMap<>();
- parameters.put("text", text);
- parameters.put("fkey", this.chatFKey);
- try {
- JsonDocument response = agent.post("http://chat.stackexchange.com/chats/16134/messages/new", parameters);
- System.out.println(response.getTitle());
- } catch (UnsupportedEncodingException ex) {
- throw new UncheckedIOException(ex);
- }
- }
- @Override
- public void stop() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement