File: DavidHooton.pm

package info (click to toggle)
mailscanner 4.79.11-2.2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 5,820 kB
  • ctags: 1,309
  • sloc: perl: 25,655; sh: 2,666; xml: 624; makefile: 242
file content (159 lines) | stat: -rw-r--r-- 4,808 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#
#   MailScanner - SMTP E-Mail Virus Scanner
#   Copyright (C) 2002  Julian Field
#

package MailScanner::CustomConfig;

use DirHandle;
use File::Temp qw(tempfile tempdir);

use strict 'vars';
use strict 'refs';
no  strict 'subs'; # Allow bare words for parameter %'s

use vars qw($VERSION);

### The package version, both in 1.23 style *and* usable by MakeMaker:
$VERSION = substr q$Revision: 2923 $, 10;

#
# Usage instructions:
# In MailScanner.conf set
#
# Inline Text Signature = &InlineTextSignature
# Inline HTML Signature = &InlineHTMLSignature
# Random Signatures = <list of names of files and directories or a ruleset>
#
# Note: If you use a ruleset for "Random Signatures" then the filename must
#       end in ".rule" or ".rules".
#
# Installation Instructions:
# 1. Copy this file into /usr/lib/MailScanner/MailScanner/CustomFunctions/
#
# 2. In /usr/lib/MailScanner/MailScanner/ConfigDefs.pl you need to add
# RandomSignatures
# on a line on its own at the very end of the file.
#

my $LastMessage = undef;
my $SigTxt = "";
my $SigHTML = "";
my $TempDir = "";

sub InitInlineTextSignature {
  # No initialisation needs doing here at all.
  MailScanner::Log::InfoLog("Initialising InlineTextSignature");

  $LastMessage = undef;
  $SigTxt = "";
  $SigHTML = "";
  my($tmpfh, $tmpfile) = tempfile("SignRandom.XXXXXX", TMPDIR => 1, UNLINK => 0);
  $TempDir = $tmpfile;
}

sub InitInlineHTMLSignature {
  # No initialisation needs doing here at all.
  MailScanner::Log::InfoLog("Initialising InlineHTMLSignature");

  $LastMessage = undef;
  $SigTxt = "";
  $SigHTML = "";
  my($tmpfh, $tmpfile) = tempfile("SignRandom.XXXXXX", TMPDIR => 1, UNLINK => 0);
  $TempDir = $tmpfile;
}

sub EndInlineTextSignature {
  # No shutdown code needed here at all.
  MailScanner::Log::InfoLog("Ending InlineTextSignature");
}

sub EndInlineHTMLSignature {
  # No shutdown code needed here at all.
  MailScanner::Log::InfoLog("Ending InlineHTMLSignature");
}


# Read the list of all the signatures for this message, pick one at
# random, and store the Txt and HTML filenames of it.
sub FindRandomSig {
  my($message) = @_;

  my(@dirs, $limit, $random, @filelist, $dir, $entry);
  my $dh = new DirHandle;
  @filelist = undef;

  #print STDERR "Finding random sig for $message\n";

  @dirs = split(/[\s,]+/, MailScanner::Config::Value('randomsignatures',
                                                     $message));
  #print STDERR "Directories are : " . join(',',@dirs) . "\n";
  foreach $dir (@dirs) {
    MailScanner::Log::WarnLog("Random signature file/dir %s does not exist",
      $dir), next unless -e $dir; # File/Directory must exist
    MailScanner::Log::WarnLog("Random signature file/dir %s cannot be read",
      $dir), next unless -r $dir;
    # If the directory is actually a file, just add it to the list
    push(@filelist, $dir), next if -f $dir;
    # $dir exists, is readable and is probably a directory
    MailScanner::Log::WarnLog("Random signature file/dir %s must be a " .
      "file or directory", $dir), next unless -d $dir;
    # $dir is a directory. So open it and read the contents.
    $dh->open($dir) or MailScanner::Log::WarnLog("Attempt to open %s failed",
                                                 $dir);
    #print STDERR "FindRandomSig: Processing $dir\n";
    while ($entry = $dh->read) {
      # Ignore dot files and HTML files
      push @filelist, "$dir/$entry"
        unless $entry =~ /^\./ || $entry =~ /html?/i;
      #print STDERR "Pushed $dir/$entry onto the list\n";
    }
    $dh->close;
  }

  # We now have a list of non-html files
  #print STDERR "We have a list of " . $#filelist . " filenames\n";
  $random = int(rand(scalar @filelist));
  #print STDERR "Random number is $random\n";

  $SigTxt = $filelist[$random];
  $SigHTML = $filelist[$random];
  $SigHTML =~ s/([^\/]+)te?xt([^\/]*)$/$1html$2/gi;
  $LastMessage = $message; # So we know we have already calculated this

  #print STDERR "Random Text Signature = $SigTxt\n";
  #print STDERR "Random HTML Signature = $SigHTML\n";
}

# Find a random TXT signature if we haven't already worked one out.
# Return it
sub InlineTextSignature {
  my($message) = @_;

  return "/dev/null" unless $message;

  #print STDERR "Finding InlineTxtSig for $message\n";

  # Find a new random signature pair if we haven't got a matching pair
  FindRandomSig($message) unless $message eq $LastMessage;
  
  return $SigTxt;
}

# Find a random HTML signature if we haven't already worked one out.
# Return it
sub InlineHTMLSignature {
  my($message) = @_;

  return "/dev/null" unless $message;

  #print STDERR "Finding InlineHTMLSig for $message\n";

  # Find a new random signature pair if we haven't got a matching pair
  FindRandomSig($message) unless $message eq $LastMessage;
  
  return $SigHTML;
}

1;