This commit is contained in:
TLINDEN
2012-02-10 20:30:38 +01:00
parent 23c301be2a
commit 773584e400
14 changed files with 1550 additions and 545 deletions

View File

@@ -3,38 +3,44 @@
# Perl module for note
# binary database backend. see docu: perldoc NOTEDB::binary
#
package NOTEDB;
package NOTEDB::binary;
$NOTEDB::binary::VERSION = "1.10";
use strict;
use Data::Dumper;
#use Data::Dumper;
use IO::Seekable;
use File::Spec;
use NOTEDB;
use Fcntl qw(LOCK_EX LOCK_UN);
use NOTEDB;
use Exporter ();
use vars qw(@ISA @EXPORT);
@ISA = qw(NOTEDB Exporter);
sub new
{
my($this, $dbdriver, $dbname, $MAX_NOTE, $MAX_TIME) = @_;
sub new {
my($this, %param) = @_;
my $class = ref($this) || $this;
my $self = {};
bless($self,$class);
if(! -e $dbname) {
open(TT,">$dbname") or die "Could not create $dbname: $!\n";
$self->{NOTEDB} = $param{dbname} || File::Spec->catfile($ENV{HOME}, ".notedb");
my $MAX_NOTE = $param{max_note} || 4096;
my $MAX_TIME = $param{max_time} || 64;
if(! -e $self->{NOTEDB}) {
open(TT,">$self->{NOTEDB}") or die "Could not create $self->{NOTEDB}: $!\n";
close (TT);
}
elsif(! -w $dbname) {
print "$dbname is not writable!\n";
elsif(! -w $self->{NOTEDB}) {
print "$self->{NOTEDB} is not writable!\n";
exit(1);
}
$self->{version} = "(NOTEDB::binary, 1.8)";
$self->{NOTEDB} = $dbname;
my $TYPEDEF = "i a$MAX_NOTE a$MAX_TIME";
my $SIZEOF = length pack($TYPEDEF, () );
@@ -42,6 +48,7 @@ sub new
$self->{sizeof} = $SIZEOF;
$self->{typedef} = $TYPEDEF;
$self->{maxnote} = $MAX_NOTE;
$self->{LOCKFILE} = $self->{NOTEDB} . "~LOCK";
return $self;
}
@@ -54,7 +61,7 @@ sub DESTROY
sub version {
my $this = shift;
return $this->{version};
return $NOTEDB::binary::VERSION;
}
@@ -116,6 +123,13 @@ sub get_all
return %res;
}
sub import_data {
my ($this, $data) = @_;
foreach my $num (keys %{$data}) {
my $pos = $this->get_nextnum();
$this->set_edit($pos, $data->{$num}->{note}, $data->{$num}->{date});
}
}
sub get_nextnum
{
@@ -365,6 +379,31 @@ sub warn_if_too_big {
}
}
sub _retrieve {
my ($this) = @_;
my $file = $this->{dbname};
if (-s $file) {
if ($this->changed() || $this->{unread}) {
my $fh = new FileHandle "<$this->{dbname}" or die "could not open $this->{dbname}\n";
flock $fh, LOCK_EX;
my %data = ParseConfig(-ConfigFile => $fh) or die "could not read to database: $!\n";
flock $fh, LOCK_UN;
$fh->close();
$this->{unread} = 0;
$this->{data} = \%data;
return %data;
}
else {
return %{$this->{data}};
}
}
else {
return ();
}
}
1; # keep this!

View File

@@ -4,32 +4,42 @@
# DBM database backend. see docu: perldoc NOTEDB::dbm
#
package NOTEDB;
package NOTEDB::dbm;
$NOTEDB::dbm::VERSION = "1.40";
use DB_File;
use Data::Dumper;
use NOTEDB;
use strict;
use Exporter ();
use vars qw(@ISA @EXPORT %note %date);
@ISA = qw(NOTEDB Exporter);
# Globals:
my (%note, %date);
sub new
{
my($this, $dbdriver, $dbm_dir) = @_;
my($this, %param) = @_;
my $class = ref($this) || $this;
my $self = {};
bless($self,$class);
my $notefile = "note.dbm";
my $timefile = "date.dbm";
$self->{version} = "(NOTEDB::dbm, 1.2)";
my $notefile = "note.dbm";
my $timefile = "date.dbm";
my $dbm_dir = $param{directory} || File::Spec->catfile($ENV{HOME}, ".note_dbm");
tie %note, "DB_File", "$dbm_dir/$notefile" || die $!;
tie %date, "DB_File", "$dbm_dir/$timefile" || die $!;
if (! -d $dbm_dir) {
# try to make it
mkdir $dbm_dir || die "Could not create $dbm_dir: $!\n";
}
tie %note, "DB_File", "$dbm_dir/$notefile" || die "Could not tie $dbm_dir/$notefile: $!\n";
tie %date, "DB_File", "$dbm_dir/$timefile" || die "Could not tie $dbm_dir/$timefile: $!\n";
$self->{LOCKFILE} = $param{dbname} . "~LOCK";
return $self;
}
@@ -66,6 +76,14 @@ sub get_all
return %res;
}
sub import_data {
my ($this, $data) = @_;
foreach my $num (keys %{$data}) {
my $pos = $this->get_nextnum();
$note{$pos} = $this->ude($note{$num}->{note});
$date{$pos} = $this->ude($date{$num}->{date});
}
}
sub get_nextnum
{

402
NOTEDB/general.pm Normal file
View File

@@ -0,0 +1,402 @@
# Perl module for note
# general database backend. see docu: perldoc NOTEDB::general
# using Config::General as backend.
package NOTEDB::general;
$NOTEDB::general::VERSION = "1.00";
use strict;
#use Data::Dumper;
use File::Spec;
use Config::General;
use MIME::Base64;
use FileHandle;
use NOTEDB;
use Fcntl qw(LOCK_EX LOCK_UN);
use Exporter ();
use vars qw(@ISA @EXPORT);
@ISA = qw(NOTEDB Exporter);
sub new {
my($this, %param) = @_;
my $class = ref($this) || $this;
my $self = {};
bless($self,$class);
$self->{dbname} = $param{dbname} || File::Spec->catfile($ENV{HOME}, ".notedb");
if(! -e $param{dbname}) {
open(TT,">$param{dbname}") or die "Could not create $param{dbname}: $!\n";
close (TT);
}
elsif(! -w $param{dbname}) {
print "$param{dbname} is not writable!\n";
exit(1);
}
$self->{mtime} = $self->get_stat();
$self->{unread} = 1;
$self->{data} = {};
$self->{LOCKFILE} = $param{dbname} . "~LOCK";
return $self;
}
sub DESTROY {
# clean the desk!
}
sub version {
my $this = shift;
return $NOTEDB::general::VERSION;
}
sub get_stat {
my ($this) = @_;
my $mtime = (stat($this->{dbname}))[9];
return $mtime;
}
sub changed {
my ($this) = @_;
my $current = $this->get_stat();
if ($current > $this->{mtime}) {
$this->{mtime} = $current;
return $current;
}
else {
return 0;
}
}
sub set_del_all {
my $this = shift;
unlink $this->{dbname};
open(TT,">$this->{dbname}") or die "Could not create $this->{dbname}: $!\n";
close (TT);
}
sub get_single {
my($this, $num) = @_;
my($address, $note, $date, $buffer, $n, $t, $buffer, );
my %data = $this->get_all();
return ($data{$num}->{note}, $data{$num}->{date});
}
sub get_all {
my $this = shift;
my($num, $note, $date, %res);
if ($this->unchanged) {
return %{$this->{cache}};
}
my %data = $this->_retrieve();
foreach my $num (keys %data) {
$res{$num}->{note} = $this->ude($data{$num}->{note});
$res{$num}->{date} = $this->ude($data{$num}->{date});
}
$this->cache(%res);
return %res;
}
sub import_data {
my ($this, $data) = @_;
my %res = $this->_retrieve();
my $pos = (scalar keys %res) + 1;
foreach my $num (keys %{$data}) {
$res{$pos}->{note} = $this->uen($data->{$num}->{note});
$res{$pos}->{date} = $this->uen($data->{$num}->{date});
$pos++;
}
$this->_store(\%res);
}
sub get_nextnum {
my $this = shift;
my($num, $te, $me, $buffer);
if ($this->unchanged) {
$num = 1;
foreach (keys %{$this->{cache}}) {
$num++;
}
return $num;
}
my %data = $this->get_all();
my $size = scalar keys %data;
$num = $size + 1;
return $num;
}
sub get_search {
my($this, $searchstring) = @_;
my($buffer, $num, $note, $date, %res, $t, $n, $match);
my $regex = $this->generate_search($searchstring);
eval $regex;
if ($@) {
print "invalid expression: \"$searchstring\"!\n";
return;
}
$match = 0;
if ($this->unchanged) {
foreach my $num (keys %{$this->{cache}}) {
$_ = $this->{cache}{$num}->{note};
eval $regex;
if ($match) {
$res{$num}->{note} = $this->{cache}{$num}->{note};
$res{$num}->{date} = $this->{cache}{$num}->{date}
}
$match = 0;
}
return %res;
}
my %data = $this->get_all();
foreach my $num(sort keys %data) {
$_ = $data{$num}->{note};
eval $regex;
if($match)
{
$res{$num}->{note} = $data{$num}->{note};
$res{$num}->{date} = $data{$num}->{data};
}
$match = 0;
}
return %res;
}
sub set_edit {
my($this, $num, $note, $date) = @_;
my %data = $this->_retrieve();
$data{$num} = {
note => $this->uen($note),
date => $this->uen($date)
};
$this->_store(\%data);
$this->changed;
}
sub set_new {
my($this, $num, $note, $date) = @_;
$this->set_edit($num, $note, $date);
}
sub set_del {
my($this, $num) = @_;
my(%data, $note, $date, $T, $setnum, $buffer, $n, $N, $t);
$setnum = 1;
%data = $this->_retrieve();
return "ERROR" if (! exists $data{$num});
delete $data{$num};
$this->_store(\%data);
$this->changed;
return;
}
sub set_recountnums {
my($this) = @_;
my(%orig, %data, $note, $date, $T, $setnum, $buffer, $n, $N, $t);
$setnum = 1;
%orig = $this->_retrieve();
foreach $N (sort {$a <=> $b} keys %orig) {
$data{$setnum} = {
note => $orig{$N}->{note},
date => $orig{$N}->{date}
};
$setnum++;
}
$this->_store(\%data);
$this->changed;
return;
}
sub uen {
my ($this, $raw) = @_;
my($crypted);
if($NOTEDB::crypt_supported == 1) {
eval {
$crypted = $this->{cipher}->encrypt($raw);
};
}
else {
$crypted = $raw;
}
my $coded = encode_base64($crypted);
return $coded;
}
sub ude {
my ($this, $crypted) = @_;
my($raw);
if($NOTEDB::crypt_supported == 1) {
eval {
$raw = $this->{cipher}->decrypt(decode_base64($crypted));
};
}
else {
$raw = decode_base64($crypted)
}
return $raw;
}
sub _store {
my ($this, $data) = @_;
open NOTE, ">$this->{dbname}" or die "could not open $this->{dbname}: $!\n";
flock NOTE, LOCK_EX;
my $content = SaveConfigString($data) or die "could not serialize data: $!\n";
print NOTE $content;
flock NOTE, LOCK_UN;
close NOTE;
# finally re-read the db, so that we always have the latest data
$this->_retrieve();
}
sub _retrieve {
my ($this) = @_;
my $file = $this->{dbname};
if (-s $file) {
if ($this->changed() || $this->{unread}) {
my $fh = new FileHandle "<$this->{dbname}" or die "could not open $this->{dbname}\n";
flock $fh, LOCK_EX;
my %data = ParseConfig(-ConfigFile => $fh) or die "could not read to database: $!\n";
flock $fh, LOCK_UN;
$fh->close();
$this->{unread} = 0;
$this->{data} = \%data;
return %data;
}
else {
return %{$this->{data}};
}
}
else {
return ();
}
}
1; # keep this!
__END__
=head1 NAME
NOTEDB::general - module lib for accessing a notedb from perl
=head1 SYNOPSIS
# include the module
use NOTEDB;
# create a new NOTEDB object
$db = new NOTEDB("text", "/home/tom/.notedb", 4096, 24);
# decide to use encryption
# $key is the cipher to use for encryption
# $method must be either Crypt::IDEA or Crypt::DES
# you need Crypt::CBC, Crypt::IDEA and Crypt::DES to have installed.
$db->use_crypt($key,$method);
# do not use encryption
# this is the default
$db->no_crypt;
# get a single note
($note, $date) = $db->get_single(1);
# search for a certain note
%matching_notes = $db->get_search("somewhat");
# format of returned hash:
#$matching_notes{$numberofnote}->{'note' => 'something', 'date' => '23.12.2000 10:33:02'}
# get all existing notes
%all_notes = $db->get_all();
# format of returnes hash like the one from get_search above
# get the next noteid available
$next_num = $db->get_nextnum();
# modify a certain note
$db->set_edit(1, "any text", "23.12.2000 10:33:02");
# create a new note
$db->set_new(5, "any new text", "23.12.2000 10:33:02");
# delete a certain note
$db->set_del(5);
# turn on encryption. CryptMethod must be IDEA, DES or BLOWFISH
$db->use_crypt("passphrase", "CryptMethod");
# turn off encryption. This is the default.
$db->no_crypt();
=head1 DESCRIPTION
You can use this module for accessing a note database. This backend uses
a text file for storage and Config::General for accessing the file.
Currently, NOTEDB module is only used by note itself. But feel free to use it
within your own project! Perhaps someone want to implement a webinterface to
note...
=head1 USAGE
please see the section SYNOPSIS, it says it all.
=head1 AUTHOR
Thomas Linden <tom@daemon.de>.
=cut

View File

@@ -1,31 +1,41 @@
#!/usr/bin/perl
# $Id: mysql.pm,v 1.3 2000/08/11 00:05:58 zarahg Exp $
#
# Perl module for note
# mysql database backend. see docu: perldoc NOTEDB::mysql
#
package NOTEDB;
package NOTEDB::mysql;
$NOTEDB::mysql::VERSION = "1.50";
use DBI;
use strict;
use Data::Dumper;
#use Data::Dumper;
use NOTEDB;
use Exporter ();
use vars qw(@ISA @EXPORT);
@ISA = qw(NOTEDB Exporter);
sub new
{
# no prototype, because of the bin-version, which takes only a filename!
my($this, $dbdriver, $dbname, $dbhost, $dbuser, $dbpasswd,
$table, $fnum, $fnote, $fdate, $dbport) = @_;
sub new {
my($this, %param) = @_;
my $class = ref($this) || $this;
my $self = {};
bless($self,$class);
my $dbname = $param{dbname} || "note";
my $dbhost = $param{dbhost} || "localhost";
my $dbuser = $param{dbuser} || "";
my $dbpasswd = $param{dbpasswd} || "";
my $dbport = $param{dbport} || "";
my $fnum = "number";
my $fnote = "note";
my $fdate = "date";
my $database;
if ($dbport) {
$database = "DBI:$dbdriver:$dbname;host=$dbhost:$dbport";
@@ -34,8 +44,7 @@ sub new
$database = "DBI:$dbdriver:$dbname;host=$dbhost";
}
$self->{version} = "(NOTEDB::mysql, 1.5)";
$self->{table} = $table;
$self->{table} = "table";
$self->{sql_getsingle} = "SELECT $fnote,$fdate FROM $self->{table} WHERE $fnum = ?";
$self->{sql_all} = "SELECT $fnum,$fnote,$fdate FROM $self->{table}";
@@ -278,6 +287,14 @@ sub set_recountnums {
$this->changed;
}
sub import_data {
my ($this, $data) = @_;
foreach my $num (keys %{$data}) {
my $pos = $this->get_nextnum();
$this->set_edit($pos, $data->{$num}->{note}, $data->{$num}->{date});
}
}
sub uen
{
my $this = shift;

355
NOTEDB/text.pm Normal file
View File

@@ -0,0 +1,355 @@
# Perl module for note
# text database backend. see docu: perldoc NOTEDB::text
# using Storable as backend.
package NOTEDB::text;
use strict;
#use Data::Dumper;
use File::Spec;
use Storable qw(lock_nstore lock_retrieve);
use MIME::Base64;
use NOTEDB;
use Fcntl qw(LOCK_EX LOCK_UN);
use Exporter ();
use vars qw(@ISA @EXPORT);
@ISA = qw(NOTEDB Exporter);
sub new {
my($this, %param) = @_;
my $class = ref($this) || $this;
my $self = {};
bless($self,$class);
$self->{NOTEDB} = $param{dbname} || File::Spec->catfile($ENV{HOME}, ".notedb");
if(! -e $param{dbname}) {
open(TT,">$param{dbname}") or die "Could not create $param{dbname}: $!\n";
close (TT);
}
elsif(! -w $param{dbname}) {
print "$param{dbname} is not writable!\n";
exit(1);
}
$self->{LOCKFILE} = $param{dbname} . "~LOCK";
return $self;
}
sub DESTROY
{
# clean the desk!
}
sub version {
my $this = shift;
return $this->{version};
}
sub set_del_all {
my $this = shift;
unlink $this->{NOTEDB};
open(TT,">$this->{NOTEDB}") or die "Could not create $this->{NOTEDB}: $!\n";
close (TT);
}
sub get_single {
my($this, $num) = @_;
my($address, $note, $date, $buffer, $n, $t, $buffer, );
my %data = $this->get_all();
return ($data{$num}->{note}, $data{$num}->{date});
}
sub get_all {
my $this = shift;
my($num, $note, $date, %res);
if ($this->unchanged) {
return %{$this->{cache}};
}
my %data = $this->_retrieve();
foreach my $num (keys %data) {
$res{$num}->{note} = $this->ude($data{$num}->{note});
$res{$num}->{date} = $this->ude($data{$num}->{date});
}
$this->cache(%res);
return %res;
}
sub import_data {
my ($this, $data) = @_;
my %res = $this->_retrieve();
my $pos = (scalar keys %res) + 1;
foreach my $num (keys %{$data}) {
$res{$pos}->{note} = $this->uen($data->{$num}->{note});
$res{$pos}->{date} = $this->uen($data->{$num}->{date});
$pos++;
}
$this->_store(\%res);
}
sub get_nextnum {
my $this = shift;
my($num, $te, $me, $buffer);
if ($this->unchanged) {
$num = 1;
foreach (keys %{$this->{cache}}) {
$num++;
}
return $num;
}
my %data = $this->get_all();
my $size = scalar keys %data;
$num = $size + 1;
return $num;
}
sub get_search {
my($this, $searchstring) = @_;
my($buffer, $num, $note, $date, %res, $t, $n, $match);
my $regex = $this->generate_search($searchstring);
eval $regex;
if ($@) {
print "invalid expression: \"$searchstring\"!\n";
return;
}
$match = 0;
if ($this->unchanged) {
foreach my $num (keys %{$this->{cache}}) {
$_ = $this->{cache}{$num}->{note};
eval $regex;
if ($match) {
$res{$num}->{note} = $this->{cache}{$num}->{note};
$res{$num}->{date} = $this->{cache}{$num}->{date}
}
$match = 0;
}
return %res;
}
my %data = $this->get_all();
foreach my $num(sort keys %data) {
$_ = $data{$num}->{note};
eval $regex;
if($match)
{
$res{$num}->{note} = $data{$num}->{note};
$res{$num}->{date} = $data{$num}->{data};
}
$match = 0;
}
return %res;
}
sub set_edit {
my($this, $num, $note, $date) = @_;
my %data = $this->_retrieve();
$data{$num} = {
note => $this->uen($note),
date => $this->uen($date)
};
$this->_store(\%data);
$this->changed;
}
sub set_new {
my($this, $num, $note, $date) = @_;
$this->set_edit($num, $note, $date);
}
sub set_del {
my($this, $num) = @_;
my(%data, $note, $date, $T, $setnum, $buffer, $n, $N, $t);
$setnum = 1;
%data = $this->_retrieve();
return "ERROR" if (! exists $data{$num});
delete $data{$num};
$this->_store(\%data);
$this->changed;
return;
}
sub set_recountnums {
my($this) = @_;
my(%orig, %data, $note, $date, $T, $setnum, $buffer, $n, $N, $t);
$setnum = 1;
%orig = $this->_retrieve();
foreach $N (sort {$a <=> $b} keys %orig) {
$data{$setnum} = {
note => $orig{$N}->{note},
date => $orig{$N}->{date}
};
$setnum++;
}
$this->_store(\%data);
$this->changed;
return;
}
sub uen {
my ($this, $raw) = @_;
my($crypted);
if($NOTEDB::crypt_supported == 1) {
eval {
$crypted = $this->{cipher}->encrypt($raw);
};
}
else {
$crypted = $raw;
}
my $coded = encode_base64($crypted);
return $coded;
}
sub ude {
my ($this, $crypted) = @_;
my($raw);
if($NOTEDB::crypt_supported == 1) {
eval {
$raw = $this->{cipher}->decrypt(decode_base64($crypted));
};
}
else {
$raw = decode_base64($crypted)
}
return $raw;
}
sub _store {
my ($this, $data) = @_;
lock_nstore($data, $this->{NOTEDB});
}
sub _retrieve {
my $this = shift;
if (-s $this->{NOTEDB}) {
my $data = lock_retrieve($this->{NOTEDB});
return %{$data};
}
else {
return ();
}
}
1; # keep this!
__END__
=head1 NAME
NOTEDB::text - module lib for accessing a notedb from perl
=head1 SYNOPSIS
# include the module
use NOTEDB;
# create a new NOTEDB object
$db = new NOTEDB("text", "/home/tom/.notedb", 4096, 24);
# decide to use encryption
# $key is the cipher to use for encryption
# $method must be either Crypt::IDEA or Crypt::DES
# you need Crypt::CBC, Crypt::IDEA and Crypt::DES to have installed.
$db->use_crypt($key,$method);
# do not use encryption
# this is the default
$db->no_crypt;
# get a single note
($note, $date) = $db->get_single(1);
# search for a certain note
%matching_notes = $db->get_search("somewhat");
# format of returned hash:
#$matching_notes{$numberofnote}->{'note' => 'something', 'date' => '23.12.2000 10:33:02'}
# get all existing notes
%all_notes = $db->get_all();
# format of returnes hash like the one from get_search above
# get the next noteid available
$next_num = $db->get_nextnum();
# modify a certain note
$db->set_edit(1, "any text", "23.12.2000 10:33:02");
# create a new note
$db->set_new(5, "any new text", "23.12.2000 10:33:02");
# delete a certain note
$db->set_del(5);
# turn on encryption. CryptMethod must be IDEA, DES or BLOWFISH
$db->use_crypt("passphrase", "CryptMethod");
# turn off encryption. This is the default.
$db->no_crypt();
=head1 DESCRIPTION
You can use this module for accessing a note database. This backend uses
a text file for storage and Storable for accessing the file.
Currently, NOTEDB module is only used by note itself. But feel free to use it
within your own project! Perhaps someone want to implement a webinterface to
note...
=head1 USAGE
please see the section SYNOPSIS, it says it all.
=head1 AUTHOR
Thomas Linden <tom@daemon.de>.
=cut