Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blocker: Autoload hosts if empty. #3245

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion source/changelog.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,10 @@ Nyxt version exists. It is only raised when the major version differs.")
(define-version "3.X.Y"
(:nsection :title "UI/UX"
(:ul
(:li "Simplify status bar CSS. Make URL area clickable."))))
(:li "Simplify status bar CSS. Make URL area clickable.")))
(:nsection :title "Bug fixes"
(:ul (:nxref :mode 'nyxt/mode/blocker:blocker-mode)
" ensures that hostlist files are loaded when missing.")))

(define-version "4-pre-release-1"
(:li "When on pre-release, push " (:code "X-pre-release")
Expand Down
36 changes: 20 additions & 16 deletions source/mode/blocker.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ seconds."))
(:documentation "A hostlist `blocker-mode' can use for its `hostlists' slot.
See `*default-hostlist*' for an example."))

(defmethod hosts :around ((hostlist hostlist))
(or (call-next-method)
(let ((path (files:expand hostlist)))
(unless (uiop:file-exists-p path)
(echo "Updating hostlist ~s..." path))
(setf (slot-value hostlist 'hosts)
(files:content hostlist
:force-update (not (uiop:file-exists-p path)))))))

(export-always 'make-hostlist)
(defun make-hostlist (&rest args)
"Return a new `hostlist'.
Expand Down Expand Up @@ -73,6 +82,16 @@ internal programming APIs."
:export nil
:documentation "The set of host names to block.")))

(defmethod blocked-hosts :around ((blocker-mode blocker-mode))
(let ((value (call-next-method)))
(unless (plusp (hash-table-count value))
(dolist (hostlist (hostlists blocker-mode))
;; TODO: Allow running in the background, but warning, it could leak
;; personal information to undesired third-party.
(dolist (host (hosts hostlist))
(setf (gethash host value) host))))
value))

(defmethod enable ((mode blocker-mode) &key)
(when (network-buffer-p (buffer mode))
(hooks:add-hook (request-resource-hook (buffer mode))
Expand Down Expand Up @@ -104,21 +123,6 @@ This gives more integrity guarantees to the user and allows external manipulatio
(custom-hosts? line))
collect (second (str:split " " line)))))

(-> load-hostlists (blocker-mode &key (:force-update-p boolean)) t)
(defun load-hostlists (blocker-mode &key force-update-p)
"Load BLOCKER-MODE's hostlists into `blocked-hosts' (in the background)."
(clrhash (blocked-hosts blocker-mode))
(dolist (hostlist (hostlists blocker-mode))
;; TODO: Allow running in the background, but warning, it could leak
;; personal information to undesired third-party.
(dolist (host (or (hosts hostlist)
(let ((path (files:expand hostlist)))
(unless (uiop:file-exists-p path)
(echo "Updating hostlist ~s..." path))
(files:content hostlist
:force-update force-update-p))))
(setf (gethash host (blocked-hosts blocker-mode)) host))))

(defmethod blocklisted-host-p ((mode blocker-mode) host)
"Return non-nil of HOST if found in the hostlists of MODE.
Return nil if MODE's hostlist cannot be parsed."
Expand Down Expand Up @@ -149,4 +153,4 @@ This is an acceptable handler for `request-resource-hook'."

(define-command update-hostlists (&optional (blocker-mode (find-submode 'nyxt/mode/blocker:blocker-mode (current-buffer))))
"Forces update for all the hostlists of `blocker-mode'."
(load-hostlists blocker-mode :force-update-p t))
(clrhash (blocked-hosts blocker-mode)))
Loading