Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 201 lines (157 sloc) 5.227 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
#!/usr/bin/env perl

use strict;
use warnings;

use Carp;
use Getopt::Long;
use Pod::Usage;

use Digest::MD5::File qw(file_md5);
use Path::Class qw(file dir);
use Array::Diff;
use YAML;
use WWW::MobileCarrierJP 0.19;
use WWW::MobileCarrierJP::DoCoMo::CIDR;
use WWW::MobileCarrierJP::EZWeb::CIDR;
use WWW::MobileCarrierJP::AirHPhone::CIDR;
use WWW::MobileCarrierJP::ThirdForce::CIDR;

my @Carriers = qw/DoCoMo EZWeb ThirdForce/;
my %_i2s = (
    DoCoMo => 'docomo',
    EZWeb => 'ezweb',
    AirHPhone => 'willcom',
    ThirdForce => 'softbank',
   );
my %_s2i = reverse %_i2s;

&main;exit 9;

sub short_name_for {
    $_i2s{+shift};
}
sub internal_name_for {
    $_s2i{+shift};
}

sub url_for {
    my $short_name = shift;
    # assume module are already loaded
    "WWW::MobileCarrierJP::$_s2i{$short_name}::CIDR"->url;
}

sub main {
    my $output_dir = "";
    my $notify_emailfrom = "";
    my $notify_email = [];

    Getopt::Long::Configure("bundling");
    GetOptions(
               'o|output_dir=s' => \$output_dir,
               'f|emailfrom=s' => \$notify_emailfrom,
               'e|email=s@' => \$notify_email,
               'help|h|?' => sub{ pod2usage(-verbose=>1) }) or pod2usage();
    -d $output_dir || pod2usage("no such directory: $output_dir");
    $output_dir = dir($output_dir);

    my $result = scrape();

    my $exit_code = 1;
    while (my($bname, $cidrs) = each %$result) {
        my $filename = $output_dir->file($bname);
        my $filename_tmp = $output_dir->file($bname.".tmp");

        my $fh = $filename_tmp->open('>', 0664) or do {
            carp "failed to open file: $filename_tmp: $!";
            next;
        };
        print $fh "$_\n" for sort byoctet @$cidrs;
        $fh->close;

        if ( updated_cidr($filename, $filename_tmp) ) {
            ## updated cidr info
            process_diff(
                carrier => $bname,
                url => url_for($bname),
                old => -e $filename ? [ $filename->slurp(chomp=>1) ] : [],
                new => [ $filename_tmp->slurp(chomp=>1) ],
                notify => { email => { from => $notify_emailfrom,
                                        to => $notify_email },
                         },
               );

            $exit_code = 0;
            rename $filename_tmp->stringify, $filename->stringify or carp $!;
        } else {
            ## not modified
            $filename_tmp->remove or carp $!;
        }
    }

    exit $exit_code;
}

sub scrape {
    my $result;
    for my $carrier (@Carriers) {
        my $class = "WWW::MobileCarrierJP::${carrier}::CIDR";
        my $dat;
        my $retry = 3;

        while ($retry-- >= 0) {
            $dat = $class->scrape;
            last if @$dat > 0;
        };
        if (@$dat <= 0) {
            carp "[ERROR] cannot scrape CIDR for $carrier";
            next;
        }

        $result->{short_name_for($carrier)} = [map { "$_->{ip}$_->{subnetmask}" } @$dat];
    }
    return $result;
}

sub byoctet {
    my $ao = [split /[.\/]/, $a];
    my $bo = [split /[.\/]/, $b];
    $ao->[0] <=> $bo->[0] or
    $ao->[1] <=> $bo->[1] or
    $ao->[2] <=> $bo->[2] or
    $ao->[3] <=> $bo->[3];
}

sub updated_cidr {
    my($old, $new) = @_;
    return ! -e $old || file_md5($old->stringify) ne file_md5($new->stringify);
}

sub process_diff {
    my %p = @_;

    my $change = +{ map { $_ => $p{$_} } qw(url old new) };
    my $diff = Array::Diff->diff($p{old}, $p{new});

    if (@{ $diff->added }) {
        $change->{added} = $diff->added;
    }

    if (@{ $diff->deleted }) {
        $change->{deleted} = $diff->deleted;
    }

    my $change_text = YAML::Dump({$p{carrier} => $change});

    if (@{ $p{notify}{email}{to} } > 0) {
        notify_email($p{notify}{email}, $p{carrier}, $change_text);
    }

}

sub notify_email {
    my($addr, $carrier, $body) = @_;

    require Email::Sender::Simple;
    require Email::Simple;
    require Email::Simple::Creator;

    my $email = Email::Simple->create(
        header => [
            To => join(', ', @{$addr->{to}}),
            From => $addr->{from},
            Subject => "[NOTIFY] $carrier GW IP address was updated",
            'X-Mailer' => "$0",
           ],
        body => $body,
       );

    Email::Sender::Simple->send($email);
}

__END__

=head1 SYNOPSIS

B<update-mobilejp-cidr>
B<-o> I<OUTPUT_DIR>
[ B<-f> I<FROM> B<-e> I<TO> [ B<-e> I<TO> ... ] ]

$ update-mobilejp-cidr -o /var/ip.d/plain
output into /var/ip.d/plain/{docomo,ezweb,willcom,softbank}

$ update-mobilejp-cidr -o /var/ip.d/plain -f notifier@example.com -e tommy@example.com -e matsu@example.org
if you specify -f and at least one -e option, do notify changing by email.

$ update-mobilejp-cidr -o /var/ip.d/plain && generate-cidr-files ...
If no update cidr then exit(1), otherwise (some cidr updated) exit(0).

=head1 EXIT CODE

If no update cidr then exit(1), otherwise (some cidr updated) exit(0).

=head1 AUTHOR

HIROSE, Masaaki E<lt>hirose31 _at_ gmail.comE<gt>

=head1 COPYRIGHT & LICENSE

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
Something went wrong with that request. Please try again.