support static values in acct_sql as well
[freeside.git] / FS / FS / part_export / acct_sql.pm
1 package FS::part_export::acct_sql;
2
3 use vars qw(@ISA %info);
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   'static'             => { label =>
21                               'Database schema mapping to static values.',
22                             type  => 'textarea',
23                           },
24   'primary_key'        => { label => 'Database primary key' },
25   'crypt'              => { label => 'Password encryption',
26                             type=>'select', options=>[qw(crypt md5)],
27                             default=>'crypt',
28                           },
29 ;
30
31 tie my %vpopmail_map, 'Tie::IxHash',
32   'pw_name'   => 'username',
33   'pw_domain' => 'domain',
34   'pw_passwd' => 'crypt_password',
35   'pw_uid'    => 'uid',
36   'pw_gid'    => 'gid',
37   'pw_gecos'  => 'finger',
38   'pw_dir'    => 'dir',
39   #'pw_shell'  => 'shell',
40   'pw_shell'  => 'quota',
41 ;
42 my $vpopmail_map = join('\n', map "$_ $vpopmail_map{$_}", keys %vpopmail_map );
43
44 tie my %postfix_courierimap_mailbox_map, 'Tie::IxHash',
45   'username' => 'email',
46   'password' => '_password',
47   'crypt'    => 'crypt_password',
48   'name'     => 'finger',
49   'maildir'  => 'virtual_maildir',
50   'domain'   => 'domain',
51   'svcnum'   => 'svcnum',
52 ;
53 my $postfix_courierimap_mailbox_map =
54   join('\n', map "$_ $postfix_courierimap_mailbox_map{$_}",
55                  keys %postfix_courierimap_mailbox_map      );
56
57 tie my %postfix_courierimap_alias_map, 'Tie::IxHash',
58   'address' => 'email',
59   'goto'    => 'email',
60   'domain'  => 'domain',
61   'svcnum'  => 'svcnum',
62 ;
63 my $postfix_courierimap_alias_map =
64   join('\n', map "$_ $postfix_courierimap_alias_map{$_}",
65                  keys %postfix_courierimap_alias_map      );
66
67 tie my %postfix_native_mailbox_map, 'Tie::IxHash',
68   'userid'   => 'email',
69   'uid'      => 'uid',
70   'gid'      => 'gid',
71   'password' => 'ldap_password',
72 ;
73 my $postfix_native_mailbox_map =
74   join('\n', map "$_ $postfix_native_mailbox_map{$_}",
75                  keys %postfix_native_mailbox_map      );
76
77 %info = (
78   'svc'      => 'svc_acct',
79   'desc'     => 'Real-time export of accounts to SQL databases '.
80                 '(vpopmail, Postfix+Courier IMAP, others?)',
81   'options'  => \%options,
82   'nodomain' => '',
83   'notes'    => <<END
84 Export accounts (svc_acct records) to SQL databases.  Currently has default
85 configurations for vpopmail and Postfix+Courier IMAP but intended to be
86 configurable for other schemas as well.
87
88 <BR><BR>In contrast to sqlmail, this is intended to export just svc_acct
89 records only, rather than a single export for svc_acct, svc_forward and
90 svc_domain records, to export in "default" database schemas rather than
91 configure the MTA or POP/IMAP server for a Freeside-specific schema, and
92 to be configured for different mail server setups.
93
94 <BR><BR>Use these buttons for some useful presets:
95 <UL>
96   <li><INPUT TYPE="button" VALUE="vpopmail" onClick='
97     this.form.table.value = "vpopmail";
98     this.form.schema.value = "$vpopmail_map";
99     this.form.primary_key.value = "pw_name, pw_domain";
100   '>
101   <LI><INPUT TYPE="button" VALUE="postfix_courierimap_mailbox" onClick='
102     this.form.table.value = "mailbox";
103     this.form.schema.value = "$postfix_courierimap_mailbox_map";
104     this.form.primary_key.value = "username";
105   '>
106   <LI><INPUT TYPE="button" VALUE="postfix_courierimap_alias" onClick='
107     this.form.table.value = "alias";
108     this.form.schema.value = "$postfix_courierimap_alias_map";
109     this.form.primary_key.value = "address";
110   '>
111   <LI><INPUT TYPE="button" VALUE="postfix_native_mailbox" onClick='
112     this.form.table.value = "users";
113     this.form.schema.value = "$postfix_native_mailbox_map";
114     this.form.primary_key.value = "userid";
115   '>
116 </UL>
117 END
118 );
119
120 sub _schema_map { shift->_map('schema'); }
121 sub _static_map { shift->_map('static'); }
122
123 sub _map {
124   my $self = shift;
125   map { /^\s*(\S+)\s*(\S+)\s*$/ } split("\n", $self->option(shift) );
126 }
127
128 sub rebless { shift; }
129
130 sub _export_insert {
131   my($self, $svc_acct) = (shift, shift);
132
133   my %schema = $self->_schema_map;
134   my %static = $self->_static_map;
135
136   my %record = (
137
138     ( map { $_ => $static{$_} } keys %static ),
139   
140     ( map { my $value = $schema{$_};
141             my @arg = ();
142             push @arg, $self->option('crypt')
143               if $value eq 'crypt_password' && $self->option('crypt');
144             $_ => $svc_acct->$value(@arg);
145           } keys %schema
146     ),
147
148   );
149
150   my $err_or_queue =
151     $self->acct_sql_queue(
152       $svc_acct->svcnum,
153       'insert',
154       $self->option('table'),
155       %record
156     );
157   return $err_or_queue unless ref($err_or_queue);
158
159   '';
160
161 }
162
163 sub _export_replace {
164   my($self, $new, $old) = (shift, shift, shift);
165
166   my %schema = $self->_schema_map;
167   my %static = $self->_static_map;
168
169   my @primary_key = ();
170   if ( $self->option('primary_key') =~ /,/ ) {
171     foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) {
172       my $keymap = $schema{$key};
173       push @primary_key, $old->$keymap();
174     }
175   } else {
176     my $keymap = $schema{$self->option('primary_key')};
177     push @primary_key, $old->$keymap();
178   }
179
180   my %record = (
181
182     ( map { $_ => $static{$_} } keys %static ),
183   
184     ( map { my $value = $schema{$_};
185             my @arg = ();
186             push @arg, $self->option('crypt')
187               if $value eq 'crypt_password' && $self->option('crypt');
188             $_ => $new->$value(@arg);
189           } keys %schema
190     ),
191
192   );
193
194   my $err_or_queue = $self->acct_sql_queue(
195     $new->svcnum,
196     'replace',
197     $self->option('table'),
198     $self->option('primary_key'), @primary_key, 
199     %record,
200   );
201   return $err_or_queue unless ref($err_or_queue);
202   '';
203 }
204
205 sub _export_delete {
206   my ( $self, $svc_acct ) = (shift, shift);
207
208   my %schema = $self->_schema_map;
209
210   my %primary_key = ();
211   if ( $self->option('primary_key') =~ /,/ ) {
212     foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) {
213       my $keymap = $schema{$key};
214       $primary_key{ $key } = $svc_acct->$keymap();
215     }
216   } else {
217     my $keymap = $schema{$self->option('primary_key')};
218     $primary_key{ $self->option('primary_key') } = $svc_acct->$keymap(),
219   }
220
221   my $err_or_queue = $self->acct_sql_queue(
222     $svc_acct->svcnum,
223     'delete',
224     $self->option('table'),
225     %primary_key,
226     #$self->option('primary_key') => $svc_acct->$keymap(),
227   );
228   return $err_or_queue unless ref($err_or_queue);
229   '';
230 }
231
232 sub acct_sql_queue {
233   my( $self, $svcnum, $method ) = (shift, shift, shift);
234   my $queue = new FS::queue {
235     'svcnum' => $svcnum,
236     'job'    => "FS::part_export::acct_sql::acct_sql_$method",
237   };
238   $queue->insert(
239     $self->option('datasrc'),
240     $self->option('username'),
241     $self->option('password'),
242     @_,
243   ) or $queue;
244 }
245
246 sub acct_sql_insert { #subroutine, not method
247   my $dbh = acct_sql_connect(shift, shift, shift);
248   my( $table, %record ) = @_;
249
250   my $sth = $dbh->prepare(
251     "INSERT INTO $table ( ". join(", ", keys %record).
252     " ) VALUES ( ". join(", ", map '?', keys %record ). " )"
253   ) or die $dbh->errstr;
254
255   $sth->execute( values(%record) )
256     or die "can't insert into $table table: ". $sth->errstr;
257
258   $dbh->disconnect;
259 }
260
261 sub acct_sql_delete { #subroutine, not method
262   my $dbh = acct_sql_connect(shift, shift, shift);
263   my( $table, %record ) = @_;
264
265   my $sth = $dbh->prepare(
266     "DELETE FROM $table WHERE ". join(' AND ', map "$_ = ? ", keys %record )
267   ) or die $dbh->errstr;
268
269   $sth->execute( map $record{$_}, keys %record )
270     or die "can't delete from $table table: ". $sth->errstr;
271
272   $dbh->disconnect;
273 }
274
275 sub acct_sql_replace { #subroutine, not method
276   my $dbh = acct_sql_connect(shift, shift, shift);
277
278   my( $table, $pkey ) = ( shift, shift );
279
280   my %primary_key = ();
281   if ( $pkey =~ /,/ ) {
282     foreach my $key ( split(/\s*,\s*/, $pkey ) ) {
283       $primary_key{$key} = shift;
284     }
285   } else {
286     $primary_key{$pkey} = shift;
287   }
288
289   my %record = @_;
290
291   my $sth = $dbh->prepare(
292     "UPDATE $table".
293     ' SET '.   join(', ',    map "$_ = ?", keys %record      ).
294     ' WHERE '. join(' AND ', map "$_ = ?", keys %primary_key )
295   ) or die $dbh->errstr;
296
297   $sth->execute( values(%record), values(%primary_key) );
298
299   $dbh->disconnect;
300 }
301
302 sub acct_sql_connect {
303   #my($datasrc, $username, $password) = @_;
304   #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
305   DBI->connect(@_) or die $DBI::errstr;
306 }
307
308 1;
309