Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2014
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.56 KB | None | 0 0
  1.  
  2. package com.skiwi.githubhooksechatservice.chatbot;
  3.  
  4. import com.gistlabs.mechanize.document.html.HtmlDocument;
  5. import com.gistlabs.mechanize.document.html.form.Form;
  6. import com.gistlabs.mechanize.document.html.form.SubmitButton;
  7. import com.gistlabs.mechanize.document.json.JsonDocument;
  8. import com.gistlabs.mechanize.impl.DefaultResource;
  9. import com.gistlabs.mechanize.impl.MechanizeAgent;
  10. import java.io.IOException;
  11. import java.io.UncheckedIOException;
  12. import java.io.UnsupportedEncodingException;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.Objects;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import java.util.stream.Stream;
  20. import org.apache.http.HttpException;
  21. import org.apache.http.HttpRequest;
  22. import org.apache.http.HttpRequestInterceptor;
  23. import org.apache.http.HttpResponse;
  24. import org.apache.http.ProtocolException;
  25. import org.apache.http.client.RedirectStrategy;
  26. import org.apache.http.client.methods.HttpGet;
  27. import org.apache.http.client.methods.HttpUriRequest;
  28. import org.apache.http.protocol.HttpContext;
  29.  
  30. /**
  31.  *
  32.  * @author Frank van Heeswijk
  33.  */
  34. public class StackExchangeChatBot implements ChatBot {
  35.     private final MechanizeAgent agent;
  36.    
  37.     private String chatFKey;
  38.    
  39.     public StackExchangeChatBot() {
  40.         this.agent = new MechanizeAgent();
  41.         //TODO $agent.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  42.        
  43.         this.agent.getClient().setRedirectStrategy(new RedirectStrategy() {
  44.             @Override
  45.             public boolean isRedirected(final HttpRequest httpRequest, final HttpResponse httpResponse, final HttpContext httpContext) throws ProtocolException {
  46.                 return (httpResponse.getStatusLine().getStatusCode() == 302);
  47.             }
  48.  
  49.             @Override
  50.             public HttpUriRequest getRedirect(final HttpRequest httpRequest, final HttpResponse httpResponse, final HttpContext httpContext) throws ProtocolException {
  51.                 httpRequest.getRequestLine().getProtocolVersion().getProtocol();
  52.                 String host = httpRequest.getFirstHeader("Host").getValue();
  53.                 String location = httpResponse.getFirstHeader("Location").getValue();
  54.                 String protocol = (httpRequest.getFirstHeader("Host").getValue().equals("openid.stackexchange.com")) ? "https" : "http";
  55.                 if (location.startsWith("http://") || location.startsWith("https://")) {
  56.                     System.out.println("Redirecting to " + location);
  57.                     return new HttpGet(location);
  58.                 }
  59.                 else {
  60.                     System.out.println("Redirecting to " + protocol + "://" + host + location);
  61.                     return new HttpGet(protocol + "://" + host + location);
  62.                 }
  63.             }
  64.         });
  65.        
  66.         this.agent.getClient().addRequestInterceptor((request, context) -> {
  67.             System.out.println("Request to " + request.getRequestLine().getUri());
  68.             if (request.getRequestLine().getUri().equals("/login/global-fallback")) {
  69.                 request.addHeader("Referer", "http://stackexchange.com/users/chat-login");
  70.             }
  71.         });
  72.     }
  73.    
  74.     @Override
  75.     public void start() {
  76.         loginOpenId();
  77.         System.out.println();
  78.        
  79.         loginRoot();
  80.         System.out.println();
  81.        
  82.         loginChat();
  83.         System.out.println();
  84.        
  85.         String fkey = getFKey();
  86.         this.chatFKey = fkey;
  87.         System.out.println("Found fkey: " + fkey);
  88.         System.out.println();
  89.     }
  90.    
  91.     private void loginOpenId() {
  92.         HtmlDocument openIdLoginPage = agent.get("https://openid.stackexchange.com/account/login");
  93.         Form loginForm = openIdLoginPage.forms().getAll().get(0);
  94.         loginForm.get("email").setValue("*secret*");
  95.         loginForm.get("password").setValue("*secret*");
  96.         List<SubmitButton> submitButtons = loginForm.findAll("input[type=submit]", SubmitButton.class);
  97.         HtmlDocument response = loginForm.submit(submitButtons.get(0));
  98.         System.out.println(response.getTitle());
  99.         System.out.println("OpenID login attempted.");
  100.     }
  101.    
  102.     private void loginRoot() {
  103.         HtmlDocument rootLoginPage = agent.get("http://stackexchange.com/users/login");
  104.         Form loginForm = rootLoginPage.forms().getAll().get(rootLoginPage.forms().getAll().size() - 1);
  105.         loginForm.get("openid_identifier").setValue("https://openid.stackexchange.com/");
  106.         List<SubmitButton> submitButtons = loginForm.findAll("input[type=submit]", SubmitButton.class);
  107.         HtmlDocument response = loginForm.submit(submitButtons.get(submitButtons.size() - 1));
  108.         System.out.println(response.getTitle());
  109.         System.out.println("Root login attempted.");
  110.     }
  111.    
  112.     private void loginChat() {
  113.         HtmlDocument chatLoginPage = agent.get("http://stackexchange.com/users/chat-login");
  114.         System.out.println("Request headers");
  115.         Stream.of(chatLoginPage.getRequest().getAllHeaders()).forEach(System.out::println);
  116.         System.out.println();
  117.         System.out.println("Response headers");
  118.         Stream.of(chatLoginPage.getResponse().getAllHeaders()).forEach(System.out::println);
  119.         Form loginForm = chatLoginPage.forms().getAll().get(chatLoginPage.forms().getAll().size() - 1);
  120.         List<SubmitButton> submitButtons = loginForm.findAll("input[type=submit]", SubmitButton.class);
  121.         HtmlDocument response = loginForm.submit(submitButtons.get(submitButtons.size() - 1));
  122.         System.out.println(response.getTitle());
  123.         System.out.println("Chat login attempted.");
  124.     }
  125.    
  126.     private String getFKey() {
  127.         HtmlDocument joinFavoritesPage = agent.get("http://chat.stackexchange.com/chats/join/favorite");
  128.         Form joinForm = joinFavoritesPage.forms().getAll().get(joinFavoritesPage.forms().getAll().size() - 1);
  129.         return joinForm.get("fkey").getValue();
  130.     }
  131.    
  132.     @Override
  133.     public void postMessage(final String text) {
  134.         Objects.requireNonNull(text, "text");
  135.         Map<String, String> parameters = new HashMap<>();
  136.         parameters.put("text", text);
  137.         parameters.put("fkey", this.chatFKey);
  138.         try {
  139.             JsonDocument response = agent.post("http://chat.stackexchange.com/chats/16134/messages/new", parameters);
  140.             System.out.println(response.getTitle());
  141.         } catch (UnsupportedEncodingException ex) {
  142.             throw new UncheckedIOException(ex);
  143.         }
  144.     }
  145.  
  146.     @Override
  147.     public void stop() {
  148.        
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement