2321
|
1 ;;; cookie.el --- retrieve random phrases from fortune cookie files
|
|
2
|
|
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
|
|
6 ;; Maintainer: FSF
|
|
7 ;; Keywords: games
|
|
8 ;; Created: Mon Mar 22 17:06:26 1993
|
|
9
|
|
10 ;; This file is part of GNU Emacs.
|
|
11
|
|
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
13 ;; it under the terms of the GNU General Public License as published by
|
|
14 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
15 ;; any later version.
|
|
16
|
|
17 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
20 ;; GNU General Public License for more details.
|
|
21
|
|
22 ;; You should have received a copy of the GNU General Public License
|
|
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
25
|
|
26 ;;; Commentary:
|
|
27
|
|
28 ;; Support for random cookie fetches from phrase files, used for such
|
|
29 ;; critical applications as emulating Zippy the Pinhead and confounding
|
|
30 ;; the NSA Trunk Trawler.
|
|
31 ;;
|
|
32 ;; The two entry points are `cookie' and `cookie-insert'. The helper
|
|
33 ;; functions `pick-random' and `shuffle-vector' may be of interest to
|
|
34 ;; programmers.
|
|
35 ;;
|
|
36 ;; The code expects phrase files to be in ITS-style LINS format
|
|
37 ;; (strings terminated by ASCII 0 characters, leading whitespace
|
|
38 ;; ignored). Everything up to the first delimiter is treated as a
|
|
39 ;; comment. Other formats (notably UNIX fortune-file format) could
|
|
40 ;; easily be supported by changing the `cookie-delimiter' constant.
|
|
41 ;;
|
|
42 ;; This code derives from Steve Strassman's 1987 spook.el package, but
|
|
43 ;; has been generalized so that it supports multiple simultaneous cookie
|
|
44 ;; databases. It is intended to be called from other packages such as
|
|
45 ;; yow.el and spook.el.
|
2322
|
46 ;;
|
|
47 ;; TO DO: teach cookie-snarf to auto-detect ITS PINS or UNIX fortune(6)
|
|
48 ;; format and do the right thing.
|
2321
|
49
|
|
50 ;;; Code:
|
|
51
|
|
52 ; Randomize the seed in the random number generator.
|
|
53 (random t)
|
|
54
|
|
55 (defconst cookie-delimiter "\0"
|
|
56 "Delimiter used to separate cookie file entries.")
|
|
57
|
|
58 (defun cookie (phrase-file startmsg endmsg)
|
|
59 "Return a random phrase from PHRASE-FILE. When the phrase file
|
|
60 is read in, display STARTMSG at beginning of load, ENDMSG at end."
|
|
61 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg)))
|
|
62 (shuffle-vector cookie-vector)
|
|
63 (aref cookie-vector 1)))
|
|
64
|
|
65 (defun cookie-insert (phrase-file &optional count startmsg endmsg)
|
|
66 "Insert random phrases from PHRASE-FILE; COUNT of them. When the phrase file
|
|
67 is read in, display STARTMSG at beginning of load, ENDMSG at end."
|
|
68 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg)))
|
|
69 (shuffle-vector cookie-vector)
|
|
70 (let ((start (point)))
|
|
71 (insert ?\n)
|
|
72 (cookie1 (min (- (length cookie-vector) 1) (or count 1)) cookie-vector)
|
|
73 (insert ?\n)
|
|
74 (fill-region-as-paragraph start (point) nil))))
|
|
75
|
|
76 (defun cookie1 (arg cookie-vec)
|
|
77 "Inserts a cookie phrase ARG times."
|
|
78 (cond ((zerop arg) t)
|
|
79 (t (insert (aref cookie-vec arg))
|
|
80 (insert " ")
|
|
81 (cookie1 (1- arg) cookie-vec))))
|
|
82
|
|
83 (defun cookie-snarf (phrase-file startmsg endmsg)
|
|
84 "Reads in the PHRASE-FILE, returns it as a vector of strings. Emit
|
|
85 STARTMSG and ENDMSG before and after. Caches the result; second and
|
|
86 subsequent calls on the same file won't go to disk."
|
|
87 (if (boundp (intern phrase-file))
|
|
88 (eval (intern phrase-file))
|
|
89 (message startmsg)
|
|
90 (save-excursion
|
|
91 (let ((buf (generate-new-buffer "*cookie*"))
|
|
92 (result nil))
|
|
93 (set-buffer buf)
|
|
94 (insert-file-contents (expand-file-name phrase-file))
|
|
95 (search-forward cookie-delimiter)
|
|
96 (while (progn (skip-chars-forward " \t\n\r\f") (not (eobp)))
|
|
97 (let ((beg (point)))
|
|
98 (search-forward "\0")
|
|
99 (setq result (cons (buffer-substring beg (1- (point)))
|
|
100 result))))
|
|
101 (kill-buffer buf)
|
|
102 (message endmsg)
|
|
103 (set (intern phrase-file) (apply 'vector result))))))
|
|
104
|
|
105 (defun pick-random (n)
|
|
106 "Returns a random number from 0 to N-1 inclusive."
|
|
107 (% (logand 0777777 (random)) n))
|
|
108
|
|
109 ; Thanks to Ian G Batten <BattenIG@CS.BHAM.AC.UK>
|
|
110 ; [of the University of Birmingham Computer Science Department]
|
|
111 ; for the iterative version of this shuffle.
|
|
112 ;
|
|
113 (defun shuffle-vector (vector)
|
|
114 "Randomly permute the elements of VECTOR (all permutations equally likely)"
|
|
115 (let ((i 0)
|
|
116 j
|
|
117 temp
|
|
118 (len (length vector)))
|
|
119 (while (< i len)
|
|
120 (setq j (+ i (pick-random (- len i))))
|
|
121 (setq temp (aref vector i))
|
|
122 (aset vector i (aref vector j))
|
|
123 (aset vector j temp)
|
|
124 (setq i (1+ i))))
|
|
125 vector)
|
|
126
|
|
127 (provide 'cookie)
|
|
128
|
|
129 ;;; cookie.el ends here
|