config root man

Current Path : /home/mf/

FreeBSD hs32.drive.ne.jp 9.1-RELEASE FreeBSD 9.1-RELEASE #1: Wed Jan 14 12:18:08 JST 2015 root@hs32.drive.ne.jp:/sys/amd64/compile/hs32 amd64
Upload File :
Current File : /home/mf/mailfw

#!/usr/bin/perl -w
#
# mailfw
#

#// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#// use Module
#// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

use strict;
use vars qw( $opt_f $opt_t $opt_m );

use Getopt::Long;
use Jcode;

use ADN::MailParse;
use ADN::Utility;

#// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#// Controller
#// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

    #// ----------------------------------------------------------
    #// Option Parse
    #// ----------------------------------------------------------

    $opt_f = '';
    $opt_t = '';
    $opt_m = 0;

    my @basename = split(/\//, $0);
    my $basename = pop(@basename);
    my $result   = GetOptions('f=s','t=s','m');

    #// --------------------------------------------------------------
    #// Item Set
    #// --------------------------------------------------------------

    my $file = '/home/mf/mailfw.check';
    my $conf = ADN::Utility::read_conf($file);

    my $item = {
        hostname => '/bin/hostname',
        sendmail => '/usr/local/sbin/sendmail',

        msg_size => 'Warning: body size over.',
    };

    #// --------------------------------------------------------------
    #// Start
    #// --------------------------------------------------------------

    my @mail  = ();
    my @env_f = ();
    my $num   = 0;

    $item->{body} = '';
    $item->{bodycheck} = 0;
    $item->{bodysize}  = 0;

    foreach (%$conf) {
        next unless ($conf->{$_});

        if ($opt_f =~ /\@$_$/) {
            $item->{bodysize} = $conf->{$_};
        }
    }

    foreach (<STDIN>) {
        push(@mail, $_);

        $_ =~ s/\r//g;

        unless (@env_f) {
            @env_f = split(/ /, $_);
            my $env_f = $env_f[1];

            if ($env_f =~ /^mailer-daemon/i) { exit; }
        }

        if ($_ =~ /^\n$/) { $num = 1; next; }

        if ($num == 1 && $item->{bodysize} > 0) {
            $item->{body} .= $_;

            if (length($item->{body}) >= $item->{bodysize}) {
                $item->{bodycheck} = 1;
                last;
            }
        }
    }

    $item->{body} = undef;

    body_check($item, \@mail);

    #//

    foreach (split(/,/, $opt_t)) {
        my $from = $opt_f;
        my $rcpt = $_;

        my ($header, $body) = body_cut(\@mail);

        my $mta = "$item->{sendmail} -f$from $rcpt";

        open MAIL, "| $mta";

        print MAIL $header;
        print MAIL "\n";
        print MAIL $body;
    }

#// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#// Model
#// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

sub get_part {
    my ($mail, $flag, $type) = @_;

    my $check = 0;
    my $line  = '';

    my $type0 = 0;

    foreach (@$mail) {
        $_ =~ s/\r//g;

        if ($_ =~ /^\n$/) {
            if ($check == 0) { $check = 1; }   #// header, body sep

            if ($flag == 1) { last; }
        }

        if ($flag == 1) {   #// header
            if ($type) {
                if ($_ =~ /^Content-Type: /) { $type0 = 1; next; }
            }

            if ($type0 == 1 && $_ =~ /^([0-9a-zA-Z\-]+): (.+)/) {
                $line .= $type;
                $type0 = 0;
            }

            if ($check == 0 && $type0 == 0) { $line .= $_; }
        }

        if ($flag == 2) {   #// body
            if ($check == 1) {
		if ($_ =~ /^\./) {
                    $line .= '.' . $_;
                } else {
                    $line .= $_;
                }
            }
        }
    }

    if ($flag == 2) { $line =~ s/^\n//g; }

    return $line;
}

sub body_check {
    my ($item, $mail) = @_;

    my $parse = new ADN::MailParse();

    my $size = ADN::Utility::comma($item->{bodysize});

    if ($item->{bodycheck} == 1) {
        $item->{msg} = $item->{msg_size};

        my $header0 = get_part($mail, 1); chomp $header0;

        my $warning = $item->{msg} . " (limit $size bytes)";

        my $body0   = <<"BODY";
$warning

-------------------------------------------------------------------
$header0
-------------------------------------------------------------------
BODY

        my $host = `$item->{hostname}`; chomp $host;

        my @header = qw( Subject );
        my ($subj) = $parse->header(@header, $mail);

        foreach (split(/,/, $opt_t)) {
            my $rcpt = $_;
            my $mta  = "$item->{sendmail} -f '' $rcpt";

            my $header = {
                From    => 'MAILER-DAEMON@' . $host,
                To      => $rcpt,
                Subject => $subj,
            };

            $header->{'Content-Type'} = 'text/plain; charset=iso-2022-jp';
            $header->{'Content-Transfer-Encoding'} = '7bit';

            $header->{'MIME-Version'}   = '1.0';
            $header->{'Auto-Submitted'} = 'auto-generated (failure)';

            ADN::Utility::mail_send($mta, $header, $body0);
        }

        exit;
    }
}

sub body_cut {
    my ($mail) = @_;

    my $header = '';
    my $body   = '';

    if ($opt_m == 1) {
        my $parse = new ADN::MailParse();
        my ($body0, $file, $show, $mime, $code, $count) = $parse->body($mail);

        my $type = "Content-Type: text/plain; charset=iso-2022-jp\n";

        $header = get_part($mail, 1, $type);
        $body   = Jcode::convert($body0, 'jis');

        if ($count > 0) {
            $body =~ s/(\n+)$//;
            $body .= "\n\n";

            for (my $h = 0; $h < $count; $h++) {
                $body .= 'attach: ' . $show->[$h] . "\n";
            }
        }

    } else {
        $header = get_part($mail, 1);
        $body   = get_part($mail, 2);
    }

    return ($header, $body);
}

Man Man