use strict;
use Getopt::Std;
-use Net::SFTP::Foreign;
+use Date::Format;
+use Net::SFTP::Foreign::Compat;
+use Net::FTP;
use FS::UID qw(adminsuidsetup datasrc);
use FS::cdr;
# parse command line
###
-use vars qw( $opt_p $opt_e $opt_d $opt_v );
-getopts('p:e:d:v');
+use vars qw( $opt_m $opt_p $opt_r $opt_e $opt_d $opt_v $opt_P $opt_a $opt_c $opt_g );
+getopts('c:m:p:r:e:d:v:P:ag');
$opt_e ||= 'csv';
#$opt_e = ".$opt_e" unless $opt_e =~ /^\./;
$opt_p ||= '';
+die "invalid cdrtypenum" if $opt_c && $opt_c !~ /^\d+$/;
+
+my %options = ();
+
my $user = shift or die &usage;
adminsuidsetup $user;
# %%%FREESIDE_CACHE%%%
-my $cachedir = '/usr/local/etc/freeside/cache.'. datasrc. '/cdrs';
+my $cachedir = '%%%FREESIDE_CACHE%%%/cache.'. datasrc. '/cdrs';
mkdir $cachedir unless -d $cachedir;
my $format = shift or die &usage;
# get the file list
###
-warn "Retreiving directory listing\n" if $opt_v;
+warn "Retrieving directory listing\n" if $opt_v;
+
+$opt_m = 'sftp' if !defined($opt_m);
+$opt_m = lc($opt_m);
+
+my $ls;
-my $ls_sftp = sftp();
+if($opt_m eq 'ftp') {
+ $options{'Port'} = $opt_P if $opt_P;
+ $options{'Debug'} = $opt_v if $opt_v;
+ $options{'Passive'} = $opt_a if $opt_a;
+
+ my $ls_ftp = ftp();
+
+ $ls = [ grep { /^$opt_p.*\.$opt_e$/i } $ls_ftp->ls ];
+}
+elsif($opt_m eq 'sftp') {
+ $options{'port'} = $opt_P if $opt_P;
+ $options{'debug'} = $opt_v if $opt_v;
-my $ls = $ls_sftp->ls('.', wanted => qr/^$opt_p.*\.$opt_e$/i );
+ my $ls_sftp = sftp();
+
+ $ls_sftp->setcwd($opt_r) or die "can't chdir to $opt_r\n"
+ if $opt_r;
+
+ $ls = $ls_sftp->ls('.', wanted => qr/^$opt_p.*\.$opt_e$/i,
+ names_only => 1 );
+}
+else {
+ die "Method '$opt_m' not supported; must be ftp or sftp\n";
+}
###
# import each file
###
-foreach my $file ( @$ls ) {
+foreach my $filename ( @$ls ) {
- my $filename = $file->{filename};
warn "Downloading $filename\n" if $opt_v;
#get the file
- my $get_sftp = sftp();
- $get_sftp->get($filename, "$cachedir/$filename")
- or die "Can't get $filename: ". $get_sftp->error;
+ if($opt_m eq 'ftp') {
+ my $ftp = ftp();
+ $ftp->get($filename, "$cachedir/$filename")
+ or die "Can't get $filename: ". $ftp->message . "\n";
+ }
+ else {
+ my $sftp = sftp();
+ $sftp->get($filename, "$cachedir/$filename")
+ or die "Can't get $filename: ". $sftp->error . "\n";
+ }
warn "Processing $filename\n" if $opt_v;
+
+ my $ungziped = $filename;
+ $ungziped =~ s/\.gz$//;
+ if ( $opt_g ) {
+ if(system('gunzip', "$cachedir/$filename") != 0) {
+ warn "gunzip of '$cachedir/$filename' failed" if $opt_v;
+ unlink "$cachedir/$filename";
+ next;
+ }
+ }
- my $error = FS::cdr::batch_import( {
- 'file' => "$cachedir/$filename",
- 'format' => $format,
- 'params' => { 'cdrbatch' => $filename, },
- 'empty_ok' => 1,
- } );
- die $error if $error;
+ my $file_timestamp = $filename.'-'.time2str('%Y-%m-%d', time);
+
+ my $import_options = {
+ 'file' => "$cachedir/$ungziped",
+ 'format' => $format,
+ 'batch_namevalue' => $file_timestamp,
+ 'empty_ok' => 1,
+ };
+ $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
+
+ my $error = FS::cdr::batch_import($import_options);
+ if ( $error ) {
+ unlink "$cachedir/$filename";
+ unlink "$cachedir/$ungziped" if $opt_g;
+ die $error;
+ }
if ( $opt_d ) {
- my $mv_sftp = sftp();
- $mv_sftp->rename($filename, "$opt_d/$filename")
- or die "can't move $filename to $opt_d: ". $mv_sftp->error;
+ if($opt_m eq 'ftp') {
+ my $ftp = ftp();
+ $ftp->rename($filename, "$opt_d/$file_timestamp")
+ or do {
+ unlink "$cachedir/$filename";
+ unlink "$cachedir/$ungziped" if $opt_g;
+ die "Can't move $filename to $opt_d: ".$ftp->message . "\n";
+ };
+ }
+ else {
+ my $sftp = sftp();
+ $sftp->rename($filename, "$opt_d/$file_timestamp")
+ or do {
+ unlink "$cachedir/$filename";
+ unlink "$cachedir/$ungziped" if $opt_g;
+ die "can't move $filename to $opt_d: ". $sftp->error . "\n";
+ };
+ }
}
unlink "$cachedir/$filename";
+ unlink "$cachedir/$ungziped" if $opt_g;
}
"Usage: \n cdr.import user format servername\n";
}
-use vars qw( $sftp );
+use vars qw( $sftp $ftp );
+
+sub ftp {
+ return $ftp if $ftp && $ftp->pwd;
+
+ my ($hostname, $userpass) = reverse split('@', $servername);
+ my ($ftp_user, $ftp_pass) = split(':', $userpass);
+
+ my $ftp = Net::FTP->new($hostname, %options)
+ or die "FTP connection to '$hostname' failed.";
+ $ftp->login($ftp_user, $ftp_pass) or die "FTP login failed: ".$ftp->message;
+ $ftp->cwd($opt_r) or die "can't chdir to $opt_r\n" if $opt_r;
+ return $ftp;
+}
sub sftp {
=head1 NAME
-cdr.sftp_and_import - Download CDR files from a remote server via SFTP
+freeside-cdr-sftp_and_import - Download CDR files from a remote server via SFTP
=head1 SYNOPSIS
- cdr.sftp_and_import [ -p prefix ] [ -e extension ] [ -d donefolder ] [ -v ] user format [sftpuser@]servername
+ cdr.sftp_and_import [ -m method ] [ -p prefix ] [ -e extension ]
+ [ -r remotefolder ] [ -d donefolder ] [ -v level ] [ -P port ]
+ [ -a ] [ -c cdrtypenum ] user format [sftpuser@]servername
=head1 DESCRIPTION
-Command line tool to download CDR files from a remote server via SFTP and then
-import them into the database.
+Command line tool to download CDR files from a remote server via SFTP
+or FTP and then import them into the database.
+
+-m: transfer method (sftp or ftp), defaults to sftp
-p: file prefix, if specified
-e: file extension, defaults to .csv
+-r: if specified, changes into this remote folder before starting
+
-d: if specified, moves files to the specified folder when done
--v: verbose
+-P: if specified, sets the port to use
+
+-a: use ftp passive mode
+
+-v: set verbosity level; this script only has one level, but it will
+ be passed as the 'debug' argument to the transport method
+
+-c: cdrtypenum to set, defaults to none
+
+-g: File is gzipped
user: freeside username
format: CDR format name
[sftpuser@]servername: remote server
+(or ftpuser:ftppass@servername)
=head1 BUGS
-Only supports SFTP right now.
-
=head1 SEE ALSO
L<FS::cdr>