move export info to the modules themselves
[freeside.git] / FS / FS / part_export / sqlmail.pm
index 0c0cb36..6d61e0e 100644 (file)
@@ -1,12 +1,49 @@
 package FS::part_export::sqlmail;
 
-use vars qw(@ISA);
+use vars qw(@ISA %info);
+use Tie::IxHash;
+use Digest::MD5 qw(md5_hex);
 use FS::Record qw(qsearchs);
 use FS::part_export;
-use Digest::MD5 qw(md5_hex);
+use FS::svc_domain;
 
 @ISA = qw(FS::part_export);
 
+tie my %options, 'Tie::IxHash',
+  'datasrc'            => { label => 'DBI data source' },
+  'username'           => { label => 'Database username' },
+  'password'           => { label => 'Database password' },
+  'server_type'        => {
+    label   => 'Server type',
+    type    => 'select',
+    options => [qw(dovecot_plain dovecot_crypt dovecot_digest_md5 courier_plain
+                   courier_crypt)],
+    default => ['dovecot_plain'], },
+  'svc_acct_table'     => { label => 'User Table', default => 'user_acct' },
+  'svc_forward_table'  => { label => 'Forward Table', default => 'forward' },
+  'svc_domain_table'   => { label => 'Domain Table', default => 'domain' },
+  'svc_acct_fields'    => { label => 'svc_acct Export Fields',
+                            default => 'username _password domsvc svcnum' },
+  'svc_forward_fields' => { label => 'svc_forward Export Fields',
+                            default => 'domain svcnum catchall' },
+  'svc_domain_fields'  => { label => 'svc_domain Export Fields',
+                            default => 'srcsvc dstsvc dst' },
+  'resolve_dstsvc'     => { label => q{Resolve svc_forward.dstsvc to an email address and store it in dst. (Doesn't require that you also export dstsvc.)},
+                            type => 'checkbox' },
+;
+
+%info = (
+  'svc'      => [qw( svc_acct svc_domain svc_forward )],
+  'desc'     => 'Real-time export to SQL-backed mail server',
+  'options'  => \%options,
+  'nodomain' => '',
+  'notes'    => <<'END'
+Database schema can be made to work with Courier IMAP, Exim and Dovecot.
+Others could work but are untested.  (more detailed description from
+Kristian / fire2wire? )
+END
+);
+
 sub rebless { shift; }
 
 sub _export_insert {
@@ -115,17 +152,19 @@ sub sqlmail_replace {
   my %attrs = @_;
   map { $attrs{$_} = $attrs{$_} ? qq!'$attrs{$_}'! : 'NULL'; } keys(%attrs);
 
-  my $query = sprintf('UPDATE %s SET %s WHERE svcnum = %s',
-                      $table, join(', ', map {"$_ = $attrs{$_}"} keys(%attrs)),
-                      $oldsvcnum);
-
-  my $rv = $dbh->do($query) or die $dbh->errstr;
-
-  if ($rv == 0) {
+  my $query = "SELECT COUNT(*) FROM $table WHERE svcnum = $oldsvcnum";
+  my $result = $dbh->selectrow_arrayref($query) or die $dbh->errstr;
+  
+  if (@$result[0] == 0) {
     $query = sprintf("INSERT INTO %s (%s) values (%s)",
                      $table, join(",", keys(%attrs)),
                      join(',', values(%attrs)));
     $dbh->do($query) or die $dbh->errstr;
+  } else {
+    $query = sprintf('UPDATE %s SET %s WHERE svcnum = %s',
+                     $table, join(', ', map {"$_ = $attrs{$_}"} keys(%attrs)),
+                     $oldsvcnum);
+    $dbh->do($query) or die $dbh->errstr;
   }
 
   $dbh->disconnect;
@@ -142,7 +181,7 @@ sub update_values {
   # Update records to conform to a particular server_type.
 
   my ($self, $svc, $svcdb) = (shift,shift,shift);
-  my $svchash = $svc->hashref or return '';
+  my $svchash = { %{$svc->hashref} } or return ''; # We need a copy.
 
   if ($svcdb eq 'svc_acct') {
     if ($self->option('server_type') eq 'courier_crypt') {
@@ -177,3 +216,5 @@ sub update_values {
 
 }
 
+1;
+