From 26fd1adbbd67b0cfe547db0ea77dbdfb8a1e2eb6 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Thu, 20 May 2021 21:33:07 +0200 Subject: restructure: wrap credentials resolving and calendar fetching in URI check --- bin/events2md.pl | 91 +++++++++++++++++++++++++++----------------------------- 1 file changed, 44 insertions(+), 47 deletions(-) diff --git a/bin/events2md.pl b/bin/events2md.pl index 9253050..7fd8a5e 100755 --- a/bin/events2md.pl +++ b/bin/events2md.pl @@ -45,58 +45,55 @@ $OUTPUT_FILE = shift @ARGV DateTime->DefaultLocale( setlocale(LC_TIME) ); # resolve calendar URIs -my ( $base_uri, $calendar_uri ); +my ( $base_uri, $calendar_uri, $calendar ); $base_uri = URI->new($BASE_URI) if ($BASE_URI); -if ( !$base_uri or !$base_uri->authority ) { - $log->fatal('bad base URI: must be an internet URI'); - exit 2; -} -$log->infof( 'will use base URI %s', $base_uri ); -$calendar_uri = URI->new( $CALENDAR_URI || $base_uri ); -if ( !$calendar_uri or !$calendar_uri->authority ) { - $log->fatal('bad calendar URI: must be an internet URI'); - exit 2; -} -elsif ( $base_uri->eq($calendar_uri) ) { - $calendar_uri = undef; +$base_uri + or $log->fatal('required base URI not provided') && exit 2; +$base_uri->scheme + or $log->fatal('bad base URI: must be an internet URI') && exit 2; +if ( $base_uri->scheme eq 'http' or $base_uri->scheme eq 'https' ) { + $log->infof( 'will use base URI %s', $base_uri ); + $calendar_uri = URI->new( $CALENDAR_URI || $base_uri ); + $calendar_uri and $calendar_uri->authority + or $log->fatal('bad calendar URI: must be an internet URI') && exit 2; + $base_uri->eq($calendar_uri) and $calendar_uri = undef + or $log->infof( 'will use calendar URI %s', $calendar_uri ); + + # resolve credentials + $log->debug('resolve credentials...'); + my ( $mach, $user, $pass ); + ( $user, $pass ) = split ':', $base_uri->userinfo + if $base_uri->userinfo; + $user ||= $ENV{CAL_DAV_USER}; + $pass ||= $ENV{CAL_DAV_PASS}; + $mach = Net::Netrc->lookup( $base_uri->host, $user ) + if !$user or !$pass; + if ($mach) { + $user ||= $mach->login; + $pass ||= $mach->password; + $log->infof( 'will use .netrc provided credentials for user %s', $user ); + } + elsif ( IO::Interactive::Tiny::is_interactive() ) { + $log->warn('will ask for missing info - this will fail in headless mode'); + $user ||= prompt 'Enter your username'; + $pass ||= prompt 'Enter your password', -echo => '*'; + } + $log->debugf( 'resolved credentials for user %s', $user ); + + # fetch and parse calendar data + $log->debug('fetch and parse calendar data...'); + $calendar = Cal::DAV->new( + user => $user, + pass => $pass, + url => $base_uri, + ); + $calendar->get($calendar_uri) + if $calendar_uri; } else { - $log->infof( 'will use calendar URI %s', $calendar_uri ); -} - -# resolve credentials -$log->debug('resolve credentials...'); -my ( $mach, $user, $pass ); -if ( $base_uri->userinfo ) { - ( $user, $pass ) = split ':', $base_uri->userinfo; -} -$user ||= $ENV{CAL_DAV_USER}; -$pass ||= $ENV{CAL_DAV_PASS}; -if ( !$user or !$pass ) { - $mach = Net::Netrc->lookup( $base_uri->host, $user ); -} -if ($mach) { - $user ||= $mach->login; - $pass ||= $mach->password; - $log->infof( 'will use .netrc provided credentials for user %s', $user ); -} -elsif ( IO::Interactive::Tiny::is_interactive() ) { - $log->warn('will ask for missing info - this will fail in headless mode'); - $user ||= prompt 'Enter your username'; - $pass ||= prompt 'Enter your password', -echo => '*'; + $log->fatal('bad base URI: must be an internet URI') && exit 2; } -$log->debugf( 'resolved credentials for user %s', $user ); - -# fetch and parse calendar data -$log->debug('fetch and parse calendar data...'); -my $calendar = Cal::DAV->new( - user => $user, - pass => $pass, - url => $base_uri, -); -$calendar->get($calendar_uri) - if $calendar_uri; if ( $log->is_trace ) { use DDP; p $calendar; -- cgit v1.2.3