Archived
1
0
Disbranĉigi 0

Copyediting for conciseness and readability

This commit is contained in:
Jaidyn Lev 2018-06-15 12:23:49 -05:00
parent 80b50e5016
commit 842ba7f911

250
src/main.lisp Normal file → Executable file
View File

@ -1,189 +1,129 @@
(in-package :cl-matrix) (in-package :cl-matrix)
(defun matrix-post-request (url post-pairlis &optional (access-token nil) (content-type "application/json"))
"Make a POST request to a Matrix homeserver, for API calls."
(let ((url (concatenate `string
"http://" *homeserver* url
(if access-token
(format nil "?access_token=~A" *access-token*)))))
(json:decode-json-from-string
(s-http-client:do-http-request
(concatenate `string "http://" *homeserver* url)
:method :post
:content
(json:encode-json-alist-to-string
post-partlis)
:content-type content-type))))
(defun matrix-get-request (url &optional (access-token))
"Make a GET request to a Matrix homeserver, for API calls."
(let ((url (concatenate `string
"http://" *homeserver* url
(if access-token
(format nil "?access_token=~A" *access-token*)))))
(json:decode-json-from-string
(s-http-client:do-http-request
url
:method get))))
(defun account-log-in (username password) (defun account-log-in (username password)
(let ((access-token "") "'Log in' by fetching the access-token of an account."
(error "")
(result (json:decode-json-from-string
(s-http-client:do-http-request
(concatenate `string "http://" *homeserver* "/_matrix/client/r0/login")
:method :post
:content
(json:encode-json-alist-to-string
(pairlis
(list `type `user `password)
(list "m.login.password" username password)))
:content-type "application/json"))))
(setq access-token (cdr (assoc `:access--token result)) ) (cdr (assoc `:access--token
(setq error (cdr (assoc `:errcode result))) (matrix-post-request "/_matrix/client/r0/login"
(pairlis
(list `type `user `password)
(list "m.login.password" username password))))))
(if access-token
access-token)))
(defun room-create (room-name) (defun room-create (room-name)
(let "Create a Matrix room."
((result (json:decode-json-from-string
(s-http-client:do-http-request (matrix-post-request "/_matrix/client/r0/createRoom" `T
(concatenate `string "http://" (pairlis
*homeserver* (list `room_alias_name)
"/_matrix/client/r0/createRoom?access_token=" (list room-name))
*access-token*) `T))
:method :post
:content
(json:encode-json-alist-to-string
(pairlis
(list `room_alias_name)
(list room-name)))
:content-type "application/json"))))
result))
;; (if result
;; (cdr (assoc `:room--id result)))))
(defun msg-send (msg room-id) (defun msg-send (msg room-id)
(let "Send a text message to a specific room."
((result (json:decode-json-from-string
(s-http-client:do-http-request (matrix-post-request (concatenate `string "/_matrix/client/r0/rooms/" room-id)
(concatenate `string "http://" (pairlis
*homeserver* (list `msgtype `body)
"/_matrix/client/r0/rooms/" (list "m.text" msg))
room-id `T))
"/send/m.room.message?access_token="
*access-token*)
:method :post
:content
(json:encode-json-alist-to-string
(pairlis
(list `msgtype `body)
(list "m.text" msg)))
:content-type "application/json"))))
(if result
(cdr (assoc `:event--id result)))))
(defun user-invite (user-id room-id) (defun user-invite (user-id room-id)
(let "Invite a user to a chat-room."
((result (json:decode-json-from-string
(s-http-client:do-http-request (matrix-post-request (concatenate `string "/_matrix/client/r0/rooms/" room-id "/invite")
(concatenate `string "http://" (pairlis
*homeserver* (list `user_id)
"/_matrix/client/r0/rooms/" (list user-id))
room-id `T))
"/invite?access_token="
*access-token*)
:method :post
:content
(json:encode-json-alist-to-string
(pairlis
(list `user_id)
(list user-id)))
:content-type "application/json"))))
result))
;; non-working `o`
(defun room-join (room-id) (defun room-join (room-id)
(let "Join a Matrix room-- currently NOT WORKING."
((result (json:decode-json-from-string
(s-http-client:do-http-request (matrix-get-request (concatenate `string "/_matrix/client/r0/rooms/"
(concatenate `string "http://" (s-http-client:uri-encode-for-query room-id)
*homeserver* "/join")
"/_matrix/client/r0/rooms/" `T))
(s-http-client:uri-encode-for-query room-id)
"/join?access_token="
*access-token*)
:method :get))))
result))
(defun account-sync () (defun account-sync ()
(let "Fetch all of the data of a Matrix account."
((result (json:decode-json-from-string
(s-http-client:do-http-request (matrix-get-request "/_matrix/client/r0/sync" `T))
(concatenate `string "http://"
*homeserver*
"/_matrix/client/r0/sync?access_token="
*access-token*)
:method :get))))
result))
(defun account-sync-since (since-value) (defun account-sync-since (since-value)
(let "Sync the account data since a certain special time-stamp."
((result (json:decode-json-from-string
(s-http-client:do-http-request (matrix-get-request (concatenate `string
(concatenate `string "http://" "/_matrix/client/r0/sync?access_token="
*homeserver* *access-token*
"/_matrix/client/r0/sync?access_token=" "&since="
*access-token* since-value)
"&since=" nil))
since-value)
:method :get))))
result))
(defun get-room-data (sync-data) (defun get-room-data (sync-data)
(cdr (nth 2 (nth 7 sync-data)))) "Single out room data from data of :account-sync or :account-sync-since."
(cdr (nth 2 (nth 7 sync-data))))
(defun room-messages (sync-data) (defun room-messages (sync-data)
(let ((rooms (get-room-data sync-data)) "Single out lists messages by room from data of :account-sync or :account-sync-since."
(x 0))
(mapcar (lambda (x) (nth 2 x)) rooms))) (let ((rooms (get-room-data sync-data)))
(mapcar (lambda (x) (nth 2 x)) rooms)))
(defun room-sync-to-intern-id (sync-id) (defun room-sync-to-intern-id (sync-id)
(string-downcase (cl-strings:replace-all (cl-strings:replace-all sync-id "-" "") "+" ""))) "Convert malformed room IDs from sync data to 'internal ID'. Since in the JSON sync
data, room-names are stored in all caps, while the actual room-names are in mixed-caps,
cl-matrix converts both sync-data and actual room-names into all downcase for internal
use."
(defun room-sync-to-room-id (sync-id) (string-downcase
;; (!S-N-BCO-OBNVE-+NV+-RVB-CY-T:MATRIX.ORG (cl-strings:replace-all
(loop (cl-strings:replace-all sync-id "-" "") "+" "")))
:for x
:from 0
:to (- (length sync-id) 1)
:do
(setq char (char sync-id x))
(if (not (or (eq char #\-) (eq char #\+)))
(if (eq upcase-state 1)
(setq char-list (append char-list (list (char (string-upcase (string char)) 0))))
(setq char-list (append char-list (list (char (string-downcase (string char)) 0))))))
(if (eq upcase-state 1)
(if (eq super-upcase 0)
(setq upcase-state 0)
(setq super-upcase 0)))
(if (eq upcase-state 1)
(setq upcase-state 0))
(if (eq char #\-)
(setq upcase-state 1)))
(if (eq char #\+)
(setq upcase-state 1))
(if (eq char #\+)
(setq super-upcase 1))
(list-to-string char-list))
(defun list-to-string (list)
"Converts a list of characters to a string."
(reduce `string-concat
(mapcar `string list)))
(defun user-joined-rooms () (defun user-joined-rooms ()
(let "Fetch rooms joined by the user."
((result (json:decode-json-from-string
(s-http-client:do-http-request
(concatenate `string "http://"
*homeserver*
"/_matrix/client/r0/joined_rooms?access_token="
*access-token*)
:method :get))))
(setq result
(mapcar #'string-downcase (cdr (car result))))
result))
(mapcar #`string-downcare
(cdr (car (matrix-get-request "/_matrix/client/r0/joined_rooms" `T)))))