home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / 05_02.iso / software / mhtmls / MagicHTMLStudio.exe / Disk1 / data1.cab / Program_Files / CGI / Chat / chat.cgi < prev   
Encoding:
Text File  |  2002-03-06  |  7.5 KB  |  295 lines

  1. #!/usr/bin/perl
  2. $chat_name = "My Chat";
  3. $update = "10";
  4. $chat_window_bg = "#ffffff";
  5. $options_window_bg = "#ffffff";
  6. $show_mes = 30;
  7. $killtags = 1;
  8.  
  9. $temp=$ENV{'QUERY_STRING'};
  10. @pairs=split(/&/,$temp);
  11. foreach $item(@pairs) {
  12.     ($key,$content)=split (/=/,$item,2);
  13.     $content=~tr/+/ /;
  14.     $content=~ s/%(..)/pack("c",hex($1))/ge;
  15.     $FORM{$key}=$content;
  16. }
  17.  
  18. if($killtags eq 1) {
  19.     $FORM{'chat'} =~ s/</</g;
  20.     $FORM{'chat'} =~ s/>/>/g;
  21.     $FORM{'nick'} =~ s/</</g;
  22.     $FORM{'nick'} =~ s/>/>/g;
  23. }
  24.  
  25. $timeout = time;
  26.  
  27. open (LOG,"+<log.txt");
  28. flock(LOG,2);
  29. @log = <LOG>;
  30.  
  31. foreach $log (@log) {
  32.     @log_params = split(/\|\|/,$log);
  33.     $timelog = $timeout - $log_params[2];
  34.     if ($timelog > 150) {
  35.         $log = "";
  36.         open(CHAT,"chat.txt");
  37.         flock(CHAT,2);
  38.         @chat = <CHAT>;
  39.         close(CHAT);
  40.         $str = "||System message||black||<i>$log_params[1] φas quitted from chat.</i>||\n";
  41.         push(@chat,$str);
  42.         $chat_many = @chat;
  43.         if ($chat_many > 99) {$chat[0] = "";}
  44.  
  45.         open(CHAT,">chat.txt");
  46.         flock(CHAT,2);
  47.         print CHAT @chat;
  48.         close(CHAT);
  49.     }
  50.     $a++;
  51. }
  52.  
  53. seek (LOG, 0, 0);
  54. truncate (LOG,0);
  55. print LOG @log;
  56. close (LOG);
  57.  
  58. $link = "<a href=http://www.agfreesoft.com</a>";
  59.  
  60. if($ENV{'QUERY_STRING'} eq '') {&login;}
  61. elsif($FORM{'action'} eq 'enter') {&enter;}
  62. elsif($FORM{'action'} eq 'chat') {&chat;}
  63. elsif($FORM{'action'} eq 'options') {&options;}
  64.  
  65. sub login {
  66.  
  67.     print "Content-Type: text/html\n\n";
  68.     print "<html>\n";
  69.     print "<head>\n";
  70.     print "<title>$chat_name</title>\n";
  71.     print "<style>\n";
  72.     print "INPUT, SELECT, BODY, TD {font-family: Tahoma; font-size: 11}\n";
  73.     print "</style>\n";
  74.     print "</head>\n";
  75.     print "<body>\n";
  76.     print "<center>\n";
  77.     print "<font face=Tahoma size=2>Please, enter yours nickname enter a chat $chat_name.\n";
  78.     print "<table>\n";
  79.     print "<form action=chat.cgi method=GET>\n";
  80.     print "<tr><td>Nickname:</td><td><input type=hidden name=action value=enter><input type=text 
  81. name=nick size=10></td></tr>\n";
  82.     print "<tr><td>Color:</td><td><select name=color>";
  83.     print "<option value=black>black</option>";
  84.     print "<option value=green>green</option>";
  85.     print "<option value=yellow>yellow</option>";
  86.     print "<option value=blue>blue</option>";
  87.     print "<option value=red>red</option>";
  88.     print "</select></td></tr>";
  89.     print "<tr><td colspan=2><input type=submit value=Enter></td></tr>\n";
  90.     print "</form></table></font>\n";
  91.     print "</center>\n";
  92.     print "</body>\n";
  93.     print "</html>\n";
  94.  
  95.     exit;
  96.  
  97. }
  98.  
  99. sub enter {
  100.  
  101.     if ($FORM{'nick'} eq '') { &error('You have not entered nickname.'); }
  102.     $nick = $FORM{'nick'};
  103.     $time = time;
  104.  
  105.     open(LOG,"log.txt");
  106.     @log = <LOG>;
  107.     foreach $log (@log) {
  108.         if($log =~ /^\|\|$nick\|\|/) {
  109.             $found = 1;
  110.         }
  111.     }
  112.     close(LOG);
  113.     if (!($found)) {
  114.         open(LOG,">>log.txt");
  115.         print LOG "||$nick||$time||\n";
  116.         close(LOG);
  117.         open(CHAT,"chat.txt");
  118.         flock(CHAT,2);
  119.         @chat = <CHAT>;
  120.         close(CHAT);
  121.         $str = "||System message||black||<i>$FORM{'nick'} φas quitted from chat.</i>||\n";
  122.         push(@chat,$str);
  123.         $chat_many = @chat;
  124.         if ($chat_many > 99) {$chat[0] = "";}
  125.  
  126.         open(CHAT,">chat.txt");
  127.         flock(CHAT,2);
  128.         print CHAT @chat;
  129.         close(CHAT);
  130.     }
  131.  
  132.     print "Content-Type: text/html\n\n";
  133.     print "<html>\n";
  134.     print "<head>\n";
  135.     print "<title>$chat_name</title>\n";
  136.     print "</head>\n";
  137.     print "<frameset rows=\"*,70\" border=0 frameborder=0>\n";
  138.     print "<frame name=\"chat\" noresize src=\"chat.cgi?nick=$nick&action=chat\">\n";
  139.     print "<frame name=\"options\" src=\"chat.cgi?nick=$nick&action=options&color=$FORM{'color'}\">\n";
  140.     print "<noframes>\n";
  141.     print "<body>\n";
  142.     print "<font face=Tahoma size=2>Unfortunately your browser does not support frames, necessary for 
  143. dialogue.</font>\n";
  144.     print "</body>\n";
  145.     print "</noframes>\n";
  146.     print "</frameset>\n";
  147.     print "</html>\n";
  148.  
  149.     exit;
  150.  
  151. }
  152.  
  153. sub chat {
  154.  
  155.     &update;
  156.     open(LOG,"log.txt");
  157.     @members = <LOG>;
  158.     close(LOG);
  159.  
  160.     foreach $member (@members) {
  161.         @member_params = split(/\|\|/,$member);
  162.         push(@online,$member_params[1]);
  163.     }
  164.  
  165.     open(CHAT,"chat.txt");
  166.     @chat = <CHAT>;
  167.     close(CHAT);
  168.  
  169.     @chat = reverse @chat;
  170.  
  171.     $end = $show_mes - 1;
  172.     $end = @chat - 1 if scalar(@chat) < 30;
  173.  
  174.     foreach $chat (@chat[0..$end]) {
  175.         @chat_params = split(/\|\|/,$chat);
  176.         $str = "$chat_params[1]: <font color=$chat_params[2]>$chat_params[3]</font>";
  177.         push(@text_chat,$str);
  178.     }
  179.  
  180.     print "Content-Type: text/html\n\n";
  181.     print "<html>\n";
  182.     print "<head>\n";
  183.     print "<meta http-equiv=\"Refresh\" CONTENT=\"$update\; 
  184. URL=chat.cgi?action=chat\&nick=$FORM{'nick'}\">";
  185.     print "<style>\n";
  186.     print "BODY {font-family: Tahoma; font-size: 12}\n";
  187.     print "</style>\n";
  188.     print "</head>\n";
  189.     print "<body bgcolor=$chat_window_bg>\n";
  190.     print "<b>On-line: ";
  191.     foreach (@online) {
  192.         print "$_, ";
  193.     }
  194.     print "</b><br>\n";
  195.     print "<br>\n";
  196.     foreach $str (@text_chat) {
  197.         print "$str\n<br>\n";
  198.     }
  199.     print "</body>\n";
  200.     print "</html>\n";
  201.  
  202.     exit;
  203.  
  204. }
  205.  
  206. sub options {
  207.  
  208.     &update;
  209.     if($FORM{'chat'} ne '') {
  210.         open(CHAT,"chat.txt");
  211.         flock(CHAT,2);
  212.         @chat = <CHAT>;
  213.         close(CHAT);
  214.         $str = "||$FORM{'nick'}||$FORM{'color'}||$FORM{'chat'}||\n";
  215.         push(@chat,$str);
  216.         $chat_many = @chat;
  217.         if ($chat_many > 99) {$chat[0] = "";}
  218.  
  219.         open(CHAT,">chat.txt");
  220.         flock(CHAT,2);
  221.         print CHAT @chat;
  222.         close(CHAT);
  223.     }
  224.  
  225.     print "Content-Type: text/html\n\n";
  226.     print "<html>\n";
  227.     print "<head>\n";
  228.     print "<style>\n";
  229.     print "INPUT, SELECT {font-family: Tahoma; font-size: 11}\n";
  230.     print "</style>\n";
  231.     print "</head>\n";
  232.     print "<body bgcolor=$options_window_bg>\n";
  233.     print "<center>\n";
  234.     print "<form action=chat.cgi method=GET>\n";
  235.     print "<input type=hidden name=action value=options>";
  236.     print "<input type=hidden name=nick value=$FORM{'nick'}>";
  237.     print "<input type=text name=chat size=45>";
  238.     print "<input type=hidden name=color value=$FORM{'color'}>";
  239.     print "<input type=submit value=Tell><br>$link\n";
  240.     print "</form></font>\n";
  241.     print "</center>\n";
  242.     print "</body>\n";
  243.     print "</html>\n";
  244.  
  245.     exit;
  246.  
  247. }
  248.  
  249. sub update {
  250.  
  251.     open (LOG,"+<log.txt");
  252.     flock(LOG,2);
  253.     @log = <LOG>;
  254.  
  255.     $a=0;
  256.     foreach (@log) {
  257.         @log_params = split(/\|\|/,$log[$a]);
  258.         if ($log_params[1] eq $FORM{'nick'}) {
  259.             $log_params[2] = time;
  260.             $log[$a] = join("\|\|",@log_params);
  261.             }
  262.         $a++;
  263.         }
  264.  
  265.     seek (LOG, 0, 0);
  266.     truncate (LOG,0);
  267.     print LOG @log;
  268.     close (LOG);
  269.  
  270. }
  271.  
  272. sub error {
  273.  
  274. $err = $_[0];
  275.  
  276.     print "Content-Type: text/html\n\n";
  277.     print "<html>\n";
  278.     print "<head>\n";
  279.     print "<title>$chat_name error</title>\n";
  280.     print "<style>\n";
  281.     print "BODY {font-family: Tahoma; font-size: 11}\n";
  282.     print "</style>\n";
  283.     print "</head>\n";
  284.     print "<body>\n";
  285.     print "<center>\n";
  286.     print "<font size=2 face=Tahoma>$err</font>\n";
  287.     print "</center>\n";
  288.     print "</body>\n";
  289.     print "</html>\n";
  290.  
  291.     exit;
  292.  
  293. }
  294.  
  295.