This is a simple and easy to use prototype function that is based on the principle of command flags. Simply put, it evaluates certain conditions in a certain order and executes an action when they are true.
It is a pretty clear and easy to understand system of how to use flags, and the comments i've added will make sure the dumbest of people could understand how it works. If you don't, i feel sorry for you.
Check out the two versions in the attachments below. One is for Justice and the other for the SVN of Sauerbraten. The SVN version has one extra flag, explains the use of the command 'local' and a slight change in the 'loopchar' command which is fundamental for its operation. Oh fun!
NOTE: You'll have to excuse my zipping of the files. I cannot change that setting on quadro without messing up the file upload limit.