#!/usr/bin/php -q
<? /*
// File: autoreply.php
// Purpose: Used for autoreply in Sendmail
// Creation: 2002-02-22
// Author: Felix <hide@address.com>
*/
//////////////////////////////////////////////////////// SETTING
// Store Autoresponder Messages in:
$tmpcfg = explode("/",__FILE__);
array_pop($tmpcfg);array_pop($tmpcfg);
$folder = implode("/",$tmpcfg)."/autoresponder/";
// ==================================================================
// Load args
$user = trim(next($HTTP_SERVER_VARS["argv"]));
$aliases = trim(next($HTTP_SERVER_VARS["argv"]));
$now = time();
// Check last autoreply timestamp / counter
if (file_exists($folder.$user.'.counter')) {
$timestamp = file($folder.$user.'.counter');
$timestamp = trim(implode("",$timestamp));
$timestamp = explode(" ",$timestamp);
// [0]: time() [1]: reply counter
// if time difference is less than 10 seconds, increase counter
if (($now - $timestamp[0]) < 10) {
++$timestamp[1];
$newstamp = $now.' '.$timestamp[1];
}
else // else set new counter
$newstamp = $now.' 0';
// write new counter
$fp = fopen($folder.$user.'.counter',"w+");
fwrite($fp,$newstamp);
fclose($fp);
// if counter is bigger than 20, stop script here.
if ($timestamp[1] > 20)
exit;
}
else { // create new stamp / counter
$newstamp = $now.' 0';
// write new counter
$fp = fopen($folder.$user.'.counter',"w+");
fwrite($fp,$newstamp);
fclose($fp);
}
// Get Incoming E-Mail & Parse it.
$data = file("php://stdin");
$tmp = '';
do {
if (eregi("^Reply-To: ",current($data)))
$from = strtolower(trim(eregi_replace("Reply-To:","",current($data))));
elseif (eregi("^From: ",current($data)) AND !$from)
$from = strtolower(trim(eregi_replace("From:","",current($data))));
elseif (eregi("^To: ",current($data)))
$to = strtolower(trim(eregi_replace("To:","",current($data))));
elseif (eregi("^Subject: ",current($data)))
$subject = trim(eregi_replace("Subject:","",current($data)));
elseif (eregi("^X-UIDL: ",current($data)) OR ($from AND $to AND $subject))
break;
$tmp .= current($data);
} while (next($data));
// If the e-mail match the following regex, die (its a spam/list/bulk mailing/postmaster e-mail)
if (eregi("(^(Mailing-List:|Precedence:.*(junk|bulk|list)|To: Multiple recipients of|(((Resent-)?(From|Sender)|X-Envelope-From):|>?From)([^>\n]*[^(.%@a-z0-9])?(Post(ma?(st(e?r)?|n)|office)|(send)?Mail(er)?|daemon|m(mdf|ajordomo)|n?uucp|LIST(SERV|proc)|NETSERV|o(wner|ps)|r(e(quest|sponse)|oot)|b(ounce|bs\.smtp)|echo|mirror|s(erv(ices?|er)|mtp(error)?|ystem)|A(dmin(istrator)?|MMGR|utoanswer))(([^).!:a-z0-9][-_a-z0-9]*)?[%@>\t\n> ][^<)]*(\(.*\).*)?)?$([^>]|$)))", $tmp, $rval))
exit;
// parse $to to get the e-mail address only
unset($email);
$i = 0;
if (ereg("(,|;|[:space:])",$to)) {
$to = split("[,;[:space:]]+",$to);
foreach($to AS $tmp) {
if (strstr($tmp,'@'))
$email[++$i] = $tmp;
}
}
// if $email contains more than 1 e-mail address, try to figure out the good one.
if ($i > 1){
unset($to);
unset($match);
// perform 2 pass, 1st check for the username@ then check for aliases@
reset($email);
foreach($email AS $tmp) {
if (stristr($tmp,"$user@")) {
$to = $tmp;
++$match;
}
}
// aliases check
if ($aliases) {
// if $aliases == *, we have no idea who we are.. set $to = $user;
if ($aliases == '*')
$to = $user;
else {
if (stristr($aliases,','))
$aliases = explode(',',$aliases);
reset($email);
foreach($email AS $tmp) {
if (!is_array($aliases)) {
if (stristr($tmp,"$aliases@")) {
$to = $tmp;
++$match;
}
}
else {
reset($aliases);
foreach($aliases AS $tmp2) {
if (stristr($tmp,"$tmp2@")) {
$to = $tmp;
++$match;
}
}
}
}
}
}
// else by default $to = $user
if (!$to OR $match > 1)
$to = $user;
}
elseif($i == 1)
$to = $email[1];
// Load Autoreply Message
if (file_exists($folder.$user)) {
$autoreply = file($folder.$user);
$autoreply = trim(stripslashes(implode("",$autoreply)));
}
else
$error = true;
if (stristr($to, $from))
$error = true;
if (stristr($subject,"autoreply"))
$error = true;
if (stristr($subject,"returned mail"))
$error = true;
// If all is well, reply!
if ($from AND $to AND $user AND !$error)
mail($from,"Autoreply: ".$subject, $autoreply, "From: $to\nReply-To: $to\n");
?>