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