Added support for download-only binary entries.
[elpher.git] / elopher.el
1 ;;; elopher.el --- gopher client
2
3 ;;; Commentary:
4
5 ;; Simple gopher client in elisp.
6
7 ;;; Code:
8
9 ;;; Global constants
10 ;;
11
12 (defconst elopher-version "1.0.0"
13   "Current version of elopher.")
14
15 (defconst elopher-margin-width 6
16   "Width of left-hand margin used when rendering indicies.")
17
18 (defconst elopher-start-index
19   (mapconcat
20    'identity
21    (list "i\tfake\tfake\t1"
22          "i--------------------------------------------\tfake\tfake\t1"
23          "i          Elopher Gopher Client             \tfake\tfake\t1"
24          (format "i              version %s\tfake\tfake\t1" elopher-version)
25          "i--------------------------------------------\tfake\tfake\t1"
26          "i\tfake\tfake\t1"
27          "iBasic usage:\tfake\tfake\t1"
28          "i\tfake\tfake\t1"
29          "i - tab/shift-tab: next/prev directory entry on current page\tfake\tfake\t1"
30          "i - RET/mouse-1: open directory entry under cursor\tfake\tfake\t1"
31          "i - u: return to parent directory entry\tfake\tfake\t1"
32          "i - g: go to a particular page\tfake\tfake\t1"
33          "i - r: reload current page\tfake\tfake\t1"
34          "i - d: download directory entry under cursor\tfake\tfake\t1"
35          "i - w: display the raw server response for the current page\tfake\tfake\t1"
36          "i\tfake\tfake\t1"
37          "iPlaces to start exploring Gopherspace:\tfake\tfake\t1"
38          "i\tfake\tfake\t1"
39          "1Floodgap Systems Gopher Server\t\tgopher.floodgap.com\t70"
40          "i\tfake\tfake\t1"
41          "iAlternatively, select the following item and enter some\tfake\tfake\t1"
42          "isearch terms:\tfake\tfake\t1"
43          "i\tfake\tfake\t1"
44          "7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
45          ".")
46    "\r\n"))
47
48
49 ;;; Customization group
50 ;;
51
52 (defgroup elopher nil
53   "A simple gopher client."
54   :group 'applications)
55
56 (defcustom elopher-index-face '(foreground-color . "cyan")
57   "Face used for index records."
58   :type '(face))
59
60 (defcustom elopher-text-face '(foreground-color . "white")
61   "Face used for text records."
62   :type '(face))
63
64 (defcustom elopher-info-face '(foreground-color . "gray")
65   "Face used for info records."
66   :type '(face))
67
68 (defcustom elopher-image-face '(foreground-color . "green")
69   "Face used for image records."
70   :type '(face))
71
72 (defcustom elopher-search-face '(foreground-color . "orange")
73   "Face used for image records."
74   :type '(face))
75
76 (defcustom elopher-http-face '(foreground-color . "yellow")
77   "Face used for image records."
78   :type '(face))
79
80 (defcustom elopher-binary-face '(foreground-color . "magenta")
81   "Face used for image records."
82   :type '(face))
83
84 (defcustom elopher-unknown-face '(foreground-color . "red")
85   "Face used for unknown record types."
86   :type '(face))
87
88 (defcustom elopher-open-urls-with-eww nil
89   "If non-nil, open URL selectors using eww.
90 Otherwise, use the system browser via the BROWSE-URL function."
91   :type '(boolean))
92
93 ;;; Model
94 ;;
95
96 ;; Address
97
98 (defun elopher-make-address (selector host port)
99   (list selector host port))
100
101 (defun elopher-address-selector (address)
102   (car address))
103
104 (defun elopher-address-host (address)
105   (cadr address))
106
107 (defun elopher-address-port (address)
108   (caddr address))
109
110 ;; Node
111
112 (defun elopher-make-node (parent address getter &optional content pos)
113   (list parent address getter content pos))
114
115 (defun elopher-node-parent (node)
116   (elt node 0))
117
118 (defun elopher-node-address (node)
119   (elt node 1))
120
121 (defun elopher-node-getter (node)
122   (elt node 2))
123
124 (defun elopher-node-content (node)
125   (elt node 3))
126
127 (defun elopher-node-pos (node)
128   (elt node 4))
129
130 (defun elopher-set-node-content (node content)
131   (setcar (nthcdr 3 node) content))
132
133 (defun elopher-set-node-pos (node pos)
134   (setcar (nthcdr 4 node) pos))
135
136 (defun elopher-save-pos ()
137   (when elopher-current-node
138     (elopher-set-node-pos elopher-current-node (point))))
139
140 (defun elopher-restore-pos ()
141   (let ((pos (elopher-node-pos elopher-current-node)))
142     (if pos
143         (goto-char pos)
144       (goto-char (point-min)))))
145
146 ;; Node graph traversal
147
148 (defvar elopher-current-node)
149
150 (defun elopher-visit-node (node &optional getter)
151   (elopher-save-pos)
152   (elopher-process-cleanup)
153   (setq elopher-current-node node)
154   (if getter
155       (funcall getter)
156     (funcall (elopher-node-getter node))))
157
158 (defun elopher-visit-parent-node ()
159   (let ((parent-node (elopher-node-parent elopher-current-node)))
160     (when parent-node
161       (elopher-visit-node parent-node))))
162       
163 (defun elopher-reload-current-node ()
164   (elopher-set-node-content elopher-current-node nil)
165   (elopher-visit-node elopher-current-node))
166
167 ;;; Buffer preparation
168 ;;
169
170 (defmacro elopher-with-clean-buffer (&rest args)
171   "Evaluate ARGS with a clean *elopher* buffer as current."
172   (list 'progn
173         '(switch-to-buffer "*elopher*")
174         '(elopher-mode)
175         (append (list 'let '((inhibit-read-only t))
176                       '(erase-buffer))
177                 args)))
178
179 ;;; Index rendering
180 ;;
181
182 (defun elopher-insert-index (string)
183   "Insert the index corresponding to STRING into the current buffer."
184   (dolist (line (split-string string "\r\n"))
185     (unless (= (length line) 0)
186       (elopher-insert-index-record line))))
187
188 (defun elopher-insert-margin (&optional type-name)
189   "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
190   (if type-name
191       (progn
192         (insert (format (concat "%" (number-to-string (- elopher-margin-width 1)) "s")
193                         (concat
194                          (propertize "[" 'face '(foreground-color . "blue"))
195                          (propertize type-name 'face '(foreground-color . "white"))
196                          (propertize "]" 'face '(foreground-color . "blue")))))
197         (insert " "))
198     (insert (make-string elopher-margin-width ?\s))))
199
200 (defun elopher-insert-index-record (line)
201   "Insert the index record corresponding to LINE into the current buffer."
202   (let* ((type (elt line 0))
203          (fields (split-string (substring line 1) "\t"))
204          (display-string (elt fields 0))
205          (selector (elt fields 1))
206          (host (elt fields 2))
207          (port (elt fields 3))
208          (address (elopher-make-address selector host port))
209          (help-string (format "mouse-1, RET: open %s on %s port %s"
210                               selector host port)))
211     (pcase type
212       (?i (elopher-insert-margin)                             ; Information 
213           (insert (propertize display-string
214                               'face elopher-info-face)))
215       (?0 (elopher-insert-margin "T")                         ; Text
216           (insert-text-button display-string
217                               'face elopher-text-face
218                               'elopher-node (elopher-make-node elopher-current-node
219                                                                address
220                                                                #'elopher-get-text-node)
221                               'action #'elopher-click-link
222                               'follow-link t
223                               'help-echo help-string))
224       (?1 (elopher-insert-margin "/")                         ; Index
225           (insert-text-button display-string
226                               'face elopher-index-face
227                               'elopher-node (elopher-make-node elopher-current-node
228                                                                address
229                                                                #'elopher-get-index-node)
230                               'action #'elopher-click-link
231                               'follow-link t
232                               'help-echo help-string))
233       ((or ?g ?p ?I) (elopher-insert-margin "im")             ; Image
234        (insert-text-button display-string
235                            'face elopher-image-face
236                            'elopher-node (elopher-make-node elopher-current-node
237                                                             address
238                                                             #'elopher-get-image-node)
239                            'action #'elopher-click-link
240                            'follow-link t
241                            'help-echo help-string))
242       ((or ?4 ?9) (elopher-insert-margin "B")                 ; Binary
243        (insert-text-button display-string
244                            'face elopher-binary-face
245                            'elopher-node (elopher-make-node elopher-current-node
246                                                             address
247                                                             #'elopher-get-node-download)
248                            'action #'elopher-click-link
249                            'follow-link t
250                            'help-echo help-string))
251       (?7 (elopher-insert-margin "S")
252           (insert-text-button display-string
253                               'face elopher-search-face
254                               'elopher-node (elopher-make-node elopher-current-node
255                                                                address
256                                                                #'elopher-get-search-node)
257                               'action #'elopher-click-link
258                               'follow-link t
259                               'help-echo help-string))
260       (?h (elopher-insert-margin "W")
261           (let ((url (elt (split-string selector "URL:") 1)))
262             (insert-text-button display-string
263                                 'face elopher-http-face
264                                 'elopher-url url
265                                 'action #'elopher-click-url
266                                 'follow-link t
267                                 'help-echo (format "mouse-1, RET: open url %s" url))))
268       (?.) ; Occurs at end of index, can safely ignore.
269       (tp (elopher-insert-margin (concat (char-to-string tp) "?"))
270           (insert (propertize display-string
271                               'face elopher-unknown-face))))
272     (insert "\n")))
273
274
275 ;;; Selector retrieval (all kinds)
276 ;;
277
278 (defun elopher-process-cleanup ()
279   "Immediately shut down any extant elopher process."
280   (let ((p (get-process "elopher-process")))
281     (if p (delete-process p))))
282
283 (defvar elopher-selector-string)
284
285 (defun elopher-get-selector (address after)
286   "Retrieve selector specified by ADDRESS, then execute AFTER.
287 The result is stored as a string in the variable elopher-selector-string."
288   (setq elopher-selector-string "")
289   (make-network-process
290    :name "elopher-process"
291    :host (elopher-address-host address)
292    :service (elopher-address-port address)
293    :filter (lambda (proc string)
294              (setq elopher-selector-string (concat elopher-selector-string string)))
295    :sentinel after)
296   (process-send-string "elopher-process"
297                        (concat (elopher-address-selector address) "\n")))
298
299 ;; Index retrieval
300
301 (defun elopher-get-index-node ()
302   (let ((content (elopher-node-content elopher-current-node))
303         (address (elopher-node-address elopher-current-node)))
304     (if content
305         (progn
306           (elopher-with-clean-buffer
307            (insert content))
308           (elopher-restore-pos))
309       (if address
310           (progn
311             (elopher-with-clean-buffer
312              (insert "LOADING DIRECTORY..."))
313             (elopher-get-selector address
314                                   (lambda (proc event)
315                                     (unless (string-prefix-p "deleted" event)
316                                       (elopher-with-clean-buffer
317                                        (elopher-insert-index elopher-selector-string))
318                                       (elopher-restore-pos)
319                                       (elopher-set-node-content elopher-current-node
320                                                                 (buffer-string))))))
321         (progn
322           (elopher-with-clean-buffer
323            (elopher-insert-index elopher-start-index))
324           (elopher-restore-pos)
325           (elopher-set-node-content elopher-current-node
326                                     (buffer-string)))))))
327
328 ;; Text retrieval
329
330 (defun elopher-process-text (string)
331   (let ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string)))
332     (replace-regexp-in-string "\r" "" chopped-str)))
333
334 (defun elopher-get-text-node ()
335   (let ((content (elopher-node-content elopher-current-node))
336         (address (elopher-node-address elopher-current-node)))
337     (if content
338         (progn
339           (elopher-with-clean-buffer
340            (insert content))
341           (elopher-restore-pos))
342       (progn
343         (elopher-with-clean-buffer
344          (insert "LOADING TEXT..."))
345         (elopher-get-selector address
346                               (lambda (proc event)
347                                 (unless (string-prefix-p "deleted" event)
348                                   (elopher-with-clean-buffer
349                                    (insert (elopher-process-text elopher-selector-string)))
350                                   (elopher-restore-pos)
351                                   (elopher-set-node-content elopher-current-node
352                                                             (buffer-string)))))))))
353
354 ;; Image retrieval
355
356 (defun elopher-get-image-node ()
357   (let ((content (elopher-node-content elopher-current-node))
358         (address (elopher-node-address elopher-current-node)))
359     (if content
360         (progn
361           (elopher-with-clean-buffer
362            (insert-image content))
363           (setq cursor-type nil)
364           (elopher-restore-pos))
365       (progn
366         (elopher-with-clean-buffer
367          (insert "LOADING IMAGE..."))
368         (elopher-get-selector address
369                               (lambda (proc event)
370                                 (unless (string-prefix-p "deleted" event)
371                                   (let ((image (create-image
372                                                 (string-as-unibyte elopher-selector-string)
373                                                 nil t)))
374                                     (elopher-with-clean-buffer
375                                      (insert-image image))
376                                     (setq cursor-type nil)
377                                     (elopher-restore-pos)
378                                     (elopher-set-node-content elopher-current-node
379                                                               image)))))))))
380
381 ;; Search retrieval
382
383 (defun elopher-get-search-node ()
384   (let* ((content (elopher-node-content elopher-current-node))
385          (address (elopher-node-address elopher-current-node)))
386     (if content
387         (progn
388           (elopher-with-clean-buffer
389             (insert content))
390           (elopher-restore-pos)
391           (message "Displaying cached search results.  Reload to perform a new search."))
392       (let* ((query-string (read-string "Query: "))
393              (query-selector (concat (elopher-address-selector address) "\t" query-string))
394              (search-address (elopher-make-address query-selector
395                                                    (elopher-address-host address)
396                                                    (elopher-address-port address))))
397         (elopher-with-clean-buffer
398          (insert "LOADING RESULTS..."))
399         (elopher-get-selector search-address
400                               (lambda (proc event)
401                                 (unless (string-prefix-p "deleted" event)
402                                   (elopher-with-clean-buffer
403                                    (elopher-insert-index elopher-selector-string))
404                                   (goto-char (point-min))
405                                   (elopher-set-node-content elopher-current-node
406                                                             (buffer-string)))))))))
407
408 ;; Raw server response retrieval
409
410 (defun elopher-get-node-raw ()
411   (let* ((content (elopher-node-content elopher-current-node))
412          (address (elopher-node-address elopher-current-node)))
413     (elopher-with-clean-buffer
414      (insert "LOADING RAW SERVER RESPONSE..."))
415     (if address
416         (elopher-get-selector address
417                               (lambda (proc event)
418                                 (unless (string-prefix-p "deleted" event)
419                                   (elopher-with-clean-buffer
420                                    (insert elopher-selector-string))
421                                   (goto-char (point-min)))))
422       (progn
423         (elopher-with-clean-buffer
424          (insert elopher-start-index))
425         (goto-char (point-min)))))
426   (message "Displaying raw server response.  Reload to return to standard view."))
427  
428
429 ;; File export retrieval
430
431 (defvar elopher-download-filename)
432
433 (defun elopher-get-node-download ()
434   (let* ((address (elopher-node-address elopher-current-node))
435          (selector (elopher-address-selector address)))
436     (unwind-protect
437         (let* ((filename-proposal (file-name-nondirectory selector))
438                (filename (read-file-name "Save file as: "
439                                          nil nil nil
440                                          (if (> (length filename-proposal) 0)
441                                              filename-proposal
442                                            "gopher.file"))))
443           (message "Downloading...")
444           (setq elopher-download-filename filename)
445           (elopher-get-selector address
446                                 (lambda (proc event)
447                                   (let ((coding-system-for-write 'binary))
448                                     (with-temp-file elopher-download-filename
449                                       (insert elopher-selector-string)))
450                                   (message (format "Download complate, saved to file %s."
451                                                    elopher-download-filename)))))
452       (elopher-visit-parent-node))))
453         
454
455 ;;; Navigation procedures
456 ;;
457
458 (defun elopher-next-link ()
459   (interactive)
460   (forward-button 1))
461
462 (defun elopher-prev-link ()
463   (interactive)
464   (backward-button 1))
465
466 (defun elopher-click-link (button)
467   (let ((node (button-get button 'elopher-node)))
468     (elopher-visit-node node)))
469
470 (defun elopher-click-url (button)
471   (let ((url (button-get button 'elopher-url)))
472     (if elopher-open-urls-with-eww
473         (browse-web url)
474       (browse-url url))))
475
476 (defun elopher-follow-closest-link ()
477   (interactive)
478   (push-button))
479
480 (defun elopher-go ()
481   "Go to a particular gopher site."
482   (interactive)
483   (let* (
484          (hostname (read-string "Gopher host: "))
485          (selector (read-string "Selector (default none): " nil nil ""))
486          (port (read-string "Port (default 70): " nil nil 70))
487          (address (list selector hostname port)))
488     (elopher-visit-node
489      (elopher-make-node elopher-current-node
490                         address
491                         #'elopher-get-index-node))))
492
493 (defun  elopher-reload ()
494   "Reload current page."
495   (interactive)
496   (elopher-reload-current-node))
497
498 (defun elopher-view-raw ()
499   "View current page as plain text."
500   (interactive)
501   (elopher-visit-node elopher-current-node
502                       #'elopher-get-node-raw))
503
504 (defun elopher-back ()
505   "Go to previous site."
506   (interactive)
507   (if (elopher-node-parent elopher-current-node)
508       (elopher-visit-parent-node)
509     (message "No previous site.")))
510
511 (defun elopher-download ()
512   "Download the link at point."
513   (interactive)
514   (let ((button (button-at (point))))
515     (if button
516         (elopher-visit-node (button-get button 'elopher-node)
517                             #'elopher-get-node-download)
518       (message "No link selected."))))
519
520
521 ;;; Mode and keymap
522 ;;
523
524 (defvar elopher-mode-map
525   (let ((map (make-sparse-keymap)))
526     (define-key map (kbd "<tab>") 'elopher-next-link)
527     (define-key map (kbd "<S-tab>") 'elopher-prev-link)
528     (define-key map (kbd "u") 'elopher-back)
529     (define-key map (kbd "g") 'elopher-go)
530     (define-key map (kbd "r") 'elopher-reload)
531     (define-key map (kbd "w") 'elopher-view-raw)
532     (define-key map (kbd "d") 'elopher-download)
533     (when (fboundp 'evil-define-key)
534       (evil-define-key 'normal map
535         (kbd "C-]") 'elopher-follow-closest-link
536         (kbd "C-t") 'elopher-back
537         (kbd "u") 'elopher-back
538         (kbd "g") 'elopher-go
539         (kbd "r") 'elopher-reload
540         (kbd "w") 'elopher-view-raw
541         (kbd "d") 'elopher-download))
542     map)
543   "Keymap for gopher client.")
544
545 (define-derived-mode elopher-mode special-mode "elopher"
546   "Major mode for elopher, an elisp gopher client.")
547
548
549 ;;; Main start procedure
550 ;;
551
552 (defun elopher ()
553   "Start elopher with default landing page."
554   (interactive)
555   (setq elopher-current-node nil)
556   (let ((start-node (elopher-make-node nil nil #'elopher-get-index-node)))
557     (elopher-visit-node start-node))
558   "Started Elopher.") ; Otherwise (elopher) evaluates to start page string.
559
560 ;;; elopher.el ends here
561