package DJabberd::Plugin::VCard::MySQL; use strict; use base 'DJabberd::Plugin::VCard'; use DBI; use DJabberd::Log; our $logger = DJabberd::Log->get_logger; sub set_config_database { my ($self, $database) = @_; $self->{'mysql_database'} = $database; } sub set_config_host { my ($self, $host) = @_; $self->{'mysql_host'} = $host; } sub set_config_port { my ($self, $port) = @_; $self->{'mysql_port'} = $port; } sub set_config_socket { my ($self, $socket) = @_; $self->{'mysql_socket'} = $socket; } sub set_config_username { my ($self, $username) = @_; $self->{'mysql_username'} = $username; } sub set_config_password { my ($self, $password) = @_; $self->{'mysql_password'} = $password; } sub finalize { my $self = shift; if (!defined $self->{'mysql_database'}) { $logger->logdie('No database set'); } if (!defined $self->{'mysql_username'}) { $logger->logdie('No username set'); } my $dsn = "DBI:mysql:database=$self->{'mysql_database'}"; if (defined $self->{'mysql_host'}) { $dsn .= ";host=$self->{'mysql_host'}"; } if (defined $self->{'mysql_port'}) { $dsn .= ";port=$self->{'mysql_port'}"; } if (defined $self->{'mysql_socket'}) { $dsn .= ";mysql_socket=$self->{'mysql_socket'}"; } my $dbh = DBI->connect_cached($dsn, $self->{'mysql_username'}, $self->{'mysql_password'}, { AutoCommit => 1, PrintError => 0, RaiseError => 1, mysql_auto_reconnect => 1 }); $self->{'mysql_dbh'} = $dbh; $self->check_install_schema; } sub check_install_schema { my $self = shift; my $dbh = $self->{'mysql_dbh'}; eval { $dbh->do('CREATE TABLE vcards (jid VARCHAR(255) NOT NULL, vcard TEXT NOT NULL, PRIMARY KEY (jid)) TYPE MyISAM, CHARACTER SET utf8'); }; if ($@) { if ($@ !~ /Table 'vcards' already exists/) { $logger->logdie("Creating vcards table failed: $@"); } } else { $logger->info('Created vcards table'); } } sub load_vcard { my ($self, $jid) = @_; my $dbh = $self->{'mysql_dbh'}; my $vcard = ($dbh->selectrow_array('SELECT vcard FROM vcards WHERE jid = ' . $dbh->quote($jid)))[0]; if ($vcard) { return $vcard; } else { return 0; } } sub store_vcard { my ($self, $jid, $vcard) = @_; my $dbh = $self->{'mysql_dbh'}; if($self->load_vcard($jid)) { $dbh->do('UPDATE vcards SET vcard = ' . $dbh->quote($vcard->as_xml) . ' WHERE jid = ' . $dbh->quote($jid)); } else { $dbh->do('INSERT INTO vcards SET jid = ' . $dbh->quote($jid) . ', vcard = ' . $dbh->quote($vcard->as_xml)); } } =head1 NAME DJabberd::Plugin::VCard::MySQL - A MySQL vCard storage module for DJabberd =head1 SYNOPSIS An example of djabberd.conf with all options for DJabberd::Plugin::VCard::MySQL (database and username are required): [...] Database djabberd Username mysql_username Password mysql_password Host 127.0.0.1 Port 3306 Socket /path/to/mysql.sock [...] =head1 COPYRIGHT & LICENSE Copyright 2006 Aleksandar Milanov. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut 1;