2023-01-13 16:22:22 +01:00
|
|
|
;; tinmop: an humble gemini and pleroma client
|
|
|
|
;; Copyright (C) 2022 cage
|
|
|
|
|
|
|
|
;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program.
|
|
|
|
;; If not, see [[http://www.gnu.org/licenses/][http://www.gnu.org/licenses/]].
|
|
|
|
|
|
|
|
(in-package :json-rpc-communication)
|
|
|
|
|
2023-02-12 11:41:33 +01:00
|
|
|
(defclass iri-complete-response (box) ())
|
|
|
|
|
2023-03-11 13:12:28 +01:00
|
|
|
(a:define-constant +tofu-error-status-code+ -1 :test #'=)
|
|
|
|
|
2023-02-12 11:41:33 +01:00
|
|
|
(defmethod yason:encode ((object iri-complete-response) &optional (stream *standard-output*))
|
|
|
|
(let ((json:*symbol-encoder* #'json:encode-symbol-as-lowercase)
|
|
|
|
(yason:*list-encoder* #'yason:encode-plist)
|
|
|
|
(json:*symbol-key-encoder* #'json:encode-symbol-as-lowercase))
|
|
|
|
(yason:with-output (stream)
|
|
|
|
(yason:with-object ()
|
|
|
|
(yason:with-object-element (:matches)
|
|
|
|
(yason:encode-array-elements (list->array (getf (unbox object) :matches))))
|
|
|
|
(yason:with-object-element (:indices)
|
|
|
|
(json:with-array ()
|
|
|
|
(loop for indices-group in (getf (unbox object) :indices) do
|
|
|
|
(yason:encode-array-elements (list->array (remove-if #'null indices-group))))))))))
|
|
|
|
|
2023-01-13 16:22:22 +01:00
|
|
|
(defun complete-net-address (hint)
|
|
|
|
(let ((prompt (ui:open-url-prompt)))
|
2023-02-12 11:41:33 +01:00
|
|
|
(multiple-value-bind (matched-strings x indices)
|
|
|
|
(funcall (complete:make-complete-gemini-iri-fn prompt) hint)
|
|
|
|
(declare (ignore x))
|
|
|
|
(make-instance 'iri-complete-response
|
|
|
|
:contents (list :matches matched-strings
|
|
|
|
:indices indices)))))
|
2023-01-13 16:22:22 +01:00
|
|
|
|
2023-03-15 17:18:38 +01:00
|
|
|
(defun request-stream-other-document-thread (wrapper-object
|
|
|
|
socket
|
|
|
|
host
|
|
|
|
port
|
|
|
|
path
|
|
|
|
query
|
|
|
|
fragment
|
|
|
|
status-code
|
|
|
|
status-code-description
|
|
|
|
meta)
|
|
|
|
(declare (ignorable host
|
|
|
|
port path query fragment
|
|
|
|
status-code status-code-description meta))
|
|
|
|
(with-accessors ((download-socket gemini-viewer:download-socket)
|
|
|
|
(download-stream gemini-viewer:download-stream)
|
|
|
|
(octect-count gemini-viewer:octect-count)
|
|
|
|
(support-file gemini-viewer:support-file)) wrapper-object
|
|
|
|
(lambda ()
|
|
|
|
(a:when-let ((extension (fs:get-extension path)))
|
|
|
|
(setf support-file (fs:temporary-file :extension extension)))
|
|
|
|
(gemini-viewer::with-open-support-file (file-stream support-file)
|
|
|
|
(labels ((download-completed-p (buffer read-so-far)
|
|
|
|
(and buffer
|
|
|
|
(< read-so-far (length buffer))))
|
|
|
|
(%fill-buffer ()
|
|
|
|
(when (gemini-viewer:downloading-allowed-p wrapper-object)
|
|
|
|
(multiple-value-bind (buffer read-so-far)
|
|
|
|
(ignore-errors (read-array download-stream
|
|
|
|
gemini-viewer::+read-buffer-size+))
|
|
|
|
(gemini-viewer::increment-bytes-count wrapper-object read-so-far)
|
|
|
|
(write-sequence buffer file-stream :start 0 :end read-so-far)
|
|
|
|
(force-output file-stream)
|
|
|
|
(if (download-completed-p buffer read-so-far)
|
|
|
|
(progn
|
|
|
|
(setf (gemini-viewer:stream-status wrapper-object) :completed)
|
|
|
|
(gemini-client:close-ssl-socket socket))
|
|
|
|
(%fill-buffer))))))
|
|
|
|
(%fill-buffer))))))
|
|
|
|
|
2023-01-13 16:22:22 +01:00
|
|
|
(defun request-success-dispatched-fn (status code-description meta response socket iri parsed-iri)
|
|
|
|
(declare (ignore iri))
|
|
|
|
(multiple-value-bind (actual-iri host path query port fragment)
|
|
|
|
(gemini-client:displace-iri parsed-iri )
|
|
|
|
(gemini-client:debug-gemini "response is a stream")
|
|
|
|
(labels ((make-text-based-stream (gemini-format-p)
|
|
|
|
(let* ((starting-status :streaming)
|
|
|
|
(gemini-stream (make-instance 'gemini-viewer::gemini-file-stream
|
|
|
|
:host host
|
|
|
|
:port port
|
|
|
|
:path path
|
|
|
|
:query query
|
|
|
|
:fragment fragment
|
|
|
|
:meta meta
|
|
|
|
:status-code status
|
|
|
|
:status-code-description
|
|
|
|
code-description
|
|
|
|
:stream-status starting-status
|
|
|
|
:download-iri actual-iri
|
|
|
|
:download-stream response
|
|
|
|
:download-socket socket))
|
|
|
|
(favicon (gemini-viewer::fetch-favicon parsed-iri))
|
|
|
|
(thread-fn (gemini-viewer::request-stream-gemini-document-thread gemini-stream
|
|
|
|
host
|
|
|
|
port
|
|
|
|
path
|
|
|
|
query
|
|
|
|
fragment
|
|
|
|
favicon
|
|
|
|
gemini-format-p)))
|
|
|
|
(gemini-viewer:push-db-stream gemini-stream)
|
|
|
|
(gemini-viewer::downloading-start-thread gemini-stream
|
2023-02-05 14:07:13 +01:00
|
|
|
thread-fn
|
|
|
|
host
|
|
|
|
port
|
|
|
|
path
|
|
|
|
query
|
|
|
|
fragment))))
|
2023-01-13 16:22:22 +01:00
|
|
|
(cond
|
|
|
|
((gemini-client:gemini-file-stream-p meta)
|
|
|
|
(gemini-client:debug-gemini "response is a gemini document stream")
|
|
|
|
(gemini-viewer:push-url-to-history *gemini-window* actual-iri)
|
|
|
|
(make-text-based-stream t)
|
2023-03-08 16:16:55 +01:00
|
|
|
(make-gemini-response (gemini-client:code gemini-client:+success+)
|
|
|
|
(gemini-client:description gemini-client:+success+)
|
|
|
|
meta
|
|
|
|
actual-iri))
|
2023-01-13 16:22:22 +01:00
|
|
|
((gemini-client:text-file-stream-p meta)
|
|
|
|
(gemini-client:debug-gemini "response is a text stream")
|
|
|
|
(make-text-based-stream nil)
|
2023-03-08 16:16:55 +01:00
|
|
|
(make-gemini-response (gemini-client:code gemini-client:+success+)
|
|
|
|
(gemini-client:description gemini-client:+success+)
|
|
|
|
meta
|
|
|
|
actual-iri))
|
2023-01-13 16:22:22 +01:00
|
|
|
(t
|
|
|
|
(let* ((starting-status :streaming)
|
2023-03-15 17:18:38 +01:00
|
|
|
(gemini-stream (make-instance 'gemini-viewer::gemini-others-data-stream
|
2023-03-16 15:11:46 +01:00
|
|
|
:host host
|
|
|
|
:port port
|
|
|
|
:path path
|
|
|
|
:query query
|
|
|
|
:fragment fragment
|
|
|
|
:meta meta
|
|
|
|
:status-code status
|
|
|
|
:status-code-description
|
|
|
|
code-description
|
2023-01-13 16:22:22 +01:00
|
|
|
:stream-status starting-status
|
|
|
|
:download-stream response
|
|
|
|
:download-socket socket))
|
2023-03-15 17:18:38 +01:00
|
|
|
(thread-fn (request-stream-other-document-thread gemini-stream
|
2023-03-16 15:11:46 +01:00
|
|
|
socket
|
|
|
|
host
|
|
|
|
port
|
|
|
|
path
|
|
|
|
query
|
|
|
|
fragment
|
|
|
|
status
|
|
|
|
code-description
|
|
|
|
meta)))
|
2023-01-13 16:22:22 +01:00
|
|
|
(gemini-client:debug-gemini "response is *not* a gemini file stream")
|
2023-03-15 17:18:38 +01:00
|
|
|
(gemini-viewer:push-db-stream gemini-stream)
|
2023-01-13 16:22:22 +01:00
|
|
|
(gemini-viewer::downloading-start-thread gemini-stream
|
|
|
|
thread-fn
|
|
|
|
host
|
|
|
|
port
|
|
|
|
path
|
|
|
|
query
|
|
|
|
fragment)
|
2023-03-08 16:16:55 +01:00
|
|
|
(make-gemini-response (gemini-client:code gemini-client:+success+)
|
|
|
|
(gemini-client:description gemini-client:+success+)
|
|
|
|
meta
|
|
|
|
actual-iri)))))))
|
2023-01-13 16:22:22 +01:00
|
|
|
|
2023-03-08 16:16:55 +01:00
|
|
|
(defun make-gemini-response (status-code status-code-description meta iri &key (cached nil))
|
|
|
|
(list (cons "status" status-code)
|
|
|
|
(cons "status-description" status-code-description)
|
2023-01-13 16:22:22 +01:00
|
|
|
(cons "meta" meta)
|
|
|
|
(cons "cached" cached)
|
|
|
|
(cons "iri" iri)))
|
|
|
|
|
|
|
|
(defun %gemini-request (url &key
|
|
|
|
(certificate nil)
|
|
|
|
(certificate-key nil)
|
|
|
|
(use-cached-file-if-exists nil)
|
|
|
|
(do-nothing-if-exists-in-db nil))
|
|
|
|
(labels ((redirect-dispatch (status code-description meta response socket iri parsed-iri)
|
2023-03-08 16:16:55 +01:00
|
|
|
(declare (ignore parsed-iri response socket))
|
2023-01-13 16:22:22 +01:00
|
|
|
(gemini-client:debug-gemini "response redirect to: ~s" meta)
|
2023-03-08 16:16:55 +01:00
|
|
|
(make-gemini-response status code-description meta iri))
|
2023-01-13 16:22:22 +01:00
|
|
|
(input-dispatch (status code-description meta response socket iri parsed-iri)
|
2023-03-08 16:16:55 +01:00
|
|
|
(declare (ignore response socket parsed-iri))
|
2023-01-13 16:22:22 +01:00
|
|
|
(gemini-client:debug-gemini "response requested input: ~s" meta)
|
2023-03-08 16:16:55 +01:00
|
|
|
(make-gemini-response status code-description meta iri))
|
2023-01-13 16:22:22 +01:00
|
|
|
(sensitive-input-dispatch (status code-description meta response socket iri parsed-iri)
|
2023-03-08 16:16:55 +01:00
|
|
|
(declare (ignore response socket parsed-iri))
|
2023-01-13 16:22:22 +01:00
|
|
|
(gemini-client:debug-gemini "response requested sensitive input: ~s"
|
|
|
|
meta)
|
2023-03-08 16:16:55 +01:00
|
|
|
(make-gemini-response status code-description meta iri))
|
2023-01-13 16:22:22 +01:00
|
|
|
(certificate-request-dispatch (status
|
|
|
|
code-description
|
|
|
|
meta
|
|
|
|
response
|
|
|
|
socket iri
|
|
|
|
parsed-iri)
|
|
|
|
(declare (ignore status code-description response socket meta parsed-iri))
|
|
|
|
(gemini-client:debug-gemini "response requested certificate")
|
|
|
|
(multiple-value-bind (cached-certificate cached-key)
|
|
|
|
(gemini-client:fetch-cached-certificate iri)
|
|
|
|
(%gemini-request iri
|
|
|
|
:do-nothing-if-exists-in-db do-nothing-if-exists-in-db
|
|
|
|
:certificate-key cached-key
|
|
|
|
:certificate cached-certificate))))
|
|
|
|
(handler-case
|
|
|
|
(gemini-client:with-request-dispatch-table ((:certificate-requested
|
|
|
|
#'certificate-request-dispatch
|
|
|
|
:input-requested
|
|
|
|
#'input-dispatch
|
|
|
|
:sensitive-input-requested
|
|
|
|
#'sensitive-input-dispatch
|
|
|
|
:redirect
|
|
|
|
#'redirect-dispatch
|
|
|
|
:success
|
|
|
|
#'request-success-dispatched-fn)
|
|
|
|
:ignore-warning nil)
|
|
|
|
(gemini-client:debug-gemini "viewer requesting iri ~s" url)
|
|
|
|
(let ((actual-iri (gemini-client:displace-iri (iri:iri-parse url))))
|
|
|
|
(if use-cached-file-if-exists
|
2023-03-15 17:18:38 +01:00
|
|
|
(let ((cached-stream (gemini-viewer:find-db-stream-url actual-iri)))
|
2023-01-13 16:22:22 +01:00
|
|
|
(gemini-client:debug-gemini "checking cache")
|
2023-03-15 17:18:38 +01:00
|
|
|
(if cached-stream
|
2023-01-13 16:22:22 +01:00
|
|
|
(progn
|
|
|
|
(gemini-client:debug-gemini "caching found for ~a" actual-iri)
|
|
|
|
(gemini-viewer:push-url-to-history *gemini-window* actual-iri)
|
2023-03-15 17:18:38 +01:00
|
|
|
(make-gemini-response (gw:status-code cached-stream)
|
|
|
|
(gw:status-code-description cached-stream)
|
|
|
|
(gw:meta cached-stream)
|
2023-01-13 16:22:22 +01:00
|
|
|
actual-iri
|
|
|
|
:cached t))
|
|
|
|
(progn
|
|
|
|
(gemini-client:debug-gemini "caching *not* found for ~a" actual-iri)
|
|
|
|
(%gemini-request actual-iri
|
|
|
|
:certificate-key certificate-key
|
|
|
|
:certificate certificate
|
|
|
|
:use-cached-file-if-exists nil
|
|
|
|
:do-nothing-if-exists-in-db
|
|
|
|
do-nothing-if-exists-in-db))))
|
|
|
|
(gemini-client:request-dispatch url
|
|
|
|
gemini-client::dispatch-table
|
|
|
|
:certificate certificate
|
|
|
|
:certificate-key certificate-key))))
|
|
|
|
(gemini-client:gemini-tofu-error (e)
|
2023-03-11 13:12:28 +01:00
|
|
|
(make-gemini-response +tofu-error-status-code+
|
|
|
|
(format nil "~a" e)
|
|
|
|
(format nil "~a" e)
|
|
|
|
url))
|
2023-01-13 16:22:22 +01:00
|
|
|
(conditions:not-implemented-error (e)
|
2023-02-19 14:52:53 +01:00
|
|
|
(error (_ "Error: ~a") e))
|
2023-01-13 16:22:22 +01:00
|
|
|
(gemini-client:gemini-protocol-error (e)
|
2023-03-11 13:12:28 +01:00
|
|
|
(make-gemini-response (gemini-client:error-code e)
|
|
|
|
(gemini-client:error-description e)
|
2023-03-12 11:42:55 +01:00
|
|
|
(gemini-client:meta e)
|
2023-03-11 13:12:28 +01:00
|
|
|
url))
|
2023-01-13 16:22:22 +01:00
|
|
|
(error (e)
|
2023-02-19 14:52:53 +01:00
|
|
|
(error (_ "Error getting ~s: ~a") url e))
|
2023-01-13 16:22:22 +01:00
|
|
|
(condition (c)
|
2023-02-19 14:52:53 +01:00
|
|
|
(error (_ "Error getting ~s: ~a") url c)))))
|
2023-01-13 16:22:22 +01:00
|
|
|
|
|
|
|
(defun gemini-request (iri use-cache)
|
|
|
|
(%gemini-request iri :use-cached-file-if-exists use-cache))
|
|
|
|
|
|
|
|
(defgeneric rearrange-for-encoding (object))
|
|
|
|
|
|
|
|
(defmethod rearrange-for-encoding (object)
|
|
|
|
object)
|
|
|
|
|
|
|
|
(defmethod rearrange-for-encoding ((object symbol))
|
|
|
|
(string-downcase (symbol-name object)))
|
|
|
|
|
|
|
|
(defun rearrange-parsed-line-for-encoding (lines)
|
|
|
|
(flet ((contains-children-p (node)
|
|
|
|
(evenp (length node))))
|
|
|
|
(loop for line in lines
|
|
|
|
collect
|
|
|
|
(let ((flattened (mapcar #'rearrange-for-encoding (a:flatten line))))
|
|
|
|
(if (contains-children-p flattened)
|
|
|
|
(append (list "type")
|
|
|
|
(subseq flattened 0 (1- (length flattened)))
|
|
|
|
(list "line" (a:last-elt flattened)))
|
|
|
|
(cons "type" flattened))))))
|
|
|
|
|
|
|
|
(defmethod yason:encode ((object gw:gemini-stream) &optional (stream *standard-output*))
|
|
|
|
(with-accessors ((stream-status gw:stream-status)
|
|
|
|
(download-iri gw:download-iri)
|
|
|
|
(start-time gw:start-time)
|
|
|
|
(support-file gw:support-file)
|
|
|
|
(parsed-lines gw:parsed-lines)
|
|
|
|
(octect-count gw:octect-count)
|
|
|
|
(port gw:port)
|
|
|
|
(status-code gw:status-code)
|
|
|
|
(status-code-description gw:status-code-description)
|
|
|
|
(meta gw:meta)
|
|
|
|
(path gw:path)
|
|
|
|
(query gw:query)
|
|
|
|
(fragment gw:fragment)
|
|
|
|
(host gw:host)) object
|
|
|
|
(let* ((actual-start-time (db-utils:decode-datetime-string start-time))
|
|
|
|
(info-alist (list (cons "stream-status" stream-status)
|
|
|
|
(cons "download-iri" download-iri)
|
|
|
|
(cons "start-time" actual-start-time)
|
|
|
|
(cons "support-file" support-file)
|
2023-03-29 18:13:46 +02:00
|
|
|
(cons "octet-count" octect-count)
|
2023-01-13 16:22:22 +01:00
|
|
|
(cons "port" port)
|
|
|
|
(cons "status-code" status-code)
|
|
|
|
(cons "status-code-description" status-code-description)
|
|
|
|
(cons "meta" meta)
|
|
|
|
(cons "path" path)
|
|
|
|
(cons "query" query)
|
|
|
|
(cons "fragment" fragment)
|
|
|
|
(cons "host" host))))
|
|
|
|
(let ((json:*symbol-encoder* #'json:encode-symbol-as-lowercase)
|
|
|
|
(yason:*list-encoder* #'yason:encode-plist)
|
|
|
|
(json:*symbol-key-encoder* #'json:encode-symbol-as-lowercase))
|
|
|
|
(yason:with-output (stream)
|
|
|
|
(json:with-object ()
|
|
|
|
(loop for ((k . v)) on info-alist do
|
|
|
|
(json:with-object-element (k)
|
|
|
|
(json:encode v)))))))))
|
|
|
|
|
2023-02-19 16:20:10 +01:00
|
|
|
(defun make-no-such-stream-error (iri)
|
|
|
|
(error (_ "no such stream ~s") iri))
|
|
|
|
|
2023-01-13 16:22:22 +01:00
|
|
|
(defun gemini-stream-parsed-line (iri line-number)
|
|
|
|
(let ((stream-wrapper (gw:find-db-stream-url iri)))
|
|
|
|
(if (null stream-wrapper)
|
2023-02-19 16:20:10 +01:00
|
|
|
(make-no-such-stream-error iri)
|
2023-01-13 16:22:22 +01:00
|
|
|
(let ((parsed-lines (gw:parsed-lines stream-wrapper)))
|
|
|
|
(cond
|
|
|
|
((or (not (integerp line-number))
|
|
|
|
(< line-number 0))
|
|
|
|
(error (format nil
|
|
|
|
"Line number argument provided is not a positive integer ~a"
|
|
|
|
line-number)))
|
|
|
|
((>= line-number (length parsed-lines))
|
|
|
|
(error (format nil
|
|
|
|
"No parsed line available for line number ~a"
|
|
|
|
line-number)))
|
|
|
|
(t
|
|
|
|
(let ((res (rearrange-parsed-line-for-encoding (list (elt parsed-lines
|
|
|
|
line-number)))))
|
|
|
|
(first res))))))))
|
|
|
|
|
2023-01-15 15:54:49 +01:00
|
|
|
(defclass parsed-lines-slice (box) ())
|
2023-01-13 16:22:22 +01:00
|
|
|
|
|
|
|
(defmethod yason:encode ((object parsed-lines-slice) &optional (stream *standard-output*))
|
|
|
|
(let ((json:*symbol-encoder* #'json:encode-symbol-as-lowercase)
|
|
|
|
(yason:*list-encoder* #'yason:encode-plist)
|
|
|
|
(json:*symbol-key-encoder* #'json:encode-symbol-as-lowercase))
|
|
|
|
(yason:with-output (stream)
|
|
|
|
(json:with-array ()
|
2023-01-15 15:54:49 +01:00
|
|
|
(loop for parsed-line in (unbox object) do
|
2023-01-13 16:22:22 +01:00
|
|
|
(json:encode-array-element parsed-line))))))
|
|
|
|
|
|
|
|
(defun gemini-stream-parsed-line-slice (iri line-number-start line-number-end)
|
|
|
|
(let ((stream-wrapper (gw:find-db-stream-url iri)))
|
|
|
|
(if (null stream-wrapper)
|
2023-02-19 16:20:10 +01:00
|
|
|
(make-no-such-stream-error iri)
|
2023-01-13 16:22:22 +01:00
|
|
|
(let ((parsed-lines (gw:parsed-lines stream-wrapper)))
|
|
|
|
(cond
|
|
|
|
((or (not (integerp line-number-start))
|
|
|
|
(< line-number-start 0))
|
|
|
|
(error (format nil
|
|
|
|
"Line number index start argument provided is not a positive integer ~a"
|
|
|
|
line-number-start)))
|
|
|
|
((and (integerp line-number-end)
|
|
|
|
(< line-number-end 0))
|
|
|
|
(error (format nil
|
|
|
|
"Line number index end argument provided is not a positive integer ~a"
|
|
|
|
line-number-end)))
|
|
|
|
((and (integerp line-number-end)
|
|
|
|
(>= line-number-end (length parsed-lines)))
|
|
|
|
(error (format nil
|
|
|
|
"No parsed line available for line number ~a"
|
|
|
|
line-number-end)))
|
|
|
|
(t
|
|
|
|
(let ((res (rearrange-parsed-line-for-encoding (subseq parsed-lines
|
|
|
|
line-number-start
|
|
|
|
line-number-end))))
|
2023-01-15 15:54:49 +01:00
|
|
|
(make-instance 'parsed-lines-slice :contents res))))))))
|
2023-01-13 16:22:22 +01:00
|
|
|
|
|
|
|
(defun gemini-stream-info (iri)
|
|
|
|
(let ((stream-wrapper (gw:find-db-stream-url iri)))
|
|
|
|
(if stream-wrapper
|
|
|
|
stream-wrapper
|
2023-02-19 16:20:10 +01:00
|
|
|
(make-no-such-stream-error iri))))
|
2023-01-13 16:22:22 +01:00
|
|
|
|
2023-03-29 18:13:46 +02:00
|
|
|
(defun gemini-remove-stream (iri)
|
|
|
|
(let ((stream-wrapper (gw:find-db-stream-url iri)))
|
|
|
|
(if stream-wrapper
|
|
|
|
(gw:remove-db-stream stream-wrapper)
|
|
|
|
(make-no-such-stream-error iri))))
|
|
|
|
|
2023-01-13 16:22:22 +01:00
|
|
|
(defun gemini-all-stream-info ()
|
|
|
|
gw:*gemini-streams-db*)
|
2023-01-15 15:54:49 +01:00
|
|
|
|
2023-02-05 14:07:13 +01:00
|
|
|
(defun gemini-stream-status (iri)
|
|
|
|
(let ((stream (gemini-stream-info iri)))
|
|
|
|
(gw:stream-status stream)))
|
|
|
|
|
|
|
|
(defun gemini-stream-completed-p (iri)
|
|
|
|
(let ((status (gemini-stream-status iri)))
|
|
|
|
(eq status :completed)))
|
|
|
|
|
2023-03-17 12:27:46 +01:00
|
|
|
(defun build-gemini-toc (iri width)
|
2023-01-15 15:54:49 +01:00
|
|
|
(a:when-let* ((ordered-headers-tag '(:h1 :h2 :h3))
|
|
|
|
(stream-wrapper (gw:find-db-stream-url iri))
|
|
|
|
(parsed-lines (gw:parsed-lines stream-wrapper))
|
|
|
|
(headers (remove-if-not (lambda (a) (member (html-utils:tag a)
|
|
|
|
ordered-headers-tag))
|
|
|
|
parsed-lines)))
|
|
|
|
(flet ((extract-level (header)
|
|
|
|
(let ((tag (html-utils:tag header)))
|
|
|
|
(1+ (position tag ordered-headers-tag))))
|
|
|
|
(extract-gid (header)
|
|
|
|
(let ((attribute (html-utils:find-attribute :header-group-id header)))
|
|
|
|
(html-utils:attribute-value attribute)))
|
|
|
|
(extract-header-text (header)
|
|
|
|
(first (html-utils:children header)))
|
|
|
|
(find-longest-string (text-list)
|
|
|
|
(reduce (lambda (a b)
|
|
|
|
(if (>= (length a) (length b))
|
|
|
|
a
|
|
|
|
b))
|
|
|
|
text-list)))
|
|
|
|
(a:when-let* ((toc (message-window:gemini-sexp->toc-plist headers
|
|
|
|
#'identity
|
|
|
|
#'extract-level
|
|
|
|
#'extract-gid
|
|
|
|
#'extract-header-text))
|
|
|
|
(toc-text (mapcar (lambda (a) (getf a :header)) toc))
|
|
|
|
(longest-text (find-longest-string toc-text))
|
|
|
|
(max-text-length (length longest-text))
|
|
|
|
(toc-numbers (mapcar (lambda (a)
|
|
|
|
(let ((numbers-as-text
|
|
|
|
(mapcar (lambda (a)
|
|
|
|
(format nil "~a" a))
|
|
|
|
(getf a :number))))
|
|
|
|
(join-with-strings numbers-as-text ".")))
|
|
|
|
toc))
|
|
|
|
(longest-number (find-longest-string toc-numbers))
|
|
|
|
(max-number-length (length longest-number))
|
|
|
|
(max-non-padded-text-size (+ max-text-length max-number-length))
|
|
|
|
(toc-entries-text (loop for text in toc-text
|
|
|
|
for number in toc-numbers
|
|
|
|
collect
|
|
|
|
(let* ((text-length (+ (length text)
|
|
|
|
(length number)))
|
|
|
|
(padding-size (- max-non-padded-text-size
|
|
|
|
text-length))
|
|
|
|
(padding (make-string padding-size
|
|
|
|
:initial-element (swconf:gemini-toc-padding-char))))
|
|
|
|
(strcat number
|
|
|
|
padding
|
|
|
|
text)))))
|
|
|
|
(values
|
|
|
|
(loop for toc-entry in toc
|
|
|
|
for text in toc-entries-text
|
|
|
|
collect
|
|
|
|
(list :header-group-id (getf toc-entry :group-id)
|
2023-03-17 12:27:46 +01:00
|
|
|
:text (ellipsize text width)))
|
2023-01-15 15:54:49 +01:00
|
|
|
toc)))))
|
|
|
|
|
|
|
|
(defclass gemini-toc (box) ())
|
|
|
|
|
|
|
|
(defun encode-flat-array-of-plists (list stream)
|
|
|
|
(let ((json:*symbol-encoder* #'json:encode-symbol-as-lowercase)
|
|
|
|
(yason:*list-encoder* #'yason:encode-plist)
|
|
|
|
(json:*symbol-key-encoder* #'json:encode-symbol-as-lowercase))
|
|
|
|
(yason:with-output (stream)
|
|
|
|
(json:with-array ()
|
|
|
|
(loop for element in list do
|
|
|
|
(json:encode-array-element element))))))
|
|
|
|
|
2023-03-17 12:27:46 +01:00
|
|
|
(defun gemini-table-of-contents (iri width)
|
|
|
|
(make-instance 'gemini-toc
|
|
|
|
:contents (build-gemini-toc iri width)))
|
2023-01-15 15:54:49 +01:00
|
|
|
|
|
|
|
(defmethod yason:encode ((object gemini-toc) &optional (stream *standard-output*))
|
|
|
|
(encode-flat-array-of-plists (unbox object) stream))
|
2023-03-06 18:38:46 +01:00
|
|
|
|
2023-03-12 15:36:13 +01:00
|
|
|
(defun gemini-parse-string (string)
|
|
|
|
(let ((parsed-lines (gemini-parser:parse-gemini-file string)))
|
2023-03-06 18:38:46 +01:00
|
|
|
(make-instance 'parsed-lines-slice
|
|
|
|
:contents (rearrange-parsed-line-for-encoding parsed-lines))))
|
|
|
|
|
2023-03-12 15:36:13 +01:00
|
|
|
(defun gemini-parse-local-file (path)
|
|
|
|
(if (fs:file-exists-p path)
|
|
|
|
(gemini-parse-string (fs:slurp-file path))
|
|
|
|
(error "No such file ~a" path)))
|
|
|
|
|
2023-03-06 18:38:46 +01:00
|
|
|
(defun gemini-slurp-local-file (path)
|
|
|
|
(fs:slurp-file path))
|
2023-03-12 15:36:13 +01:00
|
|
|
|
|
|
|
(defun make-error-page (iri code description meta)
|
|
|
|
(let* ((separator (make-string 10 :initial-element gemini-parser::+h2-underline+))
|
|
|
|
(gemtext (with-output-to-string (stream)
|
|
|
|
(write-sequence (gemini-parser:geminize-h2 (format nil
|
|
|
|
"~a ~a~%"
|
|
|
|
code
|
|
|
|
description))
|
|
|
|
stream)
|
|
|
|
(write-sequence (gemini-parser:geminize-preformatted separator)
|
|
|
|
stream)
|
|
|
|
(write-sequence (format nil "Error connecting to: ~a:~2%" iri) stream)
|
|
|
|
(write-sequence (format nil "~a~%" meta) stream))))
|
|
|
|
(gemini-parse-string gemtext)))
|