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