10 my $requestedcoinid = $cgi->param('coinid');
 
  11 my $requestedaction = $cgi->param('action');
 
  13 my $flippedcoinsindb=0;
 
  16     print "<b>ERROR:</b> @_[0]\n";
 
  20     print "<a href=/@_[0]>";
 
  21     print "<font style=\"BACKGROUND-COLOR: yellow\" color=blue><b>";
 
  22     print "http://$ENV{HTTP_HOST}/@_[0]";
 
  36 if (-e "var/coins.db") {
 
  37     tie (%coins_db, DB_File, "var/coins.db") ||
 
  38         die ("Cannot open var/coins.db");
 
  41     tie (%coins_db, DB_File, "var/coins.db", O_CREAT|O_RDWR, 0640) ||
 
  42         die ("Cannot create or open var/coins.db");
 
  45 foreach my $key ( keys %coins_db )
 
  47     @value = split(/,/, $coins_db{$key});
 
  48     if ( time-$value[0] > $COINLIFETIME*(60*60*24) ) {
 
  49         delete $coins_db{$key};
 
  53     if ( ($#value + 1) == 3 ) {
 
  58 print "Content-type: text/html\n\n";
 
  61 print "<title>flipacoin.org - The Internet's first coinflipping service.</title>";
 
  65 if ( ! ($requestedcoinid =~ /\A[a-z0-9]*\z/) ) {
 
  66     alert("You sent me an invalid coin!");
 
  70 if ( ! ($requestedaction =~ /\A[a-z]*\z/) ) {
 
  71     alert("You sent me an invalid action!");
 
  75 print "<font size=1>During the last $COINLIFETIME days $flippedcoinsindb/$coinsindb coins were flipped/created. | <a href='http://git.treefish.org/wirbrennen/flipacoin.git' target='_blank'>git</a></font>";
 
  77 print "<h3>flipacoin.org - The Internet's first coinflipping service.</h3>";
 
  79 if ( length($requestedcoinid) == 0 && $requestedaction eq "" ) {
 
  80     print "<a href=/?action=create><font color=blue>[Create a coin]</font></a>";
 
  81     print " to make a decision.";
 
  83 elsif ( length($requestedcoinid) == 0 && $requestedaction eq "create" ) {
 
  84     my @chars = ("a".."z", "0".."9");
 
  86     $newcoinid .= $chars[rand @chars] for 1..13;
 
  87     while ( exists $coins_db{$newcoinid} ) {
 
  89         $newcoinid .= $chars[rand @chars] for 1..13;
 
  92     $coins_db{$newcoinid} = time;
 
  94     print "Created a new coin ";
 
  95     printcoin($newcoinid);
 
  96     print " for you.<br>";
 
  98     print "Send the coin to the other party and tell them to flip it to make a decision.";
 
 100 elsif ( length($requestedcoinid) > 0 ) {
 
 101     if ( ! exists $coins_db{$requestedcoinid} ) {
 
 102         alert("The coinid $requestedcoinid does not exist!");
 
 106     my @coininfo = split(/,/, $coins_db{$requestedcoinid});
 
 107     my $coininfosize = $#coininfo + 1;
 
 109     printcoin($requestedcoinid);
 
 112     print "<b>Creation time:</b> " . localtime($coininfo[0]);
 
 115     if ( $coininfosize == 1 && $requestedaction eq "" ) {
 
 116         print "<b>Status:</b> Not yet flipped ";
 
 117         print "<a href=/$requestedcoinid?action=flip>";
 
 118         print "<font color=blue>[flip it]</font>";
 
 121     elsif ( $coininfosize == 1 && $requestedaction eq "flip" ) {
 
 122         $result = int(rand(2));
 
 123         $coins_db{$requestedcoinid} = $coins_db{$requestedcoinid} .
 
 126         print "<b>Status:</b> Just flipped";
 
 128         print "<b>Result:</b> ";
 
 129         printresult($result);
 
 131     elsif ( $coininfosize == 3 ) {
 
 132         print "<b>Status:</b> Flipped @ " . localtime($coininfo[1]);
 
 134         print "<b>Result:</b> ";
 
 135         printresult($coininfo[2]);