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',
73 my $postfix_native_mailbox_map =
74 join('\n', map "$_ $postfix_native_mailbox_map{$_}",
75 keys %postfix_native_mailbox_map );
79 'desc' => 'Real-time export of accounts to SQL databases '.
80 '(vpopmail, Postfix+Courier IMAP, others?)',
81 'options' => \%options,
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.
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.
94 <BR><BR>Use these buttons for some useful presets:
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";
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";
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";
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";
120 sub _schema_map { shift->_map('schema'); }
121 sub _static_map { shift->_map('static'); }
125 map { /^\s*(\S+)\s*(\S+)\s*$/ } split("\n", $self->option(shift) );
128 sub rebless { shift; }
131 my($self, $svc_acct) = (shift, shift);
133 my %schema = $self->_schema_map;
134 my %static = $self->_static_map;
138 ( map { $_ => $static{$_} } keys %static ),
140 ( map { my $value = $schema{$_};
142 push @arg, $self->option('crypt')
143 if $value eq 'crypt_password' && $self->option('crypt');
144 $_ => $svc_acct->$value(@arg);
151 $self->acct_sql_queue(
154 $self->option('table'),
157 return $err_or_queue unless ref($err_or_queue);
163 sub _export_replace {
164 my($self, $new, $old) = (shift, shift, shift);
166 my %schema = $self->_schema_map;
167 my %static = $self->_static_map;
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();
176 my $keymap = $schema{$self->option('primary_key')};
177 push @primary_key, $old->$keymap();
182 ( map { $_ => $static{$_} } keys %static ),
184 ( map { my $value = $schema{$_};
186 push @arg, $self->option('crypt')
187 if $value eq 'crypt_password' && $self->option('crypt');
188 $_ => $new->$value(@arg);
194 my $err_or_queue = $self->acct_sql_queue(
197 $self->option('table'),
198 $self->option('primary_key'), @primary_key,
201 return $err_or_queue unless ref($err_or_queue);
206 my ( $self, $svc_acct ) = (shift, shift);
208 my %schema = $self->_schema_map;
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();
217 my $keymap = $schema{$self->option('primary_key')};
218 $primary_key{ $self->option('primary_key') } = $svc_acct->$keymap(),
221 my $err_or_queue = $self->acct_sql_queue(
224 $self->option('table'),
226 #$self->option('primary_key') => $svc_acct->$keymap(),
228 return $err_or_queue unless ref($err_or_queue);
233 my( $self, $svcnum, $method ) = (shift, shift, shift);
234 my $queue = new FS::queue {
236 'job' => "FS::part_export::acct_sql::acct_sql_$method",
239 $self->option('datasrc'),
240 $self->option('username'),
241 $self->option('password'),
246 sub acct_sql_insert { #subroutine, not method
247 my $dbh = acct_sql_connect(shift, shift, shift);
248 my( $table, %record ) = @_;
250 my $sth = $dbh->prepare(
251 "INSERT INTO $table ( ". join(", ", keys %record).
252 " ) VALUES ( ". join(", ", map '?', keys %record ). " )"
253 ) or die $dbh->errstr;
255 $sth->execute( values(%record) )
256 or die "can't insert into $table table: ". $sth->errstr;
261 sub acct_sql_delete { #subroutine, not method
262 my $dbh = acct_sql_connect(shift, shift, shift);
263 my( $table, %record ) = @_;
265 my $sth = $dbh->prepare(
266 "DELETE FROM $table WHERE ". join(' AND ', map "$_ = ? ", keys %record )
267 ) or die $dbh->errstr;
269 $sth->execute( map $record{$_}, keys %record )
270 or die "can't delete from $table table: ". $sth->errstr;
275 sub acct_sql_replace { #subroutine, not method
276 my $dbh = acct_sql_connect(shift, shift, shift);
278 my( $table, $pkey ) = ( shift, shift );
280 my %primary_key = ();
281 if ( $pkey =~ /,/ ) {
282 foreach my $key ( split(/\s*,\s*/, $pkey ) ) {
283 $primary_key{$key} = shift;
286 $primary_key{$pkey} = shift;
291 my $sth = $dbh->prepare(
293 ' SET '. join(', ', map "$_ = ?", keys %record ).
294 ' WHERE '. join(' AND ', map "$_ = ?", keys %primary_key )
295 ) or die $dbh->errstr;
297 $sth->execute( values(%record), values(%primary_key) );
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;