eek, terminate the javascript
[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 tie my %postfix_native_mailbox_map, 'Tie::IxHash',
64   'userid'   => 'email',
65   'uid'      => 'uid',
66   'gid'      => 'gid',
67   'password' => 'ldap_password',
68 ;
69 my $postfix_native_mailbox_map =
70   join('\n', map "$_ $postfix_native_mailbox_map{$_}",
71                  keys %postfix_native_mailbox_map      );
72
73 %info = (
74   'svc'      => 'svc_acct',
75   'desc'     => 'Real-time export of accounts to SQL databases '.
76                 '(vpopmail, Postfix+Courier IMAP, others?)',
77   'options'  => \%options,
78   'nodomain' => '',
79   'notes'    => <<END
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.
83
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.
89
90 <BR><BR>Use these buttons for some useful presets:
91 <UL>
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";
96   '>
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";
101   '>
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";
106   '>
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";
111   '>
112 </UL>
113 END
114 );
115
116 sub _map {
117   my $self = shift;
118   map { /^\s*(\S+)\s*(\S+)\s*$/ } split("\n", $self->option('schema') );
119 }
120
121 sub rebless { shift; }
122
123 sub _export_insert {
124   my($self, $svc_acct) = (shift, shift);
125
126   my %map = $self->_map;
127
128   my %record = map { my $value = $map{$_};
129                      my @arg = ();
130                      push @arg, $self->option('crypt')
131                        if $value eq 'crypt_password' && $self->option('crypt');
132                      $_ => $svc_acct->$value(@arg);
133                    } keys %map;
134
135   my $err_or_queue =
136     $self->acct_sql_queue(
137       $svc_acct->svcnum,
138       'insert',
139       $self->option('table'),
140       %record
141     );
142   return $err_or_queue unless ref($err_or_queue);
143
144   '';
145
146 }
147
148 sub _export_replace {
149   my($self, $new, $old) = (shift, shift, shift);
150
151   my %map = $self->_map;
152
153   my @primary_key = ();
154   if ( $self->option('primary_key') =~ /,/ ) {
155     foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) {
156       my $keymap = $map{$key};
157       push @primary_key, $old->$keymap();
158     }
159   } else {
160     my $keymap = $map{$self->option('primary_key')};
161     push @primary_key, $old->$keymap();
162   }
163
164   my %record = map { my $value = $map{$_};
165                      my @arg = ();
166                      push @arg, $self->option('crypt')
167                        if $value eq 'crypt_password' && $self->option('crypt');
168                      $_ => $new->$value(@arg);
169                    } keys %map;
170
171   my $err_or_queue = $self->acct_sql_queue(
172     $new->svcnum,
173     'replace',
174     $self->option('table'),
175     $self->option('primary_key'), @primary_key, 
176     %record,
177   );
178   return $err_or_queue unless ref($err_or_queue);
179   '';
180 }
181
182 sub _export_delete {
183   my ( $self, $svc_acct ) = (shift, shift);
184
185   my %map = $self->_map;
186
187   my %primary_key = ();
188   if ( $self->option('primary_key') =~ /,/ ) {
189     foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) {
190       my $keymap = $map{$key};
191       $primary_key{ $key } = $svc_acct->$keymap();
192     }
193   } else {
194     my $keymap = $map{$self->option('primary_key')};
195     $primary_key{ $self->option('primary_key') } = $svc_acct->$keymap(),
196   }
197
198   my $err_or_queue = $self->acct_sql_queue(
199     $svc_acct->svcnum,
200     'delete',
201     $self->option('table'),
202     %primary_key,
203     #$self->option('primary_key') => $svc_acct->$keymap(),
204   );
205   return $err_or_queue unless ref($err_or_queue);
206   '';
207 }
208
209 sub acct_sql_queue {
210   my( $self, $svcnum, $method ) = (shift, shift, shift);
211   my $queue = new FS::queue {
212     'svcnum' => $svcnum,
213     'job'    => "FS::part_export::acct_sql::acct_sql_$method",
214   };
215   $queue->insert(
216     $self->option('datasrc'),
217     $self->option('username'),
218     $self->option('password'),
219     @_,
220   ) or $queue;
221 }
222
223 sub acct_sql_insert { #subroutine, not method
224   my $dbh = acct_sql_connect(shift, shift, shift);
225   my( $table, %record ) = @_;
226
227   my $sth = $dbh->prepare(
228     "INSERT INTO $table ( ". join(", ", keys %record).
229     " ) VALUES ( ". join(", ", map '?', keys %record ). " )"
230   ) or die $dbh->errstr;
231
232   $sth->execute( values(%record) )
233     or die "can't insert into $table table: ". $sth->errstr;
234
235   $dbh->disconnect;
236 }
237
238 sub acct_sql_delete { #subroutine, not method
239   my $dbh = acct_sql_connect(shift, shift, shift);
240   my( $table, %record ) = @_;
241
242   my $sth = $dbh->prepare(
243     "DELETE FROM $table WHERE ". join(' AND ', map "$_ = ? ", keys %record )
244   ) or die $dbh->errstr;
245
246   $sth->execute( map $record{$_}, keys %record )
247     or die "can't delete from $table table: ". $sth->errstr;
248
249   $dbh->disconnect;
250 }
251
252 sub acct_sql_replace { #subroutine, not method
253   my $dbh = acct_sql_connect(shift, shift, shift);
254
255   my( $table, $pkey ) = ( shift, shift );
256
257   my %primary_key = ();
258   if ( $pkey =~ /,/ ) {
259     foreach my $key ( split(/\s*,\s*/, $pkey ) ) {
260       $primary_key{$key} = shift;
261     }
262   } else {
263     $primary_key{$pkey} = shift;
264   }
265
266   my %record = @_;
267
268   my $sth = $dbh->prepare(
269     "UPDATE $table".
270     ' SET '.   join(', ',    map "$_ = ?", keys %record      ).
271     ' WHERE '. join(' AND ', map "$_ = ?", keys %primary_key )
272   ) or die $dbh->errstr;
273
274   $sth->execute( values(%record), values(%primary_key) );
275
276   $dbh->disconnect;
277 }
278
279 sub acct_sql_connect {
280   #my($datasrc, $username, $password) = @_;
281   #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
282   DBI->connect(@_) or die $DBI::errstr;
283 }
284
285 1;
286