1 package FS::part_export::acct_sql;
3 use vars qw(@ISA %info);
5 #use Digest::MD5 qw(md5_hex);
6 use FS::Record; #qw(qsearchs);
9 @ISA = qw(FS::part_export);
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.',
20 'static' => { label =>
21 'Database schema mapping to static values.',
24 'primary_key' => { label => 'Database primary key' },
25 'crypt' => { label => 'Password encryption',
26 type=>'select', options=>[qw(crypt md5)],
31 tie my %vpopmail_map, 'Tie::IxHash',
32 'pw_name' => 'username',
33 'pw_domain' => 'domain',
34 'pw_passwd' => 'crypt_password',
37 'pw_gecos' => 'finger',
39 #'pw_shell' => 'shell',
40 'pw_shell' => 'quota',
42 my $vpopmail_map = join('\n', map "$_ $vpopmail_map{$_}", keys %vpopmail_map );
44 tie my %postfix_courierimap_mailbox_map, 'Tie::IxHash',
45 'username' => 'email',
46 'password' => '_password',
47 'crypt' => 'crypt_password',
49 'maildir' => 'virtual_maildir',
53 my $postfix_courierimap_mailbox_map =
54 join('\n', map "$_ $postfix_courierimap_mailbox_map{$_}",
55 keys %postfix_courierimap_mailbox_map );
57 tie my %postfix_courierimap_alias_map, 'Tie::IxHash',
63 my $postfix_courierimap_alias_map =
64 join('\n', map "$_ $postfix_courierimap_alias_map{$_}",
65 keys %postfix_courierimap_alias_map );
67 tie my %postfix_native_mailbox_map, 'Tie::IxHash',
71 'password' => 'ldap_password',
72 'mail' => 'domain_slash_username',
74 my $postfix_native_mailbox_map =
75 join('\n', map "$_ $postfix_native_mailbox_map{$_}",
76 keys %postfix_native_mailbox_map );
80 'desc' => 'Real-time export of accounts to SQL databases '.
81 '(vpopmail, Postfix+Courier IMAP, others?)',
82 'options' => \%options,
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.
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.
95 <BR><BR>Use these buttons for some useful presets:
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";
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";
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";
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";
121 sub _schema_map { shift->_map('schema'); }
122 sub _static_map { shift->_map('static'); }
126 map { /^\s*(\S+)\s*(\S+)\s*$/ } split("\n", $self->option(shift) );
129 sub rebless { shift; }
132 my($self, $svc_acct) = (shift, shift);
134 my %schema = $self->_schema_map;
135 my %static = $self->_static_map;
139 ( map { $_ => $static{$_} } keys %static ),
141 ( map { my $value = $schema{$_};
143 push @arg, $self->option('crypt')
144 if $value eq 'crypt_password' && $self->option('crypt');
145 $_ => $svc_acct->$value(@arg);
152 $self->acct_sql_queue(
155 $self->option('table'),
158 return $err_or_queue unless ref($err_or_queue);
164 sub _export_replace {
165 my($self, $new, $old) = (shift, shift, shift);
167 my %schema = $self->_schema_map;
168 my %static = $self->_static_map;
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();
177 my $keymap = $schema{$self->option('primary_key')};
178 push @primary_key, $old->$keymap();
183 ( map { $_ => $static{$_} } keys %static ),
185 ( map { my $value = $schema{$_};
187 push @arg, $self->option('crypt')
188 if $value eq 'crypt_password' && $self->option('crypt');
189 $_ => $new->$value(@arg);
195 my $err_or_queue = $self->acct_sql_queue(
198 $self->option('table'),
199 $self->option('primary_key'), @primary_key,
202 return $err_or_queue unless ref($err_or_queue);
207 my ( $self, $svc_acct ) = (shift, shift);
209 my %schema = $self->_schema_map;
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();
218 my $keymap = $schema{$self->option('primary_key')};
219 $primary_key{ $self->option('primary_key') } = $svc_acct->$keymap(),
222 my $err_or_queue = $self->acct_sql_queue(
225 $self->option('table'),
227 #$self->option('primary_key') => $svc_acct->$keymap(),
229 return $err_or_queue unless ref($err_or_queue);
234 my( $self, $svcnum, $method ) = (shift, shift, shift);
235 my $queue = new FS::queue {
237 'job' => "FS::part_export::acct_sql::acct_sql_$method",
240 $self->option('datasrc'),
241 $self->option('username'),
242 $self->option('password'),
247 sub acct_sql_insert { #subroutine, not method
248 my $dbh = acct_sql_connect(shift, shift, shift);
249 my( $table, %record ) = @_;
251 my $sth = $dbh->prepare(
252 "INSERT INTO $table ( ". join(", ", keys %record).
253 " ) VALUES ( ". join(", ", map '?', keys %record ). " )"
254 ) or die $dbh->errstr;
256 $sth->execute( values(%record) )
257 or die "can't insert into $table table: ". $sth->errstr;
262 sub acct_sql_delete { #subroutine, not method
263 my $dbh = acct_sql_connect(shift, shift, shift);
264 my( $table, %record ) = @_;
266 my $sth = $dbh->prepare(
267 "DELETE FROM $table WHERE ". join(' AND ', map "$_ = ? ", keys %record )
268 ) or die $dbh->errstr;
270 $sth->execute( map $record{$_}, keys %record )
271 or die "can't delete from $table table: ". $sth->errstr;
276 sub acct_sql_replace { #subroutine, not method
277 my $dbh = acct_sql_connect(shift, shift, shift);
279 my( $table, $pkey ) = ( shift, shift );
281 my %primary_key = ();
282 if ( $pkey =~ /,/ ) {
283 foreach my $key ( split(/\s*,\s*/, $pkey ) ) {
284 $primary_key{$key} = shift;
287 $primary_key{$pkey} = shift;
292 my $sth = $dbh->prepare(
294 ' SET '. join(', ', map "$_ = ?", keys %record ).
295 ' WHERE '. join(' AND ', map "$_ = ?", keys %primary_key )
296 ) or die $dbh->errstr;
298 $sth->execute( values(%record), values(%primary_key) );
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;