diff options
Diffstat (limited to 'lisp')
-rw-r--r-- | lisp/mastodon-auth.el | 22 | ||||
-rw-r--r-- | lisp/mastodon-http.el | 23 | ||||
-rw-r--r-- | lisp/mastodon-toot.el | 6 |
3 files changed, 43 insertions, 8 deletions
diff --git a/lisp/mastodon-auth.el b/lisp/mastodon-auth.el index 5a52f8c..06d7685 100644 --- a/lisp/mastodon-auth.el +++ b/lisp/mastodon-auth.el @@ -9,6 +9,9 @@ (defvar mastodon--api-token-string nil) (defun mastodon--register-client-app-triage (status) + "Callback function to triage `mastodon--register-client-app' response. + +STATUS is passed by `url-retrieve'." (mastodon--http-response-triage status (lambda () (let ((client-data (mastodon--json-hash-table))) (setq mastodon--client-app-plist @@ -18,9 +21,7 @@ ,(gethash "client_secret" client-data))))))) (defun mastodon--register-client-app () - "Adds client_id and client_secret to `mastodon--client-plist'. - - Returns a `plist' with CLIENT_ID and CLIENT_SECRET." + "Adds `:client_id' and `client_secret' to `mastodon--client-plist'." (mastodon--http-post (mastodon--api-for "apps") 'mastodon--register-client-app-triage '(("client_name" . "mastodon.el") @@ -28,11 +29,13 @@ ("scopes" . "read write follow")))) (defun mastodon--register-and-return-client-app () + "Registers `mastodon' with an instance. Returns `mastodon--client-app-plist'." (progn (mastodon--register-client-app) mastodon--client-app-plist)) (defun mastodon--store-client-id-and-secret () + "Stores `:client_id' and `:client_secret' in a plstore." (let ((client-plist (mastodon--register-and-return-client-app)) (plstore (plstore-open mastodon-token-file))) (plstore-put plstore "mastodon" `(:client_id @@ -44,6 +47,10 @@ client-plist)) (defun mastodon--client-app () + "Returns `mastodon--client-app-plist'. + +If not set, retrieves client data from `mastodon-token-file'. +If no data can be found in the token file, registers the app and stores its data via `mastodon--store-client-id-and-secret'." (if (plist-get mastodon--client-app-plist :client_secret) mastodon--client-app-plist (let* ((plstore (plstore-open mastodon-token-file)) @@ -57,6 +64,9 @@ mastodon--client-app-plist))))) (defun mastodon--get-access-token-triage (status) + "Callback function to triage `mastodon--get-access-token' response. + +STATUS is passed by `url-retrieve'." (mastodon--http-response-triage status (lambda () (let ((token-data (mastodon--json-hash-table))) @@ -65,6 +75,9 @@ mastodon--api-token-string))))) (defun mastodon--get-access-token () + "Retrieves access token from instance. Authenticates with email address and password. + +Email address and password are not stored." (mastodon--http-post (concat mastodon-instance-url "/oauth/token") 'mastodon--get-access-token-triage `(("client_id" . ,(plist-get (mastodon--client-app) :client_id)) @@ -74,6 +87,9 @@ ("password" . ,(read-passwd "Password: "))))) (defun mastodon--access-token () + "Returns `mastodon--api-token-string'. + +If not set, retrieves token with `mastodon--get-access-token'." (if mastodon--api-token-string mastodon--api-token-string (progn diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el index acfda8f..f8cb60d 100644 --- a/lisp/mastodon-http.el +++ b/lisp/mastodon-http.el @@ -21,39 +21,52 @@ Response buffer is passed to the CALLBACK function." (url-retrieve url callback))) (defun mastodon--response-buffer () + "Capture response buffer content as string." (with-current-buffer (current-buffer) (buffer-substring-no-properties (point-min) (point-max)))) (defun mastodon--response-body-substring (pattern) + "Returns substring matching PATTERN from `mastodon--response-buffer'." (let ((resp (mastodon--response-buffer))) (progn (string-match pattern resp) (match-string 0 resp)))) (defun mastodon--response-match-p (pattern) + "Returns non-nil if `mastodon--response-buffer' matches PATTERN." (let ((resp (mastodon--response-buffer))) (string-match-p pattern resp))) (defun mastodon--response-status-p () + "Returns non-nil if `mastodon--response-buffer' has an HTTP Response Status-Line." (when (mastodon--response-match-p "^HTTP/1.*$") t)) (defun mastodon--response-json () - (mastodon--response-body-substring "\{.*\}")) + "Returns string of JSON response body from `mastodon--response-buffer'." + (mastodon--response-body-substring "\{.*\}")) (defun mastodon--response-code () + "Returns HTTP Response Status Code from `mastodon--response-buffer'." (let* ((status-line (mastodon--response-body-substring "^HTTP/1.*$"))) - (progn - (string-match "[0-9][0-9][0-9]" status-line) - (match-string 0 status-line)))) + (progn + (string-match "[0-9][0-9][0-9]" status-line) + (match-string 0 status-line)))) (defun mastodon--json-hash-table () - "Reads JSON string from `mastodon--response-json' into a hash table." + "Reads JSON from `mastodon--response-json' into a hash table." (let ((json-object-type 'hash-table) (json-array-type 'list) (json-key-type 'string)) (json-read-from-string (mastodon--response-json)))) (defun mastodon--http-response-triage (status success) + "Callback function to triage an HTTP response. + +Recursively waits for `mastodon--response-buffer' to contain a Status-Line. + +STATUS is passed by `url-retrieve'. +SUCCESS is a function called on a 2XX level response code. +If response code is not 2XX, switches to the response buffer created by `url-retrieve'." (when (not (mastodon--response-status-p)) (mastodon--http-response-triage status)) (if (string-prefix-p "2" (mastodon--response-code)) diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index e648a95..eb82f12 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -6,16 +6,21 @@ :group 'mastodon) (defun mastodon-new-toot () + "Updates a Mastodon instance with new toot. Content is captured in a new buffer." (interactive) (progn (switch-to-buffer-other-window (get-buffer-create "*new toot*")) (mastodon-toot-mode t))) (defun mastodon-toot--send-triage (status) + "Callback function to triage toot POST. + +STATUS is passed by `url-retrieve'." (mastodon--http-response-triage status (lambda () (switch-to-buffer (current-buffer))))) ;; FIXME (defun mastodon-toot--send () + "Kills new-toot buffer/window and POSTs contents to the Mastodon instance." (interactive) (let ((toot (buffer-string)) (endpoint (mastodon--api-for "statuses"))) @@ -29,6 +34,7 @@ (mastodon--access-token)))))))) (defun mastodon-toot--cancel () + "Kills new-toot buffer/window. Does not POST content to Mastodon." (interactive) (kill-buffer-and-window)) |