| 1 | # Perl module for note | ||
| 2 | # text database backend. see docu: perldoc NOTEDB::text | ||
| 3 | # using Storable as backend. | ||
| 4 | |||
| 5 | package NOTEDB::dumper; | ||
| 6 | |||
| 7 | $NOTEDB::dumper::VERSION = "1.03"; | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use Data::Dumper; | ||
| 11 | use File::Spec; | ||
| 12 | use MIME::Base64; | ||
| 13 | |||
| 14 | use NOTEDB; | ||
| 15 | |||
| 16 | use Fcntl qw(LOCK_EX LOCK_UN); | ||
| 17 | |||
| 18 | use Exporter (); | ||
| 19 | use vars qw(@ISA @EXPORT); | ||
| 20 | @ISA = qw(NOTEDB Exporter); | ||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | sub new { | ||
| 26 | my($this, %param) = @_; | ||
| 27 | |||
| 28 | my $class = ref($this) || $this; | ||
| 29 | my $self = {}; | ||
| 30 | bless($self,$class); | ||
| 31 | |||
| 32 | $self->{NOTEDB} = $self->{dbname} = $param{dbname} || File::Spec->catfile($ENV{HOME}, ".notedb.dumper"); | ||
| 33 | |||
| 34 | if(! -e $param{dbname}) { | ||
| 35 | open(TT,">$param{dbname}") or die "Could not create $param{dbname}: $!\n"; | ||
| 36 | close (TT); | ||
| 37 | } | ||
| 38 | elsif(! -w $param{dbname}) { | ||
| 39 | print "$param{dbname} is not writable!\n"; | ||
| 40 | exit(1); | ||
| 41 | } | ||
| 42 | |||
| 43 | $self->{LOCKFILE} = $param{dbname} . "~LOCK"; | ||
| 44 | $self->{mtime} = $self->get_stat(); | ||
| 45 | $self->{unread} = 1; | ||
| 46 | $self->{data} = {}; | ||
| 47 | |||
| 48 | return $self; | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | sub DESTROY | ||
| 53 | { | ||
| 54 | # clean the desk! | ||
| 55 | } | ||
| 56 | |||
| 57 | sub version { | ||
| 58 | my $this = shift; | ||
| 59 | return $NOTEDB::text::VERSION; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub get_stat { | ||
| 63 | my ($this) = @_; | ||
| 64 | my $mtime = (stat($this->{dbname}))[9]; | ||
| 65 | return $mtime; | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | sub set_del_all { | ||
| 70 | my $this = shift; | ||
| 71 | unlink $this->{NOTEDB}; | ||
| 72 | open(TT,">$this->{NOTEDB}") or die "Could not create $this->{NOTEDB}: $!\n"; | ||
| 73 | close (TT); | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | sub get_single { | ||
| 78 | my($this, $num) = @_; | ||
| 79 | my($address, $note, $date, $n, $t, $buffer, ); | ||
| 80 | |||
| 81 | my %data = $this->get_all(); | ||
| 82 | return ($data{$num}->{note}, $data{$num}->{date}); | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | sub get_all { | ||
| 87 | my $this = shift; | ||
| 88 | my($num, $note, $date, %res); | ||
| 89 | |||
| 90 | if ($this->unchanged) { | ||
| 91 | return %{$this->{cache}}; | ||
| 92 | } | ||
| 93 | |||
| 94 | my %data = $this->_retrieve(); | ||
| 95 | foreach my $num (keys %data) { | ||
| 96 | $res{$num}->{note} = $this->ude($data{$num}->{note}); | ||
| 97 | $res{$num}->{date} = $this->ude($data{$num}->{date}); | ||
| 98 | } | ||
| 99 | |||
| 100 | $this->cache(%res); | ||
| 101 | return %res; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub import_data { | ||
| 105 | my ($this, $data) = @_; | ||
| 106 | my %res = $this->_retrieve(); | ||
| 107 | my $pos = (scalar keys %res) + 1; | ||
| 108 | foreach my $num (keys %{$data}) { | ||
| 109 | $res{$pos}->{note} = $this->uen($data->{$num}->{note}); | ||
| 110 | $res{$pos}->{date} = $this->uen($data->{$num}->{date}); | ||
| 111 | $pos++; | ||
| 112 | } | ||
| 113 | $this->_store(\%res); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub get_nextnum { | ||
| 117 | my $this = shift; | ||
| 118 | my($num, $te, $me, $buffer); | ||
| 119 | |||
| 120 | if ($this->unchanged) { | ||
| 121 | $num = 1; | ||
| 122 | foreach (keys %{$this->{cache}}) { | ||
| 123 | $num++; | ||
| 124 | } | ||
| 125 | return $num; | ||
| 126 | } | ||
| 127 | |||
| 128 | my %data = $this->get_all(); | ||
| 129 | my $size = scalar keys %data; | ||
| 130 | $num = $size + 1; | ||
| 131 | return $num; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub get_search { | ||
| 135 | my($this, $searchstring) = @_; | ||
| 136 | my($buffer, $num, $note, $date, %res, $t, $n, $match); | ||
| 137 | |||
| 138 | my $regex = $this->generate_search($searchstring); | ||
| 139 | eval $regex; | ||
| 140 | if ($@) { | ||
| 141 | print "invalid expression: \"$searchstring\"!\n"; | ||
| 142 | return; | ||
| 143 | } | ||
| 144 | $match = 0; | ||
| 145 | |||
| 146 | if ($this->unchanged) { | ||
| 147 | foreach my $num (keys %{$this->{cache}}) { | ||
| 148 | $_ = $this->{cache}{$num}->{note}; | ||
| 149 | eval $regex; | ||
| 150 | if ($match) { | ||
| 151 | $res{$num}->{note} = $this->{cache}{$num}->{note}; | ||
| 152 | $res{$num}->{date} = $this->{cache}{$num}->{date} | ||
| 153 | } | ||
| 154 | $match = 0; | ||
| 155 | } | ||
| 156 | return %res; | ||
| 157 | } | ||
| 158 | |||
| 159 | my %data = $this->get_all(); | ||
| 160 | |||
| 161 | foreach my $num(sort keys %data) { | ||
| 162 | $_ = $data{$num}->{note}; | ||
| 163 | eval $regex; | ||
| 164 | if($match) | ||
| 165 | { | ||
| 166 | $res{$num}->{note} = $data{$num}->{note}; | ||
| 167 | $res{$num}->{date} = $data{$num}->{data}; | ||
| 168 | } | ||
| 169 | $match = 0; | ||
| 170 | } | ||
| 171 | |||
| 172 | return %res; | ||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | |||
| 177 | |||
| 178 | sub set_edit { | ||
| 179 | my($this, $num, $note, $date) = @_; | ||
| 180 | |||
| 181 | my %data = $this->_retrieve(); | ||
| 182 | |||
| 183 | $data{$num} = { | ||
| 184 | note => $this->uen($note), | ||
| 185 | date => $this->uen($date) | ||
| 186 | }; | ||
| 187 | |||
| 188 | $this->_store(\%data); | ||
| 189 | |||
| 190 | $this->changed; | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | sub set_new { | ||
| 195 | my($this, $num, $note, $date) = @_; | ||
| 196 | $this->set_edit($num, $note, $date); | ||
| 197 | } | ||
| 198 | |||
| 199 | |||
| 200 | sub set_del { | ||
| 201 | my($this, $num) = @_; | ||
| 202 | my(%data, $note, $date, $T, $setnum, $buffer, $n, $N, $t); | ||
| 203 | |||
| 204 | $setnum = 1; | ||
| 205 | |||
| 206 | %data = $this->_retrieve(); | ||
| 207 | return "ERROR" if (! exists $data{$num}); | ||
| 208 | |||
| 209 | delete $data{$num}; | ||
| 210 | |||
| 211 | $this->_store(\%data); | ||
| 212 | |||
| 213 | $this->changed; | ||
| 214 | |||
| 215 | return; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub set_recountnums { | ||
| 219 | my($this) = @_; | ||
| 220 | my(%orig, %data, $note, $date, $T, $setnum, $buffer, $n, $N, $t); | ||
| 221 | |||
| 222 | $setnum = 1; | ||
| 223 | %orig = $this->_retrieve(); | ||
| 224 | |||
| 225 | foreach $N (sort {$a <=> $b} keys %orig) { | ||
| 226 | $data{$setnum} = { | ||
| 227 | note => $orig{$N}->{note}, | ||
| 228 | date => $orig{$N}->{date} | ||
| 229 | }; | ||
| 230 | $setnum++; | ||
| 231 | } | ||
| 232 | |||
| 233 | $this->_store(\%data); | ||
| 234 | |||
| 235 | $this->changed; | ||
| 236 | |||
| 237 | return; | ||
| 238 | } | ||
| 239 | |||
| 240 | sub uen { | ||
| 241 | my ($this, $raw) = @_; | ||
| 242 | my($crypted); | ||
| 243 | if($NOTEDB::crypt_supported == 1) { | ||
| 244 | eval { | ||
| 245 | $crypted = $this->{cipher}->encrypt($raw); | ||
| 246 | return encode_base64($crypted); | ||
| 247 | }; | ||
| 248 | } | ||
| 249 | else { | ||
| 250 | return $raw; | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | sub ude { | ||
| 255 | my ($this, $crypted) = @_; | ||
| 256 | my($raw); | ||
| 257 | if($NOTEDB::crypt_supported == 1) { | ||
| 258 | eval { | ||
| 259 | $raw = $this->{cipher}->decrypt(decode_base64($crypted)); | ||
| 260 | }; | ||
| 261 | return $raw; | ||
| 262 | } | ||
| 263 | else { | ||
| 264 | return $crypted; | ||
| 265 | } | ||
| 266 | } | ||
| 267 | |||
| 268 | |||
| 269 | sub _store { | ||
| 270 | my ($this, $data) = @_; | ||
| 271 | open N, ">$this->{NOTEDB}" or die "Could not open db: $!\n"; | ||
| 272 | print N Data::Dumper->Dump([$data], [qw(*data)]); | ||
| 273 | close N; | ||
| 274 | } | ||
| 275 | |||
| 276 | sub _retrieve { | ||
| 277 | my $this = shift; | ||
| 278 | if (-s $this->{NOTEDB}) { | ||
| 279 | if ($this->changed() || $this->{unread}) { | ||
| 280 | open N, "<$this->{NOTEDB}" or die "Could not open db: $!\n"; | ||
| 281 | my $content = join "", <N>; | ||
| 282 | close N; | ||
| 283 | my %data; | ||
| 284 | eval $content; # creates %data | ||
| 285 | $this->{unread} = 0; | ||
| 286 | $this->{data} = \%data; | ||
| 287 | return %data; | ||
| 288 | } | ||
| 289 | } | ||
| 290 | else { | ||
| 291 | return (); | ||
| 292 | } | ||
| 293 | } | ||
| 294 | |||
| 295 | |||
| 296 | 1; # keep this! | ||
| 297 | |||
| 298 | __END__ |