File:  [Local Repository] / chik / chik.cgi
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Wed Nov 1 04:06:49 2000 UTC (23 years, 8 months ago) by boris
Branches: MAIN
CVS tags: HEAD
Wrote a working version.

    1: #!/usr/bin/perl
    2: 
    3: =head1 NAME
    4: 
    5: chik.cgi - a robot for competitions with separate starts
    6: 
    7: =head1 DESCRIPTION
    8: 
    9: The robot is a Web CGI interface. It must authentificate
   10: the team. If the regnumber and password are corrrect, it sends
   11: a message to the team's email and demonstrates the questions on
   12: the web. Also, it updates the field "StartTime" and sends message
   13: to the secretary "Team NN started at I<Date>"
   14: 
   15: =head1 USES
   16: 
   17: MySQL table Chik with the fields:
   18:    CREATE TABLE Chik (RegNum Int NOT NULL, 
   19:                       Name TINYTEXT, 
   20:                       Password TINYTEXT,
   21:                       Email TINYTEXT, 
   22:                       Started ENUM('Y','N') DEFAULT 'N', 
   23:                       StartDate TIMESTAMP(14), Unique(RegNum));
   24: 
   25: It expects to find the file F<../../cfg/chik/questions.cfg>, with 
   26: the questions, <F<../../cfg/chik/h.cfg> with the headers and
   27: F<../../cfg/chik/f.cfg> with the footers.
   28: 
   29: 
   30: =head1 AUTHOR
   31: 
   32: Boris Veytsman
   33: 
   34: =head1 DATE
   35: 
   36: $Date: 2000/11/01 04:06:49 $
   37: 
   38: =head1 VERSION
   39: 
   40: $Revision: 1.2 $
   41: 
   42: =cut
   43: 
   44: use strict;
   45: use CGI qw/:standard/;
   46: use DBI;
   47: 
   48: my $secretary="elir\@immisrael.com";
   49: my $SENDMAIL = "/usr/sbin/sendmail";
   50: my $questions = "../../cfg/chik/q.txt";
   51: my $header = "../../cfg/chik/h.html";
   52: my $footer = "../../cfg/f.html";
   53: 
   54: my $query=new CGI;
   55: 
   56: print $query->header;
   57: print $query->start_html(-title=>'Robot turnira CHIK',-bgcolor=>'#fff0e0');
   58: print Include_virtual("../dimrub/db/reklama.html");
   59: print $query->h1({'-align'=>'center'},'Робот турнира ЧИК');
   60: 
   61: open(HEADER,$header);
   62: print <HEADER>;
   63: close HEADER;
   64: 
   65: print "<p> Сейчас на куличках ", `date`," </p>\n";
   66: 
   67: if ($query->param('Look')) {
   68:     print_list($query);
   69:     $query->delete('Look');
   70: } elsif ($query->param('Start')) {
   71:     $query->delete('Start');
   72:     print_questions($query);
   73: }
   74: 
   75: print_query($query);
   76: 
   77: open(FOOTER,$footer);
   78: print <FOOTER>;
   79: close FOOTER;
   80: 
   81: 
   82: print $query->end_html;
   83: 
   84: 
   85: exit 0;
   86: 
   87: 
   88: sub Include_virtual {
   89:     my ($fn, $output) = (@_, '');
   90: 
   91:     open F , $fn
   92: 	or return; #die "Can't open the file $fn: $!\n";
   93: 	
   94:     while (<F>) {
   95: 	if (/<!--#include/o) {
   96: 	    s/<!--#include virtual="\/(.*)" -->/&Include_virtual($1)/e;
   97: 	}
   98: 	if (/<!--#exec/o) {
   99: 	    s/<!--#exec.*cmd\s*=\s*"([^"]*)".*-->/`$1`/e;
  100: 	}
  101: 	$output .= $_;
  102:     }
  103:     return $output;
  104: }
  105: 
  106: 
  107: 
  108: sub print_query {
  109:     my	$query = shift;
  110:     print $query->start_form;
  111:     print $query->h2("Начать игру");
  112:     print "<p>\n";
  113:     print "Рег. номер: ", 
  114:     $query->textfield('regnum'), " ",
  115:     "Пароль: ",$query->password_field('password'), "</p>";
  116:     print "<p>", $query->submit("Start","Start"), " ";
  117:     print $query->defaults('Reset'),"</p>\n";
  118:     print $query->h2("Посмотреть, кто играет");
  119:     print "<p>", $query->submit("Look","Look"), "</p>";
  120:     print $query->end_form;
  121: }
  122: 
  123: 
  124: 
  125: sub print_list {
  126:     my $query = shift;
  127: 
  128:     my $dbh = DBI->connect("DBI:mysql:chgk", "piataev", "")
  129: 	or do {
  130: 	    print h1("Временные проблемы") . "Робот временно не
  131: 			работает. Заходите попозже.";
  132: 	    return 0;
  133: 	};
  134:     my $sth=$dbh->prepare("SELECT RegNum, Name, StartDate FROM
  135:                            Chik WHERE Started='Y' Order by StartDate");
  136:     $sth->execute;
  137:     print h3("Начали игру:");
  138:     print "<table>\n";
  139:     print "<tr><th>Номер</th><th>Команда</th><th>Начало игры</th></tr>\n";
  140:     while (my @line=$sth->fetchrow) {
  141: 	my $date=pop @line;
  142: 	$date=convert_date($date);
  143: 	push @line,$date;
  144: 	print_line(@line);
  145:     }
  146:     print "</table>\n";
  147:     $sth=$dbh->prepare("SELECT RegNum, Name FROM
  148:                               Chik WHERE Started='N' Order by RegNum");
  149:     $sth->execute;
  150:     print h3("Не начали игру:");
  151:     print "<table>\n";
  152:     print "<tr><th>Номер</th><th>Команда</th></tr>\n";
  153:     while (my @line=$sth->fetchrow) {
  154: 	print_line(@line);
  155:     }
  156:     print "</table>\n";
  157:     
  158:     $dbh->disconnect;
  159:     return 0;
  160:     
  161: }
  162: 
  163: 
  164: sub print_line {
  165:     print "<tr><td>",join("</td><td>",@_),"</td></tr>\n";
  166: }
  167:     
  168:      
  169: sub print_questions {
  170:     my $query = shift;
  171:     my $regnum=$query->param('regnum');
  172:     my $password=$query->param('password');
  173:     my $dbh = DBI->connect("DBI:mysql:chgk", "piataev", "")
  174: 	or do {
  175: 	    print h1("Временные проблемы") . "Робот временно не
  176: 			работает. Заходите попозже.";
  177: 	    return 1;
  178: 	};
  179:     $regnum=$dbh->quote($regnum);
  180:     $password=$dbh->quote($password);
  181:     my $sth=$dbh->prepare("SELECT Name, Email, Started, StartDate FROM
  182:                            Chik WHERE RegNum=$regnum 
  183:                            AND Password=$password");
  184:     $sth->execute;
  185: 
  186:     my @result=$sth->fetchrow;
  187:     if (!scalar @result) {
  188: 	print "<p><strong>Пароль неверен. Попробуйте еще раз.</p>\n";
  189: 	return 1;
  190:     }
  191:     my ($name,$email,$started,$startdate)=@result;
  192:     $startdate=convert_date($startdate);
  193:     if ($started eq 'Y') {
  194: 	print "<p>Ваша команда уже начала играть в $startdate</p>\n";
  195:     } else {
  196: 	$sth=$dbh->prepare("UPDATE  Chik SET Started='Y'
  197:                        WHERE RegNum=$regnum");
  198: 	$sth->execute;
  199: 	open(F, "| $SENDMAIL -t -n" );
  200: 	print F <<"EOT";
  201: To: $secretary
  202: From: boris\@plmsc.psu.edu
  203: Subject: Chik: komanda nachala igrat
  204: MIME-Version: 1.0
  205: Content-type: text/plain; charset="koi8-r"
  206: 
  207: EOT
  208:         print F "Команда $name, регистрационный номер $regnum ",
  209:     "начала играть в $startdate\n";
  210: 	print F "\n\n--\nРобот\n";
  211: 	close F;
  212: 	open(F, "| $SENDMAIL -t -n" );
  213: 	print F <<"EOT";
  214: To: $email
  215: From: boris\@plmsc.psu.edu
  216: Subject: Voprosy CHIK
  217: MIME-Version: 1.0
  218: Content-type: text/plain; charset="koi8-r"
  219: 
  220: -------------------------------------------------------------
  221: EOT
  222: 
  223:         open(QUEST,$questions);
  224: 	
  225: 	print F <QUEST>;
  226: 	print F <<"EOT";
  227: 
  228: -------------------------------------------------------------
  229: EOT
  230: 	print F "\n\n--\nРобот\n";
  231: 	close F;
  232: 	close (QUEST);
  233:     }
  234:     open(QUEST,$questions);
  235:     print "<hr>\n<pre>\n";
  236:     print <QUEST>;
  237:     print "</pre>\n<hr>\n";
  238:     return 0;
  239: }
  240: 
  241: sub convert_date {
  242:     my $date=shift;
  243:     my $year=substr($date,0,4);
  244:     my $month=substr($date,4,2);
  245:     my $day=substr($date,6,2);
  246:     my $hour=substr($date,8,2);
  247:     my $min=substr($date,10,2);
  248:     my $sec=substr($date,12,2);
  249:     return "$year-$month-$day $hour:$min:$sec";
  250: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>