Changeset 416 for trunk/cgi-bin/IPDB.pm


Ignore:
Timestamp:
05/18/10 18:08:12 (14 years ago)
Author:
Kris Deugau
Message:

/trunk

Update mailNotify to use new table notify to decide who to spam
with notices on various events. See #2.

  • existing notification calls updated. Still need to move calls into IPDB.pm's subs
  • IPDB.pm and MyIPDB.pm updated with new semiglobal vars for org name, SMTP host, domain. Will add others as needed so MyIPDB.pm can be moved to /etc/ipdb/ (see #17).

Internal allocation types will need to be rearranged somewhat to
make full use of the flexibility possible. See #20.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/cgi-bin/IPDB.pm

    r404 r416  
    2020use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
    2121
    22 $VERSION        = 2.0;
     22$VERSION        = 2.6;
    2323@ISA            = qw(Exporter);
    2424@EXPORT_OK    = qw(
     
    5252our %bigfree;
    5353our %IPDBacl;
     54
     55our $orgname = 'Example Corp';
     56our $smtphost = 'smtp.example.com';
     57our $domain = 'example.com';
    5458
    5559# Let's initialize the globals.
     
    806810## IPDB::mailNotify()
    807811# Sends notification mail to recipients regarding an IPDB operation
    808 sub mailNotify ($$$) {
    809   my ($recip,$subj,$message) = @_;
    810   my $mailer = Net::SMTP->new("smtp.example.com", Hello => "ipdb.example.com");
    811 
    812   $mailer->mail('ipdb@example.com');
    813   $mailer->to($recip);
    814   $mailer->data("From: \"IP Database\" <ipdb\@example.com>\n",
     812sub mailNotify {
     813  my $dbh = shift;
     814  my ($action,$subj,$message) = @_;
     815
     816# split action into parts for fiddlement.  nb: there are almost certainly better ways to do this.
     817  my @actionbits = ($action =~ /^(.)(.)(.)$/);
     818
     819  # want to notify anyone who has specifically requested notify on *this* type ($action as passed),
     820  # on "all static IP types" or "all pool types" (and other last-char-in-type groupings), on eg "all DSL types",
     821  # and "all events with this action"
     822  my @actionsets = ($action);
     823##fixme: ick, eww.  really gotta find a better way to handle this...
     824  push @actionsets, ($actionbits[0].'.'.$actionbits[2],
     825        $actionbits[0].$actionbits[1].'.', $actionbits[0].'a') if $action =~ /^.{3}$/;
     826
     827  my $mailer = Net::SMTP->new($smtphost, Hello => "ipdb.$domain");
     828
     829  # get recip list from db
     830  my $sth = $dbh->prepare("SELECT reciplist FROM notify WHERE action=?");
     831
     832  my @reciplist;
     833  foreach (@actionsets) {
     834    $sth->execute($action);
     835##fixme - need to handle db errors
     836    my ($recipsub) = $sth->fetchrow_array;
     837    next if !$recipsub;
     838    foreach (split(/,/, $recipsub)) {
     839      push @reciplist, $_ if !grep {/^$_$/} @reciplist;
     840    }
     841  }
     842
     843  foreach my $recip (@reciplist) {
     844    $mailer->mail("ipdb\@$domain");
     845    $mailer->to($recip);
     846    $mailer->data("From: \"IP Database\" <ipdb\@$domain>\n",
    815847        "To: $recip\n",
    816848        "Date: ".strftime("%a, %d %b %Y %H:%M:%S %z",localtime)."\n",
    817849        "Subject: {IPDB} $subj\n",
    818850        "X-Mailer: IPDB Notify v".sprintf("%.1d",$IPDB::VERSION)."\n",
    819         "Organization: Example Corp\n",
     851        "Organization: $orgname\n",
    820852        "\n$message\n");
     853  }
    821854  $mailer->quit;
    822855}
Note: See TracChangeset for help on using the changeset viewer.