summaryrefslogtreecommitdiffstats
path: root/lib/Crawl/Bot.pm
blob: 75e8a0d93766e3c97f61e1448f2effff23d7780b (plain) (blame)
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
package Crawl::Bot;
use Moose;
use MooseX::NonMoose;
extends 'Bot::BasicBot';

use autodie;
use XML::RAI;

has [qw(username name)] => (
    # don't need (or want) accessors, just want to initialize the hash slot
    # for B::BB to use
    is      => 'bare',
    isa     => 'Str',
    default => sub { shift->nick },
);

has rss_feed => (
    is      => 'ro',
    isa     => 'Str',
    lazy    => 1,
    default => 'http://crawl.develz.org/mantis/issues_rss.php',
);

has cache_file => (
    is      => 'ro',
    isa     => 'Str',
    lazy    => 1,
    default => 'cache',
);

has update_time => (
    is      => 'ro',
    isa     => 'Int',
    default => 300,
);

has issues => (
    traits  => ['Hash'],
    isa     => 'HashRef',
    default => sub {
        my $self = shift;
        my $file = $self->cache_file;
        my $issues = {};
        if (-r $file) {
            warn "Updating seen issue list from the cache...";
            open my $fh, '<', $file;
            while (<$fh>) {
                chomp;
                $issues->{$_} = 1;
                warn "  got issue $_";
            }
        }
        else {
            warn "Updating seen issue list from a fresh copy of the feed...";
            $self->each_issue(sub {
                my $issue = shift;
                my $link = $issue->identifier;
                (my $id = $link) =~ s/.*=(\d+)$/$1/;
                $issues->{$id} = 1;
                warn "  got issue $id";
            });
        }
        return $issues;
    },
    handles => {
        has_issue  => 'exists',
        issues     => 'keys',
        _add_issue => 'set',
    },
);

sub add_issue {
    my $self = shift;
    $self->_add_issue($_[0], 1);
}

sub BUILD {
    my $self = shift;
    $self->save_cache;
}

sub each_issue {
    my $self = shift;
    my ($code) = @_;
    my $rss = XML::RAI->parse_uri($self->rss_feed);
    for my $issue (@{ $rss->items }) {
        $code->($issue);
    }
}

sub save_cache {
    my $self = shift;
    warn "Saving cache state to " . $self->cache_file;
    open my $fh, '>', $self->cache_file;
    $fh->print("$_\n") for $self->issues;
}

sub tick {
    my $self = shift;
    warn "Checking for new issues...";
    $self->each_issue(sub {
        my $issue = shift;
        my $link = $issue->identifier;
        (my $id = $link) =~ s/.*=(\d+)$/$1/;
        return if $self->has_issue($id);
        warn "New issue! ($id)";
        my $message = $issue->title;
        $message =~ s/\d+: //;
        $message = sprintf '%s (%s) by %s',
                           $message, $issue->link, $issue->creator;
        $self->say(
            channel => $_,
            body    => $message,
        ) for $self->channels;
        $self->add_issue($id);
    });
    $self->save_cache;

    return $self->update_time;
}

1;