This file is indexed.

/usr/share/perl5/XMLTV/Supplement.pm is in libxmltv-perl 0.5.70-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package XMLTV::Supplement;

use strict;

# use version number for feature detection:
# 0.005065 : addition of GetSupplementLines, GetSupplementRoot, GetSupplementDir
our $VERSION = 0.005065;

BEGIN {
    use Exporter   ();
    our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);

    @ISA         = qw(Exporter);
    @EXPORT      = qw( );
    %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
    @EXPORT_OK   = qw/GetSupplement GetSupplementLines SetSupplementRoot GetSupplementRoot GetSupplementDir/;
}
our @EXPORT_OK;

use File::Slurp qw/read_file/;
use File::Spec;
use File::Path;
use LWP::UserAgent;
use HTTP::Status qw/RC_NOT_MODIFIED RC_OK/;
use XMLTV; # We only need VERSION...

=head1 NAME

XMLTV::Supplement - Supplementary file loader for XMLTV grabbers

=head1 DESCRIPTION

Utility library that loads supplementary files for xmltv-grabbers and other
programs in the xmltv-distribution.

Supplementary files can be loaded either via http or from a local
file, depending on the configuration of the module. The default is to
load the files from http://supplement.xmltv.org. This can be
changed by setting the environment variable XMLTV_SUPPLEMENT to the
new root-directory or root-url for supplementary files.

=head1 EXPORTED FUNCTIONS

All these functions are exported on demand.

=over 4

=cut

sub d {
  print STDERR "XMLTV::Supplement: $_[0]\n" if $ENV{XMLTV_SUPPLEMENT_VERBOSE};
}

my $cachedir;

sub create_cachedir {
  my $base;
  if ($^O eq 'MSWin32') {
    require Win32;
    Win32->import( qw(CSIDL_LOCAL_APPDATA) );
    $base =  Win32::GetFolderPath( CSIDL_LOCAL_APPDATA() );
    $base =~ s/ /\ /g;
    if( not -d $base ) {
      $base =~ s/(.*?)\\Local Settings(.*)/$1$2/;
    } elsif( not -d $base ) {
      die "Unable to find suitable cache-directory: $base";
      exit 1;
    }
    $cachedir = File::Spec->catfile( $base, "xmltv", "supplement" );
  }
  else {
    $cachedir = File::Spec->catfile( $ENV{HOME}, ".xmltv", "supplement" );
  }

  d( "Using cachedir '$cachedir'" );
  create_dir( $cachedir );
}

sub create_dir {
  my( $dir ) = @_;

  eval { mkpath($dir) };
  if ($@) {
    print STDERR "Failed to create $dir: $@";
    exit 1;
  }
}

my $supplement_root;

sub set_supplement_root {
  if( defined( $ENV{XMLTV_SUPPLEMENT} ) ) {
    $supplement_root = $ENV{XMLTV_SUPPLEMENT};
  }
  else {
    $supplement_root = "http://supplement.xmltv.org/";
  }
}

my $ua;

sub init_ua {
  $ua = LWP::UserAgent->new( agent => "XMLTV::Supplement/" . $XMLTV::VERSION );
  $ua->env_proxy();
}

=item GetSupplement

Load a supplement file and return it as a string. Takes two parameters:
directory and filename.

    my $content = GetSupplement( 'tv_grab_uk_rt', 'channel_ids' );

GetSupplement will always return a string with the content. If it fails
to get the content, it prints an error-message and aborts the program.

=cut

sub GetSupplement {
  my( $directory, $name ) = @_;

  set_supplement_root() if not defined $supplement_root;

  if( $supplement_root =~ m%^http(s){0,1}://% ) {
    return GetSupplementUrl( $directory, $name );
  }
  else {
    return GetSupplementFile( $directory, $name );
  }
}

=item GetSupplementLines

Load a supplement file and return it as an array. Any comments or blank lines
will be removed. Takes two parameters: directory and filename.

    my $content = GetSupplementLines( 'tv_grab_uk_rt', 'channel_ids' );

GetSupplementLines will always return an array with the content. If it fails
to get the content, it prints an error-message and aborts the program.

=cut

sub GetSupplementLines {
  my $supplement_string = GetSupplement( @_ );
	my @supplement_array;
  my @supplement_lines = split( /\n+/, $supplement_string );
  foreach ( @supplement_lines ) {
    tr/\r//d;           # strip CRs
		s/#.*//;            # strip comment
    next if m/^\s*$/;   # ignore blank lines
		s/^\s+|\s+$//g;     # remove leading & trailing spaces
		push @supplement_array, $_;
	}

	return \@supplement_array;
}

=item GetSupplementFile

Get the supplement file from the local machine

=cut

sub GetSupplementFile {
  my( $directory, $name ) = @_;

  my $filename;

  if( defined( $directory ) ) {
    $filename = File::Spec->catfile( $supplement_root, $directory, $name );
  }
  else {
    $filename = File::Spec->catfile( $supplement_root, $name );
  }
  $filename =~ s/[:\?]/_/g;
  my $result;

  d( "Reading $filename" );
  eval { $result = read_file( $filename ) };

  if( not defined( $result ) ) {
    print STDERR "XMLTV::Supplement: Failed to read from $filename.\n";
    exit 1;
  }

  return $result;
}

=item GetSupplementUrl

Get the supplement file from a URL

=cut

sub GetSupplementUrl {
  my( $directory, $name ) = @_;

  create_cachedir() if not defined $cachedir;
  init_ua() if not defined $ua;

  my $dir;
  if( defined( $directory ) ) {
    $dir = File::Spec->catfile( $cachedir, $directory );
    create_dir( $dir );
  }
  else {
    $dir = $cachedir;
  }

  # Remove trailing slash
  $supplement_root =~ s%/$%%;

  my $url;
  if( defined( $directory ) ) {
    $url = "$supplement_root/$directory/$name";
  }
  else {
    $url = "$supplement_root/$name";
  }

  d( "Going to fetch $url" );
  $name =~ s/[:\?]/_/g;

  my $meta = read_meta( $directory, $name );
  my $cached = read_cache( $directory, $name );

  my %p;

  if( defined( $meta->{Url} ) and ($meta->{Url} eq $url ) ) {
    # The right url is stored in the cache.

    if( defined( $cached ) and defined( $meta->{'LastUpdated'} )
	and 1*60*60 > (time - $meta->{'LastUpdated'} ) ) {
      d("LastUpdated ok. Using cache.");
      return $cached;
    }

    if( defined( $cached ) ) {
      $p{'If-Modified-Since'} = $meta->{'Last-Modified'}
      if defined $meta->{'Last-Modified'};
      $p{'If-None-Match'} = $meta->{ETag}
      if defined $meta->{ETag};
    }
  }

  my $resp = $ua->get( $url, %p );

  if( $resp->code == RC_NOT_MODIFIED ) {
    write_meta( $directory, $url, $name, $resp, $meta );
    d("Not Modified. Using cache.");
    return $cached;
  }
  elsif( $resp->is_success ) {
    write_meta( $directory, $url, $name, $resp, $meta );
    write_cache( $directory, $name, $resp );
    d("Cache miss.");
    return $resp->content;
  }
  elsif( defined( $cached ) ) {
    print STDERR "XMLTV::Supplement: Failed to fetch $url: " .
	$resp->status_line . ". Using cached info.\n";
    return $cached;
  }
  else {
    print STDERR "XMLTV::Supplement: Failed to fetch $url: " .
	$resp->status_line . ".\n";
    exit 1;
  }
}

=item GetSupplementDir

Get the base directory containing supplementary files.
   e.g. $ENV{HOME}/.xmltv/supplement

=cut

sub GetSupplementDir {
  create_cachedir() if not defined $cachedir;
  return $cachedir;
}

=item SetSupplementRoot

Set the root directory for loading supplementary files.

    SetSupplementRoot( '/usr/share/xmltv' );
    SetSupplementRoot( 'http://my.server.org/xmltv' );

=cut

sub SetSupplementRoot {
  my( $root ) = @_;

  $supplement_root = $root;
}

=item GetSupplementRoot

Get the root directory for loading supplementary files.

=cut

sub GetSupplementRoot {
  set_supplement_root() if not defined $supplement_root;
  return $supplement_root;
}

sub write_meta {
  my( $directory, $url, $file, $resp, $meta ) = @_;

  my $metafile = cache_filename( $directory, "$file.meta" );

  open OUT, "> $metafile" or die "Failed to write to $metafile";
  print OUT "LastUpdated " . time() . "\n";

  print OUT "Url $url\n";

  if( defined $resp->header( 'Last-Modified' ) ) {
    print OUT "Last-Modified " . $resp->header( 'Last-Modified' ) . "\n";
  }
  elsif( defined $meta->{'Last-Modified'} ) {
    print OUT "Last-Modified " . $meta->{ 'Last-Modified' } . "\n";
  }

  print OUT "ETag " . $resp->header( 'ETag' )  . "\n"
      if defined $resp->header( 'ETag' );
  close( OUT );
}

sub read_meta {
  my( $directory, $file ) = @_;

  my $metafile = cache_filename( $directory, "$file.meta" );

  return {} if not -f( $metafile );

  my $str = read_file( $metafile );
  my @lines = split( "\n", $str );
  my $result = {};
  foreach my $line (@lines) {
    my($key, $value ) = ($line =~ /(.*?) (.*)/);
    $result->{$key} = $value;
  }

  return $result;
}

sub read_cache {
  my( $directory, $file ) = @_;

  my $filename = cache_filename( $directory, $file );

  my $result;
  eval { $result = read_file( $filename ) };
  return $result;
}

sub write_cache {
  my( $directory, $file, $resp ) = @_;

  my $filename = cache_filename( $directory, $file );

  open OUT, "> $filename" or die "Failed to write to $filename";
  binmode OUT;
  print OUT $resp->content;
  close( OUT );
}

sub cache_filename {
  my( $directory, $file ) = @_;

  if( defined( $directory ) ) {
    return File::Spec->catfile( $cachedir, $directory, $file );
  }
  else {
    return File::Spec->catfile( $cachedir, $file );
  }
}

=back

=head1 CACHING

The module stores all downloaded files in a cache. The cache is stored
on disk in ~/.xmltv/supplement on Unix and in
CSIDL_LOCAL_APPDATA//xmltv/supplement on Windows.

If a file has been downloaded less than 1 hour ago, the file from
the cache is used without contacting the server. Otherwise, if the
file has been downloaded more than 1 hour ago, then the module
checks with the server to see if an updated file is available and
downloads it if necessary.

If the server does not respond or returns an error-message, a warning
is printed to STDERR and the file from the cache is used.

=head1 ENVIRONMENT VARIABLES

The XMLTV_SUPPLEMENT environment variable can be used to tell the
module where the supplementary files are found.

  XMLTV_SUPPLEMENT=/usr/share/xmltv
  XMLTV_SUPPLEMENT=http://supplementary.xmltv.se

The XMLTV_SUPPLEMENT_VERBOSE environment variable can be used to get
more debug output from XMLTV::Supplement.

  XMLTV_SUPPLEMENT_VERBOSE=1

=head1 COPYRIGHT

Copyright (C) 2007 Mattias Holmlund.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

=cut

1;