$SEND_MAIL="/usr/sbin/sendmail";
$mailprog = $SEND_MAIL;

###########################################
sub text_notify {

    my ($to, $from, $subject, $text) = @_;
    if (validAddress($to) && validAddress($from)) {

	setTextMessage($text);
        open(MAIL, "|$mailprog -t");
        print MAIL "To: $to\n";
        print MAIL "From: $from\n";
        print MAIL "Sender: $from\n";
        print MAIL "Subject: $subject\n\n";
	print MAIL $text;
	close(MAIL);
	return(1);

    } else {
	return(0);
    }
}

################################################################
sub notify {

    my ($to, $from, $subject, $text) = @_;
    if (validAddress($to) && validAddress($from) && notSpam($text) ) {

	setHTMLMessage($text);
	setTextMessage($text);
	$boundary = "====" . time() . "====";

        open(MAIL, "|$mailprog -t");
        print MAIL "To: $to\n";
        print MAIL "From: $from\n";
        #print MAIL "bcc: wrmoss\@hbcuconnect.com\n";
        print MAIL "Sender: $from\n";
        print MAIL "Subject: $subject\n";
        print MAIL "MIME-Version: 1.0\n";
        print MAIL "Content-Type: multipart/alternative;\n";
        print MAIL "    boundary=\"$boundary\"\n\n";
        print MAIL "This is a multi-part message in MIME format.\n\n";
        print MAIL "--$boundary\n";
        print MAIL "Content-Type: text/plain;\n";
        print MAIL "    charset=\"us-ascii\"\n";
        print MAIL "Content-Transfer-Encoding: 7bit\n\n";
        print MAIL $TextMessage;
        print MAIL "\n\n--$boundary\n";
        print MAIL "Content-Type: text/html;\n";
        print MAIL "    charset=\"us-ascii\"\n";
        print MAIL "Content-Transfer-Encoding: 8bit\n\n";
        print MAIL $HTMLMessage;
        print MAIL "\n\n--$boundary--\n";
        close (MAIL);
	return(1);
    } else {
	return(0);
    }
}

###########################################
sub fixhtml() {
        my ($text) = $_[0];
	my($i,$words,$link);
        $text =~ s/\n/ <br> /g;
        @words = split(" ",$text);
        $text = "";
        for ($i=0; $i<@words; $i++) {
            if ( index($words[$i], "www")==0 ) {
                $link = $words[$i];
                chomp($link);
                $words[$i] = "<a href=http://$link target=_blank>$link</a>";
            }
            if ( index($words[$i], "http")==0 ) {
                $link = $words[$i];
                chomp($link);
                $words[$i] = "<a href=$link target=_blank>$link</a>";
            }
	    if ( index(lc($words[$i]), "cruise")==0 || index(lc($words[$i]), "vacation")==0
		|| index(lc($words[$i]),"relax")==0
		|| index(lc($words[$i]),"fun")==0
		|| index(lc($words[$i]),"hot")==0
		|| index(lc($words[$i]),"alumni")==0
		|| index(lc($words[$i]),"homecoming")==0 )
	    {
		$words[$i] = "<a href=http://hbcucruise.com target=_blank><b>$words[$i]</b></a>";
	    } elsif (  index(lc($words[$i]),"chat")==0 
		||  index(lc($words[$i]),"talk")==0 ) {
		$words[$i] = "<a href=http://forums.hbcuconnect.com target=_blank><b>$words[$i]</b></a>";
	    }

            $words[$i] .= " ";
            $text .= $words[$i];
        }
	
	return($text);
}

