1 package FS::svc_forward;
4 use vars qw( @ISA $nossh_hack $conf $shellmachine @qmailmachines
8 use FS::Record qw( fields qsearch qsearchs dbh );
14 @ISA = qw( FS::svc_Common );
16 #ask FS::UID to run this stuff for us later
17 $FS::UID::callback{'FS::svc_forward'} = sub {
19 if ( $conf->exists('qmailmachines') ) {
20 $shellmachine = $conf->config('shellmachine')
24 if ( $conf->exists('vpopmailmachines') ) {
25 @vpopmailmachines = $conf->config('vpopmailmachines');
27 @vpopmailmachines = ();
33 FS::svc_forward - Object methods for svc_forward records
39 $record = new FS::svc_forward \%hash;
40 $record = new FS::svc_forward { 'column' => 'value' };
42 $error = $record->insert;
44 $error = $new_record->replace($old_record);
46 $error = $record->delete;
48 $error = $record->check;
50 $error = $record->suspend;
52 $error = $record->unsuspend;
54 $error = $record->cancel;
58 An FS::svc_forward object represents a mail forwarding alias. FS::svc_forward
59 inherits from FS::Record. The following fields are currently supported:
63 =item svcnum - primary key (assigned automatcially for new accounts)
65 =item srcsvc - svcnum of the source of the forward (see L<FS::svc_acct>)
67 =item dstsvc - svcnum of the destination of the forward (see L<FS::svc_acct>)
69 =item dst - foreign destination (email address) - forward not local to freeside
79 Creates a new mail forwarding alias. To add the mail forwarding alias to the
80 database, see L<"insert">.
84 sub table { 'svc_forward'; }
88 Adds this mail forwarding alias to the database. If there is an error, returns
89 the error, otherwise returns false.
91 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
92 defined. An FS::cust_svc record will be created and inserted.
94 If the configuration value (see L<FS::Conf>) vpopmailmachines exists, then
97 [ -d $vpopdir/domains/$domain/$source ] && {
98 echo "$destination" >> $vpopdir/domains/$domain/$username/.$qmail
99 chown $vpopuid:$vpopgid $vpopdir/domains/$domain/$username/.$qmail
102 is executed on each vpopmailmachine via ssh (see the vpopmail documentation).
103 This behaviour can be supressed by setting $FS::svc_forward::nossh_hack true.
111 local $SIG{HUP} = 'IGNORE';
112 local $SIG{INT} = 'IGNORE';
113 local $SIG{QUIT} = 'IGNORE';
114 local $SIG{TERM} = 'IGNORE';
115 local $SIG{TSTP} = 'IGNORE';
116 local $SIG{PIPE} = 'IGNORE';
118 my $oldAutoCommit = $FS::UID::AutoCommit;
119 local $FS::UID::AutoCommit = 0;
122 $error = $self->check;
123 return $error if $error;
125 $error = $self->SUPER::insert;
127 $dbh->rollback if $oldAutoCommit;
131 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $self->srcsvc } );
132 my $username = $svc_acct->username;
133 my $domain = $svc_acct->domain;
136 $destination = $self->dstsvc_acct->email;
138 $destination = $self->dst;
141 foreach my $vpopmailmachine ( @vpopmailmachines ) {
142 my($machine, $vpopdir, $vpopuid, $vpopgid) = split(/\s+/, $vpopmailmachine);
143 my $queue = new FS::queue { 'job' => 'Net::SSH::ssh_cmd' }; # should be neater
144 my $error = $queue->insert("root\@$machine","[ -d $vpopdir/domains/$domain/$username ] && { echo \"$destination\" >> $vpopdir/domains/$domain/$username/.qmail; chown $vpopuid:$vpopgid $vpopdir/domains/$domain/$username/.qmail; }")
147 $dbh->rollback if $oldAutoCommit;
148 return "queueing job (transaction rolled back): $error";
153 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
160 Deletes this mail forwarding alias from the database. If there is an error,
161 returns the error, otherwise returns false.
163 The corresponding FS::cust_svc record will be deleted as well.
165 If the configuration value vpopmailmachines exists, then the command:
167 { sed -e '/^$destination/d' <
168 $vpopdir/domains/$srcdomain/$srcusername/.qmail >
169 $vpopdir/domains/$srcdomain/$srcusername/.qmail.temp;
170 mv $vpopdir/domains/$srcdomain/$srcusername/.qmail.temp
171 $vpopdir/domains/$srcdomain/$srcusername/.qmail;
172 chown $vpopuid.$vpopgid $vpopdir/domains/$srcdomain/$srcusername/.qmail; }
175 is executed on each vpopmailmachine via ssh. This behaviour can be supressed
176 by setting $FS::svc_forward_nossh_hack true.
183 local $SIG{HUP} = 'IGNORE';
184 local $SIG{INT} = 'IGNORE';
185 local $SIG{QUIT} = 'IGNORE';
186 local $SIG{TERM} = 'IGNORE';
187 local $SIG{TSTP} = 'IGNORE';
188 local $SIG{PIPE} = 'IGNORE';
190 my $oldAutoCommit = $FS::UID::AutoCommit;
191 local $FS::UID::Autocommit = 0;
194 my $error = $self->SUPER::delete;
196 $dbh->rollback if $oldAutoCommit;
200 my $svc_acct = $self->srcsvc_acct;
201 my $username = $svc_acct->username;
202 my $domain = $svc_acct->domain;
205 $destination = $self->dstsvc_acct->email;
207 $destination = $self->dst;
209 foreach my $vpopmailmachine ( @vpopmailmachines ) {
210 my($machine, $vpopdir, $vpopuid, $vpopgid) =
211 split(/\s+/, $vpopmailmachine);
212 my $queue = new FS::queue { 'job' => 'Net::SSH::ssh_cmd' }; # should be neater
213 my $error = $queue->insert("root\@$machine",
214 "sed -e '/^$destination/d' " .
215 "< $vpopdir/domains/$domain/$username/.qmail" .
216 "> $vpopdir/domains/$domain/$username/.qmail.temp; " .
217 "mv $vpopdir/domains/$domain/$username/.qmail.temp " .
218 "$vpopdir/domains/$domain/$username/.qmail; " .
219 "chown $vpopuid.$vpopgid $vpopdir/domains/$domain/$username/.qmail;"
224 $dbh->rollback if $oldAutoCommit;
225 return "queueing job (transaction rolled back): $error";
230 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
235 =item replace OLD_RECORD
237 Replaces OLD_RECORD with this one in the database. If there is an error,
238 returns the error, otherwise returns false.
240 If the configuration value vpopmailmachines exists, then the command:
242 { sed -e '/^$destination/d' <
243 $vpopdir/domains/$srcdomain/$srcusername/.qmail >
244 $vpopdir/domains/$srcdomain/$srcusername/.qmail.temp;
245 mv $vpopdir/domains/$srcdomain/$srcusername/.qmail.temp
246 $vpopdir/domains/$srcdomain/$srcusername/.qmail;
247 chown $vpopuid.$vpopgid $vpopdir/domains/$srcdomain/$srcusername/.qmail; }
250 is executed on each vpopmailmachine via ssh. This behaviour can be supressed
251 by setting $FS::svc_forward_nossh_hack true.
253 Also, if the configuration value vpopmailmachines exists, then the command:
255 [ -d $vpopdir/domains/$domain/$source ] && {
256 echo "$destination" >> $vpopdir/domains/$domain/$username/.$qmail
257 chown $vpopuid:$vpopgid $vpopdir/domains/$domain/$username/.$qmail
260 is executed on each vpopmailmachine via ssh. This behaviour can be supressed
261 by setting $FS::svc_forward_nossh_hack true.
266 my ( $new, $old ) = ( shift, shift );
268 if ( $new->srcsvc != $old->srcsvc
269 && ( $new->dstsvc != $old->dstsvc
270 || ! $new->dstsvc && $new->dst ne $old->dst
273 return "Can't change both source and destination of a mail forward!"
276 local $SIG{HUP} = 'IGNORE';
277 local $SIG{INT} = 'IGNORE';
278 local $SIG{QUIT} = 'IGNORE';
279 local $SIG{TERM} = 'IGNORE';
280 local $SIG{TSTP} = 'IGNORE';
281 local $SIG{PIPE} = 'IGNORE';
283 my $oldAutoCommit = $FS::UID::AutoCommit;
284 local $FS::UID::AutoCommit = 0;
287 my $error = $new->SUPER::replace($old);
289 $dbh->rollback if $oldAutoCommit;
293 my $old_svc_acct = $old->srcsvc_acct;
294 my $old_username = $old_svc_acct->username;
295 my $old_domain = $old_svc_acct->domain;
298 $destination = $old->dstsvc_acct->email;
300 $destination = $old->dst;
302 foreach my $vpopmailmachine ( @vpopmailmachines ) {
303 my($machine, $vpopdir, $vpopuid, $vpopgid) =
304 split(/\s+/, $vpopmailmachine);
305 my $queue = new FS::queue { 'job' => 'Net::SSH::ssh_cmd' }; # should be neater
306 my $error = $queue->insert("root\@$machine",
307 "sed -e '/^$destination/d' " .
308 "< $vpopdir/domains/$old_domain/$old_username/.qmail" .
309 "> $vpopdir/domains/$old_domain/$old_username/.qmail.temp; " .
310 "mv $vpopdir/domains/$old_domain/$old_username/.qmail.temp " .
311 "$vpopdir/domains/$old_domain/$old_username/.qmail; " .
312 "chown $vpopuid.$vpopgid " .
313 "$vpopdir/domains/$old_domain/$old_username/.qmail;"
318 $dbh->rollback if $oldAutoCommit;
319 return "queueing job (transaction rolled back): $error";
323 #false laziness with stuff in insert, should subroutine
324 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $new->srcsvc } );
325 my $username = $svc_acct->username;
326 my $domain = $svc_acct->domain;
328 $destination = $new->dstsvc_acct->email;
330 $destination = $new->dst;
333 foreach my $vpopmailmachine ( @vpopmailmachines ) {
334 my($machine, $vpopdir, $vpopuid, $vpopgid) = split(/\s+/, $vpopmailmachine);
335 my $queue = new FS::queue { 'job' => 'Net::SSH::ssh_cmd' }; # should be neater
336 my $error = $queue->insert("root\@$machine","[ -d $vpopdir/domains/$domain/$username ] && { echo \"$destination\" >> $vpopdir/domains/$domain/$username/.qmail; chown $vpopuid:$vpopgid $vpopdir/domains/$domain/$username/.qmail; }")
339 $dbh->rollback if $oldAutoCommit;
340 return "queueing job (transaction rolled back): $error";
343 #end subroutinable bits
345 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
351 Just returns false (no error) for now.
353 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
357 Just returns false (no error) for now.
359 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
363 Just returns false (no error) for now.
365 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
369 Checks all fields to make sure this is a valid mail forwarding alias. If there
370 is an error, returns the error, otherwise returns false. Called by the insert
373 Sets any fixed values; see L<FS::part_svc>.
380 my $x = $self->setfixed;
381 return $x unless ref($x);
384 my $error = $self->ut_numbern('svcnum')
385 || $self->ut_number('srcsvc')
386 || $self->ut_numbern('dstsvc')
388 return $error if $error;
390 return "Unknown srcsvc" unless $self->srcsvc_acct;
392 return "Both dstsvc and dst were defined; one one can be specified"
393 if $self->dstsvc && $self->dst;
395 return "one of dstsvc or dst is required"
396 unless $self->dstsvc || $self->dst;
398 #return "Unknown dstsvc: $dstsvc" unless $self->dstsvc_acct || ! $self->dstsvc;
399 return "Unknown dstsvc"
400 unless qsearchs('svc_acct', { 'svcnum' => $self->dstsvc } )
405 $self->dst =~ /^([\w\.\-]+)\@(([\w\-]+\.)+\w+)$/
406 or return "Illegal dst: ". $self->dst;
407 $self->dst("$1\@$2");
417 Returns the FS::svc_acct object referenced by the srcsvc column.
423 qsearchs('svc_acct', { 'svcnum' => $self->srcsvc } );
428 Returns the FS::svc_acct object referenced by the srcsvc column, or false for
429 forwards not local to freeside.
435 qsearchs('svc_acct', { 'svcnum' => $self->dstsvc } );
442 $Id: svc_forward.pm,v 1.10 2002-02-17 19:07:32 jeff Exp $
446 The remote commands should be configurable.
450 L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>,
451 L<FS::svc_acct>, L<FS::svc_domain>, L<Net::SSH>, L<ssh>, L<dot-qmail>,
452 schema.html from the base documentation.