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 => 'srcsvc dstsvc dst' },
29 'svc_domain_fields' => { label => 'svc_domain Export Fields',
30 default => 'domain svcnum catchall' },
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,
40 'default_svc_class' => 'Email',
42 Database schema can be made to work with Courier IMAP, Exim and Dovecot.
43 Others could work but are untested. (more detailed description from
44 Kristian / fire2wire? )
48 sub rebless { shift; }
51 my($self, $svc) = (shift, shift);
52 # this is a svc_something.
54 my $svcdb = $svc->cust_svc->part_svc->svcdb;
55 my $export_table = $self->option($svcdb . '_table')
56 or die('Export table not defined for svcdb: ' . $svcdb);
57 my @export_fields = split(/\s+/, $self->option($svcdb . '_fields'));
58 my $svchash = update_values($self, $svc, $svcdb);
60 foreach my $key (keys(%$svchash)) {
61 unless (grep { $key eq $_ } @export_fields) {
62 delete $svchash->{$key};
66 my $error = $self->sqlmail_queue( $svc->svcnum, 'insert',
67 $self->option('server_type'), $export_table,
68 (map { ($_, $svchash->{$_}); } keys(%$svchash)));
69 return $error if $error;
75 my( $self, $new, $old ) = (shift, shift, shift);
77 my $svcdb = $new->cust_svc->part_svc->svcdb;
78 my $export_table = $self->option($svcdb . '_table')
79 or die('Export table not defined for svcdb: ' . $svcdb);
80 my @export_fields = split(/\s+/, $self->option($svcdb . '_fields'));
81 my $svchash = update_values($self, $new, $svcdb);
83 foreach my $key (keys(%$svchash)) {
84 unless (grep { $key eq $_ } @export_fields) {
85 delete $svchash->{$key};
89 my $error = $self->sqlmail_queue( $new->svcnum, 'replace',
90 $old->svcnum, $self->option('server_type'), $export_table,
91 (map { ($_, $svchash->{$_}); } keys(%$svchash)));
92 return $error if $error;
98 my( $self, $svc ) = (shift, shift);
100 my $svcdb = $svc->cust_svc->part_svc->svcdb;
101 my $table = $self->option($svcdb . '_table')
102 or die('Export table not defined for svcdb: ' . $svcdb);
104 $self->sqlmail_queue( $svc->svcnum, 'delete', $table,
109 my( $self, $svcnum, $method ) = (shift, shift, shift);
110 my $queue = new FS::queue {
112 'job' => "FS::part_export::sqlmail::sqlmail_$method",
115 $self->option('datasrc'),
116 $self->option('username'),
117 $self->option('password'),
122 sub sqlmail_insert { #subroutine, not method
123 my $dbh = sqlmail_connect(shift, shift, shift);
124 my( $server_type, $table ) = (shift, shift);
128 map { $attrs{$_} = $attrs{$_} ? qq!'$attrs{$_}'! : 'NULL'; } keys(%attrs);
129 my $query = sprintf("INSERT INTO %s (%s) values (%s)",
130 $table, join(",", keys(%attrs)),
131 join(',', values(%attrs)));
133 $dbh->do($query) or die $dbh->errstr;
139 sub sqlmail_delete { #subroutine, not method
140 my $dbh = sqlmail_connect(shift, shift, shift);
141 my( $table, $svcnum ) = @_;
143 $dbh->do("DELETE FROM $table WHERE svcnum = $svcnum") or die $dbh->errstr;
149 sub sqlmail_replace {
150 my $dbh = sqlmail_connect(shift, shift, shift);
151 my($oldsvcnum, $server_type, $table) = (shift, shift, shift);
154 map { $attrs{$_} = $attrs{$_} ? qq!'$attrs{$_}'! : 'NULL'; } keys(%attrs);
156 my $query = "SELECT COUNT(*) FROM $table WHERE svcnum = $oldsvcnum";
157 my $result = $dbh->selectrow_arrayref($query) or die $dbh->errstr;
159 if (@$result[0] == 0) {
160 $query = sprintf("INSERT INTO %s (%s) values (%s)",
161 $table, join(",", keys(%attrs)),
162 join(',', values(%attrs)));
163 $dbh->do($query) or die $dbh->errstr;
165 $query = sprintf('UPDATE %s SET %s WHERE svcnum = %s',
166 $table, join(', ', map {"$_ = $attrs{$_}"} keys(%attrs)),
168 $dbh->do($query) or die $dbh->errstr;
176 sub sqlmail_connect {
177 DBI->connect(@_) or die $DBI::errstr;
182 # Update records to conform to a particular server_type.
184 my ($self, $svc, $svcdb) = (shift,shift,shift);
185 my $svchash = { %{$svc->hashref} } or return ''; # We need a copy.
187 if ($svcdb eq 'svc_acct') {
188 if ($self->option('server_type') eq 'courier_crypt') {
189 my $salt = join '', ('.', '/', 0..9,'A'..'Z', 'a'..'z')[rand 64, rand 64];
190 $svchash->{_password} = crypt($svchash->{_password}, $salt);
192 } elsif ($self->option('server_type') eq 'dovecot_plain') {
193 $svchash->{_password} = '{PLAIN}' . $svchash->{_password};
195 } elsif ($self->option('server_type') eq 'dovecot_crypt') {
196 my $salt = join '', ('.', '/', 0..9,'A'..'Z', 'a'..'z')[rand 64, rand 64];
197 $svchash->{_password} = '{CRYPT}' . crypt($svchash->{_password}, $salt);
199 } elsif ($self->option('server_type') eq 'dovecot_digest_md5') {
200 my $svc_domain = qsearchs('svc_domain', { svcnum => $svc->domsvc });
201 die('Unable to lookup svc_domain with domsvc: ' . $svc->domsvc)
202 unless ($svc_domain);
204 my $domain = $svc_domain->domain;
205 my $md5hash = '{DIGEST-MD5}' . md5_hex(join(':', $svchash->{username},
206 $domain, $svchash->{_password}));
207 $svchash->{_password} = $md5hash;
209 } elsif ($svcdb eq 'svc_forward') {
210 if ($self->option('resolve_dstsvc') && $svc->dstsvc_acct) {
211 $svchash->{dst} = $svc->dstsvc_acct->username . '@' .
212 $svc->dstsvc_acct->svc_domain->domain;