diff options
author | Jonas Smedegaard <dr@jones.dk> | 2021-05-20 21:33:07 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2024-08-27 23:39:36 +0200 |
commit | af5fb95e06f052cd52049c23680ae03a0e405b40 (patch) | |
tree | b77615a017302e5d9ed1cb738bdde800ff1dbbd6 | |
parent | 8598858ea12a92299ab8588d906095c35b6daced (diff) |
restructure: wrap credentials resolving and calendar fetching in URI check
-rwxr-xr-x | bin/events2md.pl | 91 |
1 files 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; |