summaryrefslogtreecommitdiff
path: root/fml/lib/Mail/ThreadTrack/DB.pm
blob: 4ecdaaeca02926f2f1e8ba2d1677fa1b36a86a27 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#-*- perl -*-
#
#  Copyright (C) 2001,2002 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: DB.pm,v 1.31 2002/12/22 03:21:32 fukachan Exp $
#

package Mail::ThreadTrack::DB;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;

my $debug = 0;

=head1 NAME

Mail::ThreadTrack::DB - database access.

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS

=cut

=head2 C<db_open()>

open database.
It uses tie() to bind a hash to a DB file.
Our thread model uses several DB files such as
C<%thread_id>,
C<%date>,
C<%status>,
C<%sender>,
C<%articles>,
C<%message_id>
and
C<%index>.

=head2 C<db_close()>

untie() the corresponding hashes opened by C<db_open()>.

=cut

my @kind_of_databases = qw(thread_id date status sender articles
                           message_id);


# Descriptions: open database by tie()
#    Arguments: OBJ($self)
# Side Effects: $self->{ _hash_table } initialized.
# Return Value: none
sub db_open
{
    my ($self) = @_;
    my $db_type   = $self->{ config }->{ db_type } || 'AnyDBM_File';
    my $db_dir    = $self->{ _db_dir };
    my $file_mode = $self->{ _file_mode } || 0644;

    use File::Spec;
    eval qq{ use $db_type; use Fcntl;};
    unless ($@) {
        for my $db (@kind_of_databases) {
	    my $file = File::Spec->catfile($db_dir, $db);
            my $str  = qq{
                my \%$db = ();
                tie \%$db, \$db_type, \$file, O_RDWR|O_CREAT, $file_mode;
                \$self->{ _hash_table }->{ _$db } = \\\%$db;
            };
            eval $str;
            croak($@) if $@;
        }

	my %index      = ();
	my $index_file = $self->{ _index_db };
	eval q{
	    tie %index, $db_type, $index_file, O_RDWR|O_CREAT, $file_mode;
	    $self->{ _hash_table }->{ _index } = \%index;
	};
	croak($@) if $@;
    }
    else {
        croak("failed to \"use $db_type\"");
    }

    1;
}


# Descriptions: clear database
#    Arguments: OBJ($self)
# Side Effects: update database
# Return Value: none
sub db_clear
{
    my ($self) = @_;
    my $db_dir = '';

    $db_dir = $self->{ _db_dir };
    _db_clear($db_dir) if -d $db_dir;

    $db_dir = $self->{ _db_base_dir };
    _db_clear($db_dir) if -d $db_dir;
}


# Descriptions: clear database
#    Arguments: STR($db_dir)
# Side Effects: clear database, remove file if needed
# Return Value: none
sub _db_clear
{
    my ($db_dir) = @_;

    eval q{
	use DirHandle;
	use File::Spec;
	my $dh = new DirHandle $db_dir;

	if (defined $dh) {
	    my $f = '';
	    while (defined($f = $dh->read)) {
		next if $f =~ /^\./;
		my $file = File::Spec->catfile($db_dir, $f);
		if (-f $file) {
		    unlink $file;
		    print STDERR "removed $file\n" unless -f $file;
		}
	    }
	    $dh->close;
	}
    };
    croak($@) if $@;
}


# Descriptions: close database by untie()
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: none
sub db_close
{
    my ($self) = @_;

    for my $db (@kind_of_databases) {
        my $str = qq{
            my \$${db} = \$self->{ _hash_table }->{ _$db };
	    untie \%\$${db};
        };
        eval $str;
        croak($@) if $@;
    }

    my $index = $self->{ _hash_table }->{ _index };
    untie %$index;
}


=head2 db_mkdb($min, $max)

remake database.

=cut


# Descriptions: remake database for messages from $min_id to $max_id
#    Arguments: OBJ($self) NUM($min_id) NUM($max_id)
# Side Effects: remake database
# Return Value: none
sub db_mkdb
{
    my ($self, $min_id, $max_id) = @_;
    my $config     = $self->{ _config };
    my $spool_dir  = $config->{ spool_dir };
    my $saved_args = $self->{ _saved_args }; # original $args

    return undef unless (defined $min_id && defined $max_id);

    use Mail::Message;
    use File::Spec;

    my $count = 0;
    my ($fh, $file, $msg);
    print STDERR "db_mkdb: $min_id -> $max_id\n" if $debug;

  ID:
    for my $id ( $min_id .. $max_id ) {
	print STDERR "." if $count++ % 10 == 0;
	print STDERR "process $id\n" if $debug;

	# XXX-TODO: this code is workaround, we should create more clever way.
	# XXX-TODO: overwrite (tricky)
	$self->{ _config }->{ article_id } = $id;

	# parse article and analyze it.
	$file = $self->filepath({
	    base_dir => $spool_dir,
	    id       => $id,
	});

	$fh = new FileHandle $file;
	if (defined $fh) {
	    my $msg  = Mail::Message->parse({ fd => $fh });
	    $self->analyze($msg);

	    # XXX-TODO: workaround, we should create more clever way.
	    # XXX-TODO: remove current status (tricky ;)
	    delete $self->{ _status };
	}
    }
    print STDERR "\n" if $count > 0;
}


=head2 db_dump([$type])

dump hash as text.
dump status database if $type is not specified.

=cut


# Descriptions: dump data for database $type
#    Arguments: OBJ($self) STR($type)
# Side Effects: none
# Return Value: none
sub db_dump
{
    my ($self, $type) = @_;
    my $db_type = "_" . ( defined $type ? $type : 'status' );
    my $rh      = $self->{ _hash_table }->{ $db_type };

    my ($k, $v);
    while (($k, $v) = each %$rh) {
	printf "%-20s %s\n", $k, $v;
    }
}


=head2 db_hash( $type )

return HASH REFERENCE for specified database $type.

=cut


# Descriptions: get HASH REFERENCE for specified $type.
#    Arguments: OBJ($self) STR($db_type)
# Side Effects: none
# Return Value: HASH_REF or UNDEF
sub db_hash
{
    my ($self, $db_type) = @_;
    my $type = "_" . $db_type;

    if (defined $self->{ _hash_table }->{ $type }) {
	return $self->{ _hash_table }->{ $type };
    }
    else {
	return undef;
    }
}


=head2 db_last_modified()

return the last modified time of our dateabase as unix time.
This time is the latest modified time among all database files.

=cut


# Descriptions: return the last modified time (unix time) of database
#    Arguments: OBJ($self) STR($db_type)
# Side Effects: none
# Return Value: STR or UNDEF
sub db_last_modified
{
    my ($self, $db_type) = @_;
    my $db_dir        = $self->{ _db_dir };
    my $last_modified = 0;

    # XXX-TODO: we supporse Berkeley DB. fix it.
    use File::Spec;
    my $file = File::Spec->catfile($db_dir, "date.db");
    if (-f $file) {
	$last_modified = (stat($file))[8];
    }

    return $last_modified;
}


=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) 2001,2002 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::ThreadTrack::DB first appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.

=cut


1;