User:Seph Swain/perl: Difference between revisions

From Second Life Wiki
Jump to navigation Jump to search
No edit summary
 
Strife Onizuka (talk | contribs)
No edit summary
 
(5 intermediate revisions by one other user not shown)
Line 1: Line 1:
#!/usr/bin/perl
Here's a first example of how to make server side CGI scripts to interact with SL objects.
#
This one retrieves information via HTTP from different sources and answeres requests from SL Objects like Displays or HUDs (objects can be found in-world via my picks), does some version checking with user notification and logs some usage and propagation data from you objects.
# multi purpose WWW->SL Gateway


require 5;
(Unfortunatly the Wiki-Formating doesn't really work for this, if you know how to fix it, please tell me)
use LWP::Simple;
<pre>
use CGI;
  #!/usr/bin/perl
use POSIX('strftime');
  #
 
  # multi purpose WWW->SL Gateway
# Config Section
 
  my %VERSION = (
  require 5;
    'MyControl HUD Translator' => 0.02,
  use LWP::Simple;
    'LineGraph' => 0.9,
  use CGI;
  );
  use POSIX('strftime');
# EOC
 
 
  # Config Section
# global variables
    my %VERSION = (
my $out = "ERROR";
      'MyControl HUD Translator' => 0.02,
 
      'LineGraph' => 0.9,
# collect input data
    );
my $query = new CGI;
  # EOC
my $action = $query->param("action");
 
my $client_version = $query->param("version");
  # global variables
my $client_object_name = $ENV{"HTTP_X_SECONDLIFE_OBJECT_NAME"};
  my $out = "ERROR";
  $client_object_name =~ s/ v[\d\.]*$//o;
 
my $client_owner_name = $ENV{"HTTP_X_SECONDLIFE_OWNER_NAME"};
  # collect input data
#foreach (sort keys %ENV) { print STDERR "$_ = $ENV{$_}\n"; }
  my $query = new CGI;
 
  my $action = $query->param("action");
# some logging:
  my $client_version = $query->param("version");
print STDERR (strftime "%F %T", localtime) .  
  my $client_object_name = $ENV{"HTTP_X_SECONDLIFE_OBJECT_NAME"};
            " $client_object_name [v${client_version}]" .
    $client_object_name =~ s/ v[\d\.]*$//o;
    " (owner: $client_owner_name)\n";
  my $client_owner_name = $ENV{"HTTP_X_SECONDLIFE_OWNER_NAME"};
 
  #foreach (sort keys %ENV) { print STDERR "$_ = $ENV{$_}\n"; }
# version checking:
 
if ($client_version < $VERSION{$client_object_name}) {
  # some logging:
  $out = "Version expired. New Version $VERSION{$client_object_name} " .
  print STDERR (strftime "%F %T", localtime) .  
"available from MyControl";
        " $client_object_name [v${client_version}]" .
  $action = "error";
        " (owner: $client_owner_name)\n";
}
    
 
if ($action eq 'SL-online-now') {
  my $html = get("http://www.secondlife.com/");
  my $total = $1 if $html =~ /.*Total Residents:.*\n.*>([\d,]{1,})</;
    $total =~ s/,//go;
  my $online = $1 if $html =~ /.*Online Now:.*\n.*>([\d,]{1,})</;
    $online =~ s/,//go;
   $out = join(';', $online, $total);
} elsif ($action eq 'dict.leo.org') {
   # version checking:
   # version checking:
   my $word = $query->param("word");
   if ($client_version < $VERSION{$client_object_name}) {
  my $language = $query->param("language");
    $out = "Version expired. New Version $VERSION{$client_object_name} " .
  my $language_str = "ende";
    "available from MyControl";
  # 0-englisch(default), 1-franzoesisch, 2-spanisch
    $action = "error";
    if ($language == 1) { $language_str = "frde" }
  }
  elsif ($language == 2) { $language_str = "esde" }
 
 
  if ($action eq 'SL-online-now') {
  my $html = get("http://dict.leo.org/${language_str}?lp=${language_string}&lang=de&searchLoc=0&cmpType=relaxed&sectHdr=on&spellToler=on&relink=on&search=$word");
    my $html = get("http://www.secondlife.com/");
  #print STDERR "HTML:\n$html\n";
    my $total = $1 if $html =~ /.*Total Residents:.*\n.*>([\d,]{1,})</;
  my $content;
      $total =~ s/,//go;
  $content = $1
    my $online = $1 if $html =~ /.*Online Now:.*\n.*>([\d,]{1,})</;
    if $html =~ /<table[^>]*id="results"[^>]*>(.*?)<\/table>/s; # no i!
      $online =~ s/,//go;
   #print STDERR "content:\n$content\n";
    $out = join(';', $online, $total);
  #print STDERR "splut:\n";
  } elsif ($action eq 'dict.leo.org') {
  my @entries = split /\<tr/i, $content;
    # version checking:
  # remove leading and trailing 2 entries:
    my $word = $query->param("word");
  #@entries = splice(@entries, 2, -2);
    my $language = $query->param("language");
  my @out;
    my $language_str = "ende";
  foreach (@entries) {
    # 0-englisch(default), 1-franzoesisch, 2-spanisch
    my @cells = split /<td/i, $_;
      if ($language == 1) { $language_str = "frde" }
    my $cell_count = int(@cells);
    elsif ($language == 2) { $language_str = "esde" }
    #print STDERR "entry: " . int(@cells) ."\n";
 
    @cells = map {
    my $html = get("http://dict.leo.org/${language_str}?lp=${language_string}&lang=de&searchLoc=0&cmpType=relaxed&sectHdr=on&spellToler=on&relink=on&search=$word");
      s/^[^>]*>//o;
    #print STDERR "HTML:\n$html\n";
      s/<[^>]*>//go;  
    my $content;
      $_; } @cells;
    $content = $1
    if ($cell_count == 6) {
      if $html =~ /<table[^>]*id="results"[^>]*>(.*?)<\/table>/s; # no i!
      push @out, $cells[2] . " - " . $cells[4];
    
    my @entries = split /\<tr/i, $content;
    # remove leading and trailing 2 entries:
    #@entries = splice(@entries, 2, -2);
    my @out;
    foreach (@entries) {
      my @cells = split /<td/i, $_;
      my $cell_count = int(@cells);
      #print STDERR "entry: " . int(@cells) ."\n";
      @cells = map {
s/^[^>]*>//o;
s/<[^>]*>//go;  
$_; } @cells;
      if ($cell_count == 6) {
  push @out, $cells[2] . " - " . $cells[4];
      }
    }
    if (@out) {
      $out = join("\n", @out);
    } else {
      $out = "no results";
     }
     }
 
   }
   }
   if (@out) {
    
    $out = join("\n", @out);
  # cut to size
   } else {
  $out = substr($out, 0, 2048);
    $out = "no results";
   print "Content-type: text/html\n\n$out\n";
  }


}


# cut to size
EOF
$out = substr($out, 0, 2048);
</pre>
print "Content-type: text/html\n\n$out\n";

Latest revision as of 06:31, 27 April 2007

Here's a first example of how to make server side CGI scripts to interact with SL objects. This one retrieves information via HTTP from different sources and answeres requests from SL Objects like Displays or HUDs (objects can be found in-world via my picks), does some version checking with user notification and logs some usage and propagation data from you objects.

(Unfortunatly the Wiki-Formating doesn't really work for this, if you know how to fix it, please tell me)

  #!/usr/bin/perl
  #
  # multi purpose WWW->SL Gateway
  
  require 5;
  use LWP::Simple;
  use CGI;
  use POSIX('strftime');
  
  # Config Section
    my %VERSION = (
      'MyControl HUD Translator'	=> 0.02,
      'LineGraph'			=> 0.9,
    );
  # EOC
  
  # global variables
  my $out = "ERROR";
  
  # collect input data
  my $query = new CGI;
  my $action = $query->param("action");
  my $client_version = $query->param("version");
  my $client_object_name = $ENV{"HTTP_X_SECONDLIFE_OBJECT_NAME"};
     $client_object_name =~ s/ v[\d\.]*$//o;
  my $client_owner_name = $ENV{"HTTP_X_SECONDLIFE_OWNER_NAME"};
  #foreach (sort keys %ENV) { print STDERR "$_ = $ENV{$_}\n"; }
  
  # some logging:
  print STDERR (strftime "%F %T", localtime) . 
  	       " $client_object_name [v${client_version}]" .
  	       " (owner: $client_owner_name)\n";
  
  # version checking:
  if ($client_version < $VERSION{$client_object_name}) {
    $out = "Version expired. New Version $VERSION{$client_object_name} " .
  	   "available from MyControl";
    $action = "error";
  }
  
  if ($action eq 'SL-online-now') {
    my $html = get("http://www.secondlife.com/");
    my $total = $1 if $html =~ /.*Total Residents:.*\n.*>([\d,]{1,})</;
       $total =~ s/,//go;
    my $online = $1 if $html =~ /.*Online Now:.*\n.*>([\d,]{1,})</;
       $online =~ s/,//go;
    $out = join(';', $online, $total);
  } elsif ($action eq 'dict.leo.org') {
    # version checking:
    my $word = $query->param("word");
    my $language = $query->param("language");
    my $language_str = "ende";
    # 0-englisch(default), 1-franzoesisch, 2-spanisch
       if ($language == 1) { $language_str = "frde" }
    elsif ($language == 2) { $language_str = "esde" }
  
    my $html = get("http://dict.leo.org/${language_str}?lp=${language_string}&lang=de&searchLoc=0&cmpType=relaxed&sectHdr=on&spellToler=on&relink=on&search=$word");
    #print STDERR "HTML:\n$html\n";
    my $content;
    $content = $1
      if $html =~ /<table[^>]*id="results"[^>]*>(.*?)<\/table>/s; # no i!
  
    my @entries = split /\<tr/i, $content;
    # remove leading and trailing 2 entries:
    #@entries = splice(@entries, 2, -2);
    my @out;
    foreach (@entries) {
      my @cells = split /<td/i, $_;
      my $cell_count = int(@cells);
      #print STDERR "entry: " . int(@cells) ."\n";
      @cells = map {
	s/^[^>]*>//o;
	s/<[^>]*>//go; 
	$_; } @cells;
      if ($cell_count == 6) {
  	push @out, $cells[2] . " - " . $cells[4];
      }
    }
    if (@out) {
      $out = join("\n", @out);
    } else {
      $out = "no results";
    }
  
  }
  
  # cut to size
  $out = substr($out, 0, 2048);
  print "Content-type: text/html\n\n$out\n";


EOF