#!/usr/local/bin/php
<?php
/**
 * This script bounces a message back to the sender and can be used with IMP's
 * spam reporting feature to bounce spam.
 *
 * It takes the orginal message from standard input and requires the bounce
 * message in the file imp/config/bounce.txt. Important: the bounce message
 * must be a complete message including headers!
 *
 * Copyright 2005-2013 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (GPL). If you
 * did not receive this file, see http://www.horde.org/licenses/gpl.
 *
 * @author   Jan Schneider <jan@horde.org>
 * @category Horde
 * @license  http://www.horde.org/licenses/gpl GPL
 * @package  IMP
 */

$baseFile = __DIR__ . '/../lib/Application.php';
if (file_exists($baseFile)) {
    require_once $baseFile;
} else {
    require_once 'PEAR/Config.php';
    require_once PEAR_Config::singleton()
        ->get('horde_dir', null, 'pear.horde.org') . '/imp/lib/Application.php';
}
Horde_Registry::appInit('imp', array(
    'authentication' => false,
    'cli' => true
));

/** Configuration **/

/**
 * Location of the bounce template.
 * The following strings will be replaced in the template:
 *   %TO%     - The spammer's e-mail address.
 *   %TARGET% - The target's e-mail address.
 */
$bounce_template = IMP_BASE . '/config/bounce.txt';

/** End Configuration **/

/* If there's no bounce template file then abort */
if (!file_exists($bounce_template)) {
    $cli->fatal('Bounce template does not exist.');
}

/* Read the message content. */
$data = $cli->readStdin();

/* Who's the spammer? */
$headers = Horde_Mime_Headers::parseHeaders($data);
$return_path = $headers->getOb('return-path');

/* Who's the target? */
$delivered_to = $headers->getOb('delivered-to');

/* Read the bounce template and construct the mail */
$bounce = str_replace(
    array('%TO%', '%TARGET%'),
    array($return_path[0]->bare_address, $delivered_to[0]->bare_address),
    file_get_contents($bounce_template)
);

/* Send the mail */
$sendmail = "/usr/sbin/sendmail -t -f ''";
$fd = popen($sendmail, 'w');
fputs($fd, preg_replace("/\n$/", "\r\n", $bounce . $data));
pclose($fd);
