summaryrefslogtreecommitdiff
path: root/bin/events2md.pl
blob: d7a4eb4907353943b52b72b9c12e2c75d1028f89 (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. print_event( $_, $_->start, $_->end, $output_path, );
  131. }
  132. sub print_event
  133. {
  134. my ( $entry, $start, $end, $path ) = @_;
  135. if ( $log->is_trace ) {
  136. use DDP;
  137. p $entry;
  138. p $start;
  139. p $end;
  140. p $path;
  141. }
  142. my $summary = get_property_string( $entry, 'summary' );
  143. my $description = get_property_string( $entry, 'description' );
  144. $description =~ s/\n\n[Pp]ris:\s*((?!\n).+)\s*\z//m;
  145. my $price = $1;
  146. my @attendees;
  147. if ( $entry->property('attendee') ) {
  148. for ( @{ $entry->property('attendee') } ) {
  149. push @attendees, decode_utf8 $_->parameters->{'CN'}
  150. || $_->value =~ s/^mailto://r;
  151. }
  152. }
  153. my $location = get_property_string( $entry, 'location' );
  154. my $time_begin = ucfirst( $start->strftime('%A') );
  155. $time_begin .= $start->strftime(' %e. %B kl. %k.%M');
  156. my $time_end = $end->strftime('%k.%M');
  157. my %attachments;
  158. if ( $entry->property('attach') ) {
  159. for ( @{ $entry->property('attach') } ) {
  160. my $uri = try { URI->new( $_->value ) }
  161. or next;
  162. $uri->authority and $uri->host
  163. or next;
  164. push @{ $attachments{ $uri->host } }, $uri;
  165. }
  166. }
  167. $_ = "### $time_begin.";
  168. $_ .= " $summary"
  169. if $summary;
  170. $_ .= "\n$description";
  171. $_ .= " \nMed " . join( ' og ', @attendees ) . '.'
  172. if @attendees;
  173. $_ .= " \n**Mødested:** $location"
  174. if $location;
  175. $_ .= " \n**Tid:** ${time_begin}-${time_end}."
  176. if $time_begin and $time_end;
  177. $_ .= " \n**Pris:** $price"
  178. if $price;
  179. $_ .= " \n[Køb billet på Billetto]($attachments{'billetto.dk'}[0])"
  180. if $attachments{'billetto.dk'};
  181. $_ .= " \n[Læs mere her]($attachments{'byvandring.nu'}[0])"
  182. if $attachments{'byvandring.nu'};
  183. $_ .= "\n\n---\n\n";
  184. if ($path) {
  185. $path->append_utf8($_);
  186. }
  187. else {
  188. print $_;
  189. }
  190. }
  191. sub get_property_string
  192. {
  193. my ( $entry, $key ) = @_;
  194. return ''
  195. unless $entry->property($key);
  196. return decode_utf8 $entry->property($key)->[0]->value;
  197. }
  198. 1;