-(defun elpher-make-address-from-url (url)
- "Create an elpher address corresponding to the given URL."
- (let ((url (url-generic-parse-url url-string)))
- (if (and (url-type url)
- (url-host url))
- (setf (url-filename url) (url-unhex-string (url-filename url)))
- (error "Malformed URL" url))))
-
-(defun elpher-address-get-url (address)
- "Get URL representation of ADDRESS."
- (url-encode-url (url-recreate address)))
-
-(defun elpher-address-gopher-p? (address)
- "Return non-nil if ADDRESS specifies a gopher address."
- (let ((protocol (url-type address)))
- (if (or (string-equal protocol "gopher")
- (string-equal protocol "gophers")))))
+;; An elpher "address" object is either a url object or a symbol.
+;; Symbol addresses are "special", corresponding to pages generated
+;; dynamically for and by elpher. All others represent pages which
+;; rely on content retrieved over the network.
+
+(defun elpher-address-from-url (url-string)
+ "Create a ADDRESS object corresponding to the given URL-STRING."
+ (let ((data (match-data))) ; Prevent parsing clobbering match data
+ (unwind-protect
+ (let ((url (url-generic-parse-url url-string)))
+ (setf (url-fullness url) t)
+ (unless (url-host url)
+ (setf (url-host url) (url-filename url))
+ (setf (url-filename url) ""))
+ (unless (url-type url)
+ (setf (url-type url) "gopher"))
+ (if (and (url-type url)
+ (url-host url))
+ (let ((is-gopher (or (equal "gopher" (url-type url))
+ (equal "gophers" (url-type url)))))
+ (setf (url-filename url)
+ (url-unhex-string (url-filename url)))
+ (when (or (equal (url-filename url) "")
+ (equal (url-filename url) "/"))
+ (if is-gopher
+ (setf (url-filename url) "/1")))
+ (unless (> (url-port url) 0)
+ (if is-gopher
+ (setf (url-port url) 70)))
+ url)
+ (error "Malformed URL" url)))
+ (set-match-data data))))
+
+(defun elpher-make-gopher-address (type selector host port &optional tls)
+ "Create an ADDRESS object corresponding to the given gopher directory record
+attributes: TYPE, SELECTOR, HOST and PORT."
+ (if (and (equal type ?h)
+ (string-prefix-p "URL:" selector))
+ (elpher-address-from-url (elt (split-string selector "URL:") 1))
+ (elpher-address-from-url
+ (concat "gopher" (if tls "s" "")
+ "://" host
+ ":" (number-to-string port)
+ "/" (string type)
+ selector))))
+
+(defun elpher-make-special-address (type)
+ "Create an ADDRESS object corresponding to the given special page symbol TYPE."
+ type)
+
+(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))