#!/usr/bin/perl =head1 NAME courierimap-mknmz - Run mknmz against mail spool of Courier-IMAP server for Gnus =head1 DESCRIPTION In order to build a namazu search index for C, this script executes following three steps: (1) Call C at first, (2) Coordinate URI fields of indexed articles using UID cache database generated by Courier-IMAP server, and (3) Call C. =cut use File::stat; use File::Spec::Functions qw/ abs2rel catfile canonpath splitdir /; use File::Temp; use Getopt::Long; use strict; use warnings; =head1 OPTIONS =over 4 =item --indexdir DIR =item --maildir DIR =item --exclude PATTERN =item --mknmz PATH =item --rfnmz PATH =item --update =item --no-indexing =cut our $MKNMZ = '/usr/bin/mknmz'; our $RFNMZ = '/usr/bin/rfnmz'; our $LANGUAGE = 'ja'; our $INDEXDIR; our $MAILDIR; our $EXCLUDE; our $UPDATE; our $INDEXING = 1; &GetOptions( 'indexdir=s' => \$INDEXDIR, 'maildir=s' => \$MAILDIR, 'exclude=s' => \$EXCLUDE, 'mknmz=s' => \$MKNMZ, 'rfnmz=s' => \$RFNMZ, 'language=s' => \$LANGUAGE, 'update!' => \$UPDATE, 'indexing!' => \$INDEXING ); if( $INDEXDIR ){ $INDEXDIR = &canonpath( $INDEXDIR ); } else { $INDEXDIR = &catfile( $ENV{HOME}, 'News/namazu' ); } if( $MAILDIR ){ $MAILDIR = &canonpath( $MAILDIR ); } else { $MAILDIR = &catfile( $ENV{HOME}, 'Maildir' ); } if( $INDEXING ){ # Run mknmz. if( $UPDATE ){ system $MKNMZ, sprintf( '--update=%s', $INDEXDIR ); } else { my @option = qw( --all --media-type=message/rfc822 --checkpoint --deny=courierimap.* ); push( @option, sprintf( '--exclude=^%s/%s', quotemeta $MAILDIR, $EXCLUDE ) ) if $EXCLUDE; push( @option, sprintf( '--indexing-lang=%s', $LANGUAGE ) ) if $LANGUAGE; system $MKNMZ, @option, sprintf( '--output-dir=%s', $INDEXDIR ), $MAILDIR; } } # Rewrite URI fields and run rfnmz. our %UID; &rewrite_urifile(); system $RFNMZ, $INDEXDIR; sub rewrite_urifile { my $file = &catfile( $INDEXDIR, 'NMZ.field.uri' ); my $dst = File::Temp->new( DIR => $INDEXDIR ); open( my $src, '<', $file ) or die "Cannot open $file\n"; while( defined( $_ = <$src> ) ){ my $original = $_; s/\s+\Z//; s/%([\da-fA-F]{2})/chr(hex($1))/ge; $_ = &abs2rel( $_, $MAILDIR ); if( my( $group, $id ) = m!\A(.+?)/cur/([^/]+)\Z! ){ if( $id = &getid($group, $id) ){ $group =~ s!\.!/!g; print $dst &catfile( $MAILDIR, $group, $id ), "\n"; next; } } print $dst $original; } my $x = $dst->filename; close $src; close $dst; chmod( stat($file)->mode, $x ) or die $!; rename( $x, $file ) or die $!; } sub getid { my( $group, $id ) = @_; unless( $UID{$group} ){ $UID{$group} = {}; open( my $cache, '<', &catfile($MAILDIR, $group, 'courierimapuiddb') ) or return undef; $_ = <$cache>; # Skip. while( defined( $_ = <$cache> ) ){ s/\s+\Z//; s/\A(\d+)\s+//; $UID{$group}{$_} = $1; } } $id =~ s/:[0-9]+,[A-Z]+\Z//; $UID{$group}{$id}; } =head1 GNUS SETTING Add the following expression into your ~/.gnus.el, to use Namazu index generated by this script. (setq gnus-namazu-remote-groups '(("\\`INBOX" . "/home/example/Maildir/"))) If you use a remote IMAP server, the following expression will be also required. (setq gnus-namazu-command-prefix '("ssh" "imap.example.net")) =head1 SEE ALSO mknmz(1), rfnmz(1). =head1 AUTHOR =over 4 =item TSUCHIYA Masatoshi =back =head1 COPYRIGHT This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, you can either send email to this program's maintainer or write to: The Free Software Foundation, Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA. Last Update: $Date: 2008-07-04 11:25:41 $ =cut