Changeset 80 for trunk/uribl/URIdb.pm


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

/trunk/uribl

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

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.