Skip to content

Commit

Permalink
Merge remote-tracking branch 'gh/blocker-load-hostlists-if-empty'
Browse files Browse the repository at this point in the history
  • Loading branch information
aadcg committed Nov 8, 2023
2 parents 43409a2 + a81d574 commit 95312a4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
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)))

0 comments on commit 95312a4

Please sign in to comment.