symlink template to namazu, as of at least 2018
[mharc.git] / lib / MHArc / ListDef.pm
1 ##---------------------------------------------------------------------------##
2 ## File:
3 ## $Id: ListDef.pm,v 1.5 2003/07/16 00:49:58 ehood Exp $
4 ## Description:
5 ##---------------------------------------------------------------------------##
6 ## Copyright (C) 2002 Earl Hood <earl@earlhood.com>
7 ##
8 ## This program is free software; you can redistribute it and/or modify
9 ## it under the terms of the GNU General Public License as published by
10 ## the Free Software Foundation; either version 2 of the License, or
11 ## (at your option) any later version.
12 ##
13 ## This program is distributed in the hope that it will be useful,
14 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ## GNU General Public License for more details.
17 ##
18 ## You should have received a copy of the GNU General Public License
19 ## along with this program; if not, write to the Free Software
20 ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 ## 02111-1307, USA
22 ##---------------------------------------------------------------------------##
23
24 package MHArc::ListDef;
25
26 sub _DEF_ORDER() { return '-X-DEF-ORDER-'; }
27
28 sub new {
29 my $self = { };
30 my $mod = shift; # Name of module
31 my $fh = shift; # Filehandle to read definitions from
32 my $class = ref($mod) || $mod;
33
34 bless $self, $class;
35 $self->{_DEF_ORDER} = [ ];
36 $self->load_file($fh);
37 }
38
39 sub load_file {
40 my $self = shift;
41 my $file = shift;
42 my $fh;
43
44 local(*LISTDEF);
45 if (!ref($file)) {
46 if (!open(LISTDEF, $file)) {
47 die qq/ERROR: Unable to open "$file": $!\n/;
48 }
49 $fh = \*LISTDEF;
50
51 } else {
52 $fh = $file;
53 }
54
55 my $name = undef;
56 my($key, $line);
57
58 while (defined($line = <$fh>)) {
59 next unless $line =~ /\S/;
60 next if $line =~ /^\s*#/;
61 chomp $line;
62 ($key, $value) = $line =~ /^([^:]+):\s*(.*)$/;
63 $key = lc $key;
64 if ($key eq 'name') {
65 $value =~ s/\s//g;
66 $name = $value;
67 push(@{$self->{_DEF_ORDER}}, $name);
68 $self->{$name}{'name'} = [ $name ];
69 next;
70 }
71 if (!defined($name)) {
72 warn qq/Warning: No name defined for '$line'\n/;
73 next;
74 }
75
76 if (defined($self->{$name}{$key})) {
77 push(@{$self->{$name}{$key}}, $value);
78 } else {
79 $self->{$name}{$key} = [ $value ];
80 }
81 }
82
83 if (!ref($file)) {
84 close($fh);
85 }
86
87 $self;
88 }
89
90 sub get_names {
91 my $self = shift;
92 @{$self->{_DEF_ORDER}};
93 }
94
95 1;
96 __END__
97
98 =head1 NAME
99
100 MHArc::ListDef - Load mailing lists definition file
101
102 =head1 SYNOPSIS
103
104 $def = MHArc::ListDef->new($fh);
105 $def = MHArc::ListDef->new($filename);
106
107 # Return list of names in order defined
108 @names = $def->get_names;
109
110 # Access an option
111 $description = $def->{$name}{'description'}[0];
112
113 =head1 DESCRIPTION
114
115 This module parses a mailing list definition file for use in the
116 auto-mail archiving system.
117
118 The C<new> method either takes a filehandle reference or a filename
119 string.
120
121 =head1 SEE ALSO
122
123 L<mk-procmailrc|mk-procmailrc>
124
125 =head1 VERSION
126
127 C<$Id: ListDef.pm,v 1.5 2003/07/16 00:49:58 ehood Exp $>
128
129 =head1 AUTHOR
130
131 Earl Hood, earl@earlhood.com
132
133 This module is part of the mharc archiving system and comes with
134 ABSOLUTELY NO WARRANTY and may be copied only under the terms of
135 the GNU General Public License, which may be found in the MHArc
136 distribution.
137
138 =cut
139