1 package FS::upload_target;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6 use FS::Misc qw(send_email);
9 use vars qw($me $DEBUG);
15 FS::upload_target - Object methods for upload_target records
19 use FS::upload_target;
21 $record = new FS::upload_target \%hash;
22 $record = new FS::upload_target { 'column' => 'value' };
24 $error = $record->insert;
26 $error = $new_record->replace($old_record);
28 $error = $record->delete;
30 $error = $record->check;
34 An FS::upload_target object represents a destination to deliver files (such
35 as invoice batches) by FTP, SFTP, or email. FS::upload_target inherits from
40 =item targetnum - primary key
42 =item agentnum - L<FS::agent> foreign key; can be null
44 =item protocol - 'ftp', 'sftp', or 'email'.
46 =item hostname - the DNS name of the FTP site, or the domain name of the
49 =item port - the TCP port number, if it's not standard.
51 =item username - username
53 =item password - password
55 =item path - for FTP/SFTP, the working directory to change to upon connecting.
57 =item subject - for email, the Subject: header
59 =item handling - a string naming an additional process to apply to
60 the file before sending it.
70 sub table { 'upload_target'; }
74 Creates a new FTP target. To add it to the database, see L<"insert">.
78 Adds this record to the database. If there is an error, returns the error,
79 otherwise returns false.
83 Delete this record from the database.
85 =item replace OLD_RECORD
87 Replaces the OLD_RECORD with this one in the database. If there is an error,
88 returns the error, otherwise returns false.
92 Checks all fields to make sure this is a valid example. If there is
93 an error, returns the error, otherwise returns false. Called by the insert
101 my $protocol = lc($self->protocol);
102 if ( $protocol eq 'email' ) {
103 $self->set(password => '');
104 $self->set(port => '');
105 $self->set(path => '');
106 } elsif ( $protocol eq 'sftp' ) {
107 $self->set(port => 22) unless $self->get('port');
108 $self->set(subject => '');
109 } elsif ( $protocol eq 'ftp' ) {
110 $self->set('port' => 21) unless $self->get('port');
111 $self->set(subject => '');
113 return "protocol '$protocol' not supported";
115 $self->set(protocol => $protocol); # lowercase it
118 $self->ut_numbern('targetnum')
119 || $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
120 || $self->ut_text('hostname')
121 || $self->ut_text('username')
122 || $self->ut_textn('password')
123 || $self->ut_numbern('port')
124 || $self->ut_textn('path')
125 || $self->ut_textn('subject')
126 || $self->ut_enum('handling', [ $self->handling_types ])
128 return $error if $error;
133 =item put LOCALNAME [ REMOTENAME ]
135 Uploads the file named LOCALNAME, optionally changing its name to REMOTENAME
136 on the target. For FTP/SFTP, this opens a connection, changes to the working
137 directory (C<path>), and PUTs the file. For email, it composes an empty
138 message and attaches the file.
140 Returns an error message if anything goes wrong.
146 my $localname = shift;
147 my @s = File::Spec->splitpath($localname);
148 my $remotename = shift || $s[-1];
150 my $conf = FS::Conf->new;
151 if ( $self->protocol eq 'ftp' or $self->protocol eq 'sftp' ) {
152 # could cache this if we ever want to reuse it
154 my $connection = eval { $self->connect };
156 $connection->put($localname, $remotename) or return $connection->error;
157 } elsif ( $self->protocol eq 'email' ) {
159 my $to = join('@', $self->username, $self->hostname);
160 # XXX if we were smarter, this could use a message template for the
161 # message subject, body, and source address
162 # (maybe use only the raw content, so that we don't have to supply a
163 # customer for substitutions? ewww.)
165 'from' => $conf->config('invoice_from'),
167 'subject' => $self->subject,
170 { Path => $localname,
171 Type => 'application/octet-stream',
172 Encoding => 'base64',
173 Filename => $remotename,
174 Disposition => 'attachment',
178 return send_email(%message);
181 return "unknown protocol '".$self->protocol."'";
187 Creates a Net::FTP or Net::SFTP::Foreign object (according to the setting
188 of the 'secure' flag), connects to 'hostname', attempts to log in with
189 'username' and 'password', and changes the working directory to 'path'.
190 On success, returns the object. On failure, dies with an error message.
192 Always returns an error for email targets.
198 if ( $self->protocol eq 'sftp' ) {
199 eval "use Net::SFTP::Foreign;";
203 user => $self->username,
204 password => $self->password,
205 more => ($DEBUG ? '-v' : ''),
207 autodie => 1, #we're doing this anyway
209 my $sftp = Net::SFTP::Foreign->new($self->hostname, %args);
210 $sftp->setcwd($self->path);
213 elsif ( $self->protocol eq 'ftp') {
214 eval "use Net::FTP;";
219 Passive => 1,# optional?
221 my $ftp = Net::FTP->new($self->hostname, %args)
222 or die "connect to ".$self->hostname." failed: $@";
223 $ftp->login($self->username, $self->password)
224 or die "login to ".$self->username.'@'.$self->hostname." failed: $@";
225 $ftp->binary; #optional?
226 $ftp->cwd($self->path)
227 or ($self->path eq '/')
228 or die "cwd to ".$self->hostname.'/'.$self->path." failed: $@";
232 return "can't connect() to a target of type '".$self->protocol."'";
238 Returns a descriptive label for this target.
244 $self->targetnum . ': ' . $self->username . '@' . $self->hostname;
249 Returns a list of values for the "handling" field, corresponding to the
250 known ways to preprocess a file before uploading. Currently those are
251 implemented somewhat crudely in L<FS::Cron::upload>.
257 #'billco', #not implemented this way yet
266 Handling methods should be here, but instead are in FS::Cron.
270 L<FS::Record>, schema.html from the base documentation.