#!/bin/bash
#
# Search for an individual listee's address.
# Hack hack
#
# <honey@missprint.org>

LISTHOME=/usr/local/majordomo/lists
LIST1=$LISTHOME/sinister
LIST2=$LISTHOME/sinister-digest
LIST1N=$LISTHOME/sinister.nomail
LIST2N=$LISTHOME/sinister-digest.nomail
LIST1P=$LISTHOME/sinister.nopost
LIST2P=$LISTHOME/sinister-digest.nopost

# Just want search term
QUERY=`echo "$QUERY_STRING" | sed -e 's/^query=//' \
                                  -e 's/&Search=Search$//'`
QUERY_ESC=`echo "$QUERY" | sed -e 's/\./\\\./g'`

cat <<EOF
Content-type: text/html

<HTML>
<HEAD>
<TITLE>Sinister List Email Search</TITLE>
<link rel=stylesheet
href="/sinister/sinister.css" type=
"text/css"
      media=screen title="sinister">
</HEAD>

<BODY
BACKGROUND="/sinister/lazy-crop-circle.jpg"
NOSAVE
>
<H1><FONT COLOR="#993399">
<IMG SRC="/sinister/house-shake.gif">&nbsp;
Sinister List Email Search
</FONT></H1>

<!-- Page header -->
EOF

# Don't allow short searches (stop spammers)
if [ "$QUERY" != "" ]
then

if [ "`echo $QUERY | sed -e 's/.//' -e 's/.//'`" = "" ]
then 

cat <<EOF
<H2>Search Results</H2>

Sorry, you need to enter 3 or more characters to search on.  This is
to stop unscrupulous fiends trying to send you all junk email.  If
you <STRONG>are</STRONG> an unscrupulous fiend trying to harvest
email addresses from the list, beware: your attempt has been logged.
EOF

# Only output results page if done a search already:
elif [ "$QUERY_ESC" ]
then

cat <<EOF
<H2>Search Results</H2>

The following email addresses subscribed to the Sinister list match
your search string "<STRONG>$QUERY</STRONG>":
<P>
EOF

MATCHES=`grep -ih "$QUERY_ESC" $LIST1 $LIST2 $LIST1N $LIST2N | \
         sort | uniq`
if [ "$MATCHES" = "" ]
then
 echo "<STRONG>No matches</STRONG>"
else
 for MATCH in $MATCHES
 do
  STRING="<STRONG>$MATCH ("
  [ "$(grep -ih "^${MATCH}$" $LIST1  $LIST1N)" ] && STRING="${STRING}M"
  [ "$(grep -ih "^${MATCH}$" $LIST2  $LIST2N)" ] && STRING="${STRING}D"
  [ "$(grep -ih "^${MATCH}$" $LIST1N $LIST2N)" ] && STRING="${STRING}*"
  [ "$(grep -ih "^${MATCH}$" $LIST1P $LIST2P)" ] && STRING="${STRING}N"
  echo "${STRING})</STRONG><BR>"
 done
 cat <<EOF
<P>
<STRONG>M</STRONG> = on main list<BR>
<STRONG>D</STRONG> = on main digest<BR>
<STRONG>*</STRONG> = set to nomail<BR>
<STRONG>N</STRONG> = in nursery<BR>
EOF
fi
AGAIN=again
fi
fi


cat <<EOF
<P>
<H2>Search the list of subscribed email addresses $AGAIN</H2>
Use this to look for an email address subscribed to the list.
You can enter any search term and it looks for any addresses
containing whatever you want.

<form method="GET"
      action="`basename $0`"
      enctype="application/x-www-form-urlencoded">
<input type="text" value="$QUERY" name="query" size=20>
<input type="submit" name="Search"
    value="Search">
</form>
<P>
If you want to search <a href="/sinister/search.html">the full list
archives</a> instead you can, but remember it only searches for
<STRONG>full words</STRONG> matching your search term unless you
click the relevant box below it.

<!-- Page footer -->
<P>
<HR WIDTH="100%">
<CENTER><B>Click to go...&nbsp;<A
HREF="/sinister">
<IMG SRC="/sinister/back.gif" 
 ALT="Back" BORDER=1 HEIGHT=57 WIDTH=95 ALIGN=ABSCENTER></A>
&nbsp;to the main page</B><BR>
<IMG SRC="/sinister/waitress-hand.gif" 
 HEIGHT=41 WIDTH=42 ALIGN=ABSCENTER><B>
Table Service by <A
HREF="/sinister/shanta.html">Honey
</A></B></CENTER>
</BODY>
</HTML>
EOF

exit
