1 package FS::part_export::sqlmail;
3 use vars qw(@ISA %info);
5 use Digest::MD5 qw(md5_hex);
6 use FS::Record qw(qsearchs);
10 @ISA = qw(FS::part_export);
12 tie my %options, 'Tie::IxHash',
13 'datasrc' => { label => 'DBI data source' },
14 'username' => { label => 'Database username' },
15 'password' => { label => 'Database password' },
17 label => 'Server type',
19 options => [qw(dovecot_plain dovecot_crypt dovecot_digest_md5 courier_plain
21 default => ['dovecot_plain'], },
22 'svc_acct_table' => { label => 'User Table', default => 'user_acct' },
23 'svc_forward_table' => { label => 'Forward Table', default => 'forward' },
24 'svc_domain_table' => { label => 'Domain Table', default => 'domain' },
25 'svc_acct_fields' => { label => 'svc_acct Export Fields',
26 default => 'username _password domsvc svcnum' },
27 'svc_forward_fields' => { label => 'svc_forward Export Fields',
28 default => 'domain svcnum catchall' },
29 'svc_domain_fields' => { label => 'svc_domain Export Fields',
30 default => 'srcsvc dstsvc dst' },
31 'resolve_dstsvc' => { label => q{Resolve svc_forward.dstsvc to an email address and store it in dst. (Doesn't require that you also export dstsvc.)},
36 'svc' => [qw( svc_acct svc_domain svc_forward )],
37 'desc' => 'Real-time export to SQL-backed mail server',
38 'options' => \%options,
41 Database schema can be made to work with Courier IMAP, Exim and Dovecot.
42 Others could work but are untested. (more detailed description from
43 Kristian / fire2wire? )
47 sub rebless { shift; }
50 my($self, $svc) = (shift, shift);
51 # this is a svc_something.
53 my $svcdb = $svc->cust_svc->part_svc->svcdb;
54 my $export_table = $self->option($svcdb . '_table')
55 or die('Export table not defined for svcdb: ' . $svcdb);
56 my @export_fields = split(/\s+/, $self->option($svcdb . '_fields'));
57 my $svchash = update_values($self, $svc, $svcdb);
59 foreach my $key (keys(%$svchash)) {
60 unless (grep { $key eq $_ } @export_fields) {
61 delete $svchash->{$key};
65 my $error = $self->sqlmail_queue( $svc->svcnum, 'insert',
66 $self->option('server_type'), $export_table,
67 (map { ($_, $svchash->{$_}); } keys(%$svchash)));
68 return $error if $error;
74 my( $self, $new, $old ) = (shift, shift, shift);
76 my $svcdb = $new->cust_svc->part_svc->svcdb;
77 my $export_table = $self->option($svcdb . '_table')
78 or die('Export table not defined for svcdb: ' . $svcdb);
79 my @export_fields = split(/\s+/, $self->option($svcdb . '_fields'));
80 my $svchash = update_values($self, $new, $svcdb);
82 foreach my $key (keys(%$svchash)) {
83 unless (grep { $key eq $_ } @export_fields) {
84 delete $svchash->{$key};
88 my $error = $self->sqlmail_queue( $new->svcnum, 'replace',
89 $old->svcnum, $self->option('server_type'), $export_table,
90 (map { ($_, $svchash->{$_}); } keys(%$svchash)));
91 return $error if $error;
97 my( $self, $svc ) = (shift, shift);
99 my $svcdb = $svc->cust_svc->part_svc->svcdb;
100 my $table = $self->option($svcdb . '_table')
101 or die('Export table not defined for svcdb: ' . $svcdb);
103 $self->sqlmail_queue( $svc->svcnum, 'delete', $table,
108 my( $self, $svcnum, $method ) = (shift, shift, shift);
109 my $queue = new FS::queue {
111 'job' => "FS::part_export::sqlmail::sqlmail_$method",
114 $self->option('datasrc'),
115 $self->option('username'),
116 $self->option('password'),
121 sub sqlmail_insert { #subroutine, not method
122 my $dbh = sqlmail_connect(shift, shift, shift);
123 my( $server_type, $table ) = (shift, shift);
127 map { $attrs{$_} = $attrs{$_} ? qq!'$attrs{$_}'! : 'NULL'; } keys(%attrs);
128 my $query = sprintf("INSERT INTO %s (%s) values (%s)",
129 $table, join(",", keys(%attrs)),
130 join(',', values(%attrs)));
132 $dbh->do($query) or die $dbh->errstr;
138 sub sqlmail_delete { #subroutine, not method
139 my $dbh = sqlmail_connect(shift, shift, shift);
140 my( $table, $svcnum ) = @_;
142 $dbh->do("DELETE FROM $table WHERE svcnum = $svcnum") or die $dbh->errstr;
148 sub sqlmail_replace {
149 my $dbh = sqlmail_connect(shift, shift, shift);
150 my($oldsvcnum, $server_type, $table) = (shift, shift, shift);
153 map { $attrs{$_} = $attrs{$_} ? qq!'$attrs{$_}'! : 'NULL'; } keys(%attrs);
155 my $query = "SELECT COUNT(*) FROM $table WHERE svcnum = $oldsvcnum";
156 my $result = $dbh->selectrow_arrayref($query) or die $dbh->errstr;
158 if (@$result[0] == 0) {
159 $query = sprintf("INSERT INTO %s (%s) values (%s)",
160 $table, join(",", keys(%attrs)),
161 join(',', values(%attrs)));
162 $dbh->do($query) or die $dbh->errstr;
164 $query = sprintf('UPDATE %s SET %s WHERE svcnum = %s',
165 $table, join(', ', map {"$_ = $attrs{$_}"} keys(%attrs)),
167 $dbh->do($query) or die $dbh->errstr;
175 sub sqlmail_connect {
176 DBI->connect(@_) or die $DBI::errstr;
181 # Update records to conform to a particular server_type.
183 my ($self, $svc, $svcdb) = (shift,shift,shift);
184 my $svchash = { %{$svc->hashref} } or return ''; # We need a copy.
186 if ($svcdb eq 'svc_acct') {
187 if ($self->option('server_type') eq 'courier_crypt') {
188 my $salt = join '', ('.', '/', 0..9,'A'..'Z', 'a'..'z')[rand 64, rand 64];
189 $svchash->{_password} = crypt($svchash->{_password}, $salt);
191 } elsif ($self->option('server_type') eq 'dovecot_plain') {
192 $svchash->{_password} = '{PLAIN}' . $svchash->{_password};
194 } elsif ($self->option('server_type') eq 'dovecot_crypt') {
195 my $salt = join '', ('.', '/', 0..9,'A'..'Z', 'a'..'z')[rand 64, rand 64];
196 $svchash->{_password} = '{CRYPT}' . crypt($svchash->{_password}, $salt);
198 } elsif ($self->option('server_type') eq 'dovecot_digest_md5') {
199 my $svc_domain = qsearchs('svc_domain', { svcnum => $svc->domsvc });
200 die('Unable to lookup svc_domain with domsvc: ' . $svc->domsvc)
201 unless ($svc_domain);
203 my $domain = $svc_domain->domain;
204 my $md5hash = '{DIGEST-MD5}' . md5_hex(join(':', $svchash->{username},
205 $domain, $svchash->{_password}));
206 $svchash->{_password} = $md5hash;
208 } elsif ($svcdb eq 'svc_forward') {
209 if ($self->option('resolve_dstsvc') && $svc->dstsvc_acct) {
210 $svchash->{dst} = $svc->dstsvc_acct->username . '@' .
211 $svc->dstsvc_acct->svc_domain->domain;