#!/usr/bin/perl

# Edit the above line if you must to locate the perl interpreter on your server
# this file should have suitable permissions so it can be invoked by the web server
# (compare with other scripts in your cgi-bin directory)

# join0.rwc -- a Perl script for tuning Roger Wilco (RW Mark I or later, rwbs version 0.26 or later)

# This script should reside in the cgi-bin directory for your web server
# it is accessed via a web link and dynamically creates output to simulate a
# .RWC file with contents to suit the specified parameters (RWBS address and optional
# channel name and password)
  
# File version:  1    Aug 26 1999

# usage:  link to it from HTML, and supply the following parameters:
# rwbs      the IP address or hostname of the RWBS you wish to connect to (required)
# channel   the name of the channel you wish to connect to (optional -- default channel assumed)
# passwd    the password to be used (optional)

# e.g.:  <a href="http://yoursite.com/cgi-bin/join0.rwc?rwbs=mybox.mysite.com&channel=attack">click he
re</a>
    
# This script is provided on an as-is basis by Resounding Technology
# Copyright 1999, Resounding Technology, Inc.

use POSIX;

sub CGIReadParse {
    local (*in) = @_ if @_; 
    local ($i, $loc, $key, $val, $ispath);

    $ispath = 0;
    # Read in text
    if ($ENV{'REQUEST_METHOD'} eq "GET") {
        $in = $ENV{'QUERY_STRING'};
        if (!$in) {
            $in = $ENV{'PATH_INFO'}; 
            $in =~ s/^.(.*)$/$1/;
            $ispath = 1;
        }
    } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
        read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
    }

    if ($ispath) {
        @in = split(/\//,$in);
    } else {
        @in = split(/&/,$in);
    }

    foreach $i (0 .. $#in) {
        # Convert plus's to spaces
        $in[$i] =~ s/\+/ /g;

        # Split into key and value.
        ($key, $val) = split(/=/,$in[$i],2); # splits on the first =.

        # Convert %XX from hex numbers to alphanumeric
        $key =~ s/%(..)/pack("c",hex($1))/ge;
        $val =~ s/%(..)/pack("c",hex($1))/ge;

        # Associate key and value
        $in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
        $in{$key} .= $val;

    }

    $cookie_line = $ENV{'HTTP_COOKIE'};
    $cookie_line =~ tr/ //d;
    @cookie_lines = split(/;/,$cookie_line);

    foreach $i (0 .. $#cookie_lines) {
        # Convert plus's to spaces
        $cookie_lines[$i] =~ s/\+/ /g;

        # Split cookieto key and value.
        ($key, $val) = split(/=/,$cookie_lines[$i],2); # splits on the first =.

        # Convert %XX from hex numbers to alphanumeric
        $key =~ s/%(..)/pack("c",hex($1))/ge;
        $val =~ s/%(..)/pack("c",hex($1))/ge;

        # Associate key and value
        $cookie{$key} .= "\0" if (defined($cookie{$key})); # \0 is the multiple separator
        $cookie{$key} .= $val;
    }

    return 1;
}


&CGIReadParse;


$rwbs=$in{'rwbs'};
$channel=$in{'channel'};
$passwd=$in{'passwd'};
$leavechannel=$in{'leavechannel'};

if (length($rwbs) == 0) {
  print "Content-type: text/html

<HTML>
<font size=4 color='ff0000'>You did not specify a Roger Wilco Base Station (rwbs).
</html>
";
  exit 1;
}

if (length($channel) == 0 && length($passwd) > 0) {
    $channel = "DEFAULT";
}

# converting / chars to _ to avoid confusing RW (/ is switch char)
$channel =~ tr/\//_/;    
$leavechannel =~ tr/\//_/;    

# if there is a :__, remove it and any preface
$channel =~ s/.*\:__//;    
$leavechannel =~ s/.*\:__//;    

# replace any remaining :'s with _
$channel =~ s/\:/_/g;    
$leavechannel =~ s/\:/_/g;    

# if there is a colon, remove it and any preface
$channel =~ tr/[A-Z]/[a-z]/;    
$leavechannel =~ tr/[A-Z]/[a-z]/;    

print "Content-type: application/x-rogerwilco


/leave $leavechannel
/join $rwbs $channel $passwd
";

exit 0;
