Added full package header.
[elpher.git] / elpher.el
1 ;;; elpher.el --- Full-featured gopher client.
2
3 ;; Copyright (C) 2019 Tim Vaughan
4
5 ;; Author: Tim Vaughan <tgvaughan@gmail.com>
6 ;; Created: 11 April 2019
7 ;; Version: 1.0.0
8 ;; Keywords: comm gopher
9 ;; Homepage: https://github.com/tgvaughan/elpher
10 ;; Package-Requires: ((emacs "25"))
11
12 ;; This file is not part of GNU Emacs.
13
14 ;; This program is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this file.  If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Commentary:
28
29 ;; Elpher is a tool for exploring "gopherspace" using GNU Emacs.
30
31 ;;; Code:
32
33 (provide 'elpher)
34
35 ;;; Global constants
36 ;;
37
38 (defconst elpher-version "1.0.0"
39   "Current version of elpher.")
40
41 (defconst elpher-margin-width 6
42   "Width of left-hand margin used when rendering indicies.")
43
44 (defconst elpher-start-index
45   (mapconcat
46    'identity
47    (list "i\tfake\tfake\t1"
48          "i--------------------------------------------\tfake\tfake\t1"
49          "i           Elpher Gopher Client             \tfake\tfake\t1"
50          (format "i              version %s\tfake\tfake\t1" elpher-version)
51          "i--------------------------------------------\tfake\tfake\t1"
52          "i\tfake\tfake\t1"
53          "iBasic usage:\tfake\tfake\t1"
54          "i\tfake\tfake\t1"
55          "i - tab/shift-tab: next/prev directory entry on current page\tfake\tfake\t1"
56          "i - RET/mouse-1: open directory entry under cursor\tfake\tfake\t1"
57          "i - u: return to parent directory entry\tfake\tfake\t1"
58          "i - g: go to a particular page\tfake\tfake\t1"
59          "i - r: redraw current page (using cached contents if available)\tfake\tfake\t1"
60          "i - R: reload current page (regenerates cache)\tfake\tfake\t1"
61          "i - d: download directory entry under cursor\tfake\tfake\t1"
62          "i - w: display the raw server response for the current page\tfake\tfake\t1"
63          "i\tfake\tfake\t1"
64          "iPlaces to start exploring Gopherspace:\tfake\tfake\t1"
65          "i\tfake\tfake\t1"
66          "1Floodgap Systems Gopher Server\t\tgopher.floodgap.com\t70"
67          "i\tfake\tfake\t1"
68          "iAlternatively, select the following item and enter some\tfake\tfake\t1"
69          "isearch terms:\tfake\tfake\t1"
70          "i\tfake\tfake\t1"
71          "7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
72          ".")
73    "\r\n")
74   "Source for elpher start page.")
75
76
77 ;;; Customization group
78 ;;
79
80 (defgroup elpher nil
81   "A gopher client."
82   :group 'applications)
83
84 (defface elpher-index
85   '((((background dark)) :foreground "deep sky blue")
86     (((background light)) :foreground "blue"))
87   "Face used for index records.")
88
89 (defface elpher-text
90   '((((background dark)) :foreground "white")
91     (((background light)) :weight bold))
92   "Face used for text records.")
93
94 (defface elpher-info '()
95   "Face used for info records.")
96
97 (defface elpher-image
98   '((((background dark)) :foreground "green")
99     (t :foreground "dark green"))
100   "Face used for image records.")
101
102 (defface elpher-search
103   '((((background light)) :foreground "orange")
104     (((background dark)) :foreground "dark orange"))
105   "Face used for search records.")
106
107 (defface elpher-url
108   '((((background dark)) :foreground "yellow")
109     (((background light)) :foreground "dark red"))
110   "Face used for url records.")
111
112 (defface elpher-binary
113   '((t :foreground "magenta"))
114   "Face used for binary records.")
115
116 (defface elpher-unknown
117   '((t :foreground "red"))
118   "Face used for unknown record types.")
119
120 (defface elpher-margin-key
121   '((((background dark)) :foreground "white"))
122   "Face used for margin key.")
123
124 (defface elpher-margin-brackets
125   '((t :foreground "blue"))
126   "Face used for brackets around margin key.")
127
128 (defcustom elpher-open-urls-with-eww nil
129   "If non-nil, open URL selectors using eww.
130 Otherwise, use the system browser via the BROWSE-URL function."
131   :type '(boolean))
132
133 ;;; Model
134 ;;
135
136 ;; Address
137
138 (defun elpher-make-address (selector host port)
139   "Create an address of a gopher object with SELECTOR, HOST and PORT."
140   (list selector host port))
141
142 (defun elpher-address-selector (address)
143   "Retrieve selector from ADDRESS."
144   (car address))
145
146 (defun elpher-address-host (address)
147   "Retrieve host from ADDRESS."
148   (cadr address))
149
150 (defun elpher-address-port (address)
151   "Retrieve port from ADDRESS."
152   (caddr address))
153
154 ;; Node
155
156 (defun elpher-make-node (parent address getter &optional content pos)
157   "Create a node in the gopher page hierarchy.
158
159 PARENT specifies the parent of the node, ADDRESS specifies the address of
160 the gopher page, GETTER provides the getter function used to obtain this
161 page.
162
163 The optional arguments CONTENT and POS can be used to fill the cached
164 content and cursor position fields of the node."
165   (list parent address getter content pos))
166
167 (defun elpher-node-parent (node)
168   "Retrieve the parent node of NODE."
169   (elt node 0))
170
171 (defun elpher-node-address (node)
172   "Retrieve the address of NODE."
173   (elt node 1))
174
175 (defun elpher-node-getter (node)
176   "Retrieve the preferred getter function of NODE."
177   (elt node 2))
178
179 (defun elpher-node-content (node)
180   "Retrieve the cached content of NODE, or nil if none exists."
181   (elt node 3))
182
183 (defun elpher-node-pos (node)
184   "Retrieve the cached cursor position for NODE, or nil if none exists."
185   (elt node 4))
186
187 (defun elpher-set-node-content (node content)
188   "Set the content cache of NODE to CONTENT."
189   (setcar (nthcdr 3 node) content))
190
191 (defun elpher-set-node-pos (node pos)
192   "Set the cursor position cache of NODE to POS."
193   (setcar (nthcdr 4 node) pos))
194
195 ;; Node graph traversal
196
197 (defvar elpher-current-node)
198
199 (defun elpher-visit-node (node &optional getter)
200   "Visit NODE using its own getter or GETTER, if non-nil."
201   (elpher-save-pos)
202   (elpher-process-cleanup)
203   (setq elpher-current-node node)
204   (if getter
205       (funcall getter)
206     (funcall (elpher-node-getter node))))
207
208 (defun elpher-visit-parent-node ()
209   "Visit the parent of the current node."
210   (let ((parent-node (elpher-node-parent elpher-current-node)))
211     (when parent-node
212       (elpher-visit-node parent-node))))
213       
214 (defun elpher-reload-current-node ()
215   "Reload the current node, discarding any existing cached content."
216   (elpher-set-node-content elpher-current-node nil)
217   (elpher-visit-node elpher-current-node))
218
219 (defun elpher-save-pos ()
220   "Save the current position of point to the current node."
221   (when elpher-current-node
222     (elpher-set-node-pos elpher-current-node (point))))
223
224 (defun elpher-restore-pos ()
225   "Restore the position of point to that cached in the current node."
226   (let ((pos (elpher-node-pos elpher-current-node)))
227     (if pos
228         (goto-char pos)
229       (goto-char (point-min)))))
230
231 ;;; Buffer preparation
232 ;;
233
234 (defmacro elpher-with-clean-buffer (&rest args)
235   "Evaluate ARGS with a clean *elpher* buffer as current."
236   (list 'progn
237         '(switch-to-buffer "*elpher*")
238         '(elpher-mode)
239         (append (list 'let '((inhibit-read-only t))
240                       '(erase-buffer))
241                 args)))
242
243 ;;; Index rendering
244 ;;
245
246 (defun elpher-insert-index (string)
247   "Insert the index corresponding to STRING into the current buffer."
248   (dolist (line (split-string string "\r\n"))
249     (unless (= (length line) 0)
250       (elpher-insert-index-record line))))
251
252 (defun elpher-insert-margin (&optional type-name)
253   "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
254   (if type-name
255       (progn
256         (insert (format (concat "%" (number-to-string (- elpher-margin-width 1)) "s")
257                         (concat
258                          (propertize "[" 'face 'elpher-margin-brackets)
259                          (propertize type-name 'face 'elpher-margin-key)
260                          (propertize "]" 'face 'elpher-margin-brackets))))
261         (insert " "))
262     (insert (make-string elpher-margin-width ?\s))))
263
264 (defvar elpher-type-map
265   '((?0 elpher-get-text-node "T" elpher-text)
266     (?1 elpher-get-index-node "/" elpher-index)
267     (?g elpher-get-image-node "im" elpher-image)
268     (?p elpher-get-image-node "im" elpher-image)
269     (?I elpher-get-image-node "im" elpher-image)
270     (?4 elpher-get-node-download "B" elpher-binary)
271     (?5 elpher-get-node-download "B" elpher-binary)
272     (?9 elpher-get-node-download "B" elpher-binary)
273     (?7 elpher-get-search-node "?" elpher-search))
274   "Association list from types to getters, margin codes and index faces.")
275
276 (defun elpher-insert-index-record (line)
277   "Insert the index record corresponding to LINE into the current buffer."
278   (let* ((type (elt line 0))
279          (fields (split-string (substring line 1) "\t"))
280          (display-string (elt fields 0))
281          (selector (elt fields 1))
282          (host (elt fields 2))
283          (port (elt fields 3))
284          (address (elpher-make-address selector host port))
285          (type-map-entry (alist-get type elpher-type-map)))
286     (if type-map-entry
287         (let ((getter (car type-map-entry))
288               (margin-code (cadr type-map-entry))
289               (face (caddr type-map-entry)))
290           (elpher-insert-margin margin-code)
291           (insert-text-button display-string
292                               'face face
293                               'elpher-node (elpher-make-node elpher-current-node
294                                                                address
295                                                                getter)
296                               'action #'elpher-click-link
297                               'follow-link t
298                               'help-echo (format "mouse-1, RET: open %s on %s port %s"
299                                                  selector host port)))
300       (pcase type
301         (?i (elpher-insert-margin) ; Information
302             (insert (propertize display-string
303                                 'face 'elpher-info)))
304         (?h (elpher-insert-margin "W") ; Web link
305             (let ((url (elt (split-string selector "URL:") 1)))
306               (insert-text-button display-string
307                                   'face 'elpher-url
308                                   'elpher-url url
309                                   'action #'elpher-click-url
310                                   'follow-link t
311                                   'help-echo (format "mouse-1, RET: open url %s" url))))
312         (?.) ; Occurs at end of index, can safely ignore.
313         (tp (elpher-insert-margin (concat (char-to-string tp) "?"))
314             (insert (propertize display-string
315                                 'face 'elpher-unknown-face)))))
316     (insert "\n")))
317
318
319 ;;; Selector retrieval (all kinds)
320 ;;
321
322 (defun elpher-process-cleanup ()
323   "Immediately shut down any extant elpher process."
324   (let ((p (get-process "elpher-process")))
325     (if p (delete-process p))))
326
327 (defvar elpher-selector-string)
328
329 (defun elpher-get-selector (address after)
330   "Retrieve selector specified by ADDRESS, then execute AFTER.
331 The result is stored as a string in the variable elpher-selector-string."
332   (setq elpher-selector-string "")
333   (make-network-process
334    :name "elpher-process"
335    :host (elpher-address-host address)
336    :service (elpher-address-port address)
337    :filter (lambda (proc string)
338              (setq elpher-selector-string (concat elpher-selector-string string)))
339    :sentinel after)
340   (process-send-string "elpher-process"
341                        (concat (elpher-address-selector address) "\n")))
342
343 ;; Index retrieval
344
345 (defun elpher-get-index-node ()
346   "Getter which retrieves the current node contents as an index."
347   (let ((content (elpher-node-content elpher-current-node))
348         (address (elpher-node-address elpher-current-node)))
349     (if content
350         (progn
351           (elpher-with-clean-buffer
352            (insert content))
353           (elpher-restore-pos))
354       (if address
355           (progn
356             (elpher-with-clean-buffer
357              (insert "LOADING DIRECTORY..."))
358             (elpher-get-selector address
359                                   (lambda (proc event)
360                                     (unless (string-prefix-p "deleted" event)
361                                       (elpher-with-clean-buffer
362                                        (elpher-insert-index elpher-selector-string))
363                                       (elpher-restore-pos)
364                                       (elpher-set-node-content elpher-current-node
365                                                                 (buffer-string))))))
366         (progn
367           (elpher-with-clean-buffer
368            (elpher-insert-index elpher-start-index))
369           (elpher-restore-pos)
370           (elpher-set-node-content elpher-current-node
371                                     (buffer-string)))))))
372
373 ;; Text retrieval
374
375 (defconst elpher-url-regex
376   "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
377   "Regexp used to locate and buttinofy URLs in text files loaded by elpher.")
378
379 (defun elpher-buttonify-urls (string)
380   "Turn substrings which look like urls in STRING into clickable buttons."
381   (with-temp-buffer
382     (insert string)
383     (goto-char (point-min))
384     (while (re-search-forward elpher-url-regex nil t)
385       (let ((url (match-string 0))
386             (protocol (downcase (match-string 1))))
387         (if (string= protocol "gopher")
388             (let* ((host (match-string 2))
389                    (port 70)
390                    (type-and-selector (match-string 4))
391                    (type (if (> (length type-and-selector) 1)
392                              (elt type-and-selector 1)
393                            ?1))
394                    (selector (if (> (length type-and-selector) 1)
395                                  (substring type-and-selector 2)
396                                ""))
397                    (address (elpher-make-address selector host port))
398                    (getter (car (alist-get type elpher-type-map))))
399               (make-text-button (match-beginning 0)
400                                 (match-end 0)
401                                 'elpher-node (elpher-make-node elpher-current-node
402                                                                  address
403                                                                  getter)
404                                 'action #'elpher-click-link
405                                 'follow-link t
406                                 'help-echo (format "mouse-1, RET: open %s on %s port %s"
407                                                    selector host port)))
408           (make-text-button (match-beginning 0)
409                             (match-end 0)
410                             'elpher-url url
411                             'action #'elpher-click-url
412                             'follow-link t
413                             'help-echo (format "mouse-1, RET: open url %s" url)))))
414     (buffer-string)))
415
416 (defun elpher-process-text (string)
417   "Remove CRs and trailing period from the gopher text document STRING."
418   (let* ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string))
419          (cleaned-str (replace-regexp-in-string "\r" "" chopped-str)))
420     (elpher-buttonify-urls cleaned-str)))
421
422 (defun elpher-get-text-node ()
423   "Getter which retrieves the current node contents as a text document."
424   (let ((content (elpher-node-content elpher-current-node))
425         (address (elpher-node-address elpher-current-node)))
426     (if content
427         (progn
428           (elpher-with-clean-buffer
429            (insert content))
430           (elpher-restore-pos))
431       (progn
432         (elpher-with-clean-buffer
433          (insert "LOADING TEXT..."))
434         (elpher-get-selector address
435                               (lambda (proc event)
436                                 (unless (string-prefix-p "deleted" event)
437                                   (elpher-with-clean-buffer
438                                    (insert (elpher-process-text elpher-selector-string)))
439                                   (elpher-restore-pos)
440                                   (elpher-set-node-content elpher-current-node
441                                                             (buffer-string)))))))))
442
443 ;; Image retrieval
444
445 (defun elpher-get-image-node ()
446   "Getter which retrieves the current node contents as an image to view."
447   (let ((content (elpher-node-content elpher-current-node))
448         (address (elpher-node-address elpher-current-node)))
449     (if content
450         (progn
451           (elpher-with-clean-buffer
452            (insert-image content))
453           (setq cursor-type nil)
454           (elpher-restore-pos))
455       (progn
456         (elpher-with-clean-buffer
457          (insert "LOADING IMAGE..."))
458         (elpher-get-selector address
459                               (lambda (proc event)
460                                 (unless (string-prefix-p "deleted" event)
461                                   (let ((image (create-image
462                                                 (encode-coding-string elpher-selector-string
463                                                                       'no-conversion)
464                                                 nil t)))
465                                     (elpher-with-clean-buffer
466                                      (insert-image image))
467                                     (setq cursor-type nil)
468                                     (elpher-restore-pos)
469                                     (elpher-set-node-content elpher-current-node
470                                                               image)))))))))
471
472 ;; Search retrieval
473
474 (defun elpher-get-search-node ()
475   "Getter which submits a search query to the address of the current node."
476   (let ((content (elpher-node-content elpher-current-node))
477         (address (elpher-node-address elpher-current-node))
478         (aborted t))
479     (if content
480         (progn
481           (elpher-with-clean-buffer
482            (insert content))
483           (elpher-restore-pos)
484           (message "Displaying cached search results.  Reload to perform a new search."))
485       (unwind-protect
486           (let* ((query-string (read-string "Query: "))
487                  (query-selector (concat (elpher-address-selector address) "\t" query-string))
488                  (search-address (elpher-make-address query-selector
489                                                        (elpher-address-host address)
490                                                        (elpher-address-port address))))
491             (setq aborted nil)
492             (elpher-with-clean-buffer
493              (insert "LOADING RESULTS..."))
494             (elpher-get-selector search-address
495                                   (lambda (proc event)
496                                     (unless (string-prefix-p "deleted" event)
497                                       (elpher-with-clean-buffer
498                                        (elpher-insert-index elpher-selector-string))
499                                       (goto-char (point-min))
500                                       (elpher-set-node-content elpher-current-node
501                                                                 (buffer-string))))))
502         (if aborted
503             (elpher-visit-parent-node))))))
504
505 ;; Raw server response retrieval
506
507 (defun elpher-get-node-raw ()
508   "Getter which retrieves the raw server response for the current node."
509   (let* ((content (elpher-node-content elpher-current-node))
510          (address (elpher-node-address elpher-current-node)))
511     (elpher-with-clean-buffer
512      (insert "LOADING RAW SERVER RESPONSE..."))
513     (if address
514         (elpher-get-selector address
515                               (lambda (proc event)
516                                 (unless (string-prefix-p "deleted" event)
517                                   (elpher-with-clean-buffer
518                                    (insert elpher-selector-string))
519                                   (goto-char (point-min)))))
520       (progn
521         (elpher-with-clean-buffer
522          (insert elpher-start-index))
523         (goto-char (point-min)))))
524   (message "Displaying raw server response.  Reload or redraw to return to standard view."))
525  
526 ;; File export retrieval
527
528 (defvar elpher-download-filename)
529
530 (defun elpher-get-node-download ()
531   "Getter which retrieves the current node and writes the result to a file."
532   (let* ((address (elpher-node-address elpher-current-node))
533          (selector (elpher-address-selector address)))
534     (elpher-visit-parent-node) ; Do first in case of non-local exits.
535     (let* ((filename-proposal (file-name-nondirectory selector))
536            (filename (read-file-name "Save file as: "
537                                      nil nil nil
538                                      (if (> (length filename-proposal) 0)
539                                          filename-proposal
540                                        "gopher.file"))))
541       (message "Downloading...")
542       (setq elpher-download-filename filename)
543       (elpher-get-selector address
544                             (lambda (proc event)
545                               (let ((coding-system-for-write 'binary))
546                                 (with-temp-file elpher-download-filename
547                                   (insert elpher-selector-string)
548                                   (message (format "Download complate, saved to file %s."
549                                                    elpher-download-filename)))))))))
550
551
552 ;;; Navigation procedures
553 ;;
554
555 (defun elpher-next-link ()
556   "Move point to the next link on the current page."
557   (interactive)
558   (forward-button 1))
559
560 (defun elpher-prev-link ()
561   "Move point to the previous link on the current page."
562   (interactive)
563   (backward-button 1))
564
565 (defun elpher-click-link (button)
566   "Function called when the gopher link BUTTON is activated (via mouse or keypress)."
567   (let ((node (button-get button 'elpher-node)))
568     (elpher-visit-node node)))
569
570 (defun elpher-click-url (button)
571   "Function called when the url link BUTTON is activated (via mouse or keypress)."
572   (let ((url (button-get button 'elpher-url)))
573     (if elpher-open-urls-with-eww
574         (browse-web url)
575       (browse-url url))))
576
577 (defun elpher-follow-current-link ()
578   "Open the link or url at point."
579   (interactive)
580   (push-button))
581
582 (defun elpher-go ()
583   "Go to a particular gopher site."
584   (interactive)
585   (let* (
586          (hostname (read-string "Gopher host: "))
587          (selector (read-string "Selector (default none): " nil nil ""))
588          (port (read-string "Port (default 70): " nil nil 70))
589          (address (list selector hostname port)))
590     (elpher-visit-node
591      (elpher-make-node elpher-current-node
592                         address
593                         #'elpher-get-index-node))))
594
595 (defun  elpher-redraw ()
596   "Redraw current page."
597   (interactive)
598   (elpher-visit-node elpher-current-node))
599
600 (defun  elpher-reload ()
601   "Reload current page."
602   (interactive)
603   (elpher-reload-current-node))
604
605 (defun elpher-view-raw ()
606   "View current page as plain text."
607   (interactive)
608   (elpher-visit-node elpher-current-node
609                       #'elpher-get-node-raw))
610
611 (defun elpher-back ()
612   "Go to previous site."
613   (interactive)
614   (if (elpher-node-parent elpher-current-node)
615       (elpher-visit-parent-node)
616     (message "No previous site.")))
617
618 (defun elpher-download ()
619   "Download the link at point."
620   (interactive)
621   (let ((button (button-at (point))))
622     (if button
623         (let ((node (button-get button 'elpher-node)))
624           (if node
625               (elpher-visit-node (button-get button 'elpher-node)
626                                   #'elpher-get-node-download)
627             (message "Can only download gopher links, not general URLs.")))
628       (message "No link selected."))))
629
630 ;;; Mode and keymap
631 ;;
632
633 (defvar elpher-mode-map
634   (let ((map (make-sparse-keymap)))
635     (define-key map (kbd "TAB") 'elpher-next-link)
636     (define-key map (kbd "<backtab>") 'elpher-prev-link)
637     (define-key map (kbd "u") 'elpher-back)
638     (define-key map (kbd "g") 'elpher-go)
639     (define-key map (kbd "r") 'elpher-redraw)
640     (define-key map (kbd "R") 'elpher-reload)
641     (define-key map (kbd "w") 'elpher-view-raw)
642     (define-key map (kbd "d") 'elpher-download)
643     (when (fboundp 'evil-define-key)
644       (evil-define-key 'normal map
645         (kbd "TAB") 'elpher-next-link
646         (kbd "C-]") 'elpher-follow-current-link
647         (kbd "C-t") 'elpher-back
648         (kbd "u") 'elpher-back
649         (kbd "g") 'elpher-go
650         (kbd "r") 'elpher-redraw
651         (kbd "R") 'elpher-reload
652         (kbd "w") 'elpher-view-raw
653         (kbd "d") 'elpher-download))
654     map)
655   "Keymap for gopher client.")
656
657 (define-derived-mode elpher-mode special-mode "elpher"
658   "Major mode for elpher, an elisp gopher client.")
659
660
661 ;;; Main start procedure
662 ;;
663
664 ;;;###autoload
665 (defun elpher ()
666   "Start elpher with default landing page."
667   (interactive)
668   (setq elpher-current-node nil)
669   (let ((start-node (elpher-make-node nil nil #'elpher-get-index-node)))
670     (elpher-visit-node start-node))
671   "Started Elpher.") ; Otherwise (elpher) evaluates to start page string.
672
673 ;;; elpher.el ends here