#!/usr/bin/env perl
use strict;
use warnings;
use WWW::Mailman;
use Getopt::Long;
use YAML::Tiny qw( LoadFile DumpFile Dump );

my %param = (
    config => 'mailman.yml',
    cookie => 'mailman.cookie',
);
GetOptions( \%param, 'config=s', 'cookie=s' )
    or die "Usage: $0 [ --config file ] [ --cookie file ]\n";

my $lists = LoadFile( $param{config} );
my %opt = ( cookie_file => $param{cookie} );

# actual code

# obtain all other lists we're subcribed to on the same servers
for my $list ( values %$lists ) {

    my $mm = WWW::Mailman->new( %opt, %$list );

    for my $uri ( $mm->othersubs ) {
        my $other = WWW::Mailman->new( %opt, uri => $uri );
        $lists->{ $other->list } ||= { uri => "$uri", email => $mm->email };
    }
}

# update the original configuration file
DumpFile( $param{config}, $lists );

