View | Details | Raw Unified | Return to bug 4508
Collapse All | Expand All

(-)/home/as/old/SpamAssassin/BayesStore/SQL.pm (-175 / +982 lines)
Lines 64-79 sub new { Link Here
64
64
65
  $self->{supported_db_version} = 3;
65
  $self->{supported_db_version} = 3;
66
66
67
  if (!$self->{bayes}->{conf}->{bayes_sql_dsn}) {
67
  #checks if bayes_read_write_support is enabled
68
    dbg("bayes: invalid config, must set bayes_sql_dsn config variable.\n");
68
  my $bayes_read_write_support=$self->{bayes}->{conf}->{bayes_read_write_support};
69
    return undef;
69
  
70
  $self->{bayes_read_write_support} = $self->{bayes}->{conf}->{bayes_read_write_support};
71
72
  if($bayes_read_write_support) {
73
    if (!$self->{bayes}->{conf}->{bayes_sql_dsn_read} || !$self->{bayes}->{conf}->{bayes_sql_dsn_write} || $self->{bayes}->{conf}->{bayes_sql_dsn} ) {
74
      dbg("bayes: invalid config, must set bayes_sql_dsn_write and bayes_sql_dsn_read and not bayes_sql_dsn config variable.\n");
75
      return undef;
76
    } 	
77
  }
78
  else
79
  {
80
    if (!$self->{bayes}->{conf}->{bayes_sql_dsn} || $self->{bayes}->{conf}->{bayes_sql_dsn_write} || $self->{bayes}->{conf}->{bayes_sql_dsn_read} ) {
81
      dbg("bayes: invalid config, must set bayes_sql_dsn and not bayes_sql_dsn_write and not bayes_sql_dsn_read config variable.\n");
82
      return undef;
83
    }
70
  }
84
  }
71
85
72
  $self->{_dsn} = $self->{bayes}->{conf}->{bayes_sql_dsn};
86
  $self->{_dsn} = $self->{bayes}->{conf}->{bayes_sql_dsn};
73
  $self->{_dbuser} = $self->{bayes}->{conf}->{bayes_sql_username};
87
  $self->{_dbuser} = $self->{bayes}->{conf}->{bayes_sql_username};
74
  $self->{_dbpass} = $self->{bayes}->{conf}->{bayes_sql_password};
88
  $self->{_dbpass} = $self->{bayes}->{conf}->{bayes_sql_password};
75
89
90
  $self->{_dsn_write} = $self->{bayes}->{conf}->{bayes_sql_dsn_write};
91
  $self->{_dbuser_write} = $self->{bayes}->{conf}->{bayes_sql_username_write};
92
  $self->{_dbpass_write} = $self->{bayes}->{conf}->{bayes_sql_password_write};
93
94
  $self->{_dsn_read} = $self->{bayes}->{conf}->{bayes_sql_dsn_read};
95
  $self->{_dbuser_read} = $self->{bayes}->{conf}->{bayes_sql_username_read};
96
  $self->{_dbpass_read} = $self->{bayes}->{conf}->{bayes_sql_password_read};
97
76
  $self->{_dbh} = undef;
98
  $self->{_dbh} = undef;
99
  $self->{_dbh_write} = undef;
100
  $self->{_dbh_read} = undef;
77
101
78
  unless (HAS_DBI) {
102
  unless (HAS_DBI) {
79
    dbg("bayes: Unable to connect to database: DBI module not available: $!");
103
    dbg("bayes: Unable to connect to database: DBI module not available: $!");
Lines 128-155 so that they can begin using the databas Link Here
128
152
129
sub tie_db_writable {
153
sub tie_db_writable {
130
  my ($self) = @_;
154
  my ($self) = @_;
155
  my $dbh;
156
  my $dbh_read;
157
  my $dbh_write;
131
158
132
  return 0 unless (HAS_DBI);
159
  return 0 unless (HAS_DBI);
133
160
134
  return 1 if ($self->{_dbh}); # already connected
161
  if($self->{bayes_read_write_support}) {
162
    if($self->{_dbh_write} && $self->{_dbh_read}) {
163
      return 1;#already connected readable and writeable 
164
    }
165
  }
166
  else
167
  {
168
    return 1 if ($self->{_dbh}); # already connected
169
  }
135
170
136
  my $main = $self->{bayes}->{main};
171
  my $main = $self->{bayes}->{main};
137
172
138
  $self->read_db_configs();
173
  $self->read_db_configs();
139
174
140
  # Turn off PrintError and explicitly set AutoCommit to off
175
  if($self->{bayes_read_write_support}) {
141
  my $dbh = DBI->connect($self->{_dsn}, $self->{_dbuser}, $self->{_dbpass},
176
    # connect writeable / Turn off PrintError and explicitly set AutoCommit to off
142
			 {'PrintError' => 0, 'AutoCommit' => 1});
177
     $dbh_write = DBI->connect($self->{_dsn_write}, $self->{_dbuser_write}, $self->{_dbpass_write},
178
	    		   {'PrintError' => 0, 'AutoCommit' => 1});
179
180
    # connect readable / Turn off PrintError and explicitly set AutoCommit to off
181
     $dbh_read = DBI->connect($self->{_dsn_read}, $self->{_dbuser_read}, $self->{_dbpass_read},
182
	    		   {'PrintError' => 0, 'AutoCommit' => 1});
183
  }
184
  else {
185
    # Turn off PrintError and explicitly set AutoCommit to off
186
    $dbh = DBI->connect($self->{_dsn}, $self->{_dbuser}, $self->{_dbpass},
187
	    		   {'PrintError' => 0, 'AutoCommit' => 1});
188
  }
189
143
190
144
  if (!$dbh) {
191
  if($self->{bayes_read_write_support}) {
145
    dbg("bayes: Unable to connect to database: ".DBI->errstr());
192
    if ($dbh_read && $dbh_write) {
146
    return 0;
193
       dbg("bayes: Readable Database connection established with $self->{_dsn_read} ");
194
       dbg("bayes: Writeable Database connection established with $self->{_dsn_write} ");
195
    } 
196
    else {
197
       dbg("bayes: Unable to connect to database: $self->{_dsn_read} ");
198
       dbg("bayes: Unable to connect to database: $self->{_dsn_write} ");
199
       return 0;
200
    }
147
  }
201
  }
148
  else {
202
  else {
149
    dbg("bayes: Database connection established");
203
    if (!$dbh) {
204
      dbg("bayes: Unable to connect to database: ".DBI->errstr());
205
      return 0;
206
    }
207
    else {
208
       dbg("bayes: Database connection established with $self->{_dsn}");
209
    }
150
  }
210
  }
151
211
152
  $self->{_dbh} = $dbh;
212
  if($self->{bayes_read_write_support}) {
213
    $self->{_dbh_read} = $dbh_read;
214
    $self->{_dbh_write} = $dbh_write;
215
  }
216
  else {
217
    $self->{_dbh} = $dbh;
218
  }
153
219
154
  # If the DB version is one we don't understand, abort!
220
  # If the DB version is one we don't understand, abort!
155
  my $db_ver = $self->_get_db_version();
221
  my $db_ver = $self->_get_db_version();
Lines 184-193 This method is unused for the SQL based Link Here
184
sub untie_db {
250
sub untie_db {
185
  my ($self) = @_;
251
  my ($self) = @_;
186
252
187
  return unless (defined($self->{_dbh}));
253
  if($self->{bayes_read_write_support}) {
254
    return unless ( (defined($self->{_dbh_read})) && (defined($self->{_dbh_write})) );
255
    $self->{_dbh_write}->disconnect();
256
    $self->{_dbh_read}->disconnect();
257
    $self->{_dbh_read} = undef;
258
    $self->{_dbh_write} = undef;
259
  }
260
  else {
261
    return unless (defined($self->{_dbh}));
262
    $self->{_dbh}->disconnect();
263
    $self->{_dbh} = undef;
264
  }
188
265
189
  $self->{_dbh}->disconnect();
190
  $self->{_dbh} = undef;
191
}
266
}
192
267
193
=head2 calculate_expire_delta
268
=head2 calculate_expire_delta
Lines 204-227 atime for token expiration. Link Here
204
279
205
sub calculate_expire_delta {
280
sub calculate_expire_delta {
206
  my ($self, $newest_atime, $start, $max_expire_mult) = @_;
281
  my ($self, $newest_atime, $start, $max_expire_mult) = @_;
207
282
  my $sth;
208
  my %delta = (); # use a hash since an array is going to be very sparse
283
  my %delta = (); # use a hash since an array is going to be very sparse
209
284
210
  return %delta unless (defined($self->{_dbh}));
285
  if($self->{bayes_read_write_support}) {
286
    return %delta unless ( (defined($self->{_dbh_write})) && (defined($self->{_dbh_read})) );
287
  }
288
  else {
289
    return %delta unless (defined($self->{_dbh}));
290
  }
211
291
212
  my $sql = "SELECT count(*)
292
  my $sql = "SELECT count(*)
213
               FROM bayes_token
293
               FROM bayes_token
214
              WHERE id = ?
294
              WHERE id = ?
215
                AND (? - atime) > ?";
295
                AND (? - atime) > ?";
216
296
217
  my $sth = $self->{_dbh}->prepare_cached($sql);
297
  if($self->{bayes_read_write_support}) {
298
    $sth = $self->{_dbh_read}->prepare_cached($sql);
299
  }
300
  else { 
301
    $sth = $self->{_dbh}->prepare_cached($sql);
302
  }
218
    
303
    
219
  for (my $i = 1; $i <= $max_expire_mult; $i<<=1) {
304
  for (my $i = 1; $i <= $max_expire_mult; $i<<=1) {
220
    my $rc = $sth->execute($self->{_userid}, $newest_atime, $start * $i);
305
    my $rc = $sth->execute($self->{_userid}, $newest_atime, $start * $i);
221
306
222
    unless ($rc) {
307
    unless ($rc) {
223
      dbg("bayes: calculate_expire_delta: SQL Error: ".$self->{_dbh}->errstr());
308
      if($self->{bayes_read_write_support}) {
224
      return undef;
309
        dbg("bayes: calculate_expire_delta: SQL Error: ".$self->{_dbh_read}->errstr());
310
        return undef;
311
      }
312
      else {
313
        dbg("bayes: calculate_expire_delta: SQL Error: ".$self->{_dbh}->errstr());
314
        return undef;
315
      }
225
    }
316
    }
226
317
227
    my ($count) = $sth->fetchrow_array();
318
    my ($count) = $sth->fetchrow_array();
Lines 252-257 sub token_expiration { Link Here
252
  my $num_hapaxes;
343
  my $num_hapaxes;
253
  my $num_lowfreq;
344
  my $num_lowfreq;
254
  my $deleted;
345
  my $deleted;
346
  my $rows;
347
  my $sth;
255
348
256
  # Figure out how old is too old...
349
  # Figure out how old is too old...
257
  my $too_old = $vars[10] - $newdelta; # tooold = newest - delta
350
  my $too_old = $vars[10] - $newdelta; # tooold = newest - delta
Lines 261-270 sub token_expiration { Link Here
261
              WHERE id  = ?
354
              WHERE id  = ?
262
                AND atime > ?";
355
                AND atime > ?";
263
356
264
  my $rows = $self->{_dbh}->do($sql, undef, $vars[10], $self->{_userid}, $vars[10]);
357
  if($self->{bayes_read_write_support}) {
358
    $rows = $self->{_dbh_write}->do($sql, undef, $vars[10], $self->{_userid}, $vars[10]);
359
  }
360
  else {
361
    $rows = $self->{_dbh}->do($sql, undef, $vars[10], $self->{_userid}, $vars[10]);
362
  }
265
363
266
  unless (defined($rows)) {
364
  unless (defined($rows)) {
267
    dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh}->errstr());
365
    if($self->{bayes_read_write_support}) {
366
      dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh_write}->errstr());
367
    }
368
    else {
369
      dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh}->errstr());
370
    }
371
 
268
    $deleted = 0;
372
    $deleted = 0;
269
    goto token_expiration_final;
373
    goto token_expiration_final;
270
  }
374
  }
Lines 274-283 sub token_expiration { Link Here
274
           WHERE id = ?
378
           WHERE id = ?
275
             AND atime < ?";
379
             AND atime < ?";
276
380
277
  my $sth = $self->{_dbh}->prepare_cached($sql);
381
382
  if($self->{bayes_read_write_support}) {
383
    $sth = $self->{_dbh_read}->prepare_cached($sql);
384
  }
385
  else {
386
    $sth = $self->{_dbh}->prepare_cached($sql);
387
  }
278
388
279
  unless (defined($sth)) {
389
  unless (defined($sth)) {
280
    dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh}->errstr());
390
    if($self->{bayes_read_write_support}) {
391
      dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh_read}->errstr());
392
    }
393
    else {
394
      dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh}->errstr());
395
    }
396
281
    $deleted = 0;
397
    $deleted = 0;
282
    goto token_expiration_final;
398
    goto token_expiration_final;
283
  }
399
  }
Lines 285-291 sub token_expiration { Link Here
285
  my $rc = $sth->execute($self->{_userid}, $too_old);
401
  my $rc = $sth->execute($self->{_userid}, $too_old);
286
  
402
  
287
  unless ($rc) {
403
  unless ($rc) {
288
    dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh}->errstr());
404
    if($self->{bayes_read_write_support}) {
405
      dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh_read}->errstr());
406
    }
407
    else {
408
      dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh}->errstr());
409
    }
410
289
    $deleted = 0;
411
    $deleted = 0;
290
    goto token_expiration_final;
412
    goto token_expiration_final;
291
  }
413
  }
