Fortune cookies for Linux

Fortune Teller


Fortune brings you a saying or a joke when you launch a terminal session. Or you can attach a fortune to your email signature to brighten up your messges.

By Hagen Höpfner

www.photocase.com

If you want your fortune telling and you can't wait until your next Chinese meal, why not configure your Linux computer to hand out virtual fortune cookies?

Which Version?

Most Linux distributions include a fortune-cookie tool known as Fortune. The package for Suse 9.3 is simply called fortune. Gentoo users can give the emerge fortune-mod command to add the virtual fortune teller to their software collection.

The original Fortune tool was written in 1986 by Ken Arnold and has been extended ever since. In 1995, Amy A. Lewis released an enhanced version on which fortune-mod [1], which was released in 2004, is based.

I will focus on the legacy version, but you can follow nearly all of the examples if your distribution happens to include the fortune-mod package instead.

Say What?

The simplest way of running the tool is to give the fortune command, which outputs a quotation or joke (Figure 1). The jokes are taken from database files that are included with the program. The database files typically reside below the /usr/share/fortune/ folder. Mandriva stores the files in /usr/share/games/fortunes. But fortune -f gives you a quicker way of finding out which topics your version of Fortune covers.

Figure 1: Enter the Fortune command in a terminal window to display a quotation or joke.

If you want to restrict fortune to a single quotation database file, simply specify the file as a parameter. Typing

fortune startrek linuxcookie

will restrict Fortune to quotations from Star Trek (including the stardate) on various Linux-related subjects.

Multifaceted and Verbose

If the supplied quotation databases are not to your liking, you may find something more to your taste on the Internet. The site at [2] is a good place to start. Fans of the Simpsons might like the chalkboard database at [3]. To feed Fortune with Bart Simpson quotations, download and unpack the archive file, then launch the cookie server by entering

fortune ./fortune-simpsons-chalk board/chalkboard

If you intend to run Fortune with a number of databases that are not included by default, it is a good idea to create a separate directory for each database: ~/.my_quotations, for example. You could then copy the quotation database and the matching .dat file, e.g. chalkboard and chalkboard.dat, to the new directory.

To tell Fortune to parse this folder, simply specify the path when calling the program: fortune ~/.my_quotations. If you want to add a new quotation database to the existing files, simply specify both paths at the command line. You can use percentages to increase the odds of Fortune giving you a quote from your favorite directory: fortune 90% ~/.my_quotations 10% /usr/share/fortune will ensure that 90% of all quotations come from your own collection. But make sure that the percentages add up to 100%, or Fortune will protest.

More Sayings

If you would like to add a non-English Fortune database, take a look at the collection at [4]. The page includes a Spanish Fortune database under fortunes-es_1.24.tar.gz. When you unpack the database, the quotation files will be located in the Datfiles folder. However, the package does not include index files for the collections; that is, the .dat files are missing. When you call fortune ./provierbios.fortune, the program just outputs an error message telling you that it can't find the database.

Fortunately, the tool you need to create an index, strfile, is automatically installed along with the Fortune program. strfile parses files as blocks of text separated by simple percent signs. It uses this information to create a file with an index for the text blocks. The syntax for our example would be

/usr/sbin/strfile provierbios.fortune provierbios.fortune.dat

The strfile tool is also useful if you want to create a Fortune database from a file with your own quotations. First create a text file with the required citations and sayings. Follow each entry in the file with a line containing a single percent sign, as in

Four legs good, two legs bad.
               -- G. Orwell
%
Many a mickle makes a muckle.
%

Then choose a name to store the file, such as my_fortunes, and run the /usr/sbin/strfile my_fortunes my_fortunes.dat command to create the index.

No Worries

Besides giving you a neat command line gadget, Fortune has other uses. For example, you could add a call to Fortune at the end of your ~/.bashrc file, to have Fortune welcome you with a different wacky saying each time you open a terminal window.

Fortune is also useful in combination with a screen saver. On KDE, you can combine Fortune with the Phosphor screen saver, for example (Figure 2). If you do not use KDE, the xlock -mode marquee command will give you a screen saver with lots of variety.

Figure 2: Some screen savers, such as Phosphor on KDE, can integrate Fortune.

Fortune is just as useful as a random signature generator for mail. For example, Kontact has an option for setting up a signature for each mail ID in Settings / Configure KMail. To allow Fortune to add a citation or saying to your electronic mail, double click on the identity and, in the Edit Identity window, select the Signature tab. Check the Enable Signature box in the Signature tab, then select Output of command in the Obtain Signature text from: dropdown menu; type the Fortune command in the box labeled Specify command (Figure 3).

Figure 3: Enabling Fortune as a signature generator in Kontact's identity settings.

It makes sense to run Fortune with the -s -n 320 options for mail signatures. -s tells Fortune to keep output "short," and -n 320 tells Fortune to regard all citations below 320 characters as short.

Other mail programs, such as Sylpheed, also have options for using the output from Fortune as a signature. But don't worry if your mail program does not have this ability. You can easily write a script, or set up a cronjob, to call Fortune at regular intervals and write the command output to a ~/.signature file. Then configure your mail program to use the contents of the file as a signature.

INFO
[1] fortune-mod: http://www.redellipse.net/code/fortune
[2] Fortune databases from Freshmeat: http://freshmeat.net/browse/895/
[3] Bart Simpson quotes: http://www.splitbrain.org/Fortunes/simpsons/fortune-simpsons-chalkboard.tgz
[4] Debian's fortunes: http://packages.debian.org/unstable/games/