%%folder-xobotsend-anonymous-email%%

This context gets $from and $to addresses as GET variables, via a wget call from procmail.
If a sitemember is found that matches $to, and that sitemember has p_xobot==1, it looks for a recent event matching $from.
If not recent events are found, it sends the sitemember's p_xobotmessage to $from and records a new event.


%%preprocess%%
$to= $_GET['to'];
$from= $_GET['from'];

// check for existence and correct format...
if ($to=="" || $from=="" || 0) {
	berror("Either to ($to) or from ($from) is not a valid email address.",0);
	exit;
	}
berror("Xobot-send: Found to=$to and from=$from, passed semantic checks, continuing.",1);

// look for sitemember matching $to.
$recipient= new Sitemember;
$query= "SELECT obj.* FROM sitemember AS obj, member WHERE obj.memberid=member.id AND obj.siteid='$site->id' AND member.email='$to' AND obj.status='posted' ";
$recipient->selectObject($query);
if ($recipient->id=="") {
	berror("No sitemember matches that email addess ($to).",0);
	exit;
	}
berror("Xobot-send: Using $query returned $recipient->name (#$recipient->id) with p_xobot=$recipient->p_xobot.",1);

// is xobot active for this recipient?
if ($recipient->p_xobot!=1) {
	// this should probably fail silently, but maybe not...
	berror("Recipient has disabled xobot at this time. Thanks for calling.",0);
	exit;
	}
berror("Xobot-send: Green light, ready to look for events.",1);

// look for a recent (7 days) xobotsend event referencing $from
$event= new Event;
$query= "SELECT obj.* FROM event AS obj WHERE obj.parentobjtype='sitemember' AND obj.parentid='$recipient->id' AND obj.flavor='xobotsend' AND obj.status='posted' AND obj.name='$from' ";
$event->selectObject($query);
if ($event->id!="") {
	// this should probably fail silently, but maybe not...
	berror("Sender has already been sent the auto-response, on $event->created.",0);
	exit;
	}
berror("Xobot-send: No matching events found, continuing.",1);

// aha, okay, we're gonna send some mail now.
$headers= "From: $to
MIME-Version: 1.0
Content-type: text/html; charset=iso-8859-1
Precedence: junk (autoreply)
X-Priority: 5 (lowest)
X-Loop: xobot_reply
X-Sender-IP-Address: $_SERVER[REMOTE_ADDR]";
$subject= "Autoresponse from $to";
$message= bHtml($recipient->p_xobotmessage);
if ($message=="") $message= "This is an automatic reply from $recipient->name. Thank-you for emailing me.";
$message= "<html>
<body>
".$message."
<br>
<br>
---<br>
<tt>Auto-response generated by <a href='http://berylium.org/'>berylium xobot</a>.</tt>
</body>
</html>";

// only process mail requests originating on this server
if ($_SERVER['REMOTE_ADDR']==$_SERVER['SERVER_ADDR']) {
	mail($from,$subject,$message,$headers);
	}
else {
	print nl2br("Autoresponse is:<hr>
		To: $from
		Subject: $subject
		$headers

		$message
		<hr>");
	}

// register event so that $from doesn't get more than one autoresponse (per 7 days? per edit? what?)
$event->name= $from;
$event->parentobjtype= "sitemember";
$event->parentid= $recipient->id;
$event->flavor= "xobotsend";
$event->status= "posted";
$GLOBALS['session']->insertstatus= "posted";
$event->insertObject();
berror("Xobot-send: Email rendered (maybe sent) and new event added with id=$event->id.",0);



%%header%%


%%css%%


%%template%%
OK


%%listrow%%



%%nullobject%%



%%footer%%



%%postprocess%%



%%end of context%%