sub setHTMLMessage() {

	my $text = &fixhtml($_[0]);

	$text =~ s/\n/<br>/g;

        $HTMLMessage = qq~
	<html>
	<style>
	body, td { font-family:trebuchet ms; font-size:11; }
	</style>

	<body bgcolor="#ffffff" text="black" link="#993300" vlink="#771100"
	alink="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

	<table cellpadding=0 cellspacing=3><tr><td>
	<a href=http://hbcuconnect.com target=_blank><img border=0 align=left 
	src=http://hbcuconnect.com/adimages/hbcuConnect200x40.gif></a>
	</td></tr></table>

	<table align=center width=98% cellspacing=0 cellpadding=2 bgcolor=333333><tr><td>
	<table width=100% cellspacing=0 cellpadding=3 bgcolor=993300><tr><td>
	<table width=100% cellspacing=0 cellpadding=3 bgcolor=ffffff><tr><td>
	<table width=100% cellpadding=3>
  	<tr><td valign=top>
	<!-- -------------------------- MAIN MESSAGE START --------------------------- -->

	$text

	<!-- -------------------------- MAIN HTML END --------------------------- -->
	</td></tr></table>

	</td></tr></table>
	</td></tr></table>
	</td></tr></table>


  	<br>
  	<table width="100%" border="0" cellspacing="3" cellpadding="0">
    	<tr>
      	<td align=center><font color=d3cab6>
        <a href=http://hbcuconnect.com/>Home</a> |
        <a href=http://hbcuconnect.com/alumnidb/cgi-bin/members.cgi>Sign Up</a> |
        <a href=http://hbcuconnect.com/careercenter>Jobs/Internships</a> |
        <a href=http://hbcuconnect.com/scholarships.shtml>Scholarships</a> |
        <a href=http://hbcuconnect.com/contact_us.cgi>Contact Us</a> |
        <a href=http://hbcuconnect.com/advertise.shtml>Advertise</a>
      </font></div></td>
    </tr>
  </table>
  <br><br>
  <div align=center>
        <font color=999999 style=font-size:10>
        No longer interested in receiving these updates? You can <b>
        <a href=http://hbcuconnect.com/cgi-bin/unsubscribe.cgi
        target=_blank>cancel</a></b>
        your membership on HBCUCONNECT.COM,<br>
        but you won't be notified of the latest HBCU News and site updates!
  <br>
  <br>
        We Fully Support the CAN-SPAM Act<br>
        HBCU Connect, LLC<br>
        750-Q Cross Pointe Road<br>
        Columbus, Ohio 43230<br>
        (614) 864-4446
  </div>
  </body></html>
  ~;

}

###########################################
sub setTextMessage() {

	my ($text) = $_[0];
        $TextMessage = qq~
$text
--------------------------------------------------------------------
LATEST SCHOLARSHIP OFFERS
--------------------------------------------------------------------
http://hbcuconnect.com/cgi-bin/scholarships.cgi



--------------------------------------------------------------------
Brought to you by: www.HBCUConnect.com
--------------------------------------------------------------------
750 Cross Pointe Rd, Suite Q
Columbus, Ohio 43230
614.864.4446
--------------------------------------------------------------------
Unsubscribe: www.hbcuconnect.com/cgi-bin/unsubscribe.cgi
~;
}

sub validAddress()
{
        return 0 if $_[0] =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
        return 0 if $_[0] !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
        return 1;
}

#################################################
sub privateMessage() {
    my ($dbh,$to_id,$from_id,$subject,$message) = @_;

    #### This function assumes a database connection is already made
    my $sql = "INSERT INTO hbcu_love_messages set to_id=?, from_id=?, subject=?,
		message=?, ts=now()";
    my $sth = $dbh->prepare($sql);
    $sth->execute($to_id,$from_id,$subject,$message)
	|| print $sth->errstr();

}

sub notSpam() {
        return 0 if $_[0] =~ /viagra/i;
        return 0 if $_[0] =~ /ephedrine|lipodrine/i;
        return 0 if $_[0] =~ /cialis/i;
        return 0 if $_[0] =~ /levitra/i;
        return 0 if $_[0] =~ /ringtones/i;
        return 0 if $_[0] =~ /vimpex/i;
        return 0 if $_[0] =~ /mail\.ru/i;
        return 0 if $_[0] =~ /ring tones/i;
        return 0 if $_[0] =~ /payday loan/i;
        return 1;
}
1;
