2020-06-22 13:58:04 +02:00
|
|
|
;; tinmop: an humble mastodon client
|
|
|
|
;; Copyright (C) 2020 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 :gemini-viewer)
|
|
|
|
|
2020-06-28 17:39:21 +02:00
|
|
|
(defstruct gemini-metadata
|
|
|
|
(links)
|
2020-07-15 12:40:30 +02:00
|
|
|
(history)
|
|
|
|
(source-file))
|
2020-06-28 17:39:21 +02:00
|
|
|
|
|
|
|
(defun add-url-to-history (window url)
|
2020-06-29 12:29:11 +02:00
|
|
|
(let* ((metadata (message-window:metadata window))
|
|
|
|
(history (reverse (gemini-metadata-history metadata)))
|
|
|
|
(last-entry (safe-last-elt (gemini-metadata-history metadata))))
|
|
|
|
(when (string/= last-entry
|
|
|
|
url)
|
|
|
|
(setf (gemini-metadata-history metadata)
|
|
|
|
(reverse (push url history))))))
|
2020-06-28 17:39:21 +02:00
|
|
|
|
|
|
|
(defun maybe-initialize-metadata (window)
|
|
|
|
(when (not (gemini-metadata-p (message-window:metadata window)))
|
|
|
|
(setf (message-window:metadata window)
|
|
|
|
(make-gemini-metadata)))
|
|
|
|
(message-window:metadata window))
|
|
|
|
|
2020-06-22 13:58:04 +02:00
|
|
|
(defun request (url)
|
2020-07-04 11:15:55 +02:00
|
|
|
(let ((parsed-uri (quri:uri url)))
|
2020-06-28 17:39:21 +02:00
|
|
|
(maybe-initialize-metadata specials:*message-window*)
|
2020-06-22 13:58:04 +02:00
|
|
|
(if (null parsed-uri)
|
|
|
|
(ui:error-message (format nil
|
|
|
|
(_ "Could not understand the address ~s")
|
|
|
|
url))
|
2020-07-04 11:15:55 +02:00
|
|
|
(let ((host (quri:uri-host parsed-uri))
|
|
|
|
(path (quri:uri-path parsed-uri))
|
|
|
|
(query (quri:uri-query parsed-uri))
|
|
|
|
(port (or (quri:uri-port parsed-uri)
|
2020-06-22 13:58:04 +02:00
|
|
|
gemini-client:+gemini-default-port+)))
|
|
|
|
(handler-case
|
|
|
|
(progn
|
2020-07-15 12:40:30 +02:00
|
|
|
(multiple-value-bind (status x meta response)
|
2020-06-22 13:58:04 +02:00
|
|
|
(gemini-client:request host
|
|
|
|
path
|
|
|
|
:query query
|
|
|
|
:port port)
|
|
|
|
(declare (ignore x))
|
2020-06-28 17:39:21 +02:00
|
|
|
(add-url-to-history specials:*message-window* url)
|
2020-06-22 13:58:04 +02:00
|
|
|
(cond
|
|
|
|
((gemini-client:response-redirect-p status)
|
|
|
|
(flet ((on-input-complete (maybe-accepted)
|
|
|
|
(when (ui::boolean-input-accepted-p maybe-accepted)
|
|
|
|
(let ((new-url (gemini-parser:absolutize-link meta
|
2020-07-04 11:15:55 +02:00
|
|
|
(quri:uri-host parsed-uri)
|
|
|
|
(quri:uri-port parsed-uri)
|
|
|
|
(quri:uri-path parsed-uri))))
|
2020-06-22 13:58:04 +02:00
|
|
|
(db-utils:with-ready-database (:connect nil)
|
|
|
|
(request new-url))))))
|
|
|
|
(ui:ask-string-input #'on-input-complete
|
2020-06-27 22:43:13 +02:00
|
|
|
:priority program-events:+minimum-event-priority+
|
2020-06-22 13:58:04 +02:00
|
|
|
:prompt
|
|
|
|
(format nil
|
|
|
|
(_ "Redirects to ~s, follows redirect? [y/N] ")
|
|
|
|
meta))))
|
|
|
|
((gemini-client:response-input-p status)
|
|
|
|
(flet ((on-input-complete (input)
|
|
|
|
(when (string-not-empty-p input)
|
|
|
|
(db-utils:with-ready-database (:connect nil)
|
|
|
|
(request (gemini-parser:make-gemini-uri host
|
|
|
|
path
|
|
|
|
input
|
|
|
|
port))))))
|
|
|
|
(ui:ask-string-input #'on-input-complete
|
|
|
|
:prompt
|
|
|
|
(format nil
|
|
|
|
(_ "Server ~s asks: ~s ")
|
|
|
|
host
|
|
|
|
meta))))
|
|
|
|
((gemini-client:response-sensitive-input-p status)
|
|
|
|
(error 'conditions:not-implemented-error
|
|
|
|
:text "Sensitive input not implemented"))
|
2020-07-15 12:40:30 +02:00
|
|
|
((gemini-client:gemini-file-response-p response)
|
2020-06-22 13:58:04 +02:00
|
|
|
(setf (message-window:source-text *message-window*)
|
2020-07-15 12:40:30 +02:00
|
|
|
(gemini-client:rendered-file response))
|
2020-06-28 17:39:21 +02:00
|
|
|
(setf (gemini-metadata-links (message-window:metadata *message-window*))
|
2020-07-15 12:40:30 +02:00
|
|
|
(gemini-client:links response))
|
|
|
|
(setf (gemini-metadata-source-file (message-window:metadata *message-window*))
|
|
|
|
(gemini-client:source response))
|
2020-06-22 13:58:04 +02:00
|
|
|
(setf (keybindings *message-window*)
|
|
|
|
keybindings:*gemini-message-keymap*)
|
|
|
|
(draw *message-window*))
|
|
|
|
(t
|
|
|
|
(fs:with-anaphoric-temp-file (stream)
|
2020-07-15 12:40:30 +02:00
|
|
|
(write-sequence response stream)
|
2020-06-22 13:58:04 +02:00
|
|
|
(force-output stream)
|
|
|
|
(os-utils:xdg-open fs:temp-file))))))
|
2020-06-23 11:47:21 +02:00
|
|
|
(gemini-client:gemini-tofu-error (e)
|
|
|
|
(let ((host (gemini-client:host e)))
|
|
|
|
(flet ((on-input-complete (maybe-accepted)
|
|
|
|
(when (ui::boolean-input-accepted-p maybe-accepted)
|
|
|
|
(db-utils:with-ready-database (:connect nil)
|
|
|
|
(db:tofu-delete host)
|
|
|
|
(request url)))))
|
|
|
|
(ui:ask-string-input #'on-input-complete
|
|
|
|
:prompt
|
|
|
|
(format nil
|
|
|
|
(_ "Host ~s signature changed! This is a potential security risk! Ignore this warning? [y/N] ")
|
|
|
|
host)))))
|
2020-06-22 13:58:04 +02:00
|
|
|
(conditions:not-implemented-error (e)
|
2020-06-23 13:13:59 +02:00
|
|
|
(ui:notify (format nil (_ "Error: ~a") e)
|
|
|
|
:as-error t))
|
2020-06-22 13:58:04 +02:00
|
|
|
(gemini-client:gemini-protocol-error (e)
|
2020-06-23 13:13:59 +02:00
|
|
|
(ui:notify (format nil "~a" e)
|
|
|
|
:as-error t))
|
2020-07-04 11:15:55 +02:00
|
|
|
#-debug-mode
|
2020-06-23 11:47:21 +02:00
|
|
|
(error (e)
|
2020-06-23 13:12:35 +02:00
|
|
|
(ui:notify (format nil
|
|
|
|
(_ "Error getting ~s: ~a")
|
|
|
|
url
|
|
|
|
e)
|
|
|
|
:as-error t)))))))
|
2020-06-28 17:39:21 +02:00
|
|
|
|
|
|
|
(defun history-back (window)
|
|
|
|
(when-let* ((metadata (message-window:metadata window))
|
|
|
|
(history (misc:safe-all-but-last-elt (gemini-metadata-history metadata)))
|
|
|
|
(last (last-elt history)))
|
|
|
|
(setf (gemini-metadata-history metadata)
|
2020-06-29 12:29:11 +02:00
|
|
|
history)
|
|
|
|
(ui:info-message (format nil (_ "Going back to: ~a") last))
|
2020-06-28 17:39:21 +02:00
|
|
|
(request last)))
|
2020-07-15 12:40:30 +02:00
|
|
|
|
|
|
|
(defun view-source (window)
|
|
|
|
(when-let* ((metadata (message-window:metadata window))
|
|
|
|
(source (gemini-metadata-source-file metadata))
|
|
|
|
(last (misc:safe-last-elt (gemini-metadata-history metadata))))
|
|
|
|
(setf (message-window:source-text window) source)
|
|
|
|
(draw window)
|
|
|
|
(ui:info-message (format nil (_ "Viewing source of ~a") last))))
|