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 );
65 'desc' => 'Real-time export of accounts to SQL databases '.
66 '(vpopmail, Postfix+Courier IMAP, others?)',
67 'options' => \%options,
70 Export accounts (svc_acct records) to SQL databases. Currently has default
71 configurations for vpopmail and Postfix+Courier IMAP but intended to be
72 configurable for other schemas as well.
74 <BR><BR>In contrast to sqlmail, this is intended to export just svc_acct
75 records only, rather than a single export for svc_acct, svc_forward and
76 svc_domain records, to export in "default" database schemas rather than
77 configure the MTA or POP/IMAP server for a Freeside-specific schema, and
78 to be configured for different mail server setups.
80 <BR><BR>Use these buttons for some useful presets:
82 <li><INPUT TYPE="button" VALUE="vpopmail" onClick='
83 this.form.table.value = "vpopmail";
84 this.form.schema.value = "$vpopmail_map";
85 this.form.primary_key.value = "pw_name, pw_domain";
87 <LI><INPUT TYPE="button" VALUE="postfix_courierimap_mailbox" onClick='
88 this.form.table.value = "mailbox";
89 this.form.schema.value = "$postfix_courierimap_mailbox_map";
90 this.form.primary_key.value = "username";
92 <LI><INPUT TYPE="button" VALUE="postfix_courierimap_alias" onClick='
93 this.form.table.value = "alias";
94 this.form.schema.value = "$postfix_courierimap_alias_map";
95 this.form.primary_key.value = "address";
103 map { /^\s*(\S+)\s*(\S+)\s*$/ } split("\n", $self->option('schema') );
106 sub rebless { shift; }
109 my($self, $svc_acct) = (shift, shift);
111 my %map = $self->_map;
113 my %record = map { my $value = $map{$_};
115 push @arg, $self->option('crypt')
116 if $value eq 'crypt_password' && $self->option('crypt');
117 $_ => $svc_acct->$value(@arg);
121 $self->acct_sql_queue(
124 $self->option('table'),
127 return $err_or_queue unless ref($err_or_queue);
133 sub _export_replace {
134 my($self, $new, $old) = (shift, shift, shift);
136 my %map = $self->_map;
138 my @primary_key = ();
139 if ( $self->option('primary_key') =~ /,/ ) {
140 foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) {
141 my $keymap = $map{$key};
142 push @primary_key, $old->$keymap();
145 my $keymap = $map{$self->option('primary_key')};
146 push @primary_key, $old->$keymap();
149 my %record = map { my $value = $map{$_};
151 push @arg, $self->option('crypt')
152 if $value eq 'crypt_password' && $self->option('crypt');
153 $_ => $new->$value(@arg);
156 my $err_or_queue = $self->acct_sql_queue(
159 $self->option('table'),
160 $self->option('primary_key'), @primary_key,
163 return $err_or_queue unless ref($err_or_queue);
168 my ( $self, $svc_acct ) = (shift, shift);
170 my %map = $self->_map;
172 my %primary_key = ();
173 if ( $self->option('primary_key') =~ /,/ ) {
174 foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) {
175 my $keymap = $map{$key};
176 $primary_key{ $key } = $svc_acct->$keymap();
179 my $keymap = $map{$self->option('primary_key')};
180 $primary_key{ $self->option('primary_key') } = $svc_acct->$keymap(),
183 my $err_or_queue = $self->acct_sql_queue(
186 $self->option('table'),
188 #$self->option('primary_key') => $svc_acct->$keymap(),
190 return $err_or_queue unless ref($err_or_queue);
195 my( $self, $svcnum, $method ) = (shift, shift, shift);
196 my $queue = new FS::queue {
198 'job' => "FS::part_export::acct_sql::acct_sql_$method",
201 $self->option('datasrc'),
202 $self->option('username'),
203 $self->option('password'),
208 sub acct_sql_insert { #subroutine, not method
209 my $dbh = acct_sql_connect(shift, shift, shift);
210 my( $table, %record ) = @_;
212 my $sth = $dbh->prepare(
213 "INSERT INTO $table ( ". join(", ", keys %record).
214 " ) VALUES ( ". join(", ", map '?', keys %record ). " )"
215 ) or die $dbh->errstr;
217 $sth->execute( values(%record) )
218 or die "can't insert into $table table: ". $sth->errstr;
223 sub acct_sql_delete { #subroutine, not method
224 my $dbh = acct_sql_connect(shift, shift, shift);
225 my( $table, %record ) = @_;
227 my $sth = $dbh->prepare(
228 "DELETE FROM $table WHERE ". join(' AND ', map "$_ = ? ", keys %record )
229 ) or die $dbh->errstr;
231 $sth->execute( map $record{$_}, keys %record )
232 or die "can't delete from $table table: ". $sth->errstr;
237 sub acct_sql_replace { #subroutine, not method
238 my $dbh = acct_sql_connect(shift, shift, shift);
240 my( $table, $pkey ) = ( shift, shift );
242 my %primary_key = ();
243 if ( $pkey =~ /,/ ) {
244 foreach my $key ( split(/\s*,\s*/, $pkey ) ) {
245 $primary_key{$key} = shift;
248 $primary_key{$pkey} = shift;
253 my $sth = $dbh->prepare(
255 ' SET '. join(', ', map "$_ = ?", keys %record ).
256 ' WHERE '. join(' AND ', map "$_ = ?", keys %primary_key )
257 ) or die $dbh->errstr;
259 $sth->execute( values(%record), values(%primary_key) );
264 sub acct_sql_connect {
265 #my($datasrc, $username, $password) = @_;
266 #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
267 DBI->connect(@_) or die $DBI::errstr;