#!/usr/bin/perl
use strict;
use warnings;
use IO::File;
use Cwd 'chdir';

my $dir = "/home/www/attach";

local(*DIR);
opendir(DIR,$dir);
my @files = readdir(DIR);
closedir(DIR);

my %ipcounts;
my $daily_max = 15;
my $report_max = 5;

my @protect_ip = ('68.61.179.225', '76.112.146.41', '68.42.122.142', 
                  '68.61.176.86');
my %protected;
foreach my $file (sort @files)
  {
    if($file =~ /^msg\-(\d+_\w+_\d+)\-\d+/)
      {
        my $date = $1;

        # This is a candidate
        my $fh = IO::File->new("$dir/$file");
        next if(!defined $fh);
        while(my $line = <$fh>)
          {
            if($line =~ /^FromIP:\s+([\d\.\:a-f]+)/i)
              {
                my $ip = $1;

                my $is_protected = "";
                foreach my $protect (@protect_ip)
                  {
                    if($ip eq $protect)
                      {
                        $protected{$file} = 1;
                        $is_protected = 1;
                      }
                  }
                next if($is_protected);
                # Note this one
                $ipcounts{$date} = {}
                             if(!defined $ipcounts{$date});
                $ipcounts{$date}->{$ip} = []
                             if(!defined $ipcounts{$date}->{$ip});
                push @{$ipcounts{$date}->{$ip}},$file;
                last;
              }
          }
      }
  }

foreach my $date (sort keys %ipcounts)
  {
    # Each date reports
    foreach my $ip (sort {$#{$ipcounts{$date}->{$b}} <=>
                          $#{$ipcounts{$date}->{$a}}} keys %{$ipcounts{$date}})
      {
        last if($#{$ipcounts{$date}->{$ip}} < $report_max);
        if($#{$ipcounts{$date}->{$ip}} > $daily_max)
          {
            foreach my $file (@{$ipcounts{$date}->{$ip}})
              {
                # print "rm $dir/$file\n";
                system("rm $dir/$file");
              }
          }
        else
          {
            # print "$date $ipcounts{$date}->{$ip} $ip\n";
          }
      }
  }


my @flags = ('vuitton','anel-bags','tangjin3600','coach--outlet',
      'nike-air','payday loans','airjordan','coachnomachi','rxpills',
      'shoesforsale','no prescription','freewebs.com',
      'viagra','shoesforsale','solejordanshop.com',
      'christianlouboutin','forsale.com','uggboot','onlinecheap',
      'Hong Kong-based startup','NBA Game table of Governors',
      'TIBCO Software chairman',
      ' ugg','\[url=http:',' UGG',
      'Mulberry Handbags','cheapghdsukoutlet','paulpottsofficial',
      'european-mediaculture','ruhr-connect','loveepicentre',
      'Louis Vuitton handbags','quenellesgraphiques',
      'spansklararforeningen',
      'oakley/oakley','/oakley/',
      'uggstyles','factoryoutlet','Subject: \S\S\S\S\S\S\S\S\S',
      'Bitcoin','oakley sunglas','harrietvstheworld',
      'lapinbrun','parfum-discount','artandcraftmasters',
      'fotofile.eu','hannawinery','Michael Kors',
      'les-marguerites','Chaussures_Nike_Air_Max',
      'babyliss',' fuck ','Nike Air',
      'Sini White','\.asp','\.php',' 49ers ',' NFL ',' Nike ',
      'Soccer Jerseys','minnesota twins',' nfl ',',nfl ',' nfl,',
      '\-MLB\-','\-NBA\-','\-AFL\-','\-NHL\-','\-NFL\-',
      ' Reebok ',' air max ',' burberry ',' canada hockey ',
      'www\.tattiemoon\.',' converse new',' coach outlet ',
      ' asics gel ',' chanel london ',' gucci ',
      ' jordan shoe',' adidas ',' canada goose ',
      ' adidas ','canada goose','gucci ','ray ban',
      'air jordan','chanel keychain','cheap jordan',
      'cosmetics online','urban decay','lorac palette',
      '27\.150\.230\.','59\.58\.156\.','175\.44\.46\.',
      '117\.26\.81\.','112\.109\.234\.212','27\.153\.239\.114',
      '121\.205\.238\.232','27\.159\.239\.65','117\.26\.195\.193',
      '27\.153\.235\.2','\.penonsale\.','120\.37\.206\.32',
      ' prada ','91\.207\.7\.110','27\.153\.234\.226',
      '222\.77\.206\.193','140\.237\.15\.211','hawaii-stuff',
      'tribos.biz','CANADA GOOSE','lebronjames-shoes','gcc.us.com',
      ' nike ','adriobranco','2a00:1210:fffe:196::1',
      '112\.111\.183\.208','112\.111\.166\.2','175\.42\.83\.141',
      '175\.42\.83\.141','175\.42\.81\.200','62\.210\.129\.235',
      ' Saint Laurent ','58\.22\.139\.167','58\.22\.150\.236',
      '80\.116\.30\.100','183\.252\.2\.219','socialwebpromotions',
      'game for android','37\.187\.93\.152','121\.205\.214\.129',
      '110\.85\.75\.198','36\.248\.167\.181','cb8isesb',
      'Seiko Watches','117\.26\.248\.64','192\.187\.115\.98',
      ' jimmy choo ','110\.89\.40\.242','91\.197\.49\.162',
      '175\.44\.53\.8','mimsa.com',' oakley ','newbalance',
      ' hermes ','marc jacobs','louboutin',' kors ',
      'ralph lauren',' lacoste ',' perfumes ','27\.159\.253\.183',
      'archeage','198\.15\.178\.42',' chanel ','120\.43\.11\.227',
      '72\.46\.141\.50','72\.46\.128\.122','72\.46\.141\.50',
      ' jersey',' jerseys',' jordan ',' mont blanc ',' lipstick mac',
      ' tiffany ','208\.66\.77\.','72\.46\.153\.202','63\.141\.249\.27',
      '192\.187\.114\.154','76\.164\.226\.131','104\.194\.26\.',
      '76\.164\.234\.138','72\.46\.153\.203','208\.110\.84\.34',
      '142\.54\.173\.90','72\.46\.140\.101','173\.208\.182\.74',
      '91\.200\.12\.18','No Prescription','Erectile Dysfunction',
      '193\.201\.224\.68','110\.89\.26\.5','120\.40\.144\.104',
      '27\.150\.228\.147','north face','Louis Vuitton',
      '220\.161\.169\.',' FiFa 15 ','69\.30\.254\.','107\.150\.47\.138',
      '76\.164\.192\.',
      '\.cricket/', '\.bid/', '\.top/', '\.kim/',
      '\.trade/', '\.xyz/', '\.in/', '\.red/', '\.webcam/',
      '\.xyz/', '\.link/', 
);

chdir "/home/www/attach";

foreach my $flag (@flags)
   {
    my $ifh = IO::File->new("grep -l \'$flag\' *|");
    next if(!defined $ifh);
    while(my $line = <$ifh>)
      {
        chomp($line);
        next if(!($line =~ /^msg/));
        system("rm $line");
      }
  }
