elpher-go now trims surrounding whitespace from input string.
[elpher.git] / elpher.el
index 6e885e4..1ea9e71 100644 (file)
--- a/elpher.el
+++ b/elpher.el
@@ -161,9 +161,10 @@ May be empty."
   "Specify the name of the file where elpher bookmarks will be saved."
   :type '(file))
 
-(defcustom elpher-force-ipv4 nil
-  "If non-nil, force elpher to use ipv4 instead of trying an ipv6 address
-and falling back to an ipv4 address"
+(defcustom elpher-ipv4-always nil
+  "If non-nil, elpher will always use IPv4 to establish network connections.
+This can be useful when browsing from a computer that supports IPv6, because
+some servers which do not support IPv6 can take a long time to time-out."
   :type '(boolean))
 
 ;; Face customizations
@@ -306,9 +307,25 @@ requiring gopher-over-TLS."
 
 (defun elpher-address-to-url (address)
   "Get string representation of ADDRESS, or nil if ADDRESS is special."
-  (if (not (elpher-address-special-p address))
-      (url-encode-url (url-recreate-url address))
-    nil))
+  (if (elpher-address-special-p address)
+      nil
+    (let* ((port (url-port address))
+           (address-to-convert
+            (if (= port 0)
+                address
+              (let ((address-copy (seq-copy address))
+                    (protocol (url-type address)))
+                (if (or (and (equal protocol "gopher")
+                             (= port 70))
+                        (and (equal protocol "gemini")
+                             (= port 1965))
+                        (and (equal protocol "http")
+                             (= port 80))
+                        (and (equal protocol "finger")
+                             (= port 79)))
+                    (setf (url-port address-copy) 0))
+                address-copy))))
+      (url-encode-url (url-recreate-url address-to-convert)))))
 
 (defun elpher-address-type (address)
   "Retrieve type of ADDRESS object.
@@ -563,6 +580,7 @@ to ADDRESS."
       (error "Cannot retrieve TLS gopher selector: GnuTLS not available")))
   (unless (< (elpher-address-port address) 65536)
     (error "Cannot retrieve gopher selector: port number > 65536"))
+  (defvar gnutls-verify-error)
   (condition-case nil
       (let* ((kill-buffer-query-functions nil)
              (gnutls-verify-error nil) ; We use the NSM for verification
@@ -573,7 +591,9 @@ to ADDRESS."
              (hkbytes-received 0)
              (proc (open-network-stream "elpher-process"
                                         nil
-                                        (if force-ipv4 (dns-query host) host)
+                                        (if (or elpher-ipv4-always force-ipv4)
+                                            (dns-query host)
+                                          host)
                                         (if (> port 0) port 70)
                                         :type (if elpher-use-tls 'tls 'plain)
                                         :nowait t))
@@ -590,11 +610,11 @@ to ADDRESS."
                                           (progn
                                             (message "Disabling TLS mode.")
                                             (setq elpher-use-tls nil)
-                                            (elpher-get-selector address renderer elpher-force-ipv4))
+                                            (elpher-get-selector address renderer))
                                         (elpher-network-error address "Could not establish encrypted connection")))
                                      ('connect
                                       (elpher-process-cleanup)
-                                      (unless force-ipv4
+                                      (unless (or elpher-ipv4-always force-ipv4)
                                         (message "Connection timed out. Retrying with IPv4 address.")
                                         (elpher-get-selector address renderer t))))))))
         (setq elpher-network-timer timer)
@@ -654,7 +674,7 @@ once they are retrieved from the gopher server."
       (elpher-with-clean-buffer
        (insert "LOADING... (use 'u' to cancel)\n"))
       (condition-case the-error
-          (elpher-get-selector address renderer elpher-force-ipv4)
+          (elpher-get-selector address renderer)
         (error
          (elpher-network-error address the-error))))))
 
@@ -834,7 +854,7 @@ The response is rendered using the rendering function RENDERER."
 
             (elpher-with-clean-buffer
              (insert "LOADING RESULTS... (use 'u' to cancel)"))
-            (elpher-get-selector search-address renderer elpher-force-ipv4))
+            (elpher-get-selector search-address renderer))
         (if aborted
             (elpher-visit-previous-page))))))
  
@@ -910,14 +930,16 @@ to ADDRESS."
                (hkbytes-received 0)
                (proc (open-network-stream "elpher-process"
                                           nil
-                                          (if force-ipv4 (dns-query host) host)
+                                          (if (or elpher-ipv4-always force-ipv4)
+                                              (dns-query host)
+                                            host)
                                           (if (> port 0) port 1965)
                                           :type 'tls
                                           :nowait t))
                (timer (run-at-time elpher-connection-timeout nil
                                    (lambda ()
                                      (elpher-process-cleanup)
-                                     (unless force-ipv4
+                                     (unless (or elpher-ipv4-always force-ipv4)
                                         ; Try again with IPv4
                                        (message "Connection timed out.  Retrying with IPv4.")
                                        (elpher-get-gemini-response address renderer t))))))
