File: convert_awl_dbm_to_sql

package info (click to toggle)
spamassassin 3.1.7-2
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 5,376 kB
  • ctags: 2,123
  • sloc: perl: 39,706; ansic: 3,133; sh: 1,344; sql: 170; makefile: 168
file content (100 lines) | stat: -rw-r--r-- 3,165 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl

# WARNING: This script is VERY rough and provided only as a template
# for moving a DB based autowhitelist to a SQL based one.  You should
# do backups and that sort of thing before attempting to use this
# script.

use strict;
use Fcntl;

use Getopt::Long;

use DBI;

use DB_File ;
use vars qw( %h $k $v ) ;

sub usage {
  print "This program takes the following required arguments:\n";
  print "--username <username> - This is who's whitelist you are loading.\n";
  print "                        It should match exactly what spassassin or\n";
  print "                        spamd will be using.\n";
  print "--dsn <dsn> - This is the database DSN.  It should be in the form:\n";
  print "              DBI:driver:database:hostname[:port]\n";
  print "              Consult your database drivers docs for more info.\n";
  print "--ok - Basically a sanity check that you understand how dangerous this script is.\n";
  print "\n";
  print "This program take the following optional arguments:\n";
  print "--dbautowhitelist <path>  - path to the auto-whitelist you wish to\n";
  print "                            convert. Default is to use \n";
  print "                            \$ENV{HOME}/.spamassassin/auto-whitelist\n";
  print "--sqlusername <username> - Needed if your DBI driver requires a username.\n";
  print "--sqlpassword <password> - Needed if your DBI driver requires a password.\n";
  print "\n\n";
  print "WARNING: This script is VERY rough and not well tested.  You should\n";
  print "use extreme caution when working with it.  Including backing up your\n";
  print "data and all that other good stuff.\n";
  print "Passing of the --ok flag means you read this warning.\n";
  print "\n";
  exit;
}

my %opt;

GetOptions('dsn=s' => \$opt{'dsn'},
           'sqlusername=s' => \$opt{'sqlusername'},
           'sqlpassword=s' => \$opt{'sqlpassword'},
           'dbautowhitelist=s' => \$opt{'dbautowhitelist'},
           'username=s' => \$opt{'username'},
           'help' => \$opt{'help'},
           'ok' => \$opt{'ok'},
           );

if ($opt{'help'}) {
  usage();
}

if (!$opt{'ok'}) {
  usage();
}


if (!$opt{'username'} || !$opt{'dsn'}) {
  usage();
}

my $db;
if ($opt{'dbautowhitelist'}) {
  $db = $opt{'dbautowhitelist'};
}
else {
  $db = $ENV{HOME}."/.spamassassin/auto-whitelist";
}

tie %h, "DB_File",$db, O_RDONLY,0600
    or die "Cannot open file $db: $!\n";

my $dbh = DBI->connect($opt{'dsn'}, $opt{'sqlusername'}, $opt{'sqlpassword'})
    or die $DBI::errstr;

my $sth = $dbh->prepare("DELETE FROM awl WHERE username = ?");
$sth->execute($opt{'username'});

my $sth = $dbh->prepare("INSERT INTO awl (username,email,ip,count,totscore) VALUES (?,?,?,?,?)");

my @k = grep(!/totscore$/,keys(%h));
for my $key (@k) {
  my $totscore = $h{"$key|totscore"};
  my $count = $h{$key};
  if(defined($totscore)) {
    my ($email, $ip) = split(/\|ip=/, $key);

    if ($email && $ip) {
      my $rc = $sth->execute($opt{'username'}, $email, $ip, $count, $totscore);
      printf "% 8.1f %15s  --  %s\n", $totscore/$count, (sprintf "(%.1f/%d)",$totscore/$count, $count), $key;
    }
  }
}
untie %h;
$dbh->disconnect();