8e102fcc1fbca1d79ca11e7331cd48128629bfb5
[freeside.git] / FS / FS / part_export / acct_sql.pm
1 package FS::part_export::acct_sql;
2
3 use vars qw(@ISA %info @saltset);
4 use Tie::IxHash;
5 #use Digest::MD5 qw(md5_hex);
6 use FS::Record; #qw(qsearchs);
7 use FS::part_export;
8
9 @ISA = qw(FS::part_export);
10
11 tie my %options, 'Tie::IxHash',
12   'datasrc'            => { label => 'DBI data source' },
13   'username'           => { label => 'Database username' },
14   'password'           => { label => 'Database password' },
15   'table'              => { label => 'Database table' },
16   'schema'             => { label =>
17                               'Database schema mapping to Freeside methods.',
18                             type  => 'textarea',
19                           },
20   'primary_key'        => { label => 'Database primary key' },
21 ;
22
23 tie my %postfix_courierimap_mailbox_map, 'Tie::IxHash',
24   'username' => 'email',
25   'password' => '_password',
26   'crypt'    => 'crypt_password',
27   'name'     => 'finger',
28   'maildir'  => 'virtual_maildir',
29   'domain'   => 'domain',
30   'svcnum'   => 'svcnum',
31 ;
32 my $postfix_courierimap_mailbox_map =
33   join('\n', map "$_ $postfix_courierimap_mailbox_map{$_}",
34                  keys %postfix_courierimap_mailbox_map      );
35
36 tie my %postfix_courierimap_alias_map, 'Tie::IxHash',
37   'address' => 'email',
38   'goto'    => 'email',
39   'domain'  => 'domain',
40   'svcnum'  => 'svcnum',
41 ;
42 my $postfix_courierimap_alias_map =
43   join('\n', map "$_ $postfix_courierimap_alias_map{$_}",
44                  keys %postfix_courierimap_alias_map      );
45
46 %info = (
47   'svc'      => 'svc_acct',
48   'desc'     => 'Real-time export of accounts to SQL databases '.
49                 '(Postfix+Courier IMAP, others?)',
50   'options'  => \%options,
51   'nodomain' => '',
52   'notes'    => <<END
53 Export accounts (svc_acct records) to SQL databases.  Written for
54 Postfix+Courier IMAP but intended to be generally useful for generic SQL
55 exports, eventually.
56
57 <BR><BR>In contrast to sqlmail, this is newer and less well tested, and
58 currently less flexible.  It is intended to export just svc_acct records only,
59 rather than a single export for svc_acct, svc_forward and svc_domain records,
60 to export in "default" formats rather than configure the MTA or POP/IMAP server
61 for a Freeside-specific schema, and possibly to be configured for different
62 mail server setups through some subclassing rather than options.
63
64 <BR><BR>Use these buttons for some useful presets:
65 <UL>
66   <LI><INPUT TYPE="button" VALUE="postfix_courierimap_mailbox" onClick='
67     this.form.table.value = "mailbox";
68     this.form.schema.value = "$postfix_courierimap_mailbox_map";
69     this.form.primary_key.value = "username";
70   '>
71   <LI><INPUT TYPE="button" VALUE="postfix_courierimap_alias" onClick='
72     this.form.table.value = "mailbox";
73     this.form.schema.value = "$postfix_courierimap_alias_map";
74     this.form.primary_key.value = "address";
75   '>
76 </UL>
77 END
78 );
79
80 sub _map {
81   my $self = shift;
82   map { /^\s*(\S+)\s*(\S+)\s*$/ } split("\n", $self->option('schema') );
83 }
84
85 sub rebless { shift; }
86
87 sub _export_insert {
88   my($self, $svc_acct) = (shift, shift);
89
90   my %map = $self->_map;
91
92   my %record = map { my $value = $map{$_};
93                      $_ => $svc_acct->$value();
94                    } keys %map;
95
96   my $err_or_queue =
97     $self->acct_sql_queue(
98       $svc_acct->svcnum,
99       'insert',
100       $self->option('table'),
101       %record
102     );
103   return $err_or_queue unless ref($err_or_queue);
104
105   '';
106
107 }
108
109 sub _export_replace {
110 }
111
112 sub _export_delete {
113   my ( $self, $svc_acct ) = (shift, shift);
114   my %map = $self->_map;
115   my $keymap = $map{$self->option('primary_key')};
116   my $err_or_queue = $self->acct_sql_queue(
117     $svc_acct->svcnum,
118     'delete',
119     $self->option('table'),
120     $self->option('primary_key') => $svc_acct->$keymap(),
121   );
122   return $err_or_queue unless ref($err_or_queue);
123   '';
124 }
125
126 sub acct_sql_queue {
127   my( $self, $svcnum, $method ) = (shift, shift, shift);
128   my $queue = new FS::queue {
129     'svcnum' => $svcnum,
130     'job'    => "FS::part_export::acct_sql::acct_sql_$method",
131   };
132   $queue->insert(
133     $self->option('datasrc'),
134     $self->option('username'),
135     $self->option('password'),
136     @_,
137   ) or $queue;
138 }
139
140 sub acct_sql_insert { #subroutine, not method
141   my $dbh = acct_sql_connect(shift, shift, shift);
142   my( $table, %record ) = @_;
143
144   my $sth = $dbh->prepare(
145     "INSERT INTO $table ( ". join(", ", keys %record).
146     " ) VALUES ( ". join(", ", map '?', keys %record ). " )"
147   ) or die $dbh->errstr;
148
149   $sth->execute( map $record{$_}, keys %record )
150     or die "can't insert into $table table: ". $sth->errstr;
151
152   $dbh->disconnect;
153 }
154
155 sub acct_sql_delete { #subroutine, not method
156   my $dbh = acct_sql_connect(shift, shift, shift);
157   my( $table, %record ) = @_;
158
159   my $sth = $dbh->prepare(
160     "DELETE FROM  $table WHERE ". join(' AND ', map "$_ = ? ", keys %record )
161   ) or die $dbh->errstr;
162
163   $sth->execute( map $record{$_}, keys %record )
164     or die "can't delete from $table table: ". $sth->errstr;
165
166   $dbh->disconnect;
167 }
168
169 sub acct_sql_connect {
170   #my($datasrc, $username, $password) = @_;
171   #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
172   DBI->connect(@_) or die $DBI::errstr;
173 }
174
175 1;
176
177