summaryrefslogtreecommitdiff
path: root/fml/lib/Mail/Message/Spool.pm
blob: 95442ccb1223f3d9b776f14143487bac062dde1d (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
#-*- perl -*-
#
# Copyright (C) 2002,2003,2004 Ken'ichi Fukamachi
#
# $FML: Spool.pm,v 1.11 2003/03/03 15:53:50 fukachan Exp $
#

package Mail::Message::Spool;

use strict;
use Carp;

=head1 NAME

Mail::Message::Spool - utilities to handle directory such as article spool

=head1 SYNOPSIS

   use Mail::Message::Spool;
   my $spool = new Mail::Message::Spool;
   my $file  = $spool->filepath($args);

=head1 DESCRIPTION

C<Mail::Message::Spool> class provides utility functions to handle a
directory such as article spool.

=head1 METHODS

=head2 new()

standard constructor.

=cut


# Descriptions: constructor.
#    Arguments: OBJ($self) HASH_REF($args)
# Side Effects: none
# Return Value: OBJ
sub new
{
    my ($self, $args) = @_;
    my ($type) = ref($self) || $self;
    my $me     = {};
    return bless $me, $type;
}


=head2 filepath($args)

return article file path.  If you use hierarchical subdirectories,
this filepath() conversion is useful.

   $args = {
	base_dir    => $base_dir,
	id          => $id,
	use_subdir  => 0,    # 1 or 0
	subdir_unit => 1000,
   };

where C<base_dir> and C<id> are mandatory.

=cut


# Descriptions: return article file path.
#    Arguments: OBJ($self) HASH_REF($args)
# Side Effects: croak() if failed.
# Return Value: STR(file path)
sub filepath
{
    my ($self, $args) = @_;

    # XXX-TODO: $is_hash is used for what ?
    if (defined $args->{ base_dir } && defined $args->{ id }) {
	my $base_dir = $args->{ base_dir };
	my $id       = $args->{ id };
	my $is_hash  = 0;
	my $file     = '';
	my $unit     = $args->{ subdir_unit } || 1000;
	my $subdir   = int($id/$unit);

	if (defined $args->{ use_subdir } && $args->{ use_subdir }) {
	    $is_hash = 1;
	    use File::Spec;
	    $file = File::Spec->catfile($base_dir, $subdir, $id);
	}
	else {
	    use File::Spec;
	    $file = File::Spec->catfile($base_dir, $id);
	}

	return $file;
    }
    else {
	croak("filepath: invalid input");
    }
}


# Descriptions: return article dirpath with subdir if needed.
#    Arguments: OBJ($self) HASH_REF($args)
# Side Effects: croak() if failed.
# Return Value: STR(dir path)
sub dirpath
{
    my ($self, $args) = @_;

    # XXX-TODO: $is_hash is used for what ?
    if (defined $args->{ base_dir } && defined $args->{ id }) {
	my $base_dir = $args->{ base_dir };
	my $id       = $args->{ id };
	my $is_hash  = 0;
	my $dir      = '';
	my $unit     = 1000;
	my $subdir   = int($id/$unit);

	if (defined $args->{ use_subdir } && $args->{ use_subdir }) {
	    $is_hash = 1;
	    use File::Spec;
	    $dir = File::Spec->catfile($base_dir, $subdir);
	}
	else {
	    $dir = $base_dir;
	}

	return $dir;
    }
    else {
	croak("filepath: invalid input");
    }
}


#
# test
#
if ($0 eq __FILE__) {
    my $obj = new Mail::Message::Spool;

    for my $is_hash (0, 1) {
	print "\nhashed ? ", ($is_hash ? "yes" : "no"), "\n\n";

	for my $id (qw(0 1 2 99 100 101
		       999 1000 1001 1999 2000 2001
		       9999 10000 10001
		       )) {
	    print "$id\t=>\t";
	    print $obj->filepath({
		base_dir   => '/var/spool/ml/elena/spool',
		id         => $id,
		use_subdir => $is_hash,
	    }), "\n";
	}
    }
}


=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) 2002,2003,2004 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

Mail::Message::Spool first appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.

=cut


1;