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 'primary_key' => { label => 'Database primary key' },
21 'crypt' => { label => 'Password encryption',
22 type=>'select', options=>[qw(crypt md5)],
27 tie my %vpopmail_map, 'Tie::IxHash',
28 'pw_name' => 'username',
29 'pw_domain' => 'domain',
30 'pw_passwd' => 'crypt_password',
33 'pw_gecos' => 'finger',
35 #'pw_shell' => 'shell',
36 'pw_shell' => 'quota',
38 my $vpopmail_map = join('\n', map "$_ $vpopmail_map{$_}", keys %vpopmail_map );
40 tie my %postfix_courierimap_mailbox_map, 'Tie::IxHash',
41 'username' => 'email',
42 'password' => '_password',
43 'crypt' => 'crypt_password',
45 'maildir' => 'virtual_maildir',
49 my $postfix_courierimap_mailbox_map =
50 join('\n', map "$_ $postfix_courierimap_mailbox_map{$_}",
51 keys %postfix_courierimap_mailbox_map );
53 tie my %postfix_courierimap_alias_map, 'Tie::IxHash',
59 my $postfix_courierimap_alias_map =
60 join('\n', map "$_ $postfix_courierimap_alias_map{$_}",
61 keys %postfix_courierimap_alias_map );
63 tie my %postfix_native_mailbox_map, 'Tie::IxHash',
67 'password' => 'ldap_password',
69 my $postfix_native_mailbox_map =
70 join('\n', map "$_ $postfix_native_mailbox_map{$_}",
71 keys %postfix_native_mailbox_map );
75 'desc' => 'Real-time export of accounts to SQL databases '.
76 '(vpopmail, Postfix+Courier IMAP, others?)',
77 'options' => \%options,
80 Export accounts (svc_acct records) to SQL databases. Currently has default
81 configurations for vpopmail and Postfix+Courier IMAP but intended to be
82 configurable for other schemas as well.
84 <BR><BR>In contrast to sqlmail, this is intended to export just svc_acct
85 records only, rather than a single export for svc_acct, svc_forward and
86 svc_domain records, to export in "default" database schemas rather than
87 configure the MTA or POP/IMAP server for a Freeside-specific schema, and
88 to be configured for different mail server setups.
90 <BR><BR>Use these buttons for some useful presets:
92 <li><INPUT TYPE="button" VALUE="vpopmail" onClick='
93 this.form.table.value = "vpopmail";
94 this.form.schema.value = "$vpopmail_map";
95 this.form.primary_key.value = "pw_name, pw_domain";
97 <LI><INPUT TYPE="button" VALUE="postfix_courierimap_mailbox" onClick='
98 this.form.table.value = "mailbox";
99 this.form.schema.value = "$postfix_courierimap_mailbox_map";
100 this.form.primary_key.value = "username";
102 <LI><INPUT TYPE="button" VALUE="postfix_courierimap_alias" onClick='
103 this.form.table.value = "alias";
104 this.form.schema.value = "$postfix_courierimap_alias_map";
105 this.form.primary_key.value = "address";
107 <LI><INPUT TYPE="button" VALUE="postfix_native_mailbox" onClick='
108 this.form.table.value = "users";
109 this.form.schema.value = "$postfix_native_mailbox_map";
110 this.form.primary_key.value = "userid";
117 map { /^\s*(\S+)\s*(\S+)\s*$/ } split("\n", $self->option('schema') );
120 sub rebless { shift; }
123 my($self, $svc_acct) = (shift, shift);
125 my %map = $self->_map;
127 my %record = map { my $value = $map{$_};
129 push @arg, $self->option('crypt')
130 if $value eq 'crypt_password' && $self->option('crypt');
131 $_ => $svc_acct->$value(@arg);
135 $self->acct_sql_queue(
138 $self->option('table'),
141 return $err_or_queue unless ref($err_or_queue);
147 sub _export_replace {
148 my($self, $new, $old) = (shift, shift, shift);
150 my %map = $self->_map;
152 my @primary_key = ();
153 if ( $self->option('primary_key') =~ /,/ ) {
154 foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) {
155 my $keymap = $map{$key};
156 push @primary_key, $old->$keymap();
159 my $keymap = $map{$self->option('primary_key')};
160 push @primary_key, $old->$keymap();
163 my %record = map { my $value = $map{$_};
165 push @arg, $self->option('crypt')
166 if $value eq 'crypt_password' && $self->option('crypt');
167 $_ => $new->$value(@arg);
170 my $err_or_queue = $self->acct_sql_queue(
173 $self->option('table'),
174 $self->option('primary_key'), @primary_key,
177 return $err_or_queue unless ref($err_or_queue);
182 my ( $self, $svc_acct ) = (shift, shift);
184 my %map = $self->_map;
186 my %primary_key = ();
187 if ( $self->option('primary_key') =~ /,/ ) {
188 foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) {
189 my $keymap = $map{$key};
190 $primary_key{ $key } = $svc_acct->$keymap();
193 my $keymap = $map{$self->option('primary_key')};
194 $primary_key{ $self->option('primary_key') } = $svc_acct->$keymap(),
197 my $err_or_queue = $self->acct_sql_queue(
200 $self->option('table'),
202 #$self->option('primary_key') => $svc_acct->$keymap(),
204 return $err_or_queue unless ref($err_or_queue);
209 my( $self, $svcnum, $method ) = (shift, shift, shift);
210 my $queue = new FS::queue {
212 'job' => "FS::part_export::acct_sql::acct_sql_$method",
215 $self->option('datasrc'),
216 $self->option('username'),
217 $self->option('password'),
222 sub acct_sql_insert { #subroutine, not method
223 my $dbh = acct_sql_connect(shift, shift, shift);
224 my( $table, %record ) = @_;
226 my $sth = $dbh->prepare(
227 "INSERT INTO $table ( ". join(", ", keys %record).
228 " ) VALUES ( ". join(", ", map '?', keys %record ). " )"
229 ) or die $dbh->errstr;
231 $sth->execute( values(%record) )
232 or die "can't insert into $table table: ". $sth->errstr;
237 sub acct_sql_delete { #subroutine, not method
238 my $dbh = acct_sql_connect(shift, shift, shift);
239 my( $table, %record ) = @_;
241 my $sth = $dbh->prepare(
242 "DELETE FROM $table WHERE ". join(' AND ', map "$_ = ? ", keys %record )
243 ) or die $dbh->errstr;
245 $sth->execute( map $record{$_}, keys %record )
246 or die "can't delete from $table table: ". $sth->errstr;
251 sub acct_sql_replace { #subroutine, not method
252 my $dbh = acct_sql_connect(shift, shift, shift);
254 my( $table, $pkey ) = ( shift, shift );
256 my %primary_key = ();
257 if ( $pkey =~ /,/ ) {
258 foreach my $key ( split(/\s*,\s*/, $pkey ) ) {
259 $primary_key{$key} = shift;
262 $primary_key{$pkey} = shift;
267 my $sth = $dbh->prepare(
269 ' SET '. join(', ', map "$_ = ?", keys %record ).
270 ' WHERE '. join(' AND ', map "$_ = ?", keys %primary_key )
271 ) or die $dbh->errstr;
273 $sth->execute( values(%record), values(%primary_key) );
278 sub acct_sql_connect {
279 #my($datasrc, $username, $password) = @_;
280 #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
281 DBI->connect(@_) or die $DBI::errstr;