aliases-request.pl

A short Perl script I wrote that responds to e-mails with the excerpts of /etc/aliases relevant to the domain to which the request was addressed.

#!/usr/bin/perl
use strict;
use warnings;

# aliases-request.pl
# Returns the relevant parts of /etc/aliases (or specified file)
# Delimited by the requested domain
# ##aliases-request@<domain>
# ##aliases-end@<domain>
#

use constant ALIASES => "/etc/aliases";

# save and parse message
local $/ = undef;    # clear line-ending string
my $msg      = <>;                    # slurp in message
my $domain   = "";
my $dumpfile = "/var/tmp/dumpfile";
open( DUMPFILE, ">$dumpfile" ) or die $!;

# slurp in aliases
open( FILE, ALIASES ) or die $!;
my $aliases = <FILE>;
close FILE;

# reset line-ending string
local $/ = '\n';

for ( split /^/, $msg ) {

    # extract To: domain
    if ( $_ =~ m/^To:.*@(.*)(?:\s|$)/ ) {
        $domain = $1;
        print DUMPFILE "To: Matched " . length(@-) . " items.\n";
        print DUMPFILE "\$domain = $domain\n";
    }

    if ( $_ =~ m/^From:\s+(.*)$/ ) {
        print DUMPFILE "From: Matched " . length(@-) . " items.\n";
        $return_address = $1;
        print DUMPFILE "\$return_address = $return_address\n";
    }

    if ( $_ =~ m/^Reply-to:\s+(.*)$/ ) {
        print DUMPFILE "Reply-to: Matched " . length(@-) . " items.\n";
        $return_address = $1;
        print DUMPFILE "\$return_address = $return_address\n";
    }

    if ( $_ =~ m/^Subject:\s+(.*)$/ ) {
        print DUMPFILE "Subject: Matched " . length(@-) . " items.\n";
        $subject = $1;
        print DUMPFILE "\$subject = $subject\n";
    }
}

# find delimiters and extract relevant chunk
my $delim_string   = "##aliases-request\@$domain";
my $delim_end      = "##aliases-end\@$domain";
my $start_index    = ( index $aliases, $delim_string ) + length($delim_string);
my $end_index      = index $aliases, $delim_end;
my $offset         = $end_index - $start_index;
my $relevant_chunk = substr $aliases, $start_index, $offset;

# create return mail
my $return_address = "";
my $subject        = "";
my $sendmail       = "/usr/sbin/sendmail -t";
my $reply_to = "Reply-to: mailer-daemon\@kitesfear.lakemasoniccenter.org\n";
$subject = "Subject: RE: $subject\n";
my $from = "From: " . "mailer-daemon\@kitesfear.lakemasoniccenter.org\n";
my $to   = "To: " . "$return_address\n";

print DUMPFILE $reply_to;
print DUMPFILE $subject;
print DUMPFILE $from;
print DUMPFILE $to;
print DUMPFILE "Content-type: text/plain\n\n";    # Don't forget extra \n!
print DUMPFILE "$relevant_chunk\n\n.\n";
close(DUMPFILE);

open( SENDMAIL, "|$sendmail" ) or die $!;
print SENDMAIL $reply_to;
print SENDMAIL $subject;
print SENDMAIL $from;
print SENDMAIL $to;
print SENDMAIL "Content-type: text/plain\n\n";    # Don't forget extra \n!
print SENDMAIL "$relevant_chunk\n\n.\n";
close(SENDMAIL);
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: