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 {
144 'svcnum' => $self->svcnum,
145 'job' => 'Net::SSH::ssh_cmd',
148 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; }")
151 $dbh->rollback if $oldAutoCommit;
152 return "queueing job (transaction rolled back): $error";
157 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
164 Deletes this mail forwarding alias from the database. If there is an error,
165 returns the error, otherwise returns false.
167 The corresponding FS::cust_svc record will be deleted as well.
169 If the configuration value vpopmailmachines exists, then the command:
171 { sed -e '/^$destination/d' <
172 $vpopdir/domains/$srcdomain/$srcusername/.qmail >
173 $vpopdir/domains/$srcdomain/$srcusername/.qmail.temp;
174 mv $vpopdir/domains/$srcdomain/$srcusername/.qmail.temp
175 $vpopdir/domains/$srcdomain/$srcusername/.qmail;
176 chown $vpopuid.$vpopgid $vpopdir/domains/$srcdomain/$srcusername/.qmail; }
179 is executed on each vpopmailmachine via ssh. This behaviour can be supressed
180 by setting $FS::svc_forward_nossh_hack true.
187 local $SIG{HUP} = 'IGNORE';
188 local $SIG{INT} = 'IGNORE';
189 local $SIG{QUIT} = 'IGNORE';
190 local $SIG{TERM} = 'IGNORE';
191 local $SIG{TSTP} = 'IGNORE';
192 local $SIG{PIPE} = 'IGNORE';
194 my $oldAutoCommit = $FS::UID::AutoCommit;
195 local $FS::UID::Autocommit = 0;
198 my $error = $self->SUPER::delete;
200 $dbh->rollback if $oldAutoCommit;
204 my $svc_acct = $self->srcsvc_acct;
205 my $username = $svc_acct->username;
206 my $domain = $svc_acct->domain;
209 $destination = $self->dstsvc_acct->email;
211 $destination = $self->dst;
213 foreach my $vpopmailmachine ( @vpopmailmachines ) {
214 my($machine, $vpopdir, $vpopuid, $vpopgid) =
215 split(/\s+/, $vpopmailmachine);
216 my $queue = new FS::queue { 'job' => 'Net::SSH::ssh_cmd' };
218 my $error = $queue->insert("root\@$machine",
219 "sed -e '/^$destination/d' " .
220 "< $vpopdir/domains/$domain/$username/.qmail" .
221 "> $vpopdir/domains/$domain/$username/.qmail.temp; " .
222 "mv $vpopdir/domains/$domain/$username/.qmail.temp " .
223 "$vpopdir/domains/$domain/$username/.qmail; " .
224 "chown $vpopuid.$vpopgid $vpopdir/domains/$domain/$username/.qmail;"
229 $dbh->rollback if $oldAutoCommit;
230 return "queueing job (transaction rolled back): $error";
235 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
240 =item replace OLD_RECORD
242 Replaces OLD_RECORD with this one in the database. If there is an error,
243 returns the error, otherwise returns false.
245 If the configuration value vpopmailmachines exists, then the command:
247 { sed -e '/^$destination/d' <
248 $vpopdir/domains/$srcdomain/$srcusername/.qmail >
249 $vpopdir/domains/$srcdomain/$srcusername/.qmail.temp;
250 mv $vpopdir/domains/$srcdomain/$srcusername/.qmail.temp
251 $vpopdir/domains/$srcdomain/$srcusername/.qmail;
252 chown $vpopuid.$vpopgid $vpopdir/domains/$srcdomain/$srcusername/.qmail; }
255 is executed on each vpopmailmachine via ssh. This behaviour can be supressed
256 by setting $FS::svc_forward_nossh_hack true.
258 Also, if the configuration value vpopmailmachines exists, then the command:
260 [ -d $vpopdir/domains/$domain/$source ] && {
261 echo "$destination" >> $vpopdir/domains/$domain/$username/.$qmail
262 chown $vpopuid:$vpopgid $vpopdir/domains/$domain/$username/.$qmail
265 is executed on each vpopmailmachine via ssh. This behaviour can be supressed
266 by setting $FS::svc_forward_nossh_hack true.
271 my ( $new, $old ) = ( shift, shift );
273 if ( $new->srcsvc != $old->srcsvc
274 && ( $new->dstsvc != $old->dstsvc
275 || ! $new->dstsvc && $new->dst ne $old->dst
278 return "Can't change both source and destination of a mail forward!"
281 local $SIG{HUP} = 'IGNORE';
282 local $SIG{INT} = 'IGNORE';
283 local $SIG{QUIT} = 'IGNORE';
284 local $SIG{TERM} = 'IGNORE';
285 local $SIG{TSTP} = 'IGNORE';
286 local $SIG{PIPE} = 'IGNORE';
288 my $oldAutoCommit = $FS::UID::AutoCommit;
289 local $FS::UID::AutoCommit = 0;
292 my $error = $new->SUPER::replace($old);
294 $dbh->rollback if $oldAutoCommit;
298 my $old_svc_acct = $old->srcsvc_acct;
299 my $old_username = $old_svc_acct->username;
300 my $old_domain = $old_svc_acct->domain;
303 $destination = $old->dstsvc_acct->email;
305 $destination = $old->dst;
307 foreach my $vpopmailmachine ( @vpopmailmachines ) {
308 my($machine, $vpopdir, $vpopuid, $vpopgid) =
309 split(/\s+/, $vpopmailmachine);
310 my $queue = new FS::queue {
311 'svcnum' => $new->svcnum,
312 'job' => 'Net::SSH::ssh_cmd',
315 my $error = $queue->insert("root\@$machine",
316 "sed -e '/^$destination/d' " .
317 "< $vpopdir/domains/$old_domain/$old_username/.qmail" .
318 "> $vpopdir/domains/$old_domain/$old_username/.qmail.temp; " .
319 "mv $vpopdir/domains/$old_domain/$old_username/.qmail.temp " .
320 "$vpopdir/domains/$old_domain/$old_username/.qmail; " .
321 "chown $vpopuid.$vpopgid " .
322 "$vpopdir/domains/$old_domain/$old_username/.qmail;"
327 $dbh->rollback if $oldAutoCommit;
328 return "queueing job (transaction rolled back): $error";
332 #false laziness with stuff in insert, should subroutine
333 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $new->srcsvc } );
334 my $username = $svc_acct->username;
335 my $domain = $svc_acct->domain;
337 $destination = $new->dstsvc_acct->email;
339 $destination = $new->dst;
342 foreach my $vpopmailmachine ( @vpopmailmachines ) {
343 my($machine, $vpopdir, $vpopuid, $vpopgid) = split(/\s+/, $vpopmailmachine);
344 my $queue = new FS::queue {
345 'svcnum' => $new->svcnum,
346 'job' => 'Net::SSH::ssh_cmd',
349 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; }")
352 $dbh->rollback if $oldAutoCommit;
353 return "queueing job (transaction rolled back): $error";
356 #end subroutinable bits
358 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
364 Just returns false (no error) for now.
366 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
370 Just returns false (no error) for now.
372 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
376 Just returns false (no error) for now.
378 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
382 Checks all fields to make sure this is a valid mail forwarding alias. If there
383 is an error, returns the error, otherwise returns false. Called by the insert
386 Sets any fixed values; see L<FS::part_svc>.
393 my $x = $self->setfixed;
394 return $x unless ref($x);
397 my $error = $self->ut_numbern('svcnum')
398 || $self->ut_number('srcsvc')
399 || $self->ut_numbern('dstsvc')
401 return $error if $error;
403 return "Unknown srcsvc" unless $self->srcsvc_acct;
405 return "Both dstsvc and dst were defined; only one can be specified"
406 if $self->dstsvc && $self->dst;
408 return "one of dstsvc or dst is required"
409 unless $self->dstsvc || $self->dst;
411 #return "Unknown dstsvc: $dstsvc" unless $self->dstsvc_acct || ! $self->dstsvc;
412 return "Unknown dstsvc"
413 unless qsearchs('svc_acct', { 'svcnum' => $self->dstsvc } )
418 $self->dst =~ /^([\w\.\-]+)\@(([\w\-]+\.)+\w+)$/
419 or return "Illegal dst: ". $self->dst;
420 $self->dst("$1\@$2");
430 Returns the FS::svc_acct object referenced by the srcsvc column.
436 qsearchs('svc_acct', { 'svcnum' => $self->srcsvc } );
441 Returns the FS::svc_acct object referenced by the srcsvc column, or false for
442 forwards not local to freeside.
448 qsearchs('svc_acct', { 'svcnum' => $self->dstsvc } );
455 $Id: svc_forward.pm,v 1.12 2002-05-31 17:50:37 ivan Exp $
459 The remote commands should be configurable.
463 L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>,
464 L<FS::svc_acct>, L<FS::svc_domain>, L<Net::SSH>, L<ssh>, L<dot-qmail>,
465 schema.html from the base documentation.