#!/usr/athena/bin/perl
# Zlocate Gateway
# $Id: zl,v 1.5 2004/08/03 19:47:17 root Exp $
# Matthew Gray (mkgray@mit.edu) June '93
# Matthew Gray (mkgray@mit.edu) June '94 Converted to CGI
# Rex Min (rmin@mit.edu) & Kevin Fu (fubob@mit.edu) Dec '99  HDML support


$query=$ENV{'QUERY_STRING'};

### Send HDML code to wireless phones with UP.Browser
$hdml= ($ENV{'HTTP_USER_AGENT'} =~ /UP\.Link/);

if ($hdml) {
  print("Content-Type: text/x-hdml\n\n");
} else {
  print("Content-Type: text/html\n\n");
}

$query =join(' ', @ARGV) if $ARGV[0];
print("\n");
&do_zlocate($query);

sub do_zlocate {
    local($name, $zl, *ZL) = @_;

    if($name =~ /[A-Za-z0-9_+\s]{2,}/){
	open(ZL, "-|") || (open(STDERR, ">&STDOUT"),
		 exec '/usr/athena/bin/zlocate', '-d', split(/[\+\s]+/,$name));
	while($_ = <ZL>) {$zl .= $_;}

    ($host, @rest) =split(' ', $zl);
      if ($hdml) {    ### add HDML link to finger machine
        $zl =~ s/^([^ \tH]+)/<a task=go label=Fingr dest="http:\/\/www.hdmlworld.com\/Z\/hdml-bin\/finger2\?f=$name\@$host">\1<\/a>/;     
      } else {       ### original, HTML link 
	$zl =~ s/^([^ \tH]+)/<a href="\/finger\?$name\@$host">\1<\/a>/;
      }
    	$zl =~ s/>deathtongue/>deatht&ouml;ngue/i;

      if ($hdml) {   ############ HDML CODE ############
	print <<"end_msg";
<HDML VERSION=3.0 PUBLIC=TRUE MARKABLE=TRUE TTL=0>
  <DISPLAY TITLE="Zlocate" BOOKMARK="http://stuff.mit.edu/cgi/zl">
  <ACTION TYPE=SOFT1 LABEL=Back TASK=PREV>
  Zlocate $name <BR>
  $zl
  </DISPLAY>
</HDML>
end_msg
      } else {     ########### HTML CODE ##########
	print <<"end_msg";
<html><head><title>Zlocate $name</title><body>
<h1>Zlocate $name</h1>
<isindex prompt="Enter the username of the person you wish to locate: ">
<pre>$zl</pre>
</body>
end_msg
      }
    }
    else{
      if ($hdml) {
	print <<"end_msg";    ########## HDML CODE ##############
<HDML VERSION=3.0 PUBLIC=TRUE MARKABLE=TRUE>
  <ENTRY TITLE="Zlocate" BOOKMARK="http://stuff.mit.edu/cgi/zl" FORMAT=*x KEY=zu>
     <ACTION TYPE=ACCEPT LABEL=OK TASK=GO DEST="zl?\$(zu:e)">
     User to locate:
  </ENTRY>
</HDML>
end_msg
      } else {     ########## HTML CODE #########
	print <<"end_msg";
<html><head><title>Zlocate (information)</title></head><body>
<h1>Zlocate Gateway</h1>
<isindex prompt="Enter the username of the person you wish to locate: ">

<p>Zlocate is a program used by MIT's Zephyr message system.
You can use it to find out where someone is logged in.
However, if they wish to be hidden, it is not possible to locate them.</p>

<p>Click <a href="zlocate?mkgray+nocturne+yonah+fubob+leira+annmarie+tlai">here</a>
to see if any of the Webmasters are on line.</p>

<p>In order to be locateable by the web zlocate gateway, you must set your
zephyr exposure to be net-announced using the command</p>

<p><code>zctl set exposure net-visible</code></p>

<p><em>Note</em>: This will make you realm-announced as well, so you
will appear in the znols of people who have you in their .anyone
file. For more information on
<a href="/afs/sipb/project/doc/izephyr/html/section3.html">exposure</a>
and other aspects of zephyr, check
<a href="/afs/sipb/project/doc/izephyr/html/izephyr.html">Inessential
Zephyr</a>.</p>

</body></html>
end_msg
       }
    }
}

1;
