#!/usr/bin/perl -w
use strict;

use WWW::Yahoo::Groups;
use Dumpvalue;
use IO::File;
my $d= new Dumpvalue;

my $y = WWW::Yahoo::Groups->new( autosleep => 15 );
$y->login( USERNAME=> "PASSWORD" );
$y->list( 'wolbonet' );

my $start= GetStartMsg() || $y->first_msg_id();
my $tfail= 0;

printf("starting at %d\n", $start);

my $msgend= $y->last_msg_id();
$msgend= $start+1000 if ($msgend>$start+1000);

for (my $msgid= $start ; $msgid <= $msgend ; $msgid++) {
    my $email= eval { $y->fetch_message( $msgid ) };
    if (!$@) {
        savemessage($msgid, $email);
        sleep(5);
    }
    elsif ($@ =~ /Message \d+ is not there\./) {
        print "$@\n";
        savemessage($msgid, "");
    }
    else {
        print "error: $@\n";
        my $tnow= time();
        die "failing too fast\n" if ($tnow-$tfail < 30 );
        $tfail= $tnow;
        sleep(10);
    }
}
exit(0);

sub savemessage {
    my ($id, $msg)= @_;

    my $fn= sprintf("wolbonet/msg%06d.txt", $id);

    my $fh= IO::File->new($fn, "w") or die "$fn: $@\n";
    $fh->print($msg);
    $fh->close();
}

sub GetStartMsg {
    my @files= sort glob("wolbonet/msg*.txt");

    return 0 if (!@files);

    my $lastfile= $files[-1];
    if ($lastfile =~ /msg0*(\d+)\.txt/i) {
        return int($1)+1;
    }
    return 0;
}

