#!/usr/bin/perl -T # Written by Da Beave one bored afternoon. [08/12/2005] # use Asterisk::AGI; # We hardly need ye at all my $answerfile="/var/lib/asterisk/sounds/answers.txt"; # This text file gives the answer:file name. For example. # # 1:question-one # # One is the correct answer, question-one is the wav/gsm/whatever played. # Pretty simple, eh? my $count; # used for random/count of the $answerfile my $questionnum; # for the random question my $answer; # the correct answer my $results; # what the user entered. my $AGI = new Asterisk::AGI; $|=1; # Setup some variables. Actually, we don't really need this... we don't # lookup channel info.. whatever.... Useless code... my %AGI; my $tests = 0; my $fail = 0; my $pass = 0; while() { chomp; last unless length($_); if (/^agi_(\w+)\:\s+(.*)$/) { $AGI{$1} = $2; } } # First, get the number of lines in the $answers file. Used for random # later. if (!open(ANSWER, $answerfile)) { die "Can't open answers file"; } while () { chomp; s/#.*//; s/^\s+//; s/\s+$//; next unless length; $count=$count+1; } close(ANSWER); # Got a $count of the $resultsfile.. Now, randomly pick a question. $questionnum = int(rand $count) + 1; # Grab a random question. $count=0; # reset count for re-use. if (!open(ANSWER, $answerfile)) { die "Can't open answers file"; } while () { chomp; s/#.*//; s/^\s+//; s/\s+$//; next unless length; ($answer, $questionfile) = split /:/; $count=$count+1; if ($count == $questionnum) { # Explain what the hell this is all about. Keep in mind, the # user can '#' outta this message to get directly to the question. $AGI->exec('Background', 'question-explain'); # Play the random file (we already know the answer). We give them # a bunch of time to ask on IRC or whatever... (hence the # 100000ms pause). The "10" is max digits. $result = $AGI->get_data($questionfile, "100000", "10"); # Is the answer correct, or incorrect? if ($result eq $answer) { $AGI->exec('Background', 'question-correct'); exit 0; # Correct, now continue forward.... } else { $AGI->exec('Background', 'question-incorrect'); $AGI->hangup(); exit 0; # Incorrect, you n00b. } } } # End of story.