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