1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6###################################
7# a daily cron to test if something breaks down
8# USAGE:
9#   cron_to_test.pl
10#   gmail_account.txt   - send email from, so perl-www-contact can get the report
11#   test_accounts.txt   - the test accounts
12#
13# Sample:
14#
15#   gmail_account.txt (without '# ')
16# fayland@gmail.com     password
17#
18#   test_accounts.txt (without '# '), each line contains email and password
19# test_account@gmail.com        password
20# tester_cpan@rediffmail.com    password
21# tester_cpan@@yahoo.com        password
22
23use FindBin qw/$Bin/;
24use lib "$Bin/../lib";
25
26-e "$Bin/gmail_account.txt" or die "Can't send the report, please create gmail_account.txt\n";
27-e "$Bin/test_accounts.txt" or die "No test accounts since no test_accounts.txt\n";
28
29use WWW::Contact;
30use Email::Send;
31use Email::Simple::Creator;
32
33my $is_broken = 0;
34my $body = "Test WWW::Contact $WWW::Contact::VERSION\n\n";
35
36my $wc = WWW::Contact->new();
37
38open(my $fh, '<', "$Bin/test_accounts.txt") or die $!;
39local $/;
40my $test_accounts = <$fh>;
41close($fh);
42
43my @lines = split(/\r?\n/, $test_accounts);
44foreach my $line ( @lines ) {
45    my ( $email, $pass ) = ( $line =~ /(\S+)\s+(\S+)/ );
46
47    my @contacts = $wc->get_contacts($email, $pass);
48    my $errstr = $wc->errstr;
49    if ( $errstr or scalar @contacts == 0 ) {
50        $body .= "$email is broken I guess, please check it manually.\n\n";
51        $is_broken = 1;
52    } else {
53        $body .= "$email [OK]\n\n";
54    }
55}
56
57if ( $is_broken ) {
58    print STDERR $body;
59
60    # get mail account
61    open(my $fh2, '<', "$Bin/gmail_account.txt");
62    my $mail_info = <$fh2>;
63    close($fh2);
64    my ($email, $pass) = ( $mail_info =~ /(\S+)\s+(\S+)/ );
65
66    my $mailer = Email::Send->new( {
67        mailer => 'SMTP::TLS',
68        mailer_args => [
69            Host => 'smtp.gmail.com',
70            Port => 587,
71            User => $email,
72            Password => $pass,
73            Hello => 'fayland.org',
74        ]
75    } );
76
77    my $es = Email::Simple->create(
78        header => [
79            From    => $email,
80            To      => 'perl-www-contact@googlegroups.com',
81            Subject => "Report on WWW-Contact $WWW::Contact::VERSION " . scalar(localtime()),
82        ],
83        body => $body,
84    );
85
86    eval { $mailer->send($es) };
87    die "Error sending email: $@" if $@;
88}
89
901;