This file is indexed.

/usr/share/perl5/NOTEDB/general.pm is in note 1.3.7-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# Perl module for note
# general database backend. see docu: perldoc NOTEDB::general
# using Config::General as backend.

package NOTEDB::general;

$NOTEDB::general::VERSION = "1.01";

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);
	};
	print $@;
    }
    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;

  if (%{$data}) {
    my $content = SaveConfigString($data) or die "could not serialize data: $!\n";
    print NOTE $content;
  }
  else {
    print NOTE "";
  }

  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