Advertisement
Guest User

Untitled

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