Lines 307-316 sub token_expiration { Link Here
307
             WHERE id = ?
429
             WHERE id = ?
308
               AND atime < ?";
430
               AND atime < ?";
309
431
310
    $rows = $self->{_dbh}->do($sql, undef, $self->{_userid}, $too_old);
432
    if($self->{bayes_read_write_support}) {
433
      $rows = $self->{_dbh_write}->do($sql, undef, $self->{_userid}, $too_old);
434
    }
435
    else {
436
      $rows = $self->{_dbh}->do($sql, undef, $self->{_userid}, $too_old);
437
    }
311
438
312
    unless (defined($rows)) {
439
    unless (defined($rows)) {
313
      dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh}->errstr());
440
      if($self->{bayes_read_write_support}) {
441
        dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh_write}->errstr());
442
      }
443
      else {
444
        dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh}->errstr());
445
      }
446
314
      $deleted = 0;
447
      $deleted = 0;
315
      goto token_expiration_final;
448
      goto token_expiration_final;
316
    }
449
    }
Lines 325-336 sub token_expiration { Link Here
325
                                last_expire_reduce = ?
458
                                last_expire_reduce = ?
326
				WHERE id = ?";
459
				WHERE id = ?";
327
460
328
  $rows = $self->{_dbh}->do($sql, undef, $deleted, time(), $newdelta, $deleted, $self->{_userid});
461
462
  if($self->{bayes_read_write_support}) {
463
    $rows = $self->{_dbh_write}->do($sql, undef, $deleted, time(), $newdelta, $deleted, $self->{_userid});
464
  }
465
  else {
466
    $rows = $self->{_dbh}->do($sql, undef, $deleted, time(), $newdelta, $deleted, $self->{_userid});
467
  }
329
468
330
  unless (defined($rows)) {
469
  unless (defined($rows)) {
331
    # Very bad, we actually deleted the tokens, but were unable to update
470
    # Very bad, we actually deleted the tokens, but were unable to update
332
    # bayes_vars with the new data.
471
    # bayes_vars with the new data.
333
    dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh}->errstr());
472
    if($self->{bayes_read_write_support}) {
473
      dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh_write}->errstr());
474
    }
475
    else {
476
      dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh}->errstr());
477
    }
478
334
    dbg("bayes: Bayes database now in inconsistent state, suggest a backup/restore.");
479
    dbg("bayes: Bayes database now in inconsistent state, suggest a backup/restore.");
335
    goto token_expiration_final;
480
    goto token_expiration_final;
336
  }
481
  }
Lines 344-354 sub token_expiration { Link Here
344
489
345
    $sql = "UPDATE bayes_vars SET oldest_token_age = ? WHERE id = ?";
490
    $sql = "UPDATE bayes_vars SET oldest_token_age = ? WHERE id = ?";
346
491
347
    $rows = $self->{_dbh}->do($sql, undef, $oldest_token_age, $self->{_userid});
492
    if($self->{bayes_read_write_support}) {
493
      $rows = $self->{_dbh_write}->do($sql, undef, $oldest_token_age, $self->{_userid});
494
    }
495
    else {
496
      $rows = $self->{_dbh}->do($sql, undef, $oldest_token_age, $self->{_userid});
497
    }
348
498
349
    unless (defined($rows)) {
499
    unless (defined($rows)) {
350
      # not much more we can do here, so just warn the user and bail out
500
      # not much more we can do here, so just warn the user and bail out
351
      dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh}->errstr());
501
      if($self->{bayes_read_write_support}) {
502
        dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh_write}->errstr());
503
      }
504
      else {
505
        dbg("bayes: token_expiration: SQL Error: ".$self->{_dbh}->errstr());
506
      }
507
352
      # yeah I know it's the next thing anyway, but here in case someone adds
508
      # yeah I know it's the next thing anyway, but here in case someone adds
353
      # additional code below this block
509
      # additional code below this block
354
      goto token_expiration_final; 
510
      goto token_expiration_final; 
