Advertisement
Guest User

Untitled

a guest
May 11th, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 3.69 KB | None | 0 0
  1. (defvar-local base-url "https://discordapp.com/api/v6")
  2. (defvar token "mfa.vUSb8-RKwu9yJ5SgbkbcB-ftH4T6qI5wu7dSdvDTCb3ZCAg5eYn16kLvylVyhGSHMViVDKxwyosamhzfhGEn")
  3. (defvar-local user-agent "emacs")
  4.  
  5. (defun get-discord-ws-gateway ()
  6.   "Gets discord's websocket gateway"
  7.   (let* (
  8.          (url-request-method "GET")
  9.          (url-request-extra-headers `(("User-Agent" . ,user-agent)))
  10.          )
  11.     (url-retrieve )
  12.     )
  13. )
  14.  
  15. (defun login-with-token (token)
  16.   "Login using a defined token"
  17.   (interactive)
  18.   (setq url-request-method "GET")
  19.   (setq url-request-extra-headers `(("User-Agent" . ,user-agent)
  20.                                     ("Authorization" . ,token)))
  21.   (url-retrieve "https://discordapp.com/api/v6/users/@me" 'handle-http-request `((handle-login)(,token)))
  22.   )
  23.  
  24. ;IchI1HjgKwG5JFj%93tb
  25. (defun login-with-email-passwd (email passwd)
  26.   "Login using an email/password set(No 2FA)"
  27.   (interactive "sUsername: \nsPassword: ")
  28.   (let* ((url-request-method "POST")
  29.          (url-request-extra-headers '(("Content-Type" . "application/json")))
  30.          (url-request-data (json-encode-list `(("email" . ,email) ("password" . ,passwd))))
  31.          )
  32.     (url-retrieve "https://discordapp.com/api/v6/auth/login" 'handle-http-request '((get-token)(nil)))
  33.     )
  34.   )
  35.  
  36. (defun handle-login (list-of-string token)
  37.   "Check if we logged in successfully using a token"
  38.   (let* ((json-key-type 'string)
  39.          (json-object-type 'hash-table)
  40.          (st (car (car list-of-string)))
  41.          (json (json-read-from-string st)))
  42.     (if (eq (gethash "id" json) json-null)
  43.         (progn (message "Error! Couldn't confirm ID. Try again."))
  44.       (progn
  45.         (setq discord-token token)
  46.         (setq discord-id (gethash "id" json))
  47.         (setq discord-discriminator (gethash "discriminator" json))
  48.         (setq discord-email (gethash "email" json))
  49.         (setq discord-username (gethash "username" json))
  50.         (message
  51.          (concat "Success! Username: " discord-username "#" discord-discriminator)
  52.         )
  53.        )
  54.       )
  55.     )
  56.   )
  57.  
  58. (defun get-token (list-of-string &rest spare)
  59.   "Get the user token from an email/passwd login"
  60.   (let* ((json-key-type 'string)
  61.          (json-object-type 'hash-table)
  62.          (st (car (car list-of-string)))
  63.          (json (json-read-from-string st))
  64.          )
  65.     (if (and (gethash "mfa" json) (eq (gethash "token" json) json-null))
  66.         (progn (message "Error! 2FA required. Please log in directly with a token."))
  67.       (progn (if (eq gethash "token" json) json-null)
  68.              (message "Error! Couldn't get token")
  69.              (progn (message "Success! Attempting to log in via token")
  70.                     (login-with-token (gethash "token" json))))
  71.         ))
  72.   )
  73.  
  74. (defun get-bad-request-reason (list-of-string)
  75.   (let* ((json-key-type 'string)
  76.          (json-object-type 'alist)
  77.          (st (car (car list-of-string)))
  78.          (json (json-read-from-string st))
  79.          )
  80.     (message (concat "Reason: " (aref (cdr (car (last json) ) )0)))
  81.     )
  82.   )
  83.  
  84. (defun handle-http-request (status f args)
  85.       "Switch to the buffer returned by `url-retreive'.
  86.    The buffer contains the raw HTTP response sent by the server."
  87.       (forward-word 4)
  88.       (setq return-code (string-to-number (word-at-point)))
  89.       (goto-char (+ 1 url-http-end-of-headers))
  90.       (let* ((response (thing-at-point 'line t)))
  91.         (cond
  92.        ((= return-code 400)
  93.         (message "Bad request")
  94.         (get-bad-request-reason `((,response)))
  95.         )
  96.       ((= return-code 201)
  97.        (message "Bad auth"))
  98.       ((and (< return-code 300) (> return-code 199))
  99.          (apply (car f) `((,response)) args)
  100.         )
  101.        )
  102.       )
  103. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement