summaryrefslogtreecommitdiff
path: root/Echolot/Thesaurus.pm
blob: fed040e185e4e15d895286a61d8b080e548140b3 (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
package Echolot::Thesaurus;

# (c) 2002 Peter Palfrader <peter@palfrader.org>
# $Id: Thesaurus.pm,v 1.10 2002/08/07 23:33:19 weasel Exp $
#

=pod

=head1 Name

Echolot::Thesaurus - build thesaurus pages

=head1 DESCRIPTION

This package provides necessary functions for the thesaurus.

=cut

use strict;
use Carp qw{cluck};
use English;
use HTML::Template;


sub save_thesaurus($$$) {
	my ($otype, $oid, $data) = @_;

	return 1 unless Echolot::Config::get()->{'thesaurus'};

	my ($type) = $otype =~ /^([a-z-]+)$/;
	cluck("type '$otype' is not clean in save_thesaurus"), return 0 unless defined $type;
	my ($id) = $oid =~ /^([0-9]+)$/;
	cluck("id '$oid' is not clean in save_thesaurus"), return 0 unless defined $id;

	my $file = Echolot::Config::get()->{'thesaurusdir'}.'/'.$id.'.'.$type;
	open (F, ">$file") or
		cluck ("Cannot open '$file': $!"),
		return 0;
	print F $data;
	close (F);

	return 1;
};

sub build_thesaurus() {
	return 1 unless Echolot::Config::get()->{'thesaurus'};

	my $dir = Echolot::Config::get()->{'thesaurusdir'};
	opendir(DIR, $dir) or 
		cluck ("Cannot open '$dir': $!"),
		return 0;
	my @files = grep { ! /^\./ } readdir(DIR);
	closedir(DIR);


	my $expire_date = time() - Echolot::Config::get()->{'expire_thesaurus'};

	my $data;
	for my $filename (@files) {
	    my ($id, $what) = $filename =~ /^(\d+)\.(adminkey|conf|help|key|stats)$/;
		next unless (defined $id && defined $what);

		my $remailer = Echolot::Globals::get()->{'storage'}->get_address_by_id($id);
		next unless defined $remailer;
		
		my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
			$atime,$mtime,$ctime,$blksize,$blocks)
			= stat($dir.'/'.$filename);

		if ($mtime < $expire_date) {
			unlink ($dir.'/'.$filename) or
				cluck("Cannot unlink expired $filename");
			print ("Expired thesaurus file $filename\n") if
				Echolot::Config::get()->{'verbose'};
			next;
		};

		my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday)
			= gmtime($mtime);

		my $date = sprintf("%04d-%02d-%02d", $year+1900, $mon+1, $mday);
		my $time = sprintf("%02d:%02d", $hour, $min);


		$data->{$remailer->{'address'}}->{$what.'_href'} = $filename;
		$data->{$remailer->{'address'}}->{$what.'_date'} = $date;
		$data->{$remailer->{'address'}}->{$what.'_time'} = $time;
	};


	for my $addr (keys (%$data)) {
		my $nick = Echolot::Globals::get()->{'storage'}->get_nick($addr);
		if (defined $nick) {
			$data->{$addr}->{'nick'} = $nick;
			$data->{$addr}->{'address'} = $addr;
		} else {
			delete $data->{$addr};
		};
	};

	my @data = map {$data->{$_}} (sort { $data->{$a}->{'nick'} cmp $data->{$b}->{'nick'} } keys (%$data));

	my $template =  HTML::Template->new(
		filename => Echolot::Config::get()->{'templates'}->{'thesaurusindexfile'},
		global_vars => 1 );
	$template->param ( remailers => \@data );
	$template->param ( CURRENT_TIMESTAMP => scalar gmtime() );
	$template->param ( SITE_NAME => Echolot::Config::get()->{'sitename'} );
	

	my $file = Echolot::Config::get()->{'thesaurusindexfile'};
	open (F, ">$file") or
		cluck ("Cannot open '$file': $!"),
		return 0;
	print F $template->output();
	close F;

	return;

	print F '<html><head><title>Thesaurus</title></head><body><h1>Thesaurus</h1><table border=1>'."\n";
	print F "<tr><tr><th>nick</th><th>Address</th><th>conf</th><th>help</th><th>key</th><th>stats</th><th>adminkey</th></tr>\n";

	for my $addr (sort { $data->{$a}->{'nick'} cmp $data->{$b}->{'nick'} } keys (%$data)) {
		printf F '<tr><td>%s</td><td>%s</td><td align="center">%s</td><td align="center">%s</td><td align="center">%s</td><td align="center">%s</td><td align="center">%s</td></tr>'."\n",
			$data->{$addr}->{'nick'},
			$addr,
			defined ($data->{$addr}->{'conf'}) ?
				sprintf('<a href="%s">%s<br>%s</a>', $data->{$addr}->{'conf'}->{'href'}, $data->{$addr}->{'conf'}->{'date'},
				                                                                         $data->{$addr}->{'conf'}->{'time'}) : 'N/A',
			defined ($data->{$addr}->{'help'}) ?
				sprintf('<a href="%s">%s<br>%s</a>', $data->{$addr}->{'help'}->{'href'}, $data->{$addr}->{'help'}->{'date'},
				                                                                         $data->{$addr}->{'help'}->{'time'}) : 'N/A',
			defined ($data->{$addr}->{'key'}) ?
				sprintf('<a href="%s">%s<br>%s</a>', $data->{$addr}->{'key'}->{'href'}, $data->{$addr}->{'key'}->{'date'},
				                                                                        $data->{$addr}->{'key'}->{'time'}) : 'N/A',
			defined ($data->{$addr}->{'stats'}) ?
				sprintf('<a href="%s">%s<br>%s</a>', $data->{$addr}->{'stats'}->{'href'}, $data->{$addr}->{'stats'}->{'date'},
				                                                                          $data->{$addr}->{'stats'}->{'time'}) : 'N/A',
			defined ($data->{$addr}->{'adminkey'}) ?
				sprintf('<a href="%s">%s<br>%s</a>', $data->{$addr}->{'adminkey'}->{'href'}, $data->{$addr}->{'adminkey'}->{'date'},
				                                                                             $data->{$addr}->{'adminkey'}->{'time'}) : 'N/A',
	};
	print F '</table></body>';
	close (F);
};


1;
# vim: set ts=4 shiftwidth=4: