Ticket #164: pipe-history.pl

File pipe-history.pl, 3.1 KB (added by Brian Smith, 12 years ago)

Modified version of 'pipe.pl' script from POPFile 1.1.2

Line 
1#!/usr/bin/perl
2# ----------------------------------------------------------------------------
3#
4# pipe-history.pl --- Read a message in on STDIN and write out the modified
5# version on STDOUT. Save message in message history to permit reclassification.
6#
7# Copyright (c) 2001-2011 John Graham-Cumming
8#
9# This file is part of POPFile
10#
11# POPFile is free software; you can redistribute it and/or modify it
12# under the terms of version 2 of the GNU General Public License as
13# published by the Free Software Foundation.
14#
15# POPFile is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with POPFile; if not, write to the Free Software
22# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23#
24# ----------------------------------------------------------------------------
25
26use strict;
27use lib defined($ENV{POPFILE_ROOT})?$ENV{POPFILE_ROOT}:'./';
28use POPFile::Loader;
29
30# main
31
32if ( $#ARGV == -1 ) {
33
34 # POPFile is actually loaded by the POPFile::Loader object which does all
35 # the work
36
37 my $POPFile = POPFile::Loader->new();
38
39 # Indicate that we should create not output on STDOUT (the POPFile
40 # load sequence)
41
42 $POPFile->debug(0);
43 $POPFile->CORE_loader_init();
44 $POPFile->CORE_signals();
45 $POPFile->CORE_load( 1 );
46 $POPFile->CORE_link_components();
47 $POPFile->CORE_initialize();
48
49 # Ugly hack which is needed because Bayes::classify_and_modify looks up
50 # the UI port and whether we are allowing remote connections or not
51 # to set the XPL link in the header. If we don't have these predefined
52 # then they'll be discarded when the configuration is loaded, and since
53 # we are not loading the UI, they are not defined at this point
54
55 my $c = $POPFile->get_module('POPFile::Config');
56 $c->module_config_( 'html', 'local', 1 );
57 $c->module_config_( 'html', 'port', 8080 );
58
59 if ( $POPFile->CORE_config() ) {
60
61 # Prevent the tool from finding another copy of POPFile running
62
63 my $current_piddir = $c->config_( 'piddir' );
64 $c->config_( 'piddir', $c->config_( 'piddir' ) . 'pipe.pl.' );
65
66 $POPFile->CORE_start();
67
68 my $b = $POPFile->get_module('Classifier::Bayes');
69 my $session = $b->get_session_key( 'admin', '' );
70
71 # Save this message in the message history, unlike the 'pipe.pl' script
72
73 $b->classify_and_modify( $session, \*STDIN, \*STDOUT, 0, '', 0, 1, "\n" );
74
75 $c->config_( 'piddir', $current_piddir );
76
77 # Reload configuration file ( to avoid updating configurations )
78
79 $c->load_configuration();
80
81# $b->release_session_key( $session );
82 $POPFile->CORE_stop();
83 }
84
85 exit 0;
86} else {
87 print "Classify a msg read on STDIN, update the history & send modified msg to STDOUT\n\n";
88 print "Usage: pipe-history.pl\n";
89
90 exit 1;
91}