@@ -954,7 +976,7 @@ to ADDRESS."
                                                    "\r\n"))))
                                        ((string-prefix-p "deleted" event)) ; do nothing
                                        ((and (not response-string-parts)
-                                             (not force-ipv4))
+                                             (not (or elpher-ipv4-always force-ipv4)))
                                         ; Try again with IPv4
                                         (message "Connection failed. Retrying with IPv4.")
                                         (cancel-timer timer)
@@ -1110,17 +1132,18 @@ For instance, the filename /a/b/../c/./d will reduce to /a/c/d"
 
 (defun elpher-address-from-gemini-url (url)
   "Extract address from URL with defaults as per gemini map files."
-  (let ((address (url-generic-parse-url url)))
+  (let ((address (url-generic-parse-url url))
+        (current-address (elpher-page-address elpher-current-page)))
     (unless (and (url-type address) (not (url-fullness address))) ;avoid mangling mailto: urls
       (setf (url-fullness address) t)
       (if (url-host address) ;if there is an explicit host, filenames are absolute
           (if (string-empty-p (url-filename address))
               (setf (url-filename address) "/")) ;ensure empty filename is marked as absolute
-        (setf (url-host address) (url-host (elpher-page-address elpher-current-page)))
+        (setf (url-host address) (url-host current-address))
+        (setf (url-port address) (url-port current-address))
         (unless (string-prefix-p "/" (url-filename address)) ;deal with relative links
           (setf (url-filename address)
-                (concat (file-name-directory
-                         (url-filename (elpher-page-address elpher-current-page)))
+                (concat (file-name-directory (url-filename current-address))
                         (url-filename address)))))
       (unless (url-type address)
         (setf (url-type address) "gemini"))
@@ -1218,8 +1241,9 @@ width defined by elpher-gemini-max-fill-width."
 (defun elpher-get-finger-page (renderer &optional force-ipv4)
   "Opens a finger connection to the current page address.
 The result is rendered using RENDERER.  When the optional argument
-FORCE-IPV4 is non-nil, the IPv4 address returned by a DNS lookup will
-be used explicitly in making the connection."
+FORCE-IPV4 or the variable `elpher-ipv4-always' are non-nil, the
+IPv4 address returned by a DNS lookup will be used explicitly in
+making the connection."
   (let* ((address (elpher-page-address elpher-current-page))
          (content (elpher-get-cached-content address)))
     (if (and content (funcall renderer nil))
@@ -1240,7 +1264,9 @@ be used explicitly in making the connection."
                  (selector-string-parts nil)
                  (proc (open-network-stream "elpher-process"
                                             nil
-                                            (if force-ipv4 (dns-query host) host)
+                                            (if (or elpher-ipv4-always force-ipv4)
+                                                (dns-query host)
+                                              host)
                                             port
                                             :type 'plain
                                             :nowait t))
@@ -1250,7 +1276,7 @@ be used explicitly in making the connection."
                                        (pcase (process-status proc)
                                          ('connect
                                           (elpher-process-cleanup)
-                                          (unless force-ipv4
+                                          (unless (or elpher-ipv4-always force-ipv4)
                                             (message "Connection timed out. Retrying with IPv4 address.")
                                             (elpher-get-finger-page renderer t))))))))
             (setq elpher-network-timer timer)
@@ -1512,11 +1538,12 @@ If ADDRESS is already bookmarked, update the label only."
   "Go to a particular gopher site HOST-OR-URL.
 When run interactively HOST-OR-URL is read from the minibuffer."
   (interactive "sGopher or Gemini URL: ")
-  (let ((page (elpher-make-page host-or-url
-                                (elpher-address-from-url host-or-url))))
+  (let* ((cleaned-host-or-url (string-trim host-or-url))
+         (address (elpher-address-from-url cleaned-host-or-url))
+         (page (elpher-make-page cleaned-host-or-url address))) 
     (switch-to-buffer "*elpher*")
     (elpher-visit-page page)
-    '()))
+    nil))
 
 (defun elpher-go-current ()
   "Go to a particular site read from the minibuffer, initialized with the current URL."
@@ -1675,7 +1702,8 @@ When run interactively HOST-OR-URL is read from the minibuffer."
   "Remove bookmark for the current page."
   (interactive)
   (let ((address (elpher-page-address elpher-current-page)))
-    (unless (elpher-address-special-p address)
+    (when (and (not (elpher-address-special-p address))
+               (y-or-n-p "Really remove bookmark for the current page? "))
       (elpher-remove-address-bookmark address)
       (message "Bookmark removed."))))
 
@@ -1684,10 +1712,11 @@ When run interactively HOST-OR-URL is read from the minibuffer."
   (interactive)
   (let ((button (button-at (point))))
     (if button
-        (let ((page (button-get button 'elpher-page)))
-          (elpher-remove-address-bookmark (elpher-page-address page))
-          (elpher-reload-bookmarks)
-          (message "Bookmark removed."))
+        (when (y-or-n-p "Really remove bookmark for this link? ")
+          (let ((page (button-get button 'elpher-page)))
+            (elpher-remove-address-bookmark (elpher-page-address page))
+            (elpher-reload-bookmarks)
+            (message "Bookmark removed.")))
       (error "No link selected"))))
 
 (defun elpher-bookmarks ()