aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Youri/Submit/Action/RSS.pm
blob: 51da8257cc3b1d57f8798bf4ddfda2897b97cca8 (plain)
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
# $Id: RSS.pm 1700 2006-10-16 12:57:42Z warly $
package Youri::Submit::Action::RSS;

=head1 NAME

Youri::Submit::Action::RSS - RSS notification

=head1 DESCRIPTION

This action plugin ensures RSS notification of new package revisions.

=cut

use warnings;
use strict;
use XML::RSS;
use Encode qw/from_to/;
use Carp;
use base qw/Youri::Submit::Action/;

sub _init {
    my $self   = shift;
    my %options = (
        file        => '',
        title       => '',
        link        => '',
        description => '',
        charset    => 'iso-8859-1',
        max_items   => 10,
        @_
    );

    croak "undefined rss file" unless $options{file};
    croak "invalid charset $options{charset}"
        unless Encode::resolve_alias($options{charset});

    $self->{_file}        = $options{file};
    $self->{_title}       = $options{title};
    $self->{_link}        = $options{link};
    $self->{_description} = $options{description};
    $self->{_charset}    = $options{charset};
    $self->{_max_items}   = $options{max_items};
}

sub run {
    my ($self, $package, $repository, $target, $define) = @_;
    croak "Not a class method" unless ref $self;

    return unless $package->is_source();

    my $subject = $package->as_formated_string('%{name}-%{version}-%{release}');
    my $content = $package->get_information();

    $content =~ s/$/<br\/>/mg;

    # ensure proper codeset conversion
    # for informations coming from package
    my $charset = $repository->get_package_charset();
    from_to($content, $charset, $self->{_charset});
    from_to($subject, $charset, $self->{_charset});

    my $rss = XML::RSS->new(
        encoding      => $self->{_charset},
        encode_output => 1
    );

    my $file = $self->{_file};
    if (-e $file) {
        $rss->parsefile($file);
        splice(@{$rss->{items}}, $self->{_max_items})
            if @{$rss->{items}} >= $self->{_max_items};
    } else {
        $rss->channel(
            title       => $self->{_title},
            link        => $self->{_link},
            description => $self->{_description},
            language    => 'en'
        );
    }

    $rss->add_item(
        title       => $subject,
        description => $content,
        mode        => 'insert'
    );

    if ($self->{_test}) {
        print $rss->as_string();
    } else {
        $rss->save($file);
    }
}

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2002-2006, YOURI project

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=cut

1;