Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

CGI Chat In 19 lines

by mt2k (Hermit)
on Jun 10, 2002 at 04:40 UTC ( [id://173033]=perlcraft: print w/replies, xml ) Need Help??

   1: Okay yes, this is very ugly. I had it kind of nice and then
   2: I decided to go all out and compact it a lot. So here is a 
   3: fully-functional CGI script that allows anyone to come in, 
   4: enter any name they want (yes, even multiple instances of 
   5: the same nick are allowed) and chat together. It does use 
   6: two small pieces of javascript: one to refresh the chat 
   7: messages (because inserting that meta tag in the HTML 
   8: headers would have taken more space) and one that sets focus
   9:  to the chat box when you put your mouse over it.
  10: 
  11: It doesn't really fit in the category of obfu, but it will
  12: look better here than anywhere else. Code is for reusable 
  13: stuff, same with Snippets. I don't think anyone'll want to 
  14: use this!
  15: 
  16: Features of the program? Only one: URL's are turned into 
  17: links ONLY when used in the perlmonks way (using square 
  18: brackets). And even then, it must be a URL between the 
  19: brackets and nothing else. No vertical bar to name the 
  20: link!
  21: 
  22: So here it is. All in 19 lines:<br>
  23: 
  24: 
  25: 
  26: #!c:/perl/bin/perl
  27: use strict;use CGI':Vars';use Data::Dumper;use Fcntl':flock';
  28: use IO::Scalar;my%i=CGI::Vars;my($a,$b,$d,$f,$r,$s,$g)=($ENV{
  29: 'SCRIPT_NAME'},$i{a},$i{u},$i{m});open L,">2.2"or die;flock L
  30: ,LOCK_EX or die;our%c;do"1.1";$.=new IO::Scalar \$s;select$.;unless(
  31: $b){print qq{<frameset rows="*,75"><frame src=$a?a=e><frame src=
  32: $a?a=c&u=$d></frameset>};}if($b eq"e"){print qq{<script>
  33: setTimeout('history.go(0)',8000);</script>};my$e;while(++$e){last if!
  34: $c{$e};print qq{<strong>$c{$e}[0]</strong>: $c{$e}[1]<br>}if$c{$e};}}
  35: if($b eq"c"){if(!$d){print qq{<form action=$a><input type=hidden name=a
  36: value=c>Nickname:<input type=text name=u><input type=submit value=Start>
  37: </form>};++$g;}if($f){$f=~s/</\&lt;/g;$f=~s/\[(.*?)\]/<a href='$1'
  38: target=_blank>$1<\/a>/g;$c{++$c{n}}=[$d,$f];delete$c{$c{n}-15};}if(!$g){
  39: print qq{<form action=$a><input type=hidden name=u value="$d"><input
  40: type=hidden name=a value=c>Message:<input type=text name=m size=50
  41: onmouseover=focus()><input type=submit value=Chat></form>};}}open A,">1.1"
  42: or die;flock A,LOCK_EX or die;print A Data::Dumper->Dump([\%c], ['*c']);
  43: print$b?"</body></html>":"</html>";select STDOUT;
  44: print"Content-type: text/html\n\n";print$b?"<html><body>":"<html>";print$s;

Replies are listed 'Best First'.
Re: CGI Chat In 19 lines
by cjf (Parson) on Jun 10, 2002 at 12:08 UTC

    If this is just compacted code and there's nothing really obfuscated about it, wouldn't it be better to add a few spaces and place it in The Code Catacombs?

      Amen. I can think of several uses, with a modicum of whitespace and a comment or two...
Re: CGI Chat In 19 lines
by epoptai (Curate) on Jun 11, 2002 at 02:59 UTC
    Very nice mt2k. One thing though, the javascript refresh doesn't work on mozilla. Replacing that code with a meta tag fixes it (and it's the exact same number of characters as the javascript was):
    print qq{<script>setTimeout('history.go(0)',8000);</script>}; #replace + this print qq{<meta http-equiv="refresh" content="8;url=$a?a=e">}; #with th +is

    --
    Check out my Perlmonks Related Scripts like framechat, reputer, and xNN.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-23 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found