This commit was generated by cvs2svn to compensate for changes in r4407,
[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   'primary_key'        => { label => 'Database primary key' },
21   'crypt'              => { label => 'Password encryption',
22                             type=>'select', options=>[qw(crypt md5)],
23                             default=>'crypt',
24                           },
25 ;
26
27 tie my %vpopmail_map, 'Tie::IxHash',
28   'pw_name'   => 'username',
29   'pw_domain' => 'domain',
30   'pw_passwd' => 'crypt_password',
31   'pw_uid'    => 'uid',
32   'pw_gid'    => 'gid',
33   'pw_gecos'  => 'finger',
34   'pw_dir'    => 'dir',
35   #'pw_shell'  => 'shell',
36   'pw_shell'  => 'quota',
37 ;
38 my $vpopmail_map = join('\n', map "$_ $vpopmail_map{$_}", keys %vpopmail_map );
39
40 tie my %postfix_courierimap_mailbox_map, 'Tie::IxHash',
41   'username' => 'email',
42   'password' => '_password',
43   'crypt'    => 'crypt_password',
44   'name'     => 'finger',
45   'maildir'  => 'virtual_maildir',
46   'domain'   => 'domain',
47   'svcnum'   => 'svcnum',
48 ;
49 my $postfix_courierimap_mailbox_map =
50   join('\n', map "$_ $postfix_courierimap_mailbox_map{$_}",
51                  keys %postfix_courierimap_mailbox_map      );
52
53 tie my %postfix_courierimap_alias_map, 'Tie::IxHash',
54   'address' => 'email',
55   'goto'    => 'email',
56   'domain'  => 'domain',
57   'svcnum'  => 'svcnum',
58 ;
59 my $postfix_courierimap_alias_map =
60   join('\n', map "$_ $postfix_courierimap_alias_map{$_}",
61                  keys %postfix_courierimap_alias_map      );
62
63 %info = (
64   'svc'      => 'svc_acct',
65   'desc'     => 'Real-time export of accounts to SQL databases '.
66                 '(vpopmail, Postfix+Courier IMAP, others?)',
67   'options'  => \%options,
68   'nodomain' => '',
69   'notes'    => <<END
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.
73
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.
79
80 <BR><BR>Use these buttons for some useful presets:
81 <UL>
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";
86   '>
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";
91   '>
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";
96   '>
97 </UL>
98 END
99 );
100
101 sub _map {
102   my $self = shift;
103   map { /^\s*(\S+)\s*(\S+)\s*$/ } split("\n", $self->option('schema') );
104 }
105
106 sub rebless { shift; }
107
108 sub _export_insert {
109   my($self, $svc_acct) = (shift, shift);
110
111   my %map = $self->_map;
112
113   my %record = map { my $value = $map{$_};
114                      my @arg = ();
115                      push @arg, $self->option('crypt')
116                        if $value eq 'crypt_password' && $self->option('crypt');
117                      $_ => $svc_acct->$value(@arg);
118                    } keys %map;
119
120   my $err_or_queue =
121     $self->acct_sql_queue(
122       $svc_acct->svcnum,
123       'insert',
124       $self->option('table'),
125       %record
126     );
127   return $err_or_queue unless ref($err_or_queue);
128
129   '';
130
131 }
132
133 sub _export_replace {
134   my($self, $new, $old) = (shift, shift, shift);
135
136   my %map = $self->_map;
137
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();
143     }
144   } else {
145     my $keymap = $map{$self->option('primary_key')};
146     push @primary_key, $old->$keymap();
147   }
148
149   my %record = map { my $value = $map{$_};
150                      my @arg = ();
151                      push @arg, $self->option('crypt')
152                        if $value eq 'crypt_password' && $self->option('crypt');
153                      $_ => $new->$value(@arg);
154                    } keys %map;
155
156   my $err_or_queue = $self->acct_sql_queue(
157     $new->svcnum,
158     'replace',
159     $self->option('table'),
160     $self->option('primary_key'), @primary_key, 
161     %record,
162   );
163   return $err_or_queue unless ref($err_or_queue);
164   '';
165 }
166
167 sub _export_delete {
168   my ( $self, $svc_acct ) = (shift, shift);
169
170   my %map = $self->_map;
171
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();
177     }
178   } else {
179     my $keymap = $map{$self->option('primary_key')};
180     $primary_key{ $self->option('primary_key') } = $svc_acct->$keymap(),
181   }
182
183   my $err_or_queue = $self->acct_sql_queue(
184     $svc_acct->svcnum,
185     'delete',
186     $self->option('table'),
187     %primary_key,
188     #$self->option('primary_key') => $svc_acct->$keymap(),
189   );
190   return $err_or_queue unless ref($err_or_queue);
191   '';
192 }
193
194 sub acct_sql_queue {
195   my( $self, $svcnum, $method ) = (shift, shift, shift);
196   my $queue = new FS::queue {
197     'svcnum' => $svcnum,
198     'job'    => "FS::part_export::acct_sql::acct_sql_$method",
199   };
200   $queue->insert(
201     $self->option('datasrc'),
202     $self->option('username'),
203     $self->option('password'),
204     @_,
205   ) or $queue;
206 }
207
208 sub acct_sql_insert { #subroutine, not method
209   my $dbh = acct_sql_connect(shift, shift, shift);
210   my( $table, %record ) = @_;
211
212   my $sth = $dbh->prepare(
213     "INSERT INTO $table ( ". join(", ", keys %record).
214     " ) VALUES ( ". join(", ", map '?', keys %record ). " )"
215   ) or die $dbh->errstr;
216
217   $sth->execute( values(%record) )
218     or die "can't insert into $table table: ". $sth->errstr;
219
220   $dbh->disconnect;
221 }
222
223 sub acct_sql_delete { #subroutine, not method
224   my $dbh = acct_sql_connect(shift, shift, shift);
225   my( $table, %record ) = @_;
226
227   my $sth = $dbh->prepare(
228     "DELETE FROM $table WHERE ". join(' AND ', map "$_ = ? ", keys %record )
229   ) or die $dbh->errstr;
230
231   $sth->execute( map $record{$_}, keys %record )
232     or die "can't delete from $table table: ". $sth->errstr;
233
234   $dbh->disconnect;
235 }
236
237 sub acct_sql_replace { #subroutine, not method
238   my $dbh = acct_sql_connect(shift, shift, shift);
239
240   my( $table, $pkey ) = ( shift, shift );
241
242   my %primary_key = ();
243   if ( $pkey =~ /,/ ) {
244     foreach my $key ( split(/\s*,\s*/, $pkey ) ) {
245       $primary_key{$key} = shift;
246     }
247   } else {
248     $primary_key{$pkey} = shift;
249   }
250
251   my %record = @_;
252
253   my $sth = $dbh->prepare(
254     "UPDATE $table".
255     ' SET '.   join(', ',    map "$_ = ?", keys %record      ).
256     ' WHERE '. join(' AND ', map "$_ = ?", keys %primary_key )
257   ) or die $dbh->errstr;
258
259   $sth->execute( values(%record), values(%primary_key) );
260
261   $dbh->disconnect;
262 }
263
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;
268 }
269
270 1;
271