home *** CD-ROM | disk | FTP | other *** search
/ ftp.f-secure.com / 2014.06.ftp.f-secure.com.tar / ftp.f-secure.com / support / hotfix / fsis / IS-SpamControl.fsfix / iufssc / lib / Mail / SpamAssassin / PerMsgLearner.pm < prev    next >
Text File  |  2006-11-29  |  5KB  |  198 lines

  1. # <@LICENSE>
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements.  See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to you under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License.  You may obtain a copy of the License at:
  8. #     http://www.apache.org/licenses/LICENSE-2.0
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # </@LICENSE>
  15.  
  16. =head1 NAME
  17.  
  18. Mail::SpamAssassin::PerMsgLearner - per-message status (spam or not-spam)
  19.  
  20. =head1 SYNOPSIS
  21.  
  22.   my $spamtest = new Mail::SpamAssassin ({
  23.     'rules_filename'      => '/etc/spamassassin.rules',
  24.     'userprefs_filename'  => $ENV{HOME}.'/.spamassassin/user_prefs'
  25.   });
  26.   my $mail = $spamtest->parse();
  27.  
  28.   my $status = $spamtest->learn($mail,$id,$isspam,$forget);
  29.   my $didlearn = $status->did_learn();
  30.   $status->finish();
  31.  
  32.  
  33. =head1 DESCRIPTION
  34.  
  35. The Mail::SpamAssassin C<learn()> method returns an object of this
  36. class.  This object encapsulates all the per-message state for
  37. the learning process.
  38.  
  39. =head1 METHODS
  40.  
  41. =over 4
  42.  
  43. =cut
  44.  
  45. package Mail::SpamAssassin::PerMsgLearner;
  46.  
  47. use strict;
  48. use warnings;
  49. use bytes;
  50.  
  51. use Mail::SpamAssassin;
  52. use Mail::SpamAssassin::PerMsgStatus;
  53. use Mail::SpamAssassin::Bayes;
  54. use Mail::SpamAssassin::Logger;
  55.  
  56. use vars qw{
  57.   @ISA
  58. };
  59.  
  60. @ISA = qw();
  61.  
  62. ###########################################################################
  63.  
  64. sub new {
  65.   my $class = shift;
  66.   $class = ref($class) || $class;
  67.   my ($main, $msg) = @_;
  68.  
  69.   my $self = {
  70.     'main'              => $main,
  71.     'msg'               => $msg,
  72.     'learned'        => 0,
  73.   };
  74.  
  75.   $self->{conf} = $self->{main}->{conf};
  76.  
  77.   $self->{bayes_scanner} = $self->{main}->{bayes_scanner};
  78.  
  79.   bless ($self, $class);
  80.   $self;
  81. }
  82.  
  83. ###########################################################################
  84.  
  85. # $status->learn_spam($id)
  86. # Learn the message as spam.
  87. # C<$id> is an optional message-identification string, used internally
  88. # to tag the message.  If it is C<undef>, the Message-Id of the message
  89. # will be used.  It should be unique to that message.
  90. # This is a semi-private API; callers should use
  91. # C<$spamtest-E<gt>learn($mail,$id,$isspam,$forget)> instead.
  92.  
  93. sub learn_spam {
  94.   my ($self, $id) = @_;
  95.  
  96.   if ($self->{main}->{learn_with_whitelist}) {
  97.     $self->{main}->add_all_addresses_to_blacklist ($self->{msg});
  98.   }
  99.  
  100.   # use the real message-id here instead of mass-check's idea of an "id",
  101.   # as we may deliver the msg into another mbox format but later need
  102.   # to forget it's training.
  103.   $self->{learned} = $self->{bayes_scanner}->learn (1, $self->{msg}, $id);
  104. }
  105.  
  106. ###########################################################################
  107.  
  108. # $status->learn_ham($id)
  109. # Learn the message as ham.
  110. # C<$id> is an optional message-identification string, used internally
  111. # to tag the message.  If it is C<undef>, the Message-Id of the message
  112. # will be used.  It should be unique to that message.
  113. # This is a semi-private API; callers should use
  114. # C<$spamtest-E<gt>learn($mail,$id,$isspam,$forget)> instead.
  115.  
  116. sub learn_ham {
  117.   my ($self, $id) = @_;
  118.  
  119.   if ($self->{main}->{learn_with_whitelist}) {
  120.     $self->{main}->add_all_addresses_to_whitelist ($self->{msg});
  121.   }
  122.  
  123.   $self->{learned} = $self->{bayes_scanner}->learn (0, $self->{msg}, $id);
  124. }
  125.  
  126. ###########################################################################
  127.  
  128. # $status->forget($id)
  129. # Forget about a previously-learned message.
  130. # C<$id> is an optional message-identification string, used internally
  131. # to tag the message.  If it is C<undef>, the Message-Id of the message
  132. # will be used.  It should be unique to that message.
  133. # This is a semi-private API; callers should use
  134. # C<$spamtest-E<gt>learn($mail,$id,$isspam,$forget)> instead.
  135.  
  136. sub forget {
  137.   my ($self, $id) = @_;
  138.  
  139.   if ($self->{main}->{learn_with_whitelist}) {
  140.     $self->{main}->remove_all_addresses_from_whitelist ($self->{msg});
  141.   }
  142.  
  143.   $self->{learned} = $self->{bayes_scanner}->forget ($self->{msg}, $id);
  144. }
  145.  
  146. ###########################################################################
  147.  
  148. =item $didlearn = $status->did_learn()
  149.  
  150. Returns C<1> if the message was learned from or forgotten succesfully.
  151.  
  152. =cut
  153.  
  154. sub did_learn {
  155.   my ($self) = @_;
  156.   return ($self->{learned});
  157. }
  158.  
  159. ###########################################################################
  160.  
  161. =item $status->finish()
  162.  
  163. Finish with the object.
  164.  
  165. =cut
  166.  
  167. sub finish {
  168.   my $self = shift;
  169.   delete $self->{main};
  170.   delete $self->{msg};
  171.   delete $self->{conf};
  172.   delete $self->{bayes_scanner};
  173. }
  174.  
  175. ###########################################################################
  176.  
  177. 1;
  178. __END__
  179.  
  180. =back
  181.  
  182. =head1 SEE ALSO
  183.  
  184. C<Mail::SpamAssassin>
  185. C<spamassassin>
  186.  
  187.