Changeset 80


Ignore:
Timestamp:
09/11/25 16:28:23 (3 days ago)
Author:
Kris Deugau
Message:

/trunk/uribl

Convert module to more proper object with embedded DB bits
Add module-finding blurb

Location:
trunk/uribl
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/uribl/URIdb.pm

    r41 r80  
    33##
    44# $Id$
    5 # Copyright 2010 Kris Deugau <kdeugau@deepnet.cx>
     5# Copyright 2010,2025 Kris Deugau <kdeugau@deepnet.cx>
    66#
    77#    This program is free software: you can redistribute it and/or modify
     
    2929use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
    3030
    31 $VERSION        = 1.0;
     31$VERSION        = 2.0;
    3232@ISA            = qw(Exporter);
    33 @EXPORT_OK      = qw(
    34         );
    35 
    36 @EXPORT         = (); # Export nothing by default.
     33@EXPORT_OK      = qw( $dbh );
     34
     35@EXPORT         = ( $dbh );
     36
    3737%EXPORT_TAGS    = ( ALL => [qw(
    3838                )]
     
    6060# basic object subs
    6161sub new {
    62 # iff we want to start taking arguments, or doing other things on instantiation
    63 #  my $self = {};
    64 #  bless $self, "DNSBL";
    65 #  return $self;
    66   bless {};
     62  my $this = shift;
     63  my $class = ref($this) || $this;
     64  my %args = @_;
     65
     66  # Prepopulate a basic config.  Note some of these *will* cause errors if left unset.
     67  my %defconfig = (
     68        dbhost => "localhost",
     69        dbname => "dnsbl",
     70        dbuser => "dnsbl",
     71        dbpass => "spambgone",
     72  );
     73
     74  my %siteconfig;
     75  my $dbhost;
     76  my $dbname;
     77  my $dbuser;
     78  my $dbpass;
     79  if (defined($args{configfile})) {
     80    if (-e $args{configfile} && -f $args{configfile}) {
     81      my $ret = eval `cat $args{configfile}`;
     82      unless ($ret) {
     83        if ($@) { $errstr = "couldn't parse $args{configfile}: $@\n"; return; }
     84        if (!defined($ret)) { $errstr = "couldn't load $args{configfile}: $!\n"; return; }
     85        if (!$ret) { $errstr = "couldn't load $args{configfile}\n"; return; }
     86      }
     87      # crossload legacy variables, but prefer new %siteconfig values
     88      $siteconfig{dbhost} = $dbhost if !$siteconfig{dbhost} && $dbhost;
     89      $siteconfig{dbname} = $dbname if !$siteconfig{dbname} && $dbname;
     90      $siteconfig{dbuser} = $dbuser if !$siteconfig{dbuser} && $dbuser;
     91      $siteconfig{dbpass} = $dbpass if !$siteconfig{dbpass} && $dbpass;
     92    }
     93  }
     94
     95  # Assemble the object.  Apply configuration hashes in order of precedence.
     96  my $self = {
     97        # Hardcoded defaults
     98        %defconfig,
     99        # Default config file OR caller-specified one, loaded above
     100        %siteconfig,
     101        # Caller-specified arguments
     102        %args
     103        };
     104  bless $self, $class;
     105
     106  return $self;
    67107}
    68108
     
    82122sub connect {
    83123  my $self = shift;
    84   my $dbhost = shift;
    85   my $dbname = shift;
    86   my $dbuser = shift;
    87   my $dbpass = shift;
    88124  ## want to NOT autocommit everything, it's unlikely we'll step on our own toes but...
    89   $dbh = DBI->connect("DBI:Pg:host=$dbhost;dbname=$dbname", $dbuser, $dbpass, {
     125  $dbh = DBI->connect("DBI:Pg:host=$self->{dbhost};dbname=$self->{dbname}", $self->{dbuser}, $self->{dbpass}, {
    90126        AutoCommit => 0,
    91127        PrintError => 1
  • trunk/uribl/delist-uri

    r41 r80  
    33##
    44# $Id$
    5 # Copyright 2010 Kris Deugau <kdeugau@deepnet.cx>
     5# Copyright 2010,2025 Kris Deugau <kdeugau@deepnet.cx>
    66#
    77#    This program is free software: you can redistribute it and/or modify
     
    2323use DBI;
    2424
    25 use URIdb;
     25# push "the directory the script is in" into @INC
     26use FindBin;
     27use lib "$FindBin::RealBin/";
     28
     29use URIdb 2.0;
    2630
    2731my $uridb = new URIdb;
    2832
    29 # default DB info - all other settings should be loaded from the DB.
    30 my $dbhost = "localhost";
    31 my $dbname = "dnsbl";
    32 my $dbuser = "dnsbl";
    33 my $dbpass = "spambgone";
     33die "Usage: delist-uri <domain>\n"
     34        if !$ARGV[1];
    3435
    35 die "Usage: delist-uri <domain>\n"
    36         if !$ARGV[0];
     36my $cfgname = shift @ARGV;
    3737
    38 # Load a config ref containing DB host, name, user, and pass info based on
    39 # from the server name + full script web path.  This allows us to host
    40 # multiple instances without having to duplicate the code.
    41 # This file is a Perl fragment to be processed inline.
    42 if (-e "/etc/uridb/uridb.conf") {
    43   my $cfg = `cat /etc/uridb/uridb.conf`;
    44   ($cfg) = ($cfg =~ /^(.+)$/s);         # avoid warnings, failures, and general nastiness with taint mode
    45   eval $cfg;
    46 }
    47 
    48 my $dbh = $uridb->connect($dbhost, $dbname, $dbuser, $dbpass);
     38my $uridb = new URIdb (configfile => "/etc/uridb/$cfgname.conf");
     39$uridb->connect;
    4940
    5041my %config;
  • trunk/uribl/export-uridb

    r41 r80  
    33##
    44# $Id$
    5 # Copyright 2010 Kris Deugau <kdeugau@deepnet.cx>
     5# Copyright 2010,2025 Kris Deugau <kdeugau@deepnet.cx>
    66#
    77#    This program is free software: you can redistribute it and/or modify
     
    2323use DBI;
    2424
    25 use URIdb;
     25# push "the directory the script is in" into @INC
     26use FindBin;
     27use lib "$FindBin::RealBin/";
     28
     29use URIdb 2.0;
    2630
    2731my $uridb = new URIdb;
    2832
    29 # default DB info - all other settings should be loaded from the DB.
    30 my $dbhost = "localhost";
    31 my $dbname = "uridb";
    32 my $dbuser = "uridb";
    33 my $dbpass = "spambgone";
     33die "Usage: export-uridb <list> [type]\n"
     34        if !$ARGV[0];
    3435
    35 warn "Need DNS data type argument\n" if !$ARGV[0];
     36my $cfgname = shift @ARGV;
     37
     38warn "Missing DNS data type argument, defaulting to rbldnsd\n" if !$ARGV[0];
    3639my $mode = shift @ARGV || 'rbldnsd';
    3740
    38 # Load a config ref containing DB host, name, user, and pass info based on
    39 # from the server name + full script web path.  This allows us to host
    40 # multiple instances without having to duplicate the code.
    41 # This file is a Perl fragment to be processed inline.
    42 if (-e "/etc/uridb/uridb.conf") {
    43   my $cfg = `cat /etc/uridb/uridb.conf`;
    44   ($cfg) = ($cfg =~ /^(.+)$/s);         # avoid warnings, failures, and general nastiness with taint mode
    45   eval $cfg;
    46 }
    47 
    48 my $dbh = $uridb->connect($dbhost, $dbname, $dbuser, $dbpass);
     41my $uridb = new URIdb (configfile => "/etc/uridb/$cfgname.conf");
     42$uridb->connect;
    4943
    5044my %config;
  • trunk/uribl/uridb.cgi

    r62 r80  
    33##
    44# $Id$
    5 # Copyright 2010 Kris Deugau <kdeugau@deepnet.cx>
     5# Copyright 2010,2025 Kris Deugau <kdeugau@deepnet.cx>
    66#
    77#    This program is free software: you can redistribute it and/or modify
     
    2222use warnings;
    2323no warnings qw(uninitialized);
     24
    2425use CGI::Carp qw (fatalsToBrowser);
    2526use CGI::Simple;
     
    2728use Encode;
    2829
    29 use URIdb;
     30# push "the directory the script is in" into @INC
     31use FindBin;
     32use lib "$FindBin::RealBin/";
     33
     34use URIdb 2.0;
    3035
    3136# Set up the CGI object...
     
    4045}
    4146
    42 my $uridb = new URIdb;
    43 
    4447print $q->header(-charset=>'utf8');
    45 
    46 # default DB info - all other settings should be loaded from the DB.
    47 my $dbhost = "localhost";
    48 my $dbname = "uridb";
    49 my $dbuser = "uridb";
    50 my $dbpass = "spambgone";
    5148
    5249#my %status = (0 => "Don't list",
     
    6461$cfgname =~ s|_uridb_cgi.*||;
    6562$cfgname =~ s|_$||;
    66 if (-e "/etc/uridb/$cfgname.conf") {
    67   my $cfg = `cat /etc/uridb/$cfgname.conf`;
    68   ($cfg) = ($cfg =~ /^(.+)$/s);         # avoid warnings, failures, and general nastiness with taint mode
    69   eval $cfg;
    70 }
    7163
    72 my $dbh = $uridb->connect($dbhost, $dbname, $dbuser, $dbpass);
     64my $uridb = new URIdb (configfile => "/etc/uridb/$cfgname.conf");
    7365
    7466my $page;
Note: See TracChangeset for help on using the changeset viewer.