Fortune

Fortune

Summary

An online programme that displays 'fortune cookies,' random epigrams, as found on pretty much all modern Unices.

Summary

Displays random epigrams, just like the programme with the same name found on your favourite UNIX derivative's terminal.

Project Goals

The idea behind this programme is dead simple: a port of the terminal programme 'fortune', found on most Unices, so that it works on your favourite web browser. This isn't exactly a novel idea. You'll find this programme implemented several dozen times with a simple Google query.

The reason why I went and wrote this version anyway, is that I needed a simple testing environment for two things: writing web applications in C++, and using HTTP as the wire protocol between the web server and the application. Turns out both of these are rather simple. As an added bonus, I've made this version of fortune choose random epigrams from both the offensive and inoffensive ones, which is a feature I've missed in nearly all of the other implementations.

Details

As a daemon, upon starting, this version maps all the files in /usr/share/games/fortunes into memory, scans them for the characteristic '%' lines, maintains a list of start/stop locations of the individual epigrams and whether to decode them with ROT13, then opens a local socket for the web server to connect to. I've used the fortune-mod data files that came with debian for this particular project.

If you'd like to examine the source code, just download it via this site's git repository and see the file 'src/fortune.cpp' for the programme's code and 'include/ef.gy/http.h' for the http server code. If you do examine the source code, you'll find that the daemon doesn't actually produce XHTML output. Instead, it generates extremely simple XML, which is post-processed by 'xslt/xhtml-fortune.xslt' to produce plain XHTML. This makes this particular implementation a good example of how to separate logic and content from design elements.

Update: this has been seriously updated, see the fortuned article for details. There's a JSFiddle for using the server there and everything.

Last Modified: 2017-06-06T09:20:00Z

Written by Magnus Deininger ().