summaryrefslogtreecommitdiff
path: root/FS
diff options
context:
space:
mode:
authorivan <ivan>2004-05-19 13:41:27 +0000
committerivan <ivan>2004-05-19 13:41:27 +0000
commitb970f3999c9a49146ec2f5224a60057723304d1b (patch)
treeeef75222570330423c44f50c9e7d38c1059c9e2b /FS
parent6b9a0a746535a142c95a5841f5cb5d9dc44d2f21 (diff)
adding acct_sql export
Diffstat (limited to 'FS')
-rw-r--r--FS/FS/part_export/acct_sql.pm140
-rw-r--r--FS/MANIFEST3
-rw-r--r--FS/t/part_export-acct_sql.t5
3 files changed, 148 insertions, 0 deletions
diff --git a/FS/FS/part_export/acct_sql.pm b/FS/FS/part_export/acct_sql.pm
new file mode 100644
index 000000000..0c3b49197
--- /dev/null
+++ b/FS/FS/part_export/acct_sql.pm
@@ -0,0 +1,140 @@
+package FS::part_export::acct_sql;
+
+use vars qw(@ISA %info @saltset);
+use Tie::IxHash;
+#use Digest::MD5 qw(md5_hex);
+use FS::Record; #qw(qsearchs);
+use FS::part_export;
+
+@ISA = qw(FS::part_export);
+
+tie my %options, 'Tie::IxHash',
+ 'datasrc' => { label => 'DBI data source' },
+ 'username' => { label => 'Database username' },
+ 'password' => { label => 'Database password' },
+;
+
+%info = (
+ 'svc' => 'svc_acct',
+ 'desc' => 'Real-time export of accounts to SQL databases '.
+ '(Postfix+Courier IMAP, others?)',
+ 'options' => \%options,
+ 'nodomain' => '',
+ 'notes' => <<END
+Export accounts (svc_acct records) to SQL databases. Written for
+Postfix+Courier IMAP but intended to be generally useful for generic SQL
+exports eventually.
+
+In contrast to sqlmail, this is newer and less well tested, and currently less
+flexible. It is intended to export just svc_acct records only, rather than a
+single export for svc_acct, svc_forward and svc_domain records, and to
+be configured for different mail server setups through some subclassing
+rather than options.
+END
+);
+
+@saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
+
+#mapping needs to be configurable...
+# export col => freeside col/method or callback
+my %map = (
+ 'username' => 'email',
+ 'password' => '_password',
+ 'crypt' => sub {
+ my $svc_acct = shift;
+ #false laziness w/shellcommands.pm
+ #eventually should check a "password-encoding" field
+ if ( length($svc_acct->_password) == 13
+ || $svc_acct->_password =~ /^\$(1|2a?)\$/ ) {
+ $svc_acct->_password;
+ } else {
+ crypt(
+ $svc_acct->_password,
+ $saltset[int(rand(64))].$saltset[int(rand(64))]
+ );
+ }
+
+ },
+ 'name' => 'finger',
+ 'maildir' => sub { shift->domain. '/maildirs/'. shift->username. '/' },
+ 'domain' => sub { shift->domain },
+ 'svcnum' => 'svcnum',
+);
+
+my $table = 'mailbox'; #also needs to be configurable...
+
+my $primary_key = 'username';
+
+sub rebless { shift; }
+
+sub _export_insert {
+ my($self, $svc_acct) = (shift, shift);
+
+
+ my %record = map { my $value = $map{$_};
+ $_ => ( ref($value)
+ ? &{$value}($svc_acct)
+ : $svc_acct->$value()
+ );
+ } keys %map;
+
+ my $err_or_queue =
+ $self->acct_sql_queue( $svc_acct->svcnum, 'insert', $table, %record );
+ return $err_or_queue unless ref($err_or_queue);
+
+ '';
+
+}
+
+sub _export_replace {
+}
+
+sub _export_delete {
+ my ( $self, $svc_acct ) = (shift, shift);
+ my $keymap = $map{$primary_key};
+ my $err_or_queue = $self->acct_sql_queue(
+ $svc_acct->svcnum,
+ 'delete',
+ $table,
+ $primary_key => ref($keymap) ? &{$keymap}($svc_acct) : $svc_acct->$keymap()
+ );
+}
+
+sub acct_sql_queue {
+ my( $self, $svcnum, $method ) = (shift, shift, shift);
+ my $queue = new FS::queue {
+ 'svcnum' => $svcnum,
+ 'job' => "FS::part_export::acct_sql::acct_sql_$method",
+ };
+ $queue->insert(
+ $self->option('datasrc'),
+ $self->option('username'),
+ $self->option('password'),
+ @_,
+ ) or $queue;
+}
+
+sub acct_sql_insert { #subroutine, not method
+ my $dbh = acct_sql_connect(shift, shift, shift);
+ my( $table, %record ) = @_;
+
+ my $sth = $dbh->prepare(
+ "INSERT INTO $table ( ". join(", ", keys %record).
+ " ) VALUES ( ". join(", ", map '?', keys %record ). " )"
+ ) or die $dbh->errstr;
+
+ $sth->execute( map $record{$_}, keys %record )
+ or die "can't insert into $table table: ". $sth->errstr;
+
+ $dbh->disconnect;
+}
+
+sub acct_sql_connect {
+ #my($datasrc, $username, $password) = @_;
+ #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
+ DBI->connect(@_) or die $DBI::errstr;
+}
+
+1;
+
+
diff --git a/FS/MANIFEST b/FS/MANIFEST
index 675429e04..e9c673dc2 100644
--- a/FS/MANIFEST
+++ b/FS/MANIFEST
@@ -68,6 +68,7 @@ FS/part_bill_event.pm
FS/export_svc.pm
FS/part_export.pm
FS/part_export_option.pm
+FS/part_export/acct_sql.pm
FS/part_export/apache.pm
FS/part_export/bind.pm
FS/part_export/bind_slave.pm
@@ -158,6 +159,8 @@ t/part_bill_event.t
t/export_svc.t
t/part_export.t
t/part_export_option.t
+t/part_export-acct_sql.t
+t/part_export-apache.t
t/part_export-bind.t
t/part_export-bind_slave.t
t/part_export-bsdshell.t
diff --git a/FS/t/part_export-acct_sql.t b/FS/t/part_export-acct_sql.t
new file mode 100644
index 000000000..9eed47259
--- /dev/null
+++ b/FS/t/part_export-acct_sql.t
@@ -0,0 +1,5 @@
+BEGIN { $| = 1; print "1..1\n" }
+END {print "not ok 1\n" unless $loaded;}
+use FS::part_export::acct_sql;
+$loaded=1;
+print "ok 1\n";