-
-
Save samuelkadolph/7fe271784df5eef6ca97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "ffi" | |
module XboxLiveLogin | |
module GTK | |
extend FFI::Library | |
ffi_lib "gtk-3" | |
attach_function :g_signal_connect_data, [:pointer, :string, :pointer, :pointer, :pointer, :int], :ulong | |
attach_function :gtk_init, [:int, :pointer], :void | |
attach_function :gtk_main, [], :void | |
attach_function :gtk_main_quit, [], :void | |
attach_function :gtk_widget_destroy, [:pointer], :void | |
def g_signal_connect(instance, detailed_signal, c_handler, data) | |
g_signal_connect_data(instance, detailed_signal, c_handler, data, nil, 0) | |
end | |
module_function :g_signal_connect | |
end | |
module Soup | |
extend FFI::Library | |
ffi_lib "soup-2.4" | |
attach_function :soup_cookie_jar_get_cookies, [:pointer, :pointer, :bool], :string | |
attach_function :soup_cookie_jar_get_type, [], :pointer | |
attach_function :soup_session_get_feature, [:pointer, :pointer], :pointer | |
attach_function :soup_uri_new, [:string], :pointer | |
end | |
module WebKit | |
extend FFI::Library | |
ffi_lib "webkitgtk-3.0" | |
attach_function :webkit_dom_document_get_elements_by_name, [:pointer, :string], :pointer | |
attach_function :webkit_dom_html_collection_named_item, [:pointer, :string], :pointer | |
attach_function :webkit_dom_html_form_element_get_elements, [:pointer], :pointer | |
attach_function :webkit_dom_html_input_element_click, [:pointer], :void | |
attach_function :webkit_dom_html_input_element_set_value, [:pointer, :string], :void | |
attach_function :webkit_dom_node_list_item, [:pointer, :ulong], :pointer | |
attach_function :webkit_get_default_session, [], :pointer | |
attach_function :webkit_web_view_get_dom_document, [:pointer], :pointer | |
attach_function :webkit_web_view_load_uri, [:pointer, :string], :void | |
attach_function :webkit_web_view_new, [], :pointer | |
end | |
extend GTK | |
extend Soup | |
extend WebKit | |
URL = "http://live.xbox.com/en-US/friendcenter" | |
class << self | |
def login(username, password, url = URL) | |
cookie = nil | |
gtk_init(0, FFI::MemoryPointer.new(:pointer, 0)) | |
view = webkit_web_view_new() | |
webkit_web_view_load_uri(view, url) | |
on_document_load = FFI::Function.new(:void, [:pointer, :pointer, :pointer]) do |view, frame, step| | |
case step.write_int(step.read_int + 1).read_int | |
when 2 | |
document = webkit_web_view_get_dom_document(view) | |
list = webkit_dom_document_get_elements_by_name(document, "f1") | |
form = webkit_dom_node_list_item(list, 0) | |
elements = webkit_dom_html_form_element_get_elements(form) | |
login = webkit_dom_html_collection_named_item(elements, "login") | |
passwd = webkit_dom_html_collection_named_item(elements, "passwd") | |
submit = webkit_dom_html_collection_named_item(elements, "SI") | |
webkit_dom_html_input_element_set_value(login, username) | |
webkit_dom_html_input_element_set_value(passwd, password) | |
webkit_dom_html_input_element_click(submit) | |
when 5 | |
session = webkit_get_default_session() | |
jar = soup_session_get_feature(session, soup_cookie_jar_get_type) | |
cookie = soup_cookie_jar_get_cookies(jar, soup_uri_new(url), true) | |
gtk_main_quit() | |
end | |
end | |
step = FFI::MemoryPointer.new(:pointer).write_int(0) | |
g_signal_connect(view, "document-load-finished", on_document_load, step) | |
gtk_main() | |
gtk_widget_destroy(view) | |
cookie | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment