#!/usr/bin/perl -w
# cgi-irc v0.2 - http://dgl.cx/irc/
# (C) 1999-2000 David Leadbeater (cgiirc@dgl.cx)
# cgi-irc comes with ABSOLUTELY NO WARRANTY
# This is free software, and you are welcome to redistribute it
# under certain conditions; read the file COPYING for details
use strict;
my $version="0.2";
use vars qw($channel $say $rand);
use CGI;

# You may want to change some of these
my $default_server="efnet.demon.co.uk";
my $default_port="6667";
my $default_nick="cgi-irc";
my $default_name="$default_nick";
my $default_channels="";
# Change this if you have a differnt temp dir
my $tmpfile_prefix="/tmp/cgiirc-";
# Change if you change the name of the jirc script
my $irc_script="jirc.cgi";
my $image="http://dgl.cx/irc/cgiirc.jpg";

my $q = new CGI;
print $q->header();
if(!$q->param('action')){
   print <<EOF;
<html><head>
<title>CGI IRC - Login</title>
</head><body bgcolor="#ffffff" text="#000000">
<p align="center"><a href="http://dgl.cx/irc/"><img src="$image" border=0></a><br>Release $version</p>
<form method="post" action="$ENV{SCRIPT_NAME}">
<input type="hidden" name="action" value="load">
<label>Server:</label><input type="text" name="server" value="$default_server"><br>
<label>Port:</label><input type="text" name="port" value="$default_port"><br>
<label>Nick:</label><input type="text" name="nick" value="$default_nick"><br>
<label>Realname:</label><input type="text" name="name" value="$default_name"><br>
<label>Channels</label><input type="text" name="channels" value="$default_channels"><br>
<input type="submit" value=" Login ">
</form>
<br>
<b>Basic Usage</b><br>
Set the settings above for your irc server (<a href="http://www.irchelp.org/">help</a>), names and channels, these are the channels you want to join on startup, start them with the # and seperate them with spaces.<br>
To say something select the channel in the select box and click say (or press return) there are also several commands avalible, these are:<br>
<code>/msg nick text...</code><br>
<code>/whois nick</code><br>
<code>/leave [#channel]</code> (no option leaves the default channel)<br>
<code>/join #channel</code><br>
<code>/nick newnick</code><br>
<code>/me text...</code> (emotes the text)<br>
<code>/quit message</code> (quits with optional message)<br>
</body></html>
EOF
}elsif($q->param('action') eq "load"){
   $rand=rand_text(12);
   until(!-e $tmpfile_prefix.$rand){$rand=rand_text(12)}
   my $tmp=$q->param('nick');
   print <<EOF;
<html>
   <head>
   <title>CGI IRC - $tmp</title>
   </head>
   <frameset rows="*,100">
EOF
print "<frame name=\"irc\" src=\"$irc_script?rand=$rand&server=",$q->escape($q->param('server')),"&port=",$q->escape($q->param('port')),"&nick=",$q->escape($q->param('nick')),"&name=",$q->escape($q->param('name')),"&channels=",$q->escape($q->param('channels')),"\">";
print "<frame name=\"form\" src=\"$ENV{SCRIPT_NAME}?rand=$rand&action=form&server=",$q->escape($q->param('server')),"&port=",$q->escape($q->param('port')),"&nick=",$q->escape($q->param('nick')),"&name=",$q->escape($q->param('name')),"&channels=",$q->escape($q->param('channels')),"\">";
print <<EOF;
<noframes>
   <body>
   Sorry this script cannot be used without frames, please get a frames complatable browser like Netscape, Mozilla or Internet Explorer<br>
   Athough if you are really desperate you could open each of the frames in a seperate window or terminal and use it like that...<br>
   </body>
</noframes>
</frameset>
</html>
EOF
}elsif($q->param('action') eq "form"){
   $rand=$q->param('rand');
   &print_form;
}elsif($q->param('action') eq "command"){
   $rand=$q->param('rand');
   $rand=~ s/[^a-z,0-9]//g;
   if($channel=$q->param('channel')){
	  $say=$q->param('say');
	  $say =~ s/\n//g;
	  $channel =~ s/\n//g;
	  open(ADD, ">>$tmpfile_prefix$rand");
	  if($channel ne $q->param('chan-default')){
		 print ADD "/chan $channel\n";
	  }
	  if($say){
		 print ADD "$say\n";
	  }
	  close(ADD);
   }
   &print_form($channel);
}else{
   print "Error unknown action";
}

sub print_form{
   my $channel=shift;
   print <<EOF;
<html><head><title>Form</title></head>
<body bgcolor="#ffffff"><form action="$ENV{SCRIPT_NAME}" method="post">
<input type="hidden" name="action" value="command">
<input type="hidden" name="chan-default" value="$channel">
<input type="hidden" name="rand" value="$rand">
<select name="channel">
EOF
my $file=$tmpfile_prefix."chan-".$rand;
open(CHAN, "$file");
my $chan=<CHAN>;
chomp($chan);
close(CHAN);
my @split=split(/ /,$chan);
foreach my $split(@split){
   next if $split eq "";
   my $default;
   if($channel eq $split){$default=" SELECTED";}
   print "<option name=\"$split\"$default>$split</option>";
   $default="";
}
print <<EOF;
</select>
<input type="text" name="say" size="70"><input type="submit" value=" Say ">
</form>
</body></html>
EOF
}

sub rand_text{
   my $num=shift;
   my ($lid,$i);
   my @rand=('a'..'z', '0'..'9');
   for($i=0;$i < $num;$i++){$lid.=$rand[rand()*($#rand+1)]}
   return($lid);
}
