#!/usr/local/bin/lua -- fortune.lua. -- Generates a signature randomly from a single file of witty quotes which -- the user maintains; the quotes can be multi-line, and are separated by -- lines containing only a percent sign (same format as fortune files). -- A direct translation of the Python version (rewrite from ESR's Perl -- version), so is not ``Lua-optimal''. 14 May 2000. -- Rationale: I hate waiting five to ten seconds on a loaded system for -- a Python or Perl interpreter to load up before writing a message. -- Lua loads and executes in negligible time. -- Added long signature check which will omit the constant tagline if -- the signature is over two lines long. 3 September 2000. -- Updated to use `break', new in Lua 4.0. 25 September 2000. -- Added %include FILENAME directive. 12 December 2000. -- Insert your constant tagline here. sigline = 'tjaden@users.sourceforge.net - http://www.alphalink.com.au/~tjaden/\n' -- Point to your sigfile. fortunes = '/home/tjaden/.randsig' -- Functions. function readfile (filename) local f = readfrom (filename) if not f then return end while 1 do line = read (f, "*l") if not line then break end if strsub (line, 1, 9) == "%include " then readfile (strsub (line, 10)) elseif line ~= "%" then if sig[x] then sig[x] = sig[x] .. "\n" .. line else sig[x] = line end else x = x + 1 end end end function lines (s) local _, i = gsub (s, "\n", "\n") return i + 1 end -- Main. sig = {}; x = 1 readfile (fortunes) randomseed (date ("%s")) s = sig[random (x)] if lines (s) < 3 then print (sigline .. s) else print (s) end -- fortune.lua ends here