Index: t/vcard.t
===================================================================
--- t/vcard.t (révision 762)
+++ t/vcard.t (copie de travail)
@@ -2,7 +2,7 @@
use strict;
-use Test::More tests => 26;
+use Test::More tests => 39;
use lib 't/lib';
#BEGIN { $ENV{LOGLEVEL} ||= "FATAL" }
BEGIN { require 'djabberd-test.pl' }
@@ -40,6 +40,23 @@
run_tests();
+use_ok("DJabberd::Plugin::VCard::SpoolDirectory");
+
+$Test::DJabberd::Server::PLUGIN_CB = sub {
+ my $self = shift;
+ use File::Temp "tempdir";
+ my $spool = tempdir( "vcardspoolXXXXX", CLEANUP => 1 );
+
+ my $plugins = $self->standard_plugins();
+
+ push @$plugins, DJabberd::Plugin::VCard::SpoolDirectory->new(directory => $spool);
+ return $plugins;
+};
+
+run_tests();
+
+
+
#really there should be a way to set plugins before this runs so I could just use once_logged_in
sub run_tests {
two_parties( sub {
Index: lib/DJabberd/Plugin/VCard/SpoolDirectory.pm
===================================================================
--- lib/DJabberd/Plugin/VCard/SpoolDirectory.pm (révision 0)
+++ lib/DJabberd/Plugin/VCard/SpoolDirectory.pm (révision 0)
@@ -0,0 +1,120 @@
+package DJabberd::Plugin::VCard::SpoolDirectory;
+use strict;
+use base 'DJabberd::Plugin::VCard';
+use warnings;
+use File::Path;
+use URI::Escape;
+use File::Slurp;
+use File::Basename;
+
+=head2 get_file_name($self, $user)
+
+Get the filename that should hold information for this combination of $user and
+$element. The variable are escaped in order to prevent problem with various char, like
+"/".
+
+=cut
+
+sub get_file_name {
+ my ($self, $user) = @_;
+ my ($login, $domain) = split(/@/, $user);
+ $login = uri_escape($login);
+ $domain = uri_escape($domain);
+ #~ $ perl -MURI::Escape -e 'print uri_escape("http://coin.foo.org/plop:ezez/"),"\n"
+ #http%3A%2F%2Fcoin.foo.org%2Fplop%3Aezez%2F
+ return $self->{directory} . "/$domain/$login.xml"
+}
+
+=head2 set_config_directory($self, $val)
+
+Set the directory that will be used a base of the spool directory.
+It will be created if it doesn't exist.
+
+=cut
+
+sub set_config_directory {
+ my ($self, $val) = @_;
+ $self->{directory} = $val;
+}
+
+=head2 finalize($self)
+
+Check that plugin was correctly initialized.
+
+=cut
+
+
+sub finalize {
+ my ($self) = @_;
+ die "Missing directory" if not defined $self->{directory};
+ # TODO ensure permisison ?
+ mkpath($self->{directory}) if ! -d $self->{directory};
+ die if ! -d $self->{directory};
+}
+
+=head2 load_vcard($self, $user)
+
+Load the element $element for $user from disk.
+
+=cut
+
+sub load_vcard {
+ my ($self, $user) = @_;
+ my $filename = $self->get_file_name($user);
+ print "$filename\n";
+ return undef if ! -f $filename;
+ print "found $filename\n";
+
+ return read_file($filename);
+}
+
+=head2 store_vcard($self, $user, $vcard)
+
+Store $content for $element and $user on disk.
+
+=cut
+
+sub store_vcard {
+ my ($self, $user, $vcard) = @_;
+
+ my $filename = $self->get_file_name($user);
+
+ mkpath(dirname($filename)) if ! -d dirname($filename);
+
+ write_file($filename, $vcard->as_xml);
+}
+
+1;
+__END__
+
+=head1 NAME
+
+DJabberd::Plugin::VCard::SpoolDirectory - implement vcard storage, stored in a spool directory
+
+=head1 SYNOPSIS
+
+
+ Directory /var/spool/djabberd/vcard/
+
+
+=head1 DESCRIPTION
+
+This plugin is derived from DJabberd::Plugin::VCard. It implement a spool directory storage,
+similar to the one used by jabberd, or postfix ( for the mail ). The filename is derived from username
+and the domain. Directory will be autocreated if it doesn't exist.
+
+=head1 COPYRIGHT
+
+This module is Copyright (c) 2007 Michael Scherer
+All rights reserved.
+
+You may distribute under the terms of either the GNU General Public
+License or the Artistic License, as specified in the Perl README file.
+
+=head1 WARRANTY
+
+This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
+
+=head1 AUTHORS
+
+Michael Scherer