summaryrefslogtreecommitdiff
path: root/fml/lib/FML/Merge/FML4/List.pm
blob: 24206e52d9ecd6ba68d0f542581abb79159f6d14 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#-*- perl -*-
#
#  Copyright (C) 2004,2005 Ken'ichi Fukamachi
#   All rights reserved. This program is free software; you can
#   redistribute it and/or modify it under the same terms as Perl itself.
#
# $FML: List.pm,v 1.7 2004/07/23 15:59:08 fukachan Exp $
#

package FML::Merge::FML4::List;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;

=head1 NAME

FML::Merge::FML4::List - convert member list files.

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS

=head2 new()

constructor.

=cut


# Descriptions: constructor.
#    Arguments: OBJ($self) OBJ($curproc) OBJ($m_config)
# Side Effects: none
# Return Value: OBJ
sub new
{
    my ($self, $curproc, $m_config) = @_;
    my ($type) = ref($self) || $self;
    my $me     = {
	_curproc  => $curproc,
	_m_config => $m_config,
    };

    return bless $me, $type;
}


# Descriptions: convert list files from fml4 to fml8 format.
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: none
sub convert
{
    my ($self)   = @_;
    my $m_config = $self->{ _m_config };

    use FML::Merge::FML4::Config;
    my $config = new FML::Merge::FML4::Config;
    my $files  = $config->get_old_list_files();
    my $fp;

    for my $file (@$files) {
	$fp = "_convert_$file";
	$fp =~ s/-/_/g;
	$fp =~ s@/@_@g;
	if ($self->can($fp)) {
	    $self->$fp($m_config);
	}
	else {
	    croak("cannot convert $file");
	}
    }
}


# Descriptions: convert fml4 actives to fml8 recipients file.
#    Arguments: OBJ($self) OBJ($m_config)
# Side Effects: create fml8 file.
# Return Value: none
sub _convert_actives
{
    my ($self, $m_config) = @_;
    my $src = $m_config->backup_file_path('actives');
    my $dst = $m_config->new_file_path('recipients');

    $self->_write_without_comment($src, $dst);
}


# Descriptions: convert fml4 members to fml8 members file.
#    Arguments: OBJ($self) OBJ($m_config)
# Side Effects: create fml8 file.
# Return Value: none
sub _convert_members
{
    my ($self, $m_config) = @_;
    my $src = $m_config->backup_file_path('members');
    my $dst = $m_config->new_file_path('members');

    $self->_write_without_comment($src, $dst);
}


# Descriptions: convert fml4 members-admin to
#               fml8 {recipients,members}-admin file.
#    Arguments: OBJ($self) OBJ($m_config)
# Side Effects: create fml8 file.
# Return Value: none
sub _convert_members_admin
{
    my ($self, $m_config) = @_;
    my $src = $m_config->backup_file_path('members-admin');
    my $dst = $m_config->new_file_path('members-admin');

    $self->_write_without_comment($src, $dst);

    $dst = $m_config->new_file_path('recipients-admin');
    $self->_write_without_comment($src, $dst);
}


# Descriptions: convert fml4 moderators to
#               fml8 {recipients,members}-moderator file.
#    Arguments: OBJ($self) OBJ($m_config)
# Side Effects: create fml8 file.
# Return Value: none
sub _convert_moderators
{
    my ($self, $m_config) = @_;
    my $src = $m_config->backup_file_path('moderators');
    my $dst = $m_config->new_file_path('members-moderator');

    $self->_write_without_comment($src, $dst);

    $dst = $m_config->new_file_path('recipients-moderator');
    $self->_write_without_comment($src, $dst);
}


# Descriptions: convert fml4 etc/passwd to fml8 etc/passwd-admin.
#    Arguments: OBJ($self) OBJ($m_config)
# Side Effects: create fml8 file.
# Return Value: none
sub _convert_etc_passwd
{
    my ($self, $m_config) = @_;
    my $src = $m_config->backup_file_path('etc/passwd');
    my $dst = $m_config->new_file_path('etc/passwd-admin');

    # XXX-TODO: NOT YET IMPLEMENTED
    print STDERR "warning: etc/passwrd conversion not yet implemented\n";
}


# Descriptions: filter output by removing comment line.
#    Arguments: OBJ($self) STR($src) STR($dst)
# Side Effects: create $dst file.
# Return Value: none
sub _write_without_comment
{
    my ($self, $src, $dst) = @_;
    my $tmp = sprintf("%s.new.%s", $dst, $$);

    unless (-f $src) {
	print STDERR "warning: $src not found, so not converted.\n";
	return;
    }

    print STDERR "creating $dst\n";
    print STDERR "    from $src\n";

    use FileHandle;
    my $rh = new FileHandle $src;
    my $wh = new FileHandle "> $tmp";
    if (defined $rh && defined $wh) {
	my $buf;

      LINE:
	while ($buf = <$rh>) {
	    next LINE if $buf =~ /^\#/o;
	    print $wh $buf;
	}
	$wh->close();
	$rh->close();

	unless (rename($tmp, $dst)) {
	    croak("cannot rename $tmp $dst");
	}
    }
    else {
	croak("cannot open $src") unless defined $rh;
	croak("cannot open $tmp") unless defined $wh;
    }
}


=head1 CODING STYLE

See C<http://www.fml.org/software/FNF/> on fml coding style guide.

=head1 AUTHOR

Ken'ichi Fukamachi

=head1 COPYRIGHT

Copyright (C) 2004,2005 Ken'ichi Fukamachi

All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.

=head1 HISTORY

FML::Merge::FML4::List appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.

=cut


1;