Lines 397-420 found. Link Here
397
553
398
sub seen_get {
554
sub seen_get {
399
  my ($self, $msgid) = @_;
555
  my ($self, $msgid) = @_;
556
  my $sth;
400
557
401
  return undef unless (defined($self->{_dbh}));
558
  if($self->{bayes_read_write_support}) {
559
    return undef unless ( (defined($self->{_dbh_read})) && (defined($self->{_dbh_write})) );
560
  }
561
  else { 
562
    return undef unless (defined($self->{_dbh}));
563
  }
402
 
564
 
403
  my $sql = "SELECT flag FROM bayes_seen
565
  my $sql = "SELECT flag FROM bayes_seen
404
              WHERE id = ?
566
              WHERE id = ?
405
                AND msgid = ?";
567
                AND msgid = ?";
406
568
407
  my $sth = $self->{_dbh}->prepare_cached($sql);
569
  if($self->{bayes_read_write_support}) {
570
    $sth = $self->{_dbh_read}->prepare_cached($sql);
571
  }
572
  else {
573
    $sth = $self->{_dbh}->prepare_cached($sql);
574
  }
408
575
409
  unless (defined($sth)) {
576
  unless (defined($sth)) {
410
    dbg("bayes: seen_get: SQL Error: ".$self->{_dbh}->errstr());
577
    if($self->{bayes_read_write_support}) {
578
      dbg("bayes: seen_get: SQL Error: ".$self->{_dbh_read}->errstr());
579
    }
580
    else {
581
      dbg("bayes: seen_get: SQL Error: ".$self->{_dbh}->errstr());
582
    }
583
411
    return undef;
584
    return undef;
412
  }
585
  }
413
586
414
  my $rc = $sth->execute($self->{_userid}, $msgid);
587
  my $rc = $sth->execute($self->{_userid}, $msgid);
415
  
588
  
416
  unless ($rc) {
589
  unless ($rc) {
417
    dbg("bayes: seen_get: SQL Error: ".$self->{_dbh}->errstr());
590
    if($self->{bayes_read_write_support}) {
591
      dbg("bayes: seen_get: SQL Error: ".$self->{_dbh_read}->errstr());
592
    }
593
    else {
594
      dbg("bayes: seen_get: SQL Error: ".$self->{_dbh}->errstr());
595
    }
596
418
    return undef;
597
    return undef;
419
  }
598
  }
420
599
Lines 437-457 two values 's' for spam and 'h' for ham. Link Here
437
616
438
sub seen_put {
617
sub seen_put {
439
  my ($self, $msgid, $flag) = @_;
618
  my ($self, $msgid, $flag) = @_;
619
  my $rows;
440
620
441
  return 0 if (!$msgid);
621
  return 0 if (!$msgid);
442
  return 0 if (!$flag);
622
  return 0 if (!$flag);
443
  
623
  
444
  return 0 unless (defined($self->{_dbh}));
624
625
  if($self->{bayes_read_write_support}) {
626
    return 0 unless ( defined($self->{_dbh_read})  && defined($self->{_dbh_write}) );
627
  }
628
  else {
629
    return 0 unless (defined($self->{_dbh}));
630
  }
445
631
446
  my $sql = "INSERT INTO bayes_seen (id, msgid, flag)
632
  my $sql = "INSERT INTO bayes_seen (id, msgid, flag)
447
             VALUES (?,?,?)";
633
             VALUES (?,?,?)";
448
  
634
449
  my $rows = $self->{_dbh}->do($sql,
635
  if($self->{bayes_read_write_support}) {
450
			       undef,
636
    $rows = $self->{_dbh_write}->do($sql, undef, $self->{_userid}, $msgid, $flag);
451
			       $self->{_userid}, $msgid, $flag);
637
  }
452
  
638
  else {
639
    $rows = $self->{_dbh}->do($sql, undef, $self->{_userid}, $msgid, $flag);
640
  } 
641
 
453
  unless (defined($rows)) {
642
  unless (defined($rows)) {
454
    dbg("bayes: seen_put: SQL Error: ".$self->{_dbh}->errstr());
643
    if($self->{bayes_read_write_support}) {
644
      dbg("bayes: seen_put: SQL Error: ".$self->{_dbh_write}->errstr());
645
    }
646
    else {
647
      dbg("bayes: seen_put: SQL Error: ".$self->{_dbh}->errstr());
648
    }
649
455
    return 0;
650
    return 0;
456
  }
651
  }
457
652
Lines 470-490 This method removes C<$msgid> from the d Link Here
470
665
471
sub seen_delete {
666
sub seen_delete {
472
  my ($self, $msgid) = @_;
667
  my ($self, $msgid) = @_;
668
  my $rows;
473
669
474
  return 0 if (!$msgid);
670
  return 0 if (!$msgid);
475
671
476
  return 0 unless (defined($self->{_dbh}));
672
  if($self->{bayes_read_write_support}) {
673
    return 0 unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
674
  }
675
  else {
676
    return 0 unless (defined($self->{_dbh}));
677
  }
477
678
478
  my $sql = "DELETE FROM bayes_seen
679
  my $sql = "DELETE FROM bayes_seen
479
              WHERE id = ?
680
              WHERE id = ?
480
                AND msgid = ?";
681
                AND msgid = ?";
481
  
682
  
482
  my $rows = $self->{_dbh}->do($sql,
683
  if($self->{bayes_read_write_support}) {
483
			       undef,
684
    $rows = $self->{_dbh_write}->do($sql, undef, $self->{_userid}, $msgid);
484
			       $self->{_userid}, $msgid);
685
  }
686
  else {
687
    $rows = $self->{_dbh}->do($sql, undef, $self->{_userid}, $msgid);
688
  }
485
689
486
  unless (defined($rows)) {
690
  unless (defined($rows)) {
487
    dbg("bayes: seen_delete: SQL Error: ".$self->{_dbh}->errstr());
691
    if($self->{bayes_read_write_support}) {
692
      dbg("bayes: seen_delete: SQL Error: ".$self->{_dbh_write}->errstr());
693
    }
694
    else {
695
      dbg("bayes: seen_delete: SQL Error: ".$self->{_dbh}->errstr());
696
    }
488
    return 0;
697
    return 0;
489
  }
698
  }
490
699
Lines 528-535 The values returned in the array are in Link Here
528
sub get_storage_variables {
737
sub get_storage_variables {
529
  my ($self) = @_;
738
  my ($self) = @_;
530
  my @values;
739
  my @values;
740
  my $sth;
531
741
532
  return (0,0,0,0,0,0,0,0,0,0,0) unless (defined($self->{_dbh}));
742
  if($self->{bayes_read_write_support}) {
743
    return (0,0,0,0,0,0,0,0,0,0,0) unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
744
  }
745
  else {
746
    return (0,0,0,0,0,0,0,0,0,0,0) unless (defined($self->{_dbh}));
747
  }
533
748
534
  my $sql = "SELECT spam_count, ham_count, token_count, last_expire,
749
  my $sql = "SELECT spam_count, ham_count, token_count, last_expire,
535
                    last_atime_delta, last_expire_reduce, oldest_token_age,
750
                    last_atime_delta, last_expire_reduce, oldest_token_age,
Lines 537-553 sub get_storage_variables { Link Here
537
               FROM bayes_vars
752
               FROM bayes_vars
538
              WHERE id = ?";
753
              WHERE id = ?";
539
754
540
  my $sth = $self->{_dbh}->prepare_cached($sql);
755
  if($self->{bayes_read_write_support}) {
756
    $sth = $self->{_dbh_write}->prepare_cached($sql);
757
  }
758
  else {
759
    $sth = $self->{_dbh}->prepare_cached($sql);
760
  }
761
541
762
542
  unless (defined($sth)) {
763
  unless (defined($sth)) {
543
    dbg("bayes: get_storage_variables: SQL Error: ".$self->{_dbh}->errstr());
764
    if($self->{bayes_read_write_support}) {
765
      dbg("bayes: get_storage_variables: SQL Error: ".$self->{_dbh_write}->errstr());
766
    }
767
    else {
768
      dbg("bayes: get_storage_variables: SQL Error: ".$self->{_dbh}->errstr());
769
    }
770
544
    return (0,0,0,0,0,0,0,0,0,0,0);
771
    return (0,0,0,0,0,0,0,0,0,0,0);
545
  }
772
  }
546
773
547
  my $rc = $sth->execute($self->{_userid});
774
  my $rc = $sth->execute($self->{_userid});
548
775
549
  unless ($rc) {
776
  unless ($rc) {
550
    dbg("bayes: get_storage_variables: SQL Error: ".$self->{_dbh}->errstr());
777
    if($self->{bayes_read_write_support}) {
778
      dbg("bayes: get_storage_variables: SQL Error: ".$self->{_dbh_write}->errstr());
779
    }
780
    else {
781
      dbg("bayes: get_storage_variables: SQL Error: ".$self->{_dbh}->errstr());
782
    }
783
551
    return (0,0,0,0,0,0,0,0,0,0,0);
784
    return (0,0,0,0,0,0,0,0,0,0,0);
552
  }
785
  }
553
786
Lines 588-595 printing it out according to the passed Link Here
588
821
589
sub dump_db_toks {
822
sub dump_db_toks {
590
  my ($self, $template, $regex, @vars) = @_;
823
  my ($self, $template, $regex, @vars) = @_;
824
  my $sth;
591
825
592
  return unless (defined($self->{_dbh}));
826
  if($self->{bayes_read_write_support}) {
827
    return unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
828
  }
829
  else {
830
    return unless (defined($self->{_dbh}));
831
  }
593
832
594
  # 0/0 tokens don't count, but in theory we shouldn't have any
833
  # 0/0 tokens don't count, but in theory we shouldn't have any
595
  # use RPAD to make sure we get trailing spaces in the token value
834
  # use RPAD to make sure we get trailing spaces in the token value
Lines 598-614 sub dump_db_toks { Link Here
598
              WHERE id = ?
837
              WHERE id = ?
599
                AND (spam_count > 0 OR ham_count > 0)";
838
                AND (spam_count > 0 OR ham_count > 0)";
600
839
601
  my $sth = $self->{_dbh}->prepare($sql);
840
  if($self->{bayes_read_write_support}) {
841
    $sth = $self->{_dbh_read}->prepare($sql);
842
  }
843
  else {
844
    $sth = $self->{_dbh}->prepare($sql);
845
  }
846
602
847
603
  unless (defined($sth)) {
848
  unless (defined($sth)) {
604
    dbg("bayes: dump_db_toks: SQL Error: ".$self->{_dbh}->errstr());
849
    if($self->{bayes_read_write_support}) {
850
      dbg("bayes: dump_db_toks: SQL Error: ".$self->{_dbh_read}->errstr());
851
    }
852
    else {
853
      dbg("bayes: dump_db_toks: SQL Error: ".$self->{_dbh}->errstr());
854
    }
855
605
    return;
856
    return;
606
  }
857
  }
607
858
608
  my $rc = $sth->execute($self->{_userid});
859
  my $rc = $sth->execute($self->{_userid});
609
860
610
  unless ($rc) {
861
  unless ($rc) {
611
    dbg("bayes: dump_db_toks: SQL Error: ".$self->{_dbh}->errstr());
862
    if($self->{bayes_read_write_support}) {
863
      dbg("bayes: dump_db_toks: SQL Error: ".$self->{_dbh_read}->errstr());
864
    }
865
    else {
866
      dbg("bayes: dump_db_toks: SQL Error: ".$self->{_dbh}->errstr());
867
    }
868
612
    return;
869
    return;
613
  }  
870
  }  
614
871
Lines 638-657 This method sets the last expire time. Link Here
638
895
639
sub set_last_expire {
896
sub set_last_expire {
640
  my ($self, $time) = @_;
897
  my ($self, $time) = @_;
898
  my $rows;
641
899
642
  return 0 unless (defined($time));
900
  return 0 unless (defined($time));
643
901
644
  return 0 unless (defined($self->{_dbh}));
902
  if($self->{bayes_read_write_support}) {
903
    return 0 unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
904
  }
905
  else {
906
    return 0 unless (defined($self->{_dbh}));
907
  }
645
908
646
  my $sql = "UPDATE bayes_vars SET last_expire = ? WHERE id = ?";
909
  my $sql = "UPDATE bayes_vars SET last_expire = ? WHERE id = ?";
647
 
910
 
648
  my $rows = $self->{_dbh}->do($sql,
911
  if($self->{bayes_read_write_support}) {
649
			       undef,
912
    $rows = $self->{_dbh_write}->do($sql, undef, $time, $self->{_userid});
650
			       $time,
913
  }
651
			       $self->{_userid});
914
  else {
915
    $rows = $self->{_dbh}->do($sql, undef, $time, $self->{_userid});
916
  }
652
917
653
  unless (defined($rows)) {
918
  unless (defined($rows)) {
654
    dbg("bayes: set_last_expire: SQL Error: ".$self->{_dbh}->errstr());
919
    if($self->{bayes_read_write_support}) {
920
      dbg("bayes: set_last_expire: SQL Error: ".$self->{_dbh_write}->errstr());
921
    }
922
    else {
923
      dbg("bayes: set_last_expire: SQL Error: ".$self->{_dbh}->errstr());
924
    }
925
655
    return 0;
926
    return 0;
656
  }
927
  }
657
928
Lines 673-694 value. Link Here
673
944
674
sub get_running_expire_tok {
945
sub get_running_expire_tok {
675
  my ($self) = @_;
946
  my ($self) = @_;
947
  my $sth;
676
948
677
  return 0 unless (defined($self->{_dbh}));
949
  if($self->{bayes_read_write_support}) {
950
    return 0 unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
951
  }
952
  else {
953
    return 0 unless (defined($self->{_dbh}));
954
  }
678
955
679
  my $sql = "SELECT max(runtime) from bayes_expire WHERE id = ?";
956
  my $sql = "SELECT max(runtime) from bayes_expire WHERE id = ?";
680
957
681
  my $sth = $self->{_dbh}->prepare_cached($sql);
958
  if($self->{bayes_read_write_support}) {
959
    $sth = $self->{_dbh_read}->prepare_cached($sql);
960
  }
961
  else {
962
    $sth = $self->{_dbh}->prepare_cached($sql);
963
  }
682
964
683
  unless (defined($sth)) {
965
  unless (defined($sth)) {
684
    dbg("bayes: get_running_expire_tok: SQL Error: ".$self->{_dbh}->errstr());
966
    if($self->{bayes_read_write_support}) {
967
      dbg("bayes: get_running_expire_tok: SQL Error: ".$self->{_dbh_read}->errstr());
968
    }
969
    else {
970
      dbg("bayes: get_running_expire_tok: SQL Error: ".$self->{_dbh}->errstr());
971
    }
972
685
    return 0;
973
    return 0;
686
  }
974
  }
687
975
688
  my $rc = $sth->execute($self->{_userid});
976
  my $rc = $sth->execute($self->{_userid});
689
977
690
  unless ($rc) {
978
  unless ($rc) {
691
    dbg("bayes: get_running_expire_tok: SQL Error: ".$self->{_dbh}->errstr());
979
    if($self->{bayes_read_write_support}) {
980
      dbg("bayes: get_running_expire_tok: SQL Error: ".$self->{_dbh_read}->errstr());
981
    }
982
    else {
983
      dbg("bayes: get_running_expire_tok: SQL Error: ".$self->{_dbh}->errstr());
984
    }
985
692
    return 0;
986
    return 0;
693
  }
987
  }
694
988
Lines 710-727 This method sets the time that an expire Link Here
710
1004
711
sub set_running_expire_tok {
1005
sub set_running_expire_tok {
712
  my ($self) = @_;
1006
  my ($self) = @_;
1007
  my $rows;
713
1008
714
  return 0 unless (defined($self->{_dbh}));
1009
  if($self->{bayes_read_write_support}) {
1010
    return 0 unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
1011
  }     
1012
  else {
1013
    return 0 unless (defined($self->{_dbh}));
1014
  }     
715
1015
716
  my $sql = "INSERT INTO bayes_expire (id,runtime) VALUES (?,?)";
1016
  my $sql = "INSERT INTO bayes_expire (id,runtime) VALUES (?,?)";
717
1017
718
  my $time = time();
1018
  my $time = time();
719
1019
720
  my $rows = $self->{_dbh}->do($sql,
1020
  if($self->{bayes_read_write_support}) {
721
			       undef,
1021
    $rows = $self->{_dbh_write}->do($sql, undef, $self->{_userid}, $time);
722
			       $self->{_userid}, $time);
1022
  }
1023
  else {
1024
    $rows = $self->{_dbh}->do($sql, undef, $self->{_userid}, $time);
1025
  }
1026
723
  unless (defined($rows)) {
1027
  unless (defined($rows)) {
724
    dbg("bayes: set_running_expire_tok: SQL Error: ".$self->{_dbh}->errstr());
1028
    if($self->{bayes_read_write_support}) {
1029
      dbg("bayes: set_running_expire_tok: SQL Error: ".$self->{_dbh_write}->errstr());
1030
    }
1031
    else {
1032
      dbg("bayes: set_running_expire_tok: SQL Error: ".$self->{_dbh}->errstr());
1033
    }
1034
725
    return undef;
1035
    return undef;
726
  }
1036
  }
727
1037
Lines 740-755 and expire is currently running. Link Here
740
1050
741
sub remove_running_expire_tok {
1051
sub remove_running_expire_tok {
742
  my ($self) = @_;
1052
  my ($self) = @_;
1053
  my $rows;
743
1054
744
  return 0 unless (defined($self->{_dbh}));
1055
  if($self->{bayes_read_write_support}) {
1056
    return 0 unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
1057
  }     
1058
  else {
1059
    return 0 unless (defined($self->{_dbh}));
1060
  }     
745
1061
746
  my $sql = "DELETE from bayes_expire
1062
  my $sql = "DELETE from bayes_expire
747
              WHERE id = ?";
1063
              WHERE id = ?";
748
1064
749
  my $rows = $self->{_dbh}->do($sql, undef, $self->{_userid});
1065
1066
  if($self->{bayes_read_write_support}) {
1067
    $rows = $self->{_dbh_write}->do($sql, undef, $self->{_userid});
1068
  }     
1069
  else {
1070
    $rows = $self->{_dbh}->do($sql, undef, $self->{_userid});
1071
  }
750
1072
751
  unless (defined($rows)) {
1073
  unless (defined($rows)) {
752
    dbg("bayes: remove_running_expire_tok: SQL Error: ".$self->{_dbh}->errstr());
1074
    if($self->{bayes_read_write_support}) {
1075
      dbg("bayes: remove_running_expire_tok: SQL Error: ".$self->{_dbh_write}->errstr());
1076
    }     
1077
    else {
1078
      dbg("bayes: remove_running_expire_tok: SQL Error: ".$self->{_dbh}->errstr());
1079
    }
1080
753
    return 0;
1081
    return 0;
754
  }
1082
  }
755
1083
Lines 768-792 and returns it's spam_count, ham_count a Link Here
768
1096
769
sub tok_get {
1097
sub tok_get {
770
  my ($self, $token) = @_;
1098
  my ($self, $token) = @_;
1099
  my $sth;
771
1100
772
  return (0,0,0) unless (defined($self->{_dbh}));
1101
  if($self->{bayes_read_write_support}) {
1102
    return (0,0,0) unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
1103
  }     
1104
  else {
1105
    return (0,0,0) unless (defined($self->{_dbh}));
1106
  }
773
1107
774
  my $sql = "SELECT spam_count, ham_count, atime
1108
  my $sql = "SELECT spam_count, ham_count, atime
775
               FROM bayes_token
1109
               FROM bayes_token
776
              WHERE id = ?
1110
              WHERE id = ?
777
                AND token = ?";
1111
                AND token = ?";
778
1112
779
  my $sth = $self->{_dbh}->prepare_cached($sql);
1113
  if($self->{bayes_read_write_support}) {
1114
    $sth = $self->{_dbh_read}->prepare_cached($sql);
1115
  }
1116
  else {
1117
    $sth = $self->{_dbh}->prepare_cached($sql);
1118
  }
780
1119
781
  unless (defined($sth)) {
1120
  unless (defined($sth)) {
782
    dbg("bayes: tok_get: SQL Error: ".$self->{_dbh}->errstr());
1121
    if($self->{bayes_read_write_support}) {
1122
      dbg("bayes: tok_get: SQL Error: ".$self->{_dbh_read}->errstr());
1123
    }
1124
    else {
1125
      dbg("bayes: tok_get: SQL Error: ".$self->{_dbh}->errstr());
1126
    }
1127
783
    return (0,0,0);
1128
    return (0,0,0);
784
  }
1129
  }
785
1130
786
  my $rc = $sth->execute($self->{_userid}, $token);
1131
  my $rc = $sth->execute($self->{_userid}, $token);
787
1132
788
  unless ($rc) {
1133
  unless ($rc) {
789
    dbg("bayes: tok_get: SQL Error: ".$self->{_dbh}->errstr());
1134
    if($self->{bayes_read_write_support}) {
1135
      dbg("bayes: tok_get: SQL Error: ".$self->{_dbh_read}->errstr());
1136
    }
1137
    else {
1138
      dbg("bayes: tok_get: SQL Error: ".$self->{_dbh}->errstr());
1139
    }
1140
790
    return (0,0,0);
1141
    return (0,0,0);
791
  }
1142
  }
792
1143
Lines 813-820 an array ref of arrays spam count, ham a Link Here
813
1164
814
sub tok_get_all {
1165
sub tok_get_all {
815
  my ($self, @tokens) = @_;
1166
  my ($self, @tokens) = @_;
1167
  my $sth;
816
1168
817
  return [] unless (defined($self->{_dbh}));
1169
  if($self->{bayes_read_write_support}) {
1170
    return [] unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
1171
  }
1172
  else { 
1173
    return [] unless (defined($self->{_dbh}));
1174
  }
818
1175
819
  my $token_list_size = scalar(@tokens);
1176
  my $token_list_size = scalar(@tokens);
820
  dbg("bayes: tok_get_all: Token Count: $token_list_size");
1177
  dbg("bayes: tok_get_all: Token Count: $token_list_size");
Lines 851-867 sub tok_get_all { Link Here
851
1208
852
      my $dynamic_sql = $multi_sql . $in_str;
1209
      my $dynamic_sql = $multi_sql . $in_str;
853
1210
854
      my $sth = $self->{_dbh}->prepare($dynamic_sql);
1211
      if($self->{bayes_read_write_support}) {
1212
        $sth = $self->{_dbh_read}->prepare($dynamic_sql);
1213
      }
1214
      else {
1215
        $sth = $self->{_dbh}->prepare($dynamic_sql);
1216
      } 
855
1217
856
      unless (defined($sth)) {
1218
      unless (defined($sth)) {
857
	dbg("bayes: tok_get_all: SQL Error: ".$self->{_dbh}->errstr());
1219
        if($self->{bayes_read_write_support}) {
1220
	  dbg("bayes: tok_get_all: SQL Error: ".$self->{_dbh_read}->errstr());
1221
        }
1222
        else {
1223
	  dbg("bayes: tok_get_all: SQL Error: ".$self->{_dbh}->errstr());
1224
        }
1225
858
	return [];
1226
	return [];
859
      }
1227
      }
860
1228
861
      my $rc = $sth->execute($self->{_userid}, @bindings);
1229
      my $rc = $sth->execute($self->{_userid}, @bindings);
862
1230
863
      unless ($rc) {
1231
      unless ($rc) {
864
	dbg("bayes: tok_get_all: SQL Error: ".$self->{_dbh}->errstr());
1232
        if($self->{bayes_read_write_support}) {
1233
	  dbg("bayes: tok_get_all: SQL Error: ".$self->{_dbh_read}->errstr());
1234
        }
1235
        else {
1236
	  dbg("bayes: tok_get_all: SQL Error: ".$self->{_dbh}->errstr());
1237
        }
1238
865
	return [];
1239
	return [];
866
      }
1240
      }
867
1241
Lines 881-897 sub tok_get_all { Link Here
881
  }
1255
  }
882
1256
883
  while ($search_index < $token_list_size) {
1257
  while ($search_index < $token_list_size) {
884
    my $sth = $self->{_dbh}->prepare($single_sql);
1258
    if($self->{bayes_read_write_support}) {
1259
      $sth = $self->{_dbh_read}->prepare($single_sql);
1260
    }
1261
    else {
1262
      $sth = $self->{_dbh}->prepare($single_sql);
1263
    }
1264
885
1265
886
    unless (defined($sth)) {
1266
    unless (defined($sth)) {
887
      dbg("bayes: tok_get_all: SQL Error: ".$self->{_dbh}->errstr());
1267
        if($self->{bayes_read_write_support}) {
1268
	  dbg("bayes: tok_get_all: SQL Error: ".$self->{_dbh_read}->errstr());
1269
        }
1270
        else {
1271
	  dbg("bayes: tok_get_all: SQL Error: ".$self->{_dbh}->errstr());
1272
        }
1273
888
      return [];
1274
      return [];
889
    }
1275
    }
890
1276
891
    my $rc = $sth->execute($self->{_userid}, $tokens[$search_index++]);
1277
    my $rc = $sth->execute($self->{_userid}, $tokens[$search_index++]);
892
1278
893
    unless ($rc) {
1279
    unless ($rc) {
894
      dbg("bayes: tok_get_all: SQL Error: ".$self->{_dbh}->errstr());
1280
        if($self->{bayes_read_write_support}) {
1281
	  dbg("bayes: tok_get_all: SQL Error: ".$self->{_dbh_read}->errstr());
1282
        }
1283
        else {
1284
	  dbg("bayes: tok_get_all: SQL Error: ".$self->{_dbh}->errstr());
1285
        }
1286
895
      return [];
1287
      return [];
896
    }
1288
    }
897
1289
Lines 946-952 ham learned. Link Here
946
sub nspam_nham_get {
1338
sub nspam_nham_get {
947
  my ($self) = @_;
1339
  my ($self) = @_;
948
1340
949
  return (0,0) unless (defined($self->{_dbh}));
1341
  if($self->{bayes_read_write_support}) { 
1342
    return (0,0) unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
1343
  }
1344
  else {
1345
    return (0,0) unless (defined($self->{_dbh}));
1346
  }
950
1347
951
  my @vars = $self->get_storage_variables();
1348
  my @vars = $self->get_storage_variables();
952
1349
Lines 966-974 This method updates the number of spam a Link Here
966
sub nspam_nham_change {
1363
sub nspam_nham_change {
967
  my ($self, $num_spam, $num_ham) = @_;
1364
  my ($self, $num_spam, $num_ham) = @_;
968
1365
969
  return 0 unless (defined($self->{_dbh}));
1366
  if($self->{bayes_read_write_support}) {
1367
    return 0 unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
1368
  }     
1369
  else {
1370
    return 0 unless (defined($self->{_dbh}));
1371
  }     
970
1372
971
  my $sql;
1373
  my $sql;
1374
  my $rows;
972
  my @bindings;
1375
  my @bindings;
973
1376
974
  if ($num_spam != 0 && $num_ham != 0) {
1377
  if ($num_spam != 0 && $num_ham != 0) {
Lines 996-1007 sub nspam_nham_change { Link Here
996
    return 1;
1399
    return 1;
997
  }
1400
  }
998
1401
999
  my $rows = $self->{_dbh}->do($sql,
1402
  if($self->{bayes_read_write_support}) {
1000
			       undef,
1403
    $rows = $self->{_dbh_write}->do($sql, undef, @bindings);
1001
			       @bindings);
1404
  }
1405
  else {
1406
    $rows = $self->{_dbh}->do($sql, undef, @bindings);
1407
  }
1002
1408
1003
  unless (defined($rows)) {
1409
  unless (defined($rows)) {
1004
    dbg("bayes: nspam_nham_change: SQL Error: ".$self->{_dbh}->errstr());
1410
    if($self->{bayes_read_write_support}) {
1411
      dbg("bayes: nspam_nham_change: SQL Error: ".$self->{_dbh_write}->errstr());
1412
    }
1413
    else {
1414
      dbg("bayes: nspam_nham_change: SQL Error: ".$self->{_dbh}->errstr());
1415
    }
1416
1005
    return 0;
1417
    return 0;
1006
  }
1418
  }
1007
1419
Lines 1022-1029 The assumption is that the token already Link Here
1022
1434
1023
sub tok_touch {
1435
sub tok_touch {
1024
  my ($self, $token, $atime) = @_;
1436
  my ($self, $token, $atime) = @_;
1437
  my $rows;
1025
1438
1026
  return 0 unless (defined($self->{_dbh}));
1439
  if($self->{bayes_read_write_support}) {
1440
    return 0 unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
1441
  }     
1442
  else {
1443
    return 0 unless (defined($self->{_dbh}));
1444
  }     
1027
1445
1028
  # shortcut, will only update atime for the token if the atime is less than
1446
  # shortcut, will only update atime for the token if the atime is less than
1029
  # what we are updating to
1447
  # what we are updating to
Lines 1033-1043 sub tok_touch { Link Here
1033
                AND token = ?
1451
                AND token = ?
1034
                AND atime < ?";
1452
                AND atime < ?";
1035
1453
1036
  my $rows = $self->{_dbh}->do($sql, undef, $atime, $self->{_userid},
1454
  if($self->{bayes_read_write_support}) {
1037
			       $token, $atime);
1455
    $rows = $self->{_dbh_write}->do($sql, undef, $atime, $self->{_userid}, $token, $atime);
1456
  }
1457
  else {
1458
    $rows = $self->{_dbh}->do($sql, undef, $atime, $self->{_userid}, $token, $atime);
1459
  }
1038
1460
1039
  unless (defined($rows)) {
1461
  unless (defined($rows)) {
1040
    dbg("bayes: tok_touch: SQL Error: ".$self->{_dbh}->errstr());
1462
    if($self->{bayes_read_write_support}) {
1463
      dbg("bayes: tok_touch: SQL Error: ".$self->{_dbh_write}->errstr());
1464
    }
1465
    else {
1466
      dbg("bayes: tok_touch: SQL Error: ".$self->{_dbh}->errstr());
1467
    }
1468
1041
    return 0;
1469
    return 0;
1042
  }
1470
  }
1043
1471
Lines 1052-1061 sub tok_touch { Link Here
1052
           WHERE id = ?
1480
           WHERE id = ?
1053
             AND newest_token_age < ?";
1481
             AND newest_token_age < ?";
1054
1482
1055
  $rows = $self->{_dbh}->do($sql, undef, $atime, $self->{_userid}, $atime);
1483
  if($self->{bayes_read_write_support}) {
1484
    $rows = $self->{_dbh_write}->do($sql, undef, $atime, $self->{_userid}, $atime);
1485
  }
1486
  else {
1487
    $rows = $self->{_dbh}->do($sql, undef, $atime, $self->{_userid}, $atime);
1488
  }
1056
1489
1057
  unless (defined($rows)) {
1490
  unless (defined($rows)) {
1058
    dbg("bayes: tok_touch: SQL Error: ".$self->{_dbh}->errstr());
1491
    if($self->{bayes_read_write_support}) {
1492
      dbg("bayes: tok_touch: SQL Error: ".$self->{_dbh_write}->errstr());
1493
    }
1494
    else {
1495
      dbg("bayes: tok_touch: SQL Error: ".$self->{_dbh}->errstr());
1496
    }
1059
    return 0;
1497
    return 0;
1060
  }
1498
  }
1061
1499
Lines 1081-1088 do in tok_get_all) Link Here
1081
1519
1082
sub tok_touch_all {
1520
sub tok_touch_all {
1083
  my ($self, $tokens, $atime) = @_;
1521
  my ($self, $tokens, $atime) = @_;
1522
  my $rows;
1084
1523
1085
  return 0 unless (defined($self->{_dbh}));
1524
  if($self->{bayes_read_write_support}) {
1525
    return 0 unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
1526
  }     
1527
  else {
1528
    return 0 unless (defined($self->{_dbh}));
1529
  }     
1086
1530
1087
  return 1 unless (scalar(@{$tokens}));
1531
  return 1 unless (scalar(@{$tokens}));
1088
1532
Lines 1098-1107 sub tok_touch_all { Link Here
1098
  $sql .= ") AND atime < ?";
1542
  $sql .= ") AND atime < ?";
1099
  push(@bindings, $atime);
1543
  push(@bindings, $atime);
1100
1544
1101
  my $rows = $self->{_dbh}->do($sql, undef, @bindings);
1545
  if($self->{bayes_read_write_support}) {
1546
    $rows = $self->{_dbh_write}->do($sql, undef, @bindings);
1547
  }
1548
  else {
1549
    $rows = $self->{_dbh}->do($sql, undef, @bindings);
1550
  }
1102
1551
1103
  unless (defined($rows)) {
1552
  unless (defined($rows)) {
1104
    dbg("bayes: tok_touch_all: SQL Error: ".$self->{_dbh}->errstr());
1553
    if($self->{bayes_read_write_support}) {
1554
      dbg("bayes: tok_touch_all: SQL Error: ".$self->{_dbh_write}->errstr());
1555
    }
1556
    else {
1557
      dbg("bayes: tok_touch_all: SQL Error: ".$self->{_dbh}->errstr());
1558
    }
1559
1105
    return 0;
1560
    return 0;
1106
  }
1561
  }
1107
1562
Lines 1116-1125 sub tok_touch_all { Link Here
1116
           WHERE id = ?
1571
           WHERE id = ?
1117
             AND newest_token_age < ?";
1572
             AND newest_token_age < ?";
1118
1573
1119
  $rows = $self->{_dbh}->do($sql, undef, $atime, $self->{_userid}, $atime);
1574
1575
  if($self->{bayes_read_write_support}) {
1576
    $rows = $self->{_dbh_write}->do($sql, undef, $atime, $self->{_userid}, $atime);
1577
  }
1578
  else {
1579
    $rows = $self->{_dbh}->do($sql, undef, $atime, $self->{_userid}, $atime);
1580
  }
1120
1581
1121
  unless (defined($rows)) {
1582
  unless (defined($rows)) {
1122
    dbg("bayes: tok_touch_all: SQL Error: ".$self->{_dbh}->errstr());
1583
    if($self->{bayes_read_write_support}) {
1584
      dbg("bayes: tok_touch_all: SQL Error: ".$self->{_dbh_write}->errstr());
1585
    }
1586
    else {
1587
      dbg("bayes: tok_touch_all: SQL Error: ".$self->{_dbh}->errstr());
1588
    }
1589
1123
    return 0;
1590
    return 0;
1124
  }
1591
  }
1125
1592
Lines 1138-1144 operation. Link Here
1138
1605
1139
sub cleanup {
1606
sub cleanup {
1140
  my ($self) = @_;
1607
  my ($self) = @_;
1141
1608
  my $toks_deleted;
1609
  my $rows;
1142
1610
1143
  return 1 unless ($self->{needs_cleanup});
1611
  return 1 unless ($self->{needs_cleanup});
1144
1612
Lines 1150-1159 sub cleanup { Link Here
1150
                AND spam_count = 0
1618
                AND spam_count = 0
1151
                AND ham_count = 0";
1619
                AND ham_count = 0";
1152
1620
1153
  my $toks_deleted = $self->{_dbh}->do($sql, undef, $self->{_userid});
1621
  if($self->{bayes_read_write_support}) {
1622
    $toks_deleted = $self->{_dbh_write}->do($sql, undef, $self->{_userid});
1623
  }
1624
  else {
1625
    $toks_deleted = $self->{_dbh}->do($sql, undef, $self->{_userid});
1626
  }
1154
1627
1155
  unless (defined($toks_deleted)) {
1628
  unless (defined($toks_deleted)) {
1156
    dbg("bayes: cleanup: SQL Error: ".$self->{_dbh}->errstr());
1629
    if($self->{bayes_read_write_support}) {
1630
      dbg("bayes: cleanup: SQL Error: ".$self->{_dbh_write}->errstr());
1631
    }
1632
    else {
1633
      dbg("bayes: cleanup: SQL Error: ".$self->{_dbh}->errstr());
1634
    }
1635
1157
    return 0;
1636
    return 0;
1158
  }       
1637
  }       
1159
1638
Lines 1163-1172 sub cleanup { Link Here
1163
  $sql = "UPDATE bayes_vars SET token_count = token_count - $toks_deleted
1642
  $sql = "UPDATE bayes_vars SET token_count = token_count - $toks_deleted
1164
           WHERE id = ?";
1643
           WHERE id = ?";
1165
1644
1166
  my $rows = $self->{_dbh}->do($sql, undef, $self->{_userid});
1645
  if($self->{bayes_read_write_support}) {
1646
    $rows = $self->{_dbh_write}->do($sql, undef, $self->{_userid});
1647
  }
1648
  else {
1649
    $rows = $self->{_dbh}->do($sql, undef, $self->{_userid});
1650
  }
1651
1167
1652
1168
  unless (defined($rows)) {
1653
  unless (defined($rows)) {
1169
    dbg("bayes: cleanup: SQL Error: ".$self->{_dbh}->errstr());
1654
    if($self->{bayes_read_write_support}) {
1655
      dbg("bayes: cleanup: SQL Error: ".$self->{_dbh_write}->errstr());
1656
    }
1657
    else {
1658
      dbg("bayes: cleanup: SQL Error: ".$self->{_dbh}->errstr());
1659
    }
1660
1170
    return 0;
1661
    return 0;
1171
  }       
1662
  }       
1172
1663
Lines 1236-1267 could causes the database to be inconsis Link Here
1236
1727
1237
sub clear_database {
1728
sub clear_database {
1238
  my ($self) = @_;
1729
  my ($self) = @_;
1730
  my $rows;
1239
1731
1240
  $self->tie_db_writable();
1732
  $self->tie_db_writable();
1241
1733
1242
  return 0 unless (defined($self->{_dbh}));
1734
  if($self->{bayes_read_write_support}) {
1735
    return 0 unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
1736
  }     
1737
  else {
1738
    return 0 unless (defined($self->{_dbh}));
1739
  }     
1740
1741
  if($self->{bayes_read_write_support}) {
1742
    $rows = $self->{_dbh_write}->do("DELETE FROM bayes_vars WHERE id = ?", undef, $self->{_userid});
1743
  }
1744
  else {
1745
    $rows = $self->{_dbh}->do("DELETE FROM bayes_vars WHERE id = ?", undef, $self->{_userid});
1746
  }
1243
1747
1244
  my $rows = $self->{_dbh}->do("DELETE FROM bayes_vars WHERE id = ?",
1245
			       undef,
1246
			       $self->{_userid});
1247
  unless (defined($rows)) {
1748
  unless (defined($rows)) {
1248
    dbg("SQL Error removing user (bayes_vars) data: ".$self->{_dbh}->errstr());
1749
    if($self->{bayes_read_write_support}) {
1750
      dbg("SQL Error removing user (bayes_vars) data: ".$self->{_dbh_write}->errstr());
1751
    }
1752
    else {
1753
      dbg("SQL Error removing user (bayes_vars) data: ".$self->{_dbh}->errstr());
1754
    }
1755
1249
    return 0;
1756
    return 0;
1250
  }
1757
  }
1251
1758
1252
  $rows = $self->{_dbh}->do("DELETE FROM bayes_seen WHERE id = ?",
1759
  if($self->{bayes_read_write_support}) {
1253
			    undef,
1760
    $rows = $self->{_dbh_write }->do("DELETE FROM bayes_seen WHERE id = ?", undef, $self->{_userid});
1254
			    $self->{_userid});
1761
  }
1762
  else {
1763
    $rows = $self->{_dbh}->do("DELETE FROM bayes_seen WHERE id = ?", undef, $self->{_userid});
1764
  }
1765
1766
1255
  unless (defined($rows)) {
1767
  unless (defined($rows)) {
1256
    dbg("SQL Error removing seen data: ".$self->{_dbh}->errstr());
1768
    if($self->{bayes_read_write_support}) {
1769
      dbg("SQL Error removing seen data: ".$self->{_dbh_write}->errstr());
1770
    }
1771
    else {
1772
      dbg("SQL Error removing seen data: ".$self->{_dbh}->errstr());
1773
    }
1774
1257
    return 0;
1775
    return 0;
1258
  }
1776
  }
1259
1777
1260
  $rows = $self->{_dbh}->do("DELETE FROM bayes_token WHERE id = ?",
1778
1261
			    undef,
1779
    if($self->{bayes_read_write_support}) {
1262
			    $self->{_userid});
1780
      $rows = $self->{_dbh_write}->do("DELETE FROM bayes_token WHERE id = ?", undef, $self->{_userid});
1781
    }
1782
    else {
1783
      $rows = $self->{_dbh}->do("DELETE FROM bayes_token WHERE id = ?", undef, $self->{_userid});
1784
    }
1785
1263
  unless (defined($rows)) {
1786
  unless (defined($rows)) {
1264
    dbg("SQL Error removing token data: ".$self->{_dbh}->errstr());
1787
    if($self->{bayes_read_write_support}) {
1788
      dbg("SQL Error removing token data: ".$self->{_dbh_write}->errstr());
1789
    }
1790
    else {
1791
      dbg("SQL Error removing token data: ".$self->{_dbh}->errstr());
1792
    }
1265
    return 0;
1793
    return 0;
1266
  }
1794
  }
1267
1795
Lines 1279-1288 This method will dump the users database Link Here
1279
1807
1280
sub backup_database {
1808
sub backup_database {
1281
  my ($self) = @_;
1809
  my ($self) = @_;
1810
  my $sth;
1282
1811
1283
  return 0 unless ($self->tie_db_readonly());
1812
  return 0 unless ($self->tie_db_readonly());
1284
1813
1285
  return 0 unless (defined($self->{_dbh}));
1814
  if($self->{bayes_read_write_support}) {
1815
    return 0 unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
1816
  }     
1817
  else {
1818
    return 0 unless (defined($self->{_dbh}));
1819
  }     
1286
1820
1287
  my @vars = $self->get_storage_variables();
1821
  my @vars = $self->get_storage_variables();
1288
1822
Lines 1302-1318 sub backup_database { Link Here
1302
                    FROM bayes_seen
1836
                    FROM bayes_seen
1303
                   WHERE id = ?";
1837
                   WHERE id = ?";
1304
1838
1305
  my $sth = $self->{_dbh}->prepare($token_sql);
1839
  if($self->{bayes_read_write_support}) {
1840
    $sth = $self->{_dbh_write}->prepare($token_sql);
1841
  }
1842
  else {
1843
    $sth = $self->{_dbh}->prepare($token_sql);
1844
  }
1306
1845
1307
  unless (defined ($sth)) {
1846
  unless (defined ($sth)) {
1308
    dbg("bayes: backup_database: SQL Error: ".$self->{_dbh}->errstr());
1847
    if($self->{bayes_read_write_support}) {
1848
      dbg("bayes: backup_database: SQL Error: ".$self->{_dbh_read}->errstr());
1849
    }
1850
    else {
1851
      dbg("bayes: backup_database: SQL Error: ".$self->{_dbh}->errstr());
1852
    } 
1853
1309
    return 0;
1854
    return 0;
1310
  }
1855
  }
1311
1856
1312
  my $rc = $sth->execute($self->{_userid});
1857
  my $rc = $sth->execute($self->{_userid});
1313
1858
1314
  unless ($rc) {
1859
  unless ($rc) {
1315
    dbg("bayes: backup_database: SQL Error: ".$self->{_dbh}->errstr());
1860
    if($self->{bayes_read_write_support}) {
1861
      dbg("bayes: backup_database: SQL Error: ".$self->{_dbh_read}->errstr());
1862
    }
1863
    else {
1864
      dbg("bayes: backup_database: SQL Error: ".$self->{_dbh}->errstr());
1865
    } 
1866
1316
    return 0;
1867
    return 0;
1317
  }
1868
  }
1318
1869
Lines 1323-1339 sub backup_database { Link Here
1323
1874
1324
  $sth->finish();
1875
  $sth->finish();
1325
1876
1326
  $sth = $self->{_dbh}->prepare($seen_sql);
1877
    if($self->{bayes_read_write_support}) {
1878
      $sth = $self->{_dbh_read}->prepare($seen_sql);
1879
    }
1880
    else {
1881
      $sth = $self->{_dbh}->prepare($seen_sql);
1882
    }
1327
1883
1328
  unless (defined ($sth)) {
1884
  unless (defined ($sth)) {
1329
    dbg("bayes: backup_database: SQL Error: ".$self->{_dbh}->errstr());
1885
    if($self->{bayes_read_write_support}) {
1886
      dbg("bayes: backup_database: SQL Error: ".$self->{_dbh_read}->errstr());
1887
    }
1888
    else {
1889
      dbg("bayes: backup_database: SQL Error: ".$self->{_dbh}->errstr());
1890
    } 
1891
1330
    return 0;
1892
    return 0;
1331
  }
1893
  }
1332
1894
1333
  $rc = $sth->execute($self->{_userid});
1895
  $rc = $sth->execute($self->{_userid});
1334
1896
1335
  unless ($rc) {
1897
  unless ($rc) {
1336
    dbg("bayes: backup_database: SQL Error: ".$self->{_dbh}->errstr());
1898
    if($self->{bayes_read_write_support}) {
1899
      dbg("bayes: backup_database: SQL Error: ".$self->{_dbh_read}->errstr());
1900
    }
1901
    else {
1902
      dbg("bayes: backup_database: SQL Error: ".$self->{_dbh}->errstr());
1903
    } 
1904
1337
    return 0;
1905
    return 0;
1338
  }
1906
  }
1339
1907
Lines 1370-1377 sub restore_database { Link Here
1370
1938
1371
  return 0 unless ($self->tie_db_writable());
1939
  return 0 unless ($self->tie_db_writable());
1372
1940
1373
  return 0 unless (defined($self->{_dbh}));
1941
  if($self->{bayes_read_write_support}) {
1374
1942
    return 0 unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
1943
  }     
1944
  else {
1945
    return 0 unless (defined($self->{_dbh}));
1946
  }     
1947
 
1375
  # This is the critical phase (moving sql around), so don't allow it
1948
  # This is the critical phase (moving sql around), so don't allow it
1376
  # to be interrupted.
1949
  # to be interrupted.
1377
  local $SIG{'INT'} = 'IGNORE';
1950
  local $SIG{'INT'} = 'IGNORE';
Lines 1578-1601 tables. Link Here
1578
2151
1579
sub _get_db_version {
2152
sub _get_db_version {
1580
  my ($self) = @_;
2153
  my ($self) = @_;
2154
  my $sth;
1581
2155
1582
  return 0 unless (defined($self->{_dbh}));
2156
  if($self->{bayes_read_write_support}) {
2157
    return 0 unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
2158
  }     
2159
  else {
2160
    return 0 unless (defined($self->{_dbh}));
2161
  }     
1583
2162
1584
  return ($self->{_db_version_cache}) if (defined($self->{_db_version_cache}));
2163
  return ($self->{_db_version_cache}) if (defined($self->{_db_version_cache}));
1585
2164
1586
  my $sql = "SELECT value FROM bayes_global_vars WHERE variable = 'VERSION'";
2165
  my $sql = "SELECT value FROM bayes_global_vars WHERE variable = 'VERSION'";
1587
2166
1588
  my $sth = $self->{_dbh}->prepare_cached($sql);
2167
  if($self->{bayes_read_write_support}) {
2168
    $sth = $self->{_dbh_read}->prepare_cached($sql);
2169
  }
2170
  else {
2171
    $sth = $self->{_dbh}->prepare_cached($sql);
2172
  }
1589
2173
1590
  unless (defined($sth)) {
2174
  unless (defined($sth)) {
1591
    dbg("bayes: _get_db_version: SQL Error: ".$self->{_dbh}->errstr());
2175
    if($self->{bayes_read_write_support}) {
2176
      dbg("bayes: _get_db_version: SQL Error: ".$self->{_dbh_read}->errstr());
2177
    }
2178
    else {
2179
      dbg("bayes: _get_db_version: SQL Error: ".$self->{_dbh}->errstr());
2180
    }
2181
1592
    return 0;
2182
    return 0;
1593
  }
2183
  }
1594
2184
1595
  my $rc = $sth->execute();
2185
  my $rc = $sth->execute();
1596
2186
1597
  unless ($rc) {
2187
  unless ($rc) {
1598
    dbg("bayes: _get_db_version: SQL Error: ".$self->{_dbh}->errstr());
2188
    if($self->{bayes_read_write_support}) {
2189
      dbg("bayes: _get_db_version: SQL Error: ".$self->{_dbh_read}->errstr());
2190
    }
2191
    else {
2192
      dbg("bayes: _get_db_version: SQL Error: ".$self->{_dbh}->errstr());
2193
    }
2194
1599
    return 0;
2195
    return 0;
1600
  }
2196
  }
1601
2197
Lines 1620-1643 initialized. If not then it will perform Link Here
1620
2216
1621
sub _initialize_db {
2217
sub _initialize_db {
1622
  my ($self) = @_;
2218
  my ($self) = @_;
2219
  my $sthselect;
2220
  my $rows;
1623
2221
1624
  return 0 unless (defined($self->{_dbh}));
2222
  if($self->{bayes_read_write_support}) {
2223
    return 0 unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
2224
  }     
2225
  else {
2226
    return 0 unless (defined($self->{_dbh}));
2227
  }     
1625
2228
1626
  return 0 if (!$self->{_username});
2229
  return 0 if (!$self->{_username});
1627
2230
1628
  my $sqlselect = "SELECT id FROM bayes_vars WHERE username = ?";
2231
  my $sqlselect = "SELECT id FROM bayes_vars WHERE username = ?";
1629
2232
1630
  my $sthselect = $self->{_dbh}->prepare_cached($sqlselect);
2233
  if($self->{bayes_read_write_support}) {
2234
    $sthselect = $self->{_dbh_read}->prepare_cached($sqlselect);
2235
  }
2236
  else {
2237
    $sthselect = $self->{_dbh}->prepare_cached($sqlselect);
2238
  }
1631
2239
1632
  unless (defined($sthselect)) {
2240
  unless (defined($sthselect)) {
1633
    dbg("bayes: _initialize_db: SQL Error: ".$self->{_dbh}->errstr());
2241
    if($self->{bayes_read_write_support}) {
2242
      dbg("bayes: _initialize_db: SQL Error: ".$self->{_dbh_read}->errstr());
2243
    }
2244
    else {
2245
      dbg("bayes: _initialize_db: SQL Error: ".$self->{_dbh}->errstr());
2246
    }
2247
1634
    return 0;
2248
    return 0;
1635
  }
2249
  }
1636
2250
1637
  my $rc = $sthselect->execute($self->{_username});
2251
  my $rc = $sthselect->execute($self->{_username});
1638
2252
1639
  unless ($rc) {
2253
  unless ($rc) {
1640
    dbg("bayes: _initialize_db: SQL Error: ".$self->{_dbh}->errstr());
2254
    if($self->{bayes_read_write_support}) {
2255
      dbg("bayes: _initialize_db: SQL Error: ".$self->{_dbh_read}->errstr());
2256
    }
2257
    else {
2258
      dbg("bayes: _initialize_db: SQL Error: ".$self->{_dbh}->errstr());
2259
    }
2260
1641
    return 0;
2261
    return 0;
1642
  }
2262
  }
1643
2263
Lines 1653-1663 sub _initialize_db { Link Here
1653
  # For now let the database setup the other variables as defaults
2273
  # For now let the database setup the other variables as defaults
1654
  my $sqlinsert = "INSERT INTO bayes_vars (username) VALUES (?)";
2274
  my $sqlinsert = "INSERT INTO bayes_vars (username) VALUES (?)";
1655
2275
1656
  my $rows = $self->{_dbh}->do($sqlinsert,
2276
  if($self->{bayes_read_write_support}) {
1657
			       undef,
2277
    $rows = $self->{_dbh_write}->do($sqlinsert, undef, $self->{_username});
1658
			       $self->{_username});
2278
  }
2279
  else {
2280
    $rows = $self->{_dbh}->do($sqlinsert, undef, $self->{_username});
2281
  } 
2282
1659
  unless (defined($rows)) {
2283
  unless (defined($rows)) {
1660
    dbg("bayes: _initialize_db: SQL Error: ".$self->{_dbh}->errstr());
2284
    if($self->{bayes_read_write_support}) {
2285
      dbg("bayes: _initialize_db: SQL Error: ".$self->{_dbh_write}->errstr());
2286
    }
2287
    else {
2288
      dbg("bayes: _initialize_db: SQL Error: ".$self->{_dbh}->errstr());
2289
    }
2290
1661
    return 0;
2291
    return 0;
1662
  }
2292
  }
1663
2293
Lines 1668-1674 sub _initialize_db { Link Here
1668
  $rc = $sthselect->execute($self->{_username});
2298
  $rc = $sthselect->execute($self->{_username});
1669
2299
1670
  unless ($rc) {
2300
  unless ($rc) {
1671
    dbg("bayes: _initialize_db: SQL Error: ".$self->{_dbh}->errstr());
2301
    if($self->{bayes_read_write_support}) {
2302
      dbg("bayes: _initialize_db: SQL Error: ".$self->{_dbh_read}->errstr());
2303
    }
2304
    else {
2305
      dbg("bayes: _initialize_db: SQL Error: ".$self->{_dbh}->errstr());
2306
    }
2307
1672
    return 0;
2308
    return 0;
1673
  }
2309
  }
1674
2310
Lines 1700-1707 the database. Link Here
1700
2336
1701
sub _put_token {
2337
sub _put_token {
1702
  my ($self, $token, $spam_count, $ham_count, $atime) = @_;
2338
  my ($self, $token, $spam_count, $ham_count, $atime) = @_;
2339
  my $sth;
2340
  my $rows;
2341
1703
2342
1704
  return 0 unless (defined($self->{_dbh}));
2343
  if($self->{bayes_read_write_support}) {
2344
    return 0 unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
2345
  }     
2346
  else {
2347
    return 0 unless (defined($self->{_dbh}));
2348
  }     
1705
2349
1706
  $spam_count ||= 0;
2350
  $spam_count ||= 0;
1707
  $ham_count ||= 0;
2351
  $ham_count ||= 0;
Lines 1724-1733 sub _put_token { Link Here
1724
               (id, token, spam_count, ham_count, atime)
2368
               (id, token, spam_count, ham_count, atime)
1725
               VALUES (?,?,?,?,?)";
2369
               VALUES (?,?,?,?,?)";
1726
2370
1727
    my $sth = $self->{_dbh}->prepare_cached($sql);
2371
    if($self->{bayes_read_write_support}) {
2372
        $sth = $self->{_dbh_write}->prepare_cached($sql);
2373
    }
2374
    else {
2375
        $sth = $self->{_dbh}->prepare_cached($sql);
2376
    }
1728
2377
1729
    unless (defined($sth)) {
2378
    unless (defined($sth)) {
1730
      dbg("bayes: _put_token: SQL Error: ".$self->{_dbh}->errstr());
2379
      if($self->{bayes_read_write_support}) {
2380
        dbg("bayes: _put_token: SQL Error: ".$self->{_dbh_write}->errstr());
2381
      }
2382
      else {
2383
        dbg("bayes: _put_token: SQL Error: ".$self->{_dbh}->errstr());
2384
      }
2385
1731
      return 0;
2386
      return 0;
1732
    }
2387
    }
1733
2388
Lines 1738-1744 sub _put_token { Link Here
1738
			   $atime);
2393
			   $atime);
1739
    
2394
    
1740
    unless ($rc) {
2395
    unless ($rc) {
1741
      dbg("bayes: _put_token: SQL Error: ".$self->{_dbh}->errstr());
2396
      if($self->{bayes_read_write_support}) {
2397
        dbg("bayes: _put_token: SQL Error: ".$self->{_dbh_write}->errstr());
2398
      }
2399
      else {
2400
        dbg("bayes: _put_token: SQL Error: ".$self->{_dbh}->errstr());
2401
      }
2402
1742
      return 0;
2403
      return 0;
1743
    }
2404
    }
1744
2405
Lines 1747-1766 sub _put_token { Link Here
1747
    $sql = "UPDATE bayes_vars SET token_count = token_count + 1
2408
    $sql = "UPDATE bayes_vars SET token_count = token_count + 1
1748
             WHERE id = ?";
2409
             WHERE id = ?";
1749
2410
1750
    my $rows = $self->{_dbh}->do($sql, undef, $self->{_userid});
2411
    if($self->{bayes_read_write_support}) {
2412
      $rows = $self->{_dbh_write}->do($sql, undef, $self->{_userid});
2413
    }
2414
    else {
2415
      $rows = $self->{_dbh}->do($sql, undef, $self->{_userid});
2416
    }
1751
    
2417
    
1752
    unless (defined($rows)) {
2418
    unless (defined($rows)) {
1753
      dbg("bayes: _put_token: SQL Error: ".$self->{_dbh}->errstr());
2419
      if($self->{bayes_read_write_support}) {
2420
        dbg("bayes: _put_token: SQL Error: ".$self->{_dbh_write}->errstr());
2421
      }
2422
      else {
2423
        dbg("bayes: _put_token: SQL Error: ".$self->{_dbh}->errstr());
2424
      }
2425
     
1754
      return 0;
2426
      return 0;
1755
    }
2427
    }
1756
2428
1757
    $sql = "UPDATE bayes_vars SET newest_token_age = ?
2429
    $sql = "UPDATE bayes_vars SET newest_token_age = ?
1758
             WHERE id = ? AND newest_token_age < ?";
2430
             WHERE id = ? AND newest_token_age < ?";
1759
2431
1760
    $rows = $self->{_dbh}->do($sql, undef, $atime, $self->{_userid}, $atime);
2432
    if($self->{bayes_read_write_support}) {
2433
      $rows = $self->{_dbh_write}->do($sql, undef, $atime, $self->{_userid}, $atime);
2434
    }
2435
    else {
2436
      $rows = $self->{_dbh}->do($sql, undef, $atime, $self->{_userid}, $atime);
2437
    }
1761
2438
1762
    unless (defined($rows)) {
2439
    unless (defined($rows)) {
1763
      dbg("bayes: _put_token: SQL Error: ".$self->{_dbh}->errstr());
2440
      if($self->{bayes_read_write_support}) {
2441
        dbg("bayes: _put_token: SQL Error: ".$self->{_dbh_write}->errstr());
2442
      }
2443
      else {
2444
        dbg("bayes: _put_token: SQL Error: ".$self->{_dbh}->errstr());
2445
      }
2446
1764
      return 0;
2447
      return 0;
1765
    }
2448
    }
1766
2449
Lines 1770-1779 sub _put_token { Link Here
1770
      $sql = "UPDATE bayes_vars SET oldest_token_age = ?
2453
      $sql = "UPDATE bayes_vars SET oldest_token_age = ?
1771
               WHERE id = ? AND oldest_token_age > ?";
2454
               WHERE id = ? AND oldest_token_age > ?";
1772
2455
1773
      $rows = $self->{_dbh}->do($sql, undef, $atime, $self->{_userid}, $atime);
2456
       if($self->{bayes_read_write_support}) {
2457
         $rows = $self->{_dbh_write}->do($sql, undef, $atime, $self->{_userid}, $atime);
2458
       }
2459
       else { 
2460
         $rows = $self->{_dbh}->do($sql, undef, $atime, $self->{_userid}, $atime);
2461
       }
1774
      
2462
      
1775
      unless (defined($rows)) {
2463
      unless (defined($rows)) {
1776
	dbg("bayes: _put_token: SQL Error: ".$self->{_dbh}->errstr());
2464
        if($self->{bayes_read_write_support}) {
2465
          dbg("bayes: _put_token: SQL Error: ".$self->{_dbh_write}->errstr());
2466
        }
2467
        else {
2468
          dbg("bayes: _put_token: SQL Error: ".$self->{_dbh}->errstr());
2469
        }
2470
1777
	return 0;
2471
	return 0;
1778
      }
2472
      }
1779
    }
2473
    }
Lines 1824-1834 sub _put_token { Link Here
1824
                   AND spam_count + ? >= 0";
2518
                   AND spam_count + ? >= 0";
1825
	@args = ($spam_count, $self->{_userid}, $token, $spam_count);
2519
	@args = ($spam_count, $self->{_userid}, $token, $spam_count);
1826
      }
2520
      }
1827
2521
     
1828
      my $rows = $self->{_dbh}->do($sql, undef, @args);
2522
     if($self->{bayes_read_write_support}) {
2523
        $rows = $self->{_dbh_write}->do($sql, undef, @args);
2524
      }     
2525
      else {
2526
        $rows = $self->{_dbh}->do($sql, undef, @args);
2527
      }
1829
2528
1830
      unless (defined($rows)) {
2529
      unless (defined($rows)) {
1831
	dbg("bayes: _put_token: SQL Error: ".$self->{_dbh}->errstr());
2530
        if($self->{bayes_read_write_support}) {
2531
          dbg("bayes: _put_token: SQL Error: ".$self->{_dbh_write}->errstr());
2532
        }
2533
        else {
2534
          dbg("bayes: _put_token: SQL Error: ".$self->{_dbh}->errstr());
2535
        }
2536
1832
	return 0;
2537
	return 0;
1833
      }
2538
      }
1834
    }
2539
    }
Lines 1854-1864 sub _put_token { Link Here
1854
                   AND ham_count + ? >= 0";
2559
                   AND ham_count + ? >= 0";
1855
	@args = ($ham_count, $self->{_userid}, $token, $ham_count);
2560
	@args = ($ham_count, $self->{_userid}, $token, $ham_count);
1856
      }
2561
      }
1857
2562
   
1858
      my $rows = $self->{_dbh}->do($sql, undef, @args);
2563
      if($self->{bayes_read_write_support}) {
2564
        $rows = $self->{_dbh_write}->do($sql, undef, @args);
2565
      }
2566
      else {
2567
        $rows = $self->{_dbh}->do($sql, undef, @args);
2568
      }
1859
2569
1860
      unless (defined($rows)) {
2570
      unless (defined($rows)) {
1861
	dbg("bayes: _put_token: SQL Error: ".$self->{_dbh}->errstr());
2571
        if($self->{bayes_read_write_support}) {
2572
          dbg("bayes: _put_token: SQL Error: ".$self->{_dbh_write}->errstr());
2573
        }
2574
        else {
2575
          dbg("bayes: _put_token: SQL Error: ".$self->{_dbh}->errstr());
2576
        }
2577
1862
	return 0;
2578
	return 0;
1863
      }
2579
      }
1864
    }
2580
    }
Lines 1869-1879 sub _put_token { Link Here
1869
      # only updated the atime if it was > the previous value
2585
      # only updated the atime if it was > the previous value
1870
      my $sql = "UPDATE bayes_vars SET newest_token_age = ?
2586
      my $sql = "UPDATE bayes_vars SET newest_token_age = ?
1871
                  WHERE id = ? AND newest_token_age < ?";
2587
                  WHERE id = ? AND newest_token_age < ?";
2588
      
2589
      if($self->{bayes_read_write_support}) { 	
2590
        $rows = $self->{_dbh_write}->do($sql, undef, $atime, $self->{_userid}, $atime);
2591
      }
2592
      else {
2593
        $rows = $self->{_dbh}->do($sql, undef, $atime, $self->{_userid}, $atime);
2594
      }
1872
2595
1873
      my $rows = $self->{_dbh}->do($sql, undef, $atime, $self->{_userid}, $atime);
1874
2596
1875
      unless (defined($rows)) {
2597
      unless (defined($rows)) {
1876
	dbg("bayes: _put_token: SQL Error: ".$self->{_dbh}->errstr());
2598
        if($self->{bayes_read_write_support}) {
2599
          dbg("bayes: _put_token: SQL Error: ".$self->{_dbh_write}->errstr());
2600
        }
2601
        else {
2602
          dbg("bayes: _put_token: SQL Error: ".$self->{_dbh}->errstr());
2603
        }
2604
1877
	return 0;
2605
	return 0;
1878
      }
2606
      }
1879
    }
2607
    }
Lines 1897-1919 only be called at expire time. Link Here
1897
2625
1898
sub _get_oldest_token_age {
2626
sub _get_oldest_token_age {
1899
  my ($self) = @_;
2627
  my ($self) = @_;
2628
  my $sth;
1900
2629
1901
  return 0 unless (defined($self->{_dbh}));
2630
  if($self->{bayes_read_write_support}) {
2631
    return 0 unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
2632
  }     
2633
  else {
2634
    return 0 unless (defined($self->{_dbh}));
2635
  }     
1902
2636
1903
  my $sql = "SELECT min(atime) FROM bayes_token
2637
  my $sql = "SELECT min(atime) FROM bayes_token
1904
              WHERE id = ?";
2638
              WHERE id = ?";
1905
2639
1906
  my $sth = $self->{_dbh}->prepare_cached($sql);
2640
  if($self->{bayes_read_write_support}) {
2641
    $sth = $self->{_dbh_read}->prepare_cached($sql);
2642
  }
2643
  else {
2644
    $sth = $self->{_dbh}->prepare_cached($sql);
2645
  }
1907
2646
1908
  unless (defined($sth)) {
2647
  unless (defined($sth)) {
1909
    dbg("bayes: _get_oldest_token_age: SQL Error: ".$self->{_dbh}->errstr());
2648
    if($self->{bayes_read_write_support}) {
2649
      dbg("bayes: _get_oldest_token_age: SQL Error: ".$self->{_dbh_read}->errstr());
2650
    }
2651
    else {
2652
      dbg("bayes: _get_oldest_token_age: SQL Error: ".$self->{_dbh}->errstr());
2653
    }
2654
1910
    return 0;
2655
    return 0;
1911
  }
2656
  }
1912
2657
1913
  my $rc = $sth->execute($self->{_userid});
2658
  my $rc = $sth->execute($self->{_userid});
1914
2659
1915
  unless ($rc) {
2660
  unless ($rc) {
1916
    dbg("bayes: _get_oldest_token_age: SQL Error: ".$self->{_dbh}->errstr());
2661
    if($self->{bayes_read_write_support}) {
2662
      dbg("bayes: _get_oldest_token_age: SQL Error: ".$self->{_dbh_read}->errstr());
2663
    }
2664
    else {
2665
      dbg("bayes: _get_oldest_token_age: SQL Error: ".$self->{_dbh}->errstr());
2666
    }
2667
1917
    return 0;
2668
    return 0;
1918
  }
2669
  }
1919
2670
Lines 1924-1930 sub _get_oldest_token_age { Link Here
1924
  return $atime;
2675
  return $atime;
1925
}
2676
}
1926
2677
1927
1928
=head2 _get_num_hapaxes
2678
=head2 _get_num_hapaxes
1929
2679
1930
private instance (Integer) _get_num_hapaxes ()
2680
private instance (Integer) _get_num_hapaxes ()
Lines 1937-1961 the token database for a user. Link Here
1937
2687
1938
sub _get_num_hapaxes {
2688
sub _get_num_hapaxes {
1939
  my ($self) = @_;
2689
  my ($self) = @_;
2690
  my $sth;
1940
2691
1941
  return 0 unless (defined($self->{_dbh}));
2692
  if($self->{bayes_read_write_support}) {
1942
2693
    return 0 unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
2694
  }     
2695
  else {
2696
    return 0 unless (defined($self->{_dbh}));
2697
  }     
2698
 
1943
  my $sql = "SELECT count(*)
2699
  my $sql = "SELECT count(*)
1944
               FROM bayes_token
2700
               FROM bayes_token
1945
              WHERE id = ?
2701
              WHERE id = ?
1946
                AND spam_count + ham_count = 1";
2702
                AND spam_count + ham_count = 1";
1947
2703
1948
  my $sth = $self->{_dbh}->prepare_cached($sql);
2704
   if($self->{bayes_read_write_support}) {
2705
     $sth = $self->{_dbh_read}->prepare_cached($sql);
2706
   }
2707
   else {
2708
     $sth = $self->{_dbh}->prepare_cached($sql);
2709
   }
1949
2710
1950
  unless (defined($sth)) {
2711
  unless (defined($sth)) {
1951
    dbg("bayes: _get_num_hapaxes: SQL Error: ".$self->{_dbh}->errstr());
2712
    if($self->{bayes_read_write_support}) {
2713
      dbg("bayes: _get_num_hapaxes: SQL Error: ".$self->{_dbh_read}->errstr());
2714
    }
2715
    else {
2716
      dbg("bayes: _get_num_hapaxes: SQL Error: ".$self->{_dbh}->errstr());
2717
    }
2718
1952
    return 0;
2719
    return 0;
1953
  }
2720
  }
1954
2721
1955
  my $rc = $sth->execute($self->{_userid});
2722
  my $rc = $sth->execute($self->{_userid});
1956
2723
1957
  unless ($rc) {
2724
  unless ($rc) {
1958
    dbg("bayes: _get_num_hapaxes: SQL Error: ".$self->{_dbh}->errstr());
2725
    if($self->{bayes_read_write_support}) {
2726
      dbg("bayes: _get_num_hapaxes: SQL Error: ".$self->{_dbh_read}->errstr());
2727
    }
2728
    else {
2729
      dbg("bayes: _get_num_hapaxes: SQL Error: ".$self->{_dbh}->errstr());
2730
    }
2731
1959
    return 0;
2732
    return 0;
1960
  }
2733
  }
1961
2734
Lines 1979-1986 ham_count < 8) in the token database for Link Here
1979
2752
1980
sub _get_num_lowfreq {
2753
sub _get_num_lowfreq {
1981
  my ($self) = @_;
2754
  my ($self) = @_;
2755
  my $sth;
1982
2756
1983
  return 0 unless (defined($self->{_dbh}));
2757
  if($self->{bayes_read_write_support}) {
2758
    return 0 unless ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
2759
  }     
2760
  else {
2761
    return 0 unless (defined($self->{_dbh}));
2762
  }     
1984
2763
1985
  my $sql = "SELECT count(*)
2764
  my $sql = "SELECT count(*)
1986
               FROM bayes_token
2765
               FROM bayes_token
Lines 1989-2005 sub _get_num_lowfreq { Link Here
1989
                AND (ham_count >= 0 AND ham_count < 8)
2768
                AND (ham_count >= 0 AND ham_count < 8)
1990
                AND spam_count + ham_count != 1";
2769
                AND spam_count + ham_count != 1";
1991
2770
1992
  my $sth = $self->{_dbh}->prepare_cached($sql);
2771
  if($self->{bayes_read_write_support}) {
2772
    $sth = $self->{_dbh_read}->prepare_cached($sql);
2773
  }
2774
  else {
2775
    $sth = $self->{_dbh}->prepare_cached($sql);
2776
  }
1993
2777
1994
  unless (defined($sth)) {
2778
  unless (defined($sth)) {
1995
    dbg("bayes: _get_num_lowfreq: SQL Error: ".$self->{_dbh}->errstr());
2779
    if($self->{bayes_read_write_support}) {
2780
      dbg("bayes: _get_num_lowfreq: SQL Error: ".$self->{_dbh_read}->errstr());
2781
    }
2782
    else {
2783
      dbg("bayes: _get_num_lowfreq: SQL Error: ".$self->{_dbh}->errstr());
2784
    }
2785
1996
    return 0;
2786
    return 0;
1997
  }
2787
  }
1998
2788
1999
  my $rc = $sth->execute($self->{_userid});
2789
  my $rc = $sth->execute($self->{_userid});
2000
2790
2001
  unless ($rc) {
2791
  unless ($rc) {
2002
    dbg("bayes: _get_num_lowfreq: SQL Error: ".$self->{_dbh}->errstr());
2792
    if($self->{bayes_read_write_support}) {
2793
      dbg("bayes: _get_num_lowfreq: SQL Error: ".$self->{_dbh_read}->errstr());
2794
    }
2795
    else {
2796
      dbg("bayes: _get_num_lowfreq: SQL Error: ".$self->{_dbh}->errstr());
2797
    }
2798
2003
    return 0;
2799
    return 0;
2004
  }
2800
  }
2005
2801
Lines 2014-2020 sub db_readable { Link Here
2014
  my($self) = @_;
2810
  my($self) = @_;
2015
2811
2016
  # if there's a database handle, we can read...
2812
  # if there's a database handle, we can read...
2017
  return defined $self->{_dbh};
2813
  if($self->{bayes_read_write_support}) {
2814
    return ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
2815
  }     
2816
  else {
2817
    return defined($self->{_dbh});
2818
  }     
2819
2018
}
2820
}
2019
2821
2020
sub db_writable {
2822
sub db_writable {
Lines 2022-2028 sub db_writable { Link Here
2022
2824
2023
  # since in the SA SQL code, there is no difference between R/O and
2825
  # since in the SA SQL code, there is no difference between R/O and
2024
  # R/W access, we just care if we have DB access.
2826
  # R/W access, we just care if we have DB access.
2025
  return defined $self->{_dbh};
2827
  if($self->{bayes_read_write_support}) {
2828
    return ( defined($self->{_dbh_read}) && defined($self->{_dbh_write}) );
2829
  }
2830
  else {
2831
    return defined($self->{_dbh});
2832
  }
2026
}
2833
}
2027
2834
2028
sub dbg { Mail::SpamAssassin::dbg (@_); }
2835
sub dbg { Mail::SpamAssassin::dbg (@_); }
(-)/home/as/old/SpamAssassin/Conf.pm (-1 / +134 lines)
Lines 2778-2783 specification (see Mail::SpamAssassin::B Link Here
2778
2778
2779
=item bayes_sql_dsn DBI::databasetype:databasename:hostname:port
2779
=item bayes_sql_dsn DBI::databasetype:databasename:hostname:port
2780
2780
2781
To use 2 different Databases (for example : You have mysql replication. Than you can write to the master and read from the slave
2782
2783
in general localhost.) you can enable bayes_read_write_support.
2784
2785
New Bayes Vars here :
2786
2787
bayes_read_write_support BOOL
2788
2789
bayes_sql_dsn_read
2790
2791
bayes_sql_username_read
2792
2793
bayes_sql_password_read
2794
2795
bayes_sql_dsn_write
2796
2797
bayes_sql_username_write
2798
2799
bayes_sql_password_write
2800
2801
=cut
2802
2803
  push (@cmds, {
2804
    setting => 'bayes_read_write_support',
2805
    is_admin => 1,
2806
    default => 0,
2807
    type => $CONF_TYPE_BOOL
2808
  });
2809
2810
  push (@cmds, {
2811
    setting => 'bayes_sql_dsn_read',
2812
    is_admin => 1,
2813
    default => '',
2814
    type => $CONF_TYPE_STRING
2815
  });
2816
2817
  push (@cmds, {
2818
    setting => 'bayes_sql_username_read',
2819
    is_admin => 1,
2820
    default => '',
2821
    type => $CONF_TYPE_STRING
2822
  });
2823
2824
  push (@cmds, {
2825
    setting => 'bayes_sql_password_read',
2826
    is_admin => 1,
2827
    default => '',
2828
    type => $CONF_TYPE_STRING
2829
  });
2830
2831
  push (@cmds, {
2832
    setting => 'bayes_sql_dsn_write',
2833
    is_admin => 1,
2834
    default => '',
2835
    type => $CONF_TYPE_STRING
2836
  });
2837
2838
  push (@cmds, {
2839
    setting => 'bayes_sql_username_write',
2840
    is_admin => 1,
2841
    default => '',
2842
    type => $CONF_TYPE_STRING
2843
  });
2844
2845
  push (@cmds, {
2846
    setting => 'bayes_sql_password_write',
2847
    is_admin => 1,
2848
    default => '',
2849
    type => $CONF_TYPE_STRING
2850
  });
2851
2852
2853
=item bayes_sql_dsn DBI::databasetype:databasename:hostname:port
2854
2781
Used for BayesStore::SQL storage implementation.
2855
Used for BayesStore::SQL storage implementation.
2782
2856
2783
This option give the connect string used to connect to the SQL based Bayes storage.
2857
This option give the connect string used to connect to the SQL based Bayes storage.
Lines 2876-2882 The password for the database username, Link Here
2876
=item user_scores_sql_custom_query query
2950
=item user_scores_sql_custom_query query
2877
2951
2878
This option gives you the ability to create a custom SQL query to
2952
This option gives you the ability to create a custom SQL query to
2879
retrieve user scores and preferences.  In order to work correctly your
2953
Retrieve user scores and preferences.  In order to work correctly your
2880
query should return two values, the preference name and value, in that
2954
query should return two values, the preference name and value, in that
2881
order.  In addition, there are several "variables" that you can use
2955
order.  In addition, there are several "variables" that you can use
2882
as part of your query, these variables will be substituted for the
2956
as part of your query, these variables will be substituted for the
Lines 2982-2987 The table user auto-whitelists are store Link Here
2982
    setting => 'user_awl_sql_table',
3056
    setting => 'user_awl_sql_table',
2983
    is_admin => 1,
3057
    is_admin => 1,
2984
    default => 'awl',
3058
    default => 'awl',
3059
    type => $CONF_TYPE_STRING
3060
  });
3061
3062
=item user_awl_sql_read_write_support 
3063
3064
This patch make it possible to read from one host and write to another.
3065
Very usefull for mysql replication.
3066
3067
if user_awl_sql_read_write_support is true, you have following extra options :
3068
3069
user_awl_dsn_read
3070
user_awl_sql_username_read (optional) 
3071
user_awl_sql_password_read (optional)
3072
user_awl_dsn_write 
3073
user_awl_sql_username_write (optional)
3074
user_awl_sql_password_write (optional)
3075
3076
=cut
3077
3078
  push (@cmds, {
3079
    setting => 'user_awl_read_write_support',
3080
    is_admin => 1,
3081
    default => 0,
3082
    type => $CONF_TYPE_BOOL
3083
  });
3084
3085
  push (@cmds, {
3086
    setting => 'user_awl_dsn_read',
3087
    is_admin => 1,
3088
    type => $CONF_TYPE_STRING
3089
  });
3090
3091
  push (@cmds, {
3092
    setting => 'user_awl_sql_username_read',
3093
    is_admin => 1,
3094
    type => $CONF_TYPE_STRING
3095
  });
3096
3097
  push (@cmds, {
3098
    setting => 'user_awl_sql_password_read',
3099
    is_admin => 1,
3100
    type => $CONF_TYPE_STRING
3101
  });
3102
3103
  push (@cmds, {
3104
    setting => 'user_awl_dsn_write',
3105
    is_admin => 1,
3106
    type => $CONF_TYPE_STRING
3107
  });
3108
3109
  push (@cmds, {
3110
    setting => 'user_awl_sql_username_write',
3111
    is_admin => 1,
3112
    type => $CONF_TYPE_STRING
3113
  });
3114
3115
  push (@cmds, {
3116
    setting => 'user_awl_sql_password_write',
3117
    is_admin => 1,
2985
    type => $CONF_TYPE_STRING
3118
    type => $CONF_TYPE_STRING
2986
  });
3119
  });
2987
3120
(-)/home/as/old/SpamAssassin/SQLBasedAddrList.pm (-23 / +155 lines)
Lines 51-56 CREATE TABLE awl ( Link Here
51
51
52
You're table definition may change depending on which database driver
52
You're table definition may change depending on which database driver
53
you choose.  There is a config option to override the table name.
53
you choose.  There is a config option to override the table name.
54
The username and passwords for read /write support are optional.
55
If they are not set user_awl_sql_username and user_awl_sql_password is used. 
54
56
55
This module introduces several new config variables:
57
This module introduces several new config variables:
56
58
Lines 62-67 user_awl_sql_password Link Here
62
64
63
user_awl_sql_table
65
user_awl_sql_table
64
66
67
user_awl_read_write_support
68
69
user_awl_dsn_read
70
71
user_awl_sql_username_read (optional)
72
73
user_awl_sql_password_read (optional)
74
75
user_awl_dsn_write
76
77
user_awl_sql_username_write (optional)
78
79
user_awl_sql_password_write (optional)
80
65
see C<Mail::SpamAssassin::Conf> for more information.
81
see C<Mail::SpamAssassin::Conf> for more information.
66
82
67
83
Lines 116-145 sub new_checker { Link Here
116
132
117
  my $class = $self->{class};
133
  my $class = $self->{class};
118
134
119
  if (!$main->{conf}->{user_awl_dsn} ||
135
  #checks if user_awl_read_write_support is enabled
120
      !$main->{conf}->{user_awl_sql_table}) {
136
  my $user_awl_read_write_support=$main->{conf}->{user_awl_read_write_support};
121
    dbg("auto-whitelist (sql-based): invalid config");
137
138
  if ($user_awl_read_write_support) {
139
    if (!$main->{conf}->{user_awl_dsn_read} || !$main->{conf}->{user_awl_dsn_write} || !$main->{conf}->{user_awl_sql_table} || $main->{conf}->{user_awl_dsn}) {
140
    dbg("auto-whitelist (sql-based): invalid config 1");
122
    return undef;
141
    return undef;
142
    }
143
  }
144
  else {
145
    if (!$main->{conf}->{user_awl_dsn} ||
146
        !$main->{conf}->{user_awl_sql_table}) {
147
      dbg("auto-whitelist (sql-based): invalid config 2");
148
      return undef;
149
    }
123
  }
150
  }
124
151
152
  #original
125
  my $dsn    = $main->{conf}->{user_awl_dsn};
153
  my $dsn    = $main->{conf}->{user_awl_dsn};
126
  my $dbuser = $main->{conf}->{user_awl_sql_username};
154
  my $dbuser = $main->{conf}->{user_awl_sql_username};
127
  my $dbpass = $main->{conf}->{user_awl_sql_password};
155
  my $dbpass = $main->{conf}->{user_awl_sql_password};
128
156
129
  my $dbh = DBI->connect($dsn, $dbuser, $dbpass, {'PrintError' => 0});
157
  #new
158
  my $dsn_read    = $main->{conf}->{user_awl_dsn_read};
159
  my $dbuser_read = $main->{conf}->{user_awl_sql_username_read};
160
  my $dbpass_read = $main->{conf}->{user_awl_sql_password_read};
161
162
  my $dsn_write    = $main->{conf}->{user_awl_dsn_write};
163
  my $dbuser_write = $main->{conf}->{user_awl_sql_username_write};
164
  my $dbpass_write = $main->{conf}->{user_awl_sql_password_write};
165
166
  if( !$user_awl_read_write_support ) {
167
    my $dbh = DBI->connect($dsn, $dbuser, $dbpass, {'PrintError' => 0});
168
    if(!$dbh) {
169
      dbg("auto-whitelist (sql-based): Unable to Connect to DB");
170
      return undef;
171
    }
130
172
131
  if(!$dbh) {
173
    $self = { 'main'      => $main,
132
    dbg("auto-whitelist (sql-based): Unable to Connect to DB");
174
              'dsn'       => $dsn,
133
    return undef;
175
              'dbh'       => $dbh,
176
              'tablename' => $main->{conf}->{user_awl_sql_table},
177
            };
178
    dbg("SQL Based AWL: Connected to $dsn");
134
  }
179
  }
180
  else
181
  {
182
    #use user_awl_sql_password and username if only one pass for both DBs (read/write) is used 
183
    if($dbpass && $dbuser && !$dbuser_read && !$dbpass_read && !$dbuser_write && !$dbpass_write) {    
184
      $dbuser_read=$dbuser;
185
      $dbpass_read=$dbpass;
186
      $dbuser_write=$dbuser;
187
      $dbpass_write=$dbpass;
188
    }
135
189
136
  $self = { 'main'      => $main,
190
    my $dbh_to_read = DBI->connect($dsn_read, $dbuser_read, $dbpass_read, {'PrintError' => 0});
137
            'dsn'       => $dsn,
191
    if(!$dbh_to_read){
138
            'dbh'       => $dbh,
192
      dbg("auto-whitelist (sql-based): Unable to Connect to read DB");
139
            'tablename' => $main->{conf}->{user_awl_sql_table},
193
      return undef;
140
          };
194
    }    
195
196
    my $dbh_to_write = DBI->connect($dsn_write, $dbuser_write, $dbpass_write, {'PrintError' => 0});
197
    if(!$dbh_to_write){
198
      dbg("auto-whitelist (sql-based): Unable to Connect to write DB");
199
      return undef;
200
    }    
201
202
    $self = { 'main'      => $main,
203
              'dsn_read'  => $dsn_read,
204
              'dsn_write' => $dsn_write,
205
	      'tablename' => $main->{conf}->{user_awl_sql_table},
206
	      'dbh_to_read' => $dbh_to_read,
207
   	      'dbh_to_write'=> $dbh_to_write, 	
208
              'user_awl_read_write_support' => $user_awl_read_write_support,
209
            };
141
210
142
  dbg("SQL Based AWL: Connected to $dsn");
211
    dbg("SQL Based AWL: Connected readable to $dsn_read");
212
    dbg("SQL Based AWL: Connected writeable to $dsn_write");
213
  }
143
214
144
  return bless ($self, $class);
215
  return bless ($self, $class);
145
}
216
}
Lines 161-166 otherwise it is set to 0. Link Here
161
232
162
sub get_addr_entry {
233
sub get_addr_entry {
163
  my ($self, $addr) = @_;
234
  my ($self, $addr) = @_;
235
  my $sth;
236
  my $err;
164
237
165
  my $entry = { addr     => $addr,
238
  my $entry = { addr     => $addr,
166
                exists_p => 0,
239
                exists_p => 0,
Lines 176-186 sub get_addr_entry { Link Here
176
249
177
  my $sql = "SELECT count, totscore FROM $self->{tablename}
250
  my $sql = "SELECT count, totscore FROM $self->{tablename}
178
              WHERE username = ? AND email = ? AND ip = ?";
251
              WHERE username = ? AND email = ? AND ip = ?";
179
  my $sth = $self->{dbh}->prepare($sql);
252
253
  #new
254
  if ( $self->{user_awl_read_write_support} ) {
255
     $sth = $self->{dbh_to_read}->prepare($sql);
256
  }
257
  else {
258
     $sth = $self->{dbh}->prepare($sql);
259
  }
260
180
  my $rc = $sth->execute($username, $email, $ip);
261
  my $rc = $sth->execute($username, $email, $ip);
181
262
182
  if (!$rc) { # there was an error, but try to go on
263
  if (!$rc) { # there was an error, but try to go on
183
    my $err = $self->{dbh}->errstr;
264
    if( $self->{user_awl_read_write_support} ) {
265
      $err = $self->{dbh_to_read}->errstr;
266
    }
267
    else {
268
      $err = $self->{dbh}->errstr;
269
    }
184
    dbg("auto-whitelist (sql-based) get_addr_entry: SQL Error: $err");
270
    dbg("auto-whitelist (sql-based) get_addr_entry: SQL Error: $err");
185
    $entry->{count} = 0;
271
    $entry->{count} = 0;
186
    $entry->{totscore} = 0;
272
    $entry->{totscore} = 0;
Lines 222-228 itself a SQL database. Link Here
222
308
223
sub add_score {
309
sub add_score {
224
  my($self, $entry, $score) = @_;
310
  my($self, $entry, $score) = @_;
225
311
  my $sth;
312
  my $err;
226
  return if (!$entry->{addr});
313
  return if (!$entry->{addr});
227
  
314
  
228
  my ($email, $ip) = $self->_unpack_addr($entry->{addr});
315
  my ($email, $ip) = $self->_unpack_addr($entry->{addr});
Lines 239-249 sub add_score { Link Here
239
                                             totscore = totscore + ?
326
                                             totscore = totscore + ?
240
                WHERE username = ? AND email = ? AND ip = ?";
327
                WHERE username = ? AND email = ? AND ip = ?";
241
    
328
    
242
    my $sth = $self->{dbh}->prepare($sql);
329
    #new
330
    if( $self->{user_awl_read_write_support} ) {
331
      $sth = $self->{dbh_to_write}->prepare($sql);
332
    }
333
    else {
334
      $sth = $self->{dbh}->prepare($sql);
335
    }
336
243
    my $rc = $sth->execute($score, $username, $email, $ip);
337
    my $rc = $sth->execute($score, $username, $email, $ip);
244
    
338
    
245
    if (!$rc) {
339
    if (!$rc) {
246
      my $err = $self->{dbh}->errstr;
340
      if( $self->{user_awl_read_write_support} ) {
341
        $err = $self->{dbh_to_write}->errstr;
342
      }
343
      else {
344
        $err = $self->{dbh}->errstr;
345
      }
247
      dbg("auto-whitelist (sql-based) add_score: SQL Error: $err");
346
      dbg("auto-whitelist (sql-based) add_score: SQL Error: $err");
248
    }
347
    }
249
    else {
348
    else {
Lines 253-262 sub add_score { Link Here
253
  }
352
  }
254
  else { # no entry yet, so insert a new entry
353
  else { # no entry yet, so insert a new entry
255
    my $sql = "INSERT INTO $self->{tablename} (username,email,ip,count,totscore) VALUES (?,?,?,?,?)";
354
    my $sql = "INSERT INTO $self->{tablename} (username,email,ip,count,totscore) VALUES (?,?,?,?,?)";
256
    my $sth = $self->{dbh}->prepare($sql);
355
    #new
356
    if( $self->{user_awl_read_write_support} ) {
357
      $sth = $self->{dbh_to_write}->prepare($sql);
358
    }
359
    else {
360
      $sth = $self->{dbh}->prepare($sql);
361
    }
362
257
    my $rc = $sth->execute($username,$email,$ip,1,$score);
363
    my $rc = $sth->execute($username,$email,$ip,1,$score);
258
    if (!$rc) {
364
    if (!$rc) {
259
      my $err = $self->{dbh}->errstr;
365
      if( $self->{user_awl_read_write_support} ) {
366
        $err = $self->{dbh_to_write}->errstr;
367
      }
368
      else {
369
        $err = $self->{dbh}->errstr;
370
      }
260
      dbg("auto-whitelist (sql-based) add_score: SQL Error: $err");
371
      dbg("auto-whitelist (sql-based) add_score: SQL Error: $err");
261
    }
372
    }
262
    $entry->{exists_p} = 1;
373
    $entry->{exists_p} = 1;
Lines 280-286 perl-IP entries for this address as well Link Here
280
391
281
sub remove_entry {
392
sub remove_entry {
282
  my ($self, $entry) = @_;
393
  my ($self, $entry) = @_;
283
394
  my $sth;
395
  my $err; 
284
  my ($email, $ip) = $self->_unpack_addr($entry->{addr});
396
  my ($email, $ip) = $self->_unpack_addr($entry->{addr});
285
397
286
  return unless ($email && $ip);
398
  return unless ($email && $ip);
Lines 304-314 sub remove_entry { Link Here
304
    dbg("auto-whitelist (sql-based) remove_entry: Removing single entry matching ".$entry->{addr});
416
    dbg("auto-whitelist (sql-based) remove_entry: Removing single entry matching ".$entry->{addr});
305
  }
417
  }
306
418
307
  my $sth = $self->{dbh}->prepare($sql);
419
  #new
420
  if( $self->{user_awl_read_write_support} ) {
421
    $sth = $self->{dbh_to_write}->prepare($sql);
422
  }
423
  else {
424
    $sth = $self->{dbh}->prepare($sql);
425
  }
308
  my $rc = $sth->execute(@args);
426
  my $rc = $sth->execute(@args);
309
427
310
  if (!$rc) {
428
  if (!$rc) {
311
    my $err = $self->{dbh}->errstr;
429
    if( $self->{user_awl_read_write_support} ) {
430
      $err = $self->{dbh_to_write}->errstr;
431
    }
432
    else {
433
      $err = $self->{dbh}->errstr;
434
    }
312
    dbg("auto-whitelist (sql-based) remove_entry: SQL Error: $err");
435
    dbg("auto-whitelist (sql-based) remove_entry: SQL Error: $err");
313
  }
436
  }
314
  else {
437
  else {
Lines 329-336 This method provides the necessary clean Link Here
329
452
330
sub finish {
453
sub finish {
331
  my ($self) = @_;
454
  my ($self) = @_;
455
  if( $self->{user_awl_read_write_support} ) {
456
    $self->{dbh_to_write}->disconnect();
457
    $self->{dbh_to_read}->disconnect();
458
    dbg("auto-whitelist (sql-based) finish: Disconnected from " . $self->{dsn_write});
459
    dbg("auto-whitelist (sql-based) finish: Disconnected from " . $self->{dsn_read});
460
  }
461
  else {
332
  dbg("auto-whitelist (sql-based) finish: Disconnected from " . $self->{dsn});
462
  dbg("auto-whitelist (sql-based) finish: Disconnected from " . $self->{dsn});
333
  $self->{dbh}->disconnect();
463
  $self->{dbh}->disconnect();
464
  }
465
334
}
466
}
335
467
336
=head2 _unpack_addr
468
=head2 _unpack_addr

Return to bug 4508