| 1 | package WWW::Topica; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Cwd; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Date::Parse; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Email::Date; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Email::Simple; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Email::Simple::Creator; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use HTML::Entities; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use HTML::Scrubber; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use URI; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use WWW::Topica::Index; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use WWW::Topica::Mail; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use WWW::Topica::Reply; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 1 | $VERSION = '0.6'; | |||||||||||||||||||||||||||||||
| 22 | 1 | my $USER_AGENT = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'; | |||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | =pod | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | WWW::Topica - read emails from a Topica mailing list | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | my $topica = WWW::Topica->new( list => 'mylist', login => 'mylogin', password => 'mypass' ); | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | while (my $mail = $topica->mail) { | ||||||||||||||||||||||||||||||||
| 37 | Email::LocalDelivery->deliver($mail, 'mylist.mbox'); | ||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | This module screen scrapes the Topica website and fetches back RFC822 text representations | ||||||||||||||||||||||||||||||||
| 43 | of all the mails posted to a given list. Where possible it fills in the from, to and date | ||||||||||||||||||||||||||||||||
| 44 | fields. It should be noted that in some cases it's impossible to get both the sender name | ||||||||||||||||||||||||||||||||
| 45 | and their email address. | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | =head2 new | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | Takes three options, the list name, your login account and your password; | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | You can also pass in C<local> and C<debug>. C<debug> will print out | ||||||||||||||||||||||||||||||||
| 56 | various debugging messages whereas C<local> will use local files for | ||||||||||||||||||||||||||||||||
| 57 | testing. C<local> automatically sets C<debug> to C<1> unless debug is | ||||||||||||||||||||||||||||||||
| 58 | explicitly set to C<0>. | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | Furthermore if you pass in a C<first> option the parsing will start from | ||||||||||||||||||||||||||||||||
| 62 | that offset. A C<last> lets you set an upper bound. | ||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=17.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 67 | 1 | my $class = shift; | |||||||||||||||||||||||||||||||
| 68 | 1 | my %opts = @_; | |||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 1 | ▶ | die "You must pass a list\n" unless defined $opts{list}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 71 | #die "You must pass an email\n" unless defined $opts{email}; | ||||||||||||||||||||||||||||||||
| 72 | #die "You must pass a password\n" unless defined $opts{password}; | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 1 | ▶ | $opts{_next} = $opts{first} || 0; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts{'first'} || 0
MC/DC0%0/1
$opts{'first'} || 0
$opts{'first'}
Truth table50%1/2
A
A= $opts{'first'}
| |||||||||||||||||||||||||||||||||
| 76 | 1 | ▶ | $opts{debug} = $opts{local} if exists $opts{local} and not exists $opts{local}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $opts{"local"} and not exists $opts{"local"}
MC/DC0%0/2
exists $opts{"local"} and not exists $opts{"local"}
exists $opts{"local"}
not exists $opts{"local"}
Truth table33%1/3
A and not(B)
A= exists $opts{"local"}B= not exists $opts{"local"}
| |||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | 1 | $opts{scrubber} = HTML::Scrubber->new( allow => [] ); | |||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 1 | return bless \%opts, $class; | |||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | =head2 mail | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | Returns a mail at a time | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | Logs in automatically. | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 301 | ▶ | sub mail { | ||||||||||||||||||||||||||||||
|
Subroutine
mail: called CC=16 SCAR=29.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 94 | 301 | my $self = shift; | |||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | # first time ever | ||||||||||||||||||||||||||||||||
| 97 | 301 | ▶ | unless ($self->{_index}) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||||||
| 99 | 1 | $self->login; | |||||||||||||||||||||||||||||||
| 100 | 1 | ▶ | print STDERR "Beginning to collect mails\n" if $self->{debug}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | # relog in after an hour | ||||||||||||||||||||||||||||||||
| 104 | # TODO: untested | ||||||||||||||||||||||||||||||||
| 105 | 301 | ▶ | unless ($self->{local}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 106 | 0 | my $time_diff = time() - $self->{_logged_in}; | |||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | $self->login() if ($time_diff>60*60); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | INDEX: | ||||||||||||||||||||||||||||||||
| 112 | # need to get new message ids | ||||||||||||||||||||||||||||||||
| 113 | 301 | ▶ | unless (defined $self->{_message_ids} && @{$self->{_message_ids}}) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->{"_message_ids"} and @{$self->{"_message_ids"};}
MC/DC100%2/2
defined $self->{"_message_ids"} and @{$self->{"_message_ids"};}
defined $self->{"_message_ids"}
@{$self->{"_message_ids"};}
Truth table100%3/3
A and B
A= defined $self->{"_message_ids"}B= @{$self->{"_message_ids"};}
| |||||||||||||||||||||||||||||||||
| 114 | { | ||||||||||||||||||||||||||||||||
| 115 | # all over | ||||||||||||||||||||||||||||||||
| 116 | 4 | ▶ | return undef unless defined $self->{_next}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | # the last one we want | ||||||||||||||||||||||||||||||||
| 119 | 3 | ▶ | return undef if defined $self->{last} and $self->{_next} >= $self->{last}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{"last"} and $self->{"_next"} >= $self->{"last"}
MC/DC0%0/2
defined $self->{"last"} and $self->{"_next"} >= $self->{"last"}
defined $self->{"last"}
$self->{"_next"} >= $self->{"last"}
Truth table33%1/3
A and B
A= defined $self->{"last"}B= $self->{"_next"} >= $self->{"last"}
| |||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | # end of first page | ||||||||||||||||||||||||||||||||
| 122 | # return undef if $self->{debug} && $self->{_index}; | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | 3 | $self->{_index} = WWW::Topica::Index->new($self->fetch_index($self->{_next})); | |||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | 3 | ▶ | return undef unless $self->{_index}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | 3 | $self->{_next} = $self->{_index}->next(); | |||||||||||||||||||||||||||||||
| 129 | 3 | $self->{_prev} = $self->{_index}->prev(); | |||||||||||||||||||||||||||||||
| 130 | 3 | @{$self->{_message_ids}} = $self->{_index}->message_ids; | |||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 300 | GET: my $mess_id = shift @{$self->{_message_ids}}; | |||||||||||||||||||||||||||||||
| 135 | 300 | ▶ | goto INDEX unless defined $mess_id; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | # the mail has some information and also provides a link to the reply if we're logged in... | ||||||||||||||||||||||||||||||||
| 138 | 300 | my $mail_html = $self->fetch_mail($mess_id); | |||||||||||||||||||||||||||||||
| 139 | 300 | ▶ | goto GET unless $mail_html; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 140 | 300 | my $mail = WWW::Topica::Mail->new($mail_html, $mess_id); | |||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | 300 | my $reply; | |||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | # which has other information (like the un-htmled mail and the email address) ... | ||||||||||||||||||||||||||||||||
| 145 | 300 | ▶ | if ($mail->eto) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 146 | 300 | ▶ | my $reply_html = $self->fetch_reply($mess_id,$mail->eto) if defined $mail->eto; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 147 | 300 | ▶ | goto GET unless $reply_html; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 148 | 300 | $reply = WWW::Topica::Reply->new($reply_html, $mess_id, $mail->eto); | |||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | # now build the rfc822 mail string | ||||||||||||||||||||||||||||||||
| 152 | 300 | return $self->build_rfc822($mail, $reply); | |||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | =head2 login | ||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | Logs in to Topica and stashes the cookie. | ||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | Called automatically by the first call to C<mail>. | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | Builds the loader automatically. | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | 1 | ▶ | sub login { | ||||||||||||||||||||||||||||||
|
Subroutine
login: called CC=6 SCAR=25.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 167 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | 1 | $self->build_loader; | |||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | 1 | ▶ | my $anon = !defined $self->{email} || !defined $self->{password}; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $self->{'email'} && defined $self->{'password'}
MC/DC0%0/2
defined $self->{'email'} && defined $self->{'password'}
defined $self->{'email'}
defined $self->{'password'}
Truth table33%1/3
A && B
A= defined $self->{'email'}B= defined $self->{'password'}
| |||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | 1 | ▶ | if ($anon) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 175 | 0 | $self->{email} = $self->{password} = 'anonymous'; | |||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | 1 | ▶ | print STDERR "Logging in using ".$self->{email}."/".$self->{password}."\n" if $self->{debug}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | 1 | ▶ | return if $self->{local}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | if (!$anon) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 185 | 0 | (undef) = $self->fetch_page("http://lists.topica.com/"); | |||||||||||||||||||||||||||||||
| 186 | 0 | (undef) = $self->fetch_page("http://lists.topica.com/list.html"); | |||||||||||||||||||||||||||||||
| 187 | 0 | (undef) = $self->fetch_page("http://lists.topica.com/perl/login.pl?email=".$self->{email}."&password=".$self->{password}); | |||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | # store when we logged in so that we can relog in again after an hour | ||||||||||||||||||||||||||||||||
| 195 | 0 | $self->{_logged_in} = time; | |||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | =head2 fetch_index <offset> | ||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | Retrieve the html of the index page with the given offset. | ||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | =cut | ||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | 3 | ▶ | sub fetch_index { | ||||||||||||||||||||||||||||||
|
Subroutine
fetch_index: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 207 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||
| 208 | 3 | my $offset = shift; | |||||||||||||||||||||||||||||||
| 209 | 3 | my $list = $self->{list}; | |||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||
| 211 | 3 | ▶ | print STDERR "Fetching index $offset of list ${list}\n" if $self->{debug}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||
| 213 | 3 | my $url = "http://lists.topica.com/lists/${list}/read?sort=d&start=$offset"; | |||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||
| 215 | 3 | ▶ | if ($self->{local}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 216 | 3 | $url = "file://".cwd."/t/local_files/"; | |||||||||||||||||||||||||||||||
| 217 | 3 | ▶ | if (0 == $offset) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 218 | 1 | $url .= "list_first.html"; | |||||||||||||||||||||||||||||||
| 219 | } elsif (100 == $offset) { | ||||||||||||||||||||||||||||||||
| 220 | 1 | $url .= "list_middle.html"; | |||||||||||||||||||||||||||||||
| 221 | } elsif (200 == $offset) { | ||||||||||||||||||||||||||||||||
| 222 | 1 | $url .= "list_last.html"; | |||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | 3 | return $self->fetch_page($url); | |||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||
| 232 | =head2 fetch_mail <id> | ||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||
| 234 | Retrieve the html of a the message page with the given id. | ||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | =cut | ||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | 300 | ▶ | sub fetch_mail { | ||||||||||||||||||||||||||||||
|
Subroutine
fetch_mail: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 239 | 300 | my $self = shift; | |||||||||||||||||||||||||||||||
| 240 | 300 | my $id = shift; | |||||||||||||||||||||||||||||||
| 241 | 300 | my $list = $self->{list}; | |||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||
| 244 | 300 | ▶ | print STDERR "\tFetching mail $id\n" if $self->{debug}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | 300 | my $url = "http://lists.topica.com/lists/${list}/read/message.html?mid=$id"; | |||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||
| 248 | 300 | ▶ | if ($self->{local}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 249 | 300 | $url = "file://".cwd."/t/local_files/mail.html"; | |||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||
| 252 | 300 | return $self->fetch_page($url); | |||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||
| 257 | =head2 fetch_reply <id> <eto> | ||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||
| 259 | Retrieve the html of a the reply page with the given id and eto. | ||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||
| 261 | =cut | ||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||
| 263 | 300 | ▶ | sub fetch_reply { | ||||||||||||||||||||||||||||||
|
Subroutine
fetch_reply: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 264 | 300 | my $self = shift; | |||||||||||||||||||||||||||||||
| 265 | 300 | my $id = shift; | |||||||||||||||||||||||||||||||
| 266 | 300 | my $eto = shift; | |||||||||||||||||||||||||||||||
| 267 | 300 | my $list = $self->{list}; | |||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||
| 269 | 300 | ▶ | print STDERR "\t\tFetching reply $id - $eto\n" if $self->{debug}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||
| 272 | 300 | my $url = "http://lists.topica.com/lists/${list}/read/post.html?mode=replytosender&mid=$id&eto=$eto"; | |||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | 300 | ▶ | if ($self->{local}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 275 | 300 | $url = "file://".cwd."/t/local_files/reply.html"; | |||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||
| 278 | 300 | return $self->fetch_page($url); | |||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||
| 284 | =head2 build_rfc822 <WWW::Topic::Mail> <WWW::Topica::Reply> | ||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | Given a C<WWW::Topic::Mail> object and a C<WWW::Topica::Reply> object | ||||||||||||||||||||||||||||||||
| 287 | build up the text of an RFC822 compliant email. | ||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||
| 289 | =cut | ||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||
| 291 | 300 | ▶ | sub build_rfc822 { | ||||||||||||||||||||||||||||||
|
Subroutine
build_rfc822: called CC=14 SCAR=31.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 292 | 300 | my $self = shift; | |||||||||||||||||||||||||||||||
| 293 | 300 | my $mail = shift; | |||||||||||||||||||||||||||||||
| 294 | 300 | my $reply = shift; | |||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||
| 296 | 300 | my $list = $self->{list}; | |||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||
| 298 | 300 | my $mid = $mail->id; | |||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||
| 300 | 300 | my $name = decode_entities($mail->from); | |||||||||||||||||||||||||||||||
| 301 | 300 | my $email = ""; | |||||||||||||||||||||||||||||||
| 302 | 300 | ▶ | if (defined $reply) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 303 | 300 | $email = decode_entities($reply->email); | |||||||||||||||||||||||||||||||
| 304 | } else { | ||||||||||||||||||||||||||||||||
| 305 | 0 | $email = "${list}\@topica.com"; | |||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||
| 308 | # we may have been confused and got name and email mixed up | ||||||||||||||||||||||||||||||||
| 309 | 300 | ▶ | if ($name =~ /@/ && $email !~ /@/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$name =~ /@/ and not $email =~ /@/
MC/DC0%0/2
$name =~ /@/ and not $email =~ /@/
$name =~ /@/
not $email =~ /@/
Truth table33%1/3
A and not(B)
A= $name =~ /@/B= not $email =~ /@/
| |||||||||||||||||||||||||||||||||
| 310 | 0 | my $tmp = $name; | |||||||||||||||||||||||||||||||
| 311 | 0 | $name = $email; | |||||||||||||||||||||||||||||||
| 312 | 0 | $email = $tmp; | |||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||
| 315 | # try and build a sane From: line | ||||||||||||||||||||||||||||||||
| 316 | 300 | my $from; | |||||||||||||||||||||||||||||||
| 317 | 300 | ▶ | if ($name ne $email && $email =~ /@/) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$name ne $email and $email =~ /@/
MC/DC0%0/2
$name ne $email and $email =~ /@/
$name ne $email
$email =~ /@/
Truth table33%1/3
A and B
A= $name ne $emailB= $email =~ /@/
| |||||||||||||||||||||||||||||||||
| 318 | 300 | $from = "$name <$email>"; | |||||||||||||||||||||||||||||||
| 319 | } elsif ($email =~ /@/) { | ||||||||||||||||||||||||||||||||
| 320 | 0 | $from = "<$email>"; | |||||||||||||||||||||||||||||||
| 321 | } else { | ||||||||||||||||||||||||||||||||
| 322 | 0 | $from = "$name <${list}\@topica.com>"; | |||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||
| 325 | # get the subject from somewhere - mail preferably because then it | ||||||||||||||||||||||||||||||||
| 326 | # doesn't have the Re: which we don't know whether to strip out or not | ||||||||||||||||||||||||||||||||
| 327 | 300 | my $subject = $mail->subject; | |||||||||||||||||||||||||||||||
| 328 | 300 | ▶ | $subject = $reply->subject if defined $reply && $subject =~ /^\s*$/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $reply and $subject =~ /^\s*$/
MC/DC0%0/2
defined $reply and $subject =~ /^\s*$/
defined $reply
$subject =~ /^\s*$/
Truth table33%1/3
A and B
A= defined $replyB= $subject =~ /^\s*$/
| |||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||
| 330 | # remove newlines | ||||||||||||||||||||||||||||||||
| 331 | 300 | $subject =~ s/[\n\r]//gs; | |||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||
| 333 | # strip out html | ||||||||||||||||||||||||||||||||
| 334 | 300 | $subject =~ s!<BR>\s+!!sg; # hack | |||||||||||||||||||||||||||||||
| 335 | 300 | $subject = $self->{scrubber}->scrub($subject); | |||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||
| 337 | 300 | $subject = decode_entities($subject); | |||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||
| 339 | # time | ||||||||||||||||||||||||||||||||
| 340 | 300 | ▶ | my $time = str2time(decode_entities($mail->date)) || gmtime; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
str2time(decode_entities($mail->date)) || gmtime
MC/DC0%0/2
str2time(decode_entities($mail->date)) || gmtime
str2time(decode_entities($mail->date))
gmtime
Truth table33%1/3
A || B
A= str2time(decode_entities($mail->date))B= gmtime
| |||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||
| 343 | # message-id | ||||||||||||||||||||||||||||||||
| 344 | 300 | my $message_id = "${mid}\@lists.topica.com"; | |||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||
| 347 | # time to build the mail | ||||||||||||||||||||||||||||||||
| 348 | # we should probably use Email::Simple::Creator for this | ||||||||||||||||||||||||||||||||
| 349 | 300 | my $string = ""; | |||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||
| 351 | 300 | my $body = ""; | |||||||||||||||||||||||||||||||
| 352 | 300 | ▶ | if ($reply && defined $reply->body) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$reply and defined $reply->body
MC/DC0%0/2
$reply and defined $reply->body
$reply
defined $reply->body
Truth table33%1/3
A and B
A= $replyB= defined $reply->body
| |||||||||||||||||||||||||||||||||
| 353 | 300 | $body = $reply->body; | |||||||||||||||||||||||||||||||
| 354 | }else { | ||||||||||||||||||||||||||||||||
| 355 | 0 | ▶ | $body = $self->{scrubber}->scrub($mail->body) || ""; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'scrubber'}->scrub($mail->body) || ''
MC/DC0%0/1
$self->{'scrubber'}->scrub($mail->body) || ''
$self->{'scrubber'}->scrub($mail->body)
Truth table0%0/2
A
A= $self->{'scrubber'}->scrub($mail->body)
| |||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||
| 358 | 300 | $string .= "Date: ".format_date($time)."\n"; | |||||||||||||||||||||||||||||||
| 359 | 300 | $string .= "To: ${list}\@topica.com\n"; | |||||||||||||||||||||||||||||||
| 360 | 300 | $string .= "From: $from\n"; | |||||||||||||||||||||||||||||||
| 361 | 300 | $string .= "Message-ID: $message_id\n"; | |||||||||||||||||||||||||||||||
| 362 | 300 | $string .= "X-TopicaMailUrl: http://lists.topica.com/lists/${list}/read/message.html?mid=${mid}\n"; | |||||||||||||||||||||||||||||||
| 363 | 300 | ▶ | if ($reply) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 364 | 300 | my $rid = $reply->id; | |||||||||||||||||||||||||||||||
| 365 | 300 | my $eto = $reply->eto; | |||||||||||||||||||||||||||||||
| 366 | 300 | $string .= "X-TopicaReplyUrl: http://lists.topica.com/lists/${list}/read/post.html?mode=replytosender&mid=${rid}&eto=${eto}\n"; | |||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||
| 368 | 300 | $string .= "Subject: $subject\n"; | |||||||||||||||||||||||||||||||
| 369 | 300 | $string .= "\n$body\n\n"; | |||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||
| 371 | 300 | return $string; | |||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||
| 374 | =head2 build_loader | ||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||
| 376 | Set up the LWP::UserAgent object used to fetch pages. | ||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||
| 378 | =cut | ||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||
| 380 | 1 | ▶ | sub build_loader { | ||||||||||||||||||||||||||||||
|
Subroutine
build_loader: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 381 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||
| 383 | 1 | my $ua = new LWP::UserAgent( keep_alive => 1, timeout => 30, agent => $USER_AGENT, ); | |||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||
| 386 | # setting it in the 'new' seems not to work sometimes | ||||||||||||||||||||||||||||||||
| 387 | 1 | $ua->agent($USER_AGENT); | |||||||||||||||||||||||||||||||
| 388 | # for some reason this makes stuff work | ||||||||||||||||||||||||||||||||
| 389 | 1 | $ua->max_redirect( 0 ); | |||||||||||||||||||||||||||||||
| 390 | # cookies! | ||||||||||||||||||||||||||||||||
| 391 | 1 | $ua->cookie_jar( {} ); | |||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | 1 | $self->{_ua} = $ua; | |||||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||
| 396 | =head2 fetch_page <url> | ||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||
| 398 | Utility function for getting a page with various niceties. | ||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||
| 400 | =cut | ||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||
| 402 | 603 | ▶ | sub fetch_page { | ||||||||||||||||||||||||||||||
|
Subroutine
fetch_page: called CC=6 SCAR=22.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 403 | 603 | my $self = shift; | |||||||||||||||||||||||||||||||
| 404 | 603 | my $url = shift; | |||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||
| 406 | # print STDERR "\tfetching $url\n" if $self->{debug}; | ||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||
| 408 | # make a full set of headers | ||||||||||||||||||||||||||||||||
| 409 | 603 | my $h = new HTTP::Headers( | |||||||||||||||||||||||||||||||
| 410 | 'Host' => "lists.topica.com", | ||||||||||||||||||||||||||||||||
| 411 | 'User-Agent' => $USER_AGENT, | ||||||||||||||||||||||||||||||||
| 412 | 'Referer' => $url, | ||||||||||||||||||||||||||||||||
| 413 | 'Accept' => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1', | ||||||||||||||||||||||||||||||||
| 414 | 'Accept-Language' => 'en-us,en;q=0.5', | ||||||||||||||||||||||||||||||||
| 415 | 'Accept-Charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', | ||||||||||||||||||||||||||||||||
| 416 | #'Accept-Encoding' => 'gzip,deflate', | ||||||||||||||||||||||||||||||||
| 417 | 'Keep-Alive' => '300', | ||||||||||||||||||||||||||||||||
| 418 | 'Connection' => 'keep-alive', | ||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||
| 420 | ); | ||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||
| 422 | 603 | $h->referer("$url"); | |||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||
| 426 | 603 | my $request = HTTP::Request->new ( 'GET', $url, $h ); | |||||||||||||||||||||||||||||||
| 427 | 603 | my $response; | |||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||
| 429 | 603 | my $times = 0; | |||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||
| 431 | # LWP should be able to do this but seemingly fails sometimes | ||||||||||||||||||||||||||||||||
| 432 | 603 | ▶ | while ($times++<3) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 433 | 603 | $response = $self->{_ua}->request($request); | |||||||||||||||||||||||||||||||
| 434 | 603 | ▶ | last if $response->is_success; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 435 | 0 | ▶ | if ($response->is_redirect) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 436 | 0 | $url = URI->new($response->header("Location")); | |||||||||||||||||||||||||||||||
| 437 | 0 | $url = $url->abs("http://lists.topica.com"); | |||||||||||||||||||||||||||||||
| 438 | 0 | $h->referer("$url"); | |||||||||||||||||||||||||||||||
| 439 | 0 | $request = HTTP::Request->new ( 'GET', $url, $h ); | |||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||
| 443 | 603 | ▶ | if (!$response->is_success && !$response->is_redirect) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$response->is_success or $response->is_redirect
MC/DC0%0/2
$response->is_success or $response->is_redirect
$response->is_success
$response->is_redirect
Truth table33%1/3
A or B
A= $response->is_successB= $response->is_redirect
| |||||||||||||||||||||||||||||||||
| 444 | 0 | carp "Failed to retrieve $url"; | |||||||||||||||||||||||||||||||
| 445 | 0 | return undef; | |||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||
| 448 | 603 | return $response->content; | |||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||
| 452 | 1 | 1; | |||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||
| 454 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||
| 456 | Simon Wistow <simon@thegestalt.org> | ||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||
| 458 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||
| 460 | Copyright (c) 2004, Simon Wistow | ||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||
| 462 | =cut | ||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||