aboutsummaryrefslogtreecommitdiff
path: root/lib/Object/Groupware/DAV.pm
blob: 675724a1cbd783820970e1936782b810bc659f5c (plain)
  1. use v5.36;
  2. #use Feature::Compat::Class 0.07;
  3. use Object::Pad 0.78;
  4. package Object::Groupware::DAV 0.01;
  5. class Object::Groupware::DAV : isa(Object::Groupware);
  6. use utf8;
  7. use Feature::Compat::Try;
  8. use Net::Netrc;
  9. use IO::Interactive::Tiny;
  10. use Log::Any qw( );
  11. use URI;
  12. use IO::Prompter;
  13. use Cal::DAV;
  14. use DateTime;
  15. use Object::Groupware::Calendar;
  16. field $log = Log::Any->get_logger;
  17. field $uri : param;
  18. field $user : param = undef;
  19. field $pass : param = undef;
  20. field $session;
  21. ADJUST {
  22. $uri = URI->new($uri)
  23. or $log->fatal( 'failed to parse URI %s', $uri ) && exit 2;
  24. $log->debug('resolve credentials...');
  25. my $mach;
  26. ( $user, $pass ) = split ':', $uri->userinfo
  27. if !$user and $uri->userinfo;
  28. $mach = Net::Netrc->lookup( $uri->host, $user )
  29. unless $user and defined $pass;
  30. if ($mach) {
  31. $user ||= $mach->login;
  32. $pass ||= $mach->password;
  33. $log->infof(
  34. 'will use .netrc provided credentials for user %s',
  35. $user
  36. );
  37. }
  38. elsif ( IO::Interactive::Tiny::is_interactive() ) {
  39. $log->warn(
  40. 'will ask for missing info - this will fail in headless mode');
  41. $user ||= prompt 'Enter your username';
  42. $pass ||= prompt 'Enter your password', -echo => '*';
  43. }
  44. $log->debugf( 'resolved credentials for user %s', $user );
  45. }
  46. method get ($new_uri)
  47. {
  48. $uri = URI->new($new_uri)
  49. or $log->fatal( 'failed to parse URI %s', $uri ) && exit 2
  50. if $new_uri;
  51. # fetch and parse CalDAV calendar data
  52. $log->debug('fetch and parse CalDAV calendar data...');
  53. $session //= Cal::DAV->new(
  54. user => $user,
  55. pass => $pass,
  56. url => $uri,
  57. );
  58. # TODO: if calendar object is empty on reused session with no new uri,
  59. # warn on stderr with list of available collections using this sequence:
  60. # 1. PROPFIND on base-URL for {DAV:}current-user-principal
  61. # 2. PROPFIND for calendar-home-set property in caldav namespace
  62. # 3. PROPFIND with depth: 1
  63. # as documented at <https://stackoverflow.com/a/11673483>
  64. return Object::Groupware::Calendar->new(
  65. data => $session->cal,
  66. dt_locale => $self->dt_locale,
  67. dt_time_zone => $self->dt_time_zone,
  68. );
  69. }
  70. 1;