aboutsummaryrefslogtreecommitdiff
path: root/bin/events2md.pl
blob: 7cb41f01918794ce3d6259bd6446cb5d9f90e74a (plain)
  1. #!/usr/bin/perl
  2. use v5.14;
  3. use utf8;
  4. use open qw(:std :encoding(UTF-8));
  5. use strictures;
  6. use autodie;
  7. use POSIX qw(locale_h);
  8. use locale;
  9. use Encode qw(decode_utf8); # TODO: modernize CalDAV access instead
  10. use Net::Netrc;
  11. use List::Util qw(first);
  12. use IO::Interactive::Tiny;
  13. use Log::Any qw($log);
  14. use Log::Any::Adapter;
  15. use URI;
  16. use IO::Prompter;
  17. use Cal::DAV;
  18. use Data::ICal::DateTime;
  19. use DateTime;
  20. use Try::Tiny;
  21. use Path::Tiny;
  22. if ( IO::Interactive::Tiny::is_interactive() ) {
  23. Log::Any::Adapter->set( 'Screen', default_level => 'info' );
  24. }
  25. # set defaults and parse command-line options
  26. my ( $BASE_URI, $CALENDAR_URI, $OUTPUT_FILE );
  27. $BASE_URI = $ENV{CAL_DAV_URL_BASE};
  28. $CALENDAR_URI = $ENV{CAL_DAV_URL_CALENDAR};
  29. $BASE_URI ||= shift @ARGV
  30. if @ARGV;
  31. $CALENDAR_URI ||= shift @ARGV
  32. if @ARGV;
  33. $OUTPUT_FILE = shift @ARGV
  34. if @ARGV;
  35. # use system locale to format DateTime objects parsed from iCal data
  36. DateTime->DefaultLocale( setlocale(LC_TIME) );
  37. # resolve calendar URIs
  38. my ( $base_uri, $calendar_uri, $calendar );
  39. $base_uri = URI->new($BASE_URI)
  40. if ($BASE_URI);
  41. $base_uri
  42. or $log->fatal('required base URI not provided') && exit 2;
  43. $base_uri->scheme
  44. or $base_uri->scheme('file');
  45. if ( $base_uri->scheme eq 'http' or $base_uri->scheme eq 'https' ) {
  46. $log->infof( 'will use base URI %s', $base_uri );
  47. $calendar_uri = URI->new( $CALENDAR_URI || $base_uri );
  48. $calendar_uri and $calendar_uri->authority
  49. or $log->fatal('bad calendar URI: must be an internet URI') && exit 2;
  50. $base_uri->eq($calendar_uri) and $calendar_uri = undef
  51. or $log->infof( 'will use calendar URI %s', $calendar_uri );
  52. # resolve credentials
  53. $log->debug('resolve credentials...');
  54. my ( $mach, $user, $pass );
  55. ( $user, $pass ) = split ':', $base_uri->userinfo
  56. if $base_uri->userinfo;
  57. $user ||= $ENV{CAL_DAV_USER};
  58. $pass ||= $ENV{CAL_DAV_PASS};
  59. $mach = Net::Netrc->lookup( $base_uri->host, $user )
  60. if !$user or !$pass;
  61. if ($mach) {
  62. $user ||= $mach->login;
  63. $pass ||= $mach->password;
  64. $log->infof( 'will use .netrc provided credentials for user %s', $user );
  65. }
  66. elsif ( IO::Interactive::Tiny::is_interactive() ) {
  67. $log->warn('will ask for missing info - this will fail in headless mode');
  68. $user ||= prompt 'Enter your username';
  69. $pass ||= prompt 'Enter your password', -echo => '*';
  70. }
  71. $log->debugf( 'resolved credentials for user %s', $user );
  72. # fetch and parse CalDAV calendar data
  73. $log->debug('fetch and parse CalDAV calendar data...');
  74. $calendar = Cal::DAV->new(
  75. user => $user,
  76. pass => $pass,
  77. url => $base_uri,
  78. );
  79. $calendar->get($calendar_uri)
  80. if $calendar_uri;
  81. }
  82. elsif ( $base_uri->scheme eq 'file' ) {
  83. defined $base_uri->file
  84. or $log->fatal('bad base URI: cannot open file') && exit 2;
  85. $log->infof( 'will use base URI %s', $base_uri );
  86. # parse local calendar data
  87. $log->debug('parse local calendar data...');
  88. my $path = path( $base_uri->file );
  89. if ( $path->is_file ) {
  90. $calendar = Data::ICal->new( data => $path->slurp_raw );
  91. }
  92. else {
  93. my $data;
  94. $path->visit( sub { $data .= $_->slurp_raw if $_->is_file } );
  95. $calendar = Data::ICal->new( data => $data );
  96. }
  97. }
  98. if ( $log->is_trace ) {
  99. use DDP;
  100. p $calendar;
  101. }
  102. # TODO: if list is empty and no calendar uri was explicitly supplied,
  103. # warn on stdout with list of abailable collections using this sequence:
  104. # 1. PROPFIND on base-URL for {DAV:}current-user-principal
  105. # 2. PROPFIND for calendar-home-set property in caldav namespace
  106. # 3. PROPFIND with depth: 1
  107. # as documented at <https://stackoverflow.com/a/11673483>
  108. # serialize calendar events
  109. $log->debug('serialize calendar events...');
  110. my $start = DateTime->now;
  111. my $end = $start->clone->add( months => 6 );
  112. my $span = DateTime::Span->from_datetimes( start => $start, end => $end );
  113. my @events = sort {
  114. DateTime->compare( $a->start, $b->start )
  115. || DateTime->compare( $a->end, $b->end )
  116. || get_property_string( $a, 'summary' )
  117. cmp get_property_string( $b, 'summary' )
  118. } $calendar->events($span);
  119. if ( $log->is_trace ) {
  120. use DDP;
  121. p @events;
  122. }
  123. my $output_path;
  124. if ($OUTPUT_FILE) {
  125. $output_path = path($OUTPUT_FILE);
  126. $output_path->parent->mkpath;
  127. $output_path->remove;
  128. }
  129. for (@events) {
  130. next unless $_->summary;
  131. print_event( $_, $_->start, $_->end, $output_path, );
  132. }
  133. sub print_event
  134. {
  135. my ( $entry, $start, $end, $path ) = @_;
  136. if ( $log->is_trace ) {
  137. use DDP;
  138. p $entry;
  139. p $start;
  140. p $end;
  141. p $path;
  142. }
  143. my $summary = get_property_string( $entry, 'summary' );
  144. my $description = get_property_string( $entry, 'description' );
  145. $description =~ s/\n\n[Pp]ris:\s*((?!\n).+)\s*\z//m;
  146. my $price = $1;
  147. my @attendees;
  148. if ( $entry->property('attendee') ) {
  149. for ( @{ $entry->property('attendee') } ) {
  150. push @attendees, decode_utf8 $_->parameters->{'CN'}
  151. || $_->value =~ s/^mailto://r;
  152. }
  153. }
  154. my $location = get_property_string( $entry, 'location' );
  155. my $time_begin = ucfirst( $start->strftime('%A') );
  156. $time_begin .= $start->strftime(' %e. %B kl. %k.%M');
  157. my $time_end = $end->strftime('%k.%M');
  158. my %attachments;
  159. if ( $entry->property('attach') ) {
  160. for ( @{ $entry->property('attach') } ) {
  161. my $uri = try { URI->new( $_->value ) }
  162. or next;
  163. $uri->authority and $uri->host
  164. or next;
  165. push @{ $attachments{ $uri->host } }, $uri;
  166. }
  167. }
  168. $_ = "### $time_begin.";
  169. $_ .= " $summary"
  170. if $summary;
  171. $_ .= "\n$description";
  172. $_ .= " \nMed " . join( ' og ', @attendees ) . '.'
  173. if @attendees;
  174. $_ .= " \n**Mødested:** $location"
  175. if $location;
  176. $_ .= " \n**Tid:** ${time_begin}-${time_end}."
  177. if $time_begin and $time_end;
  178. $_ .= " \n**Pris:** $price"
  179. if $price;
  180. $_ .= " \n[Køb billet på Billetto]($attachments{'billetto.dk'}[0])"
  181. if $attachments{'billetto.dk'};
  182. $_ .= " \n[Læs mere her]($attachments{'byvandring.nu'}[0])"
  183. if $attachments{'byvandring.nu'};
  184. $_ .= "\n\n---\n\n";
  185. if ($path) {
  186. $path->append_utf8($_);
  187. }
  188. else {
  189. print $_;
  190. }
  191. }
  192. sub get_property_string
  193. {
  194. my ( $entry, $key ) = @_;
  195. return ''
  196. unless $entry->property($key);
  197. return decode_utf8 $entry->property($key)->[0]->value;
  198. }
  199. 1;