[rt-devel] Contrib: CreateMessageId (RT2.0)

Bruce Campbell bruce_campbell at ripe.net
Thu Apr 25 07:41:33 EDT 2002


On Thu, 25 Apr 2002, Bruce Campbell wrote:

> The attached perl script will update the Attachments.MessageId field of
> all attachments in an RT database which:

Of course, I meant *this* attached perl script.  My apologies for using a
non-existent coding method in the previous mail ;)

-- 
                             Bruce Campbell                            RIPE
                   Systems/Network Engineer                             NCC
                 www.ripe.net - PGP562C8B1B                      Operations
-------------- next part --------------
#!/usr/bin/perl -w
###############################################################################
## @(#) $Id: CreateMessageIds.pl,v 1.2 2002/04/25 11:40:03 bc Exp $
##
## Create all Message Ids in an RT database (where MessageId field is NULL)
##
## Copyright (C) 2002 Bruce Campbell and the RIPE NCC
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 1, or (at your option)
## any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##
###############################################################################

###############################################################################
##                     S  E  C  U  R  I  T  Y
###############################################################################

# IF this program is run as ROOT, do secure setup.
# die "error: perl library is writable by others !\n" if $< && -W $INC[0];
# $ENV{'IFS'} = '' if $ENV{'IFS'};	# plug sh security hole
# $ENV{'PATH'} = '/bin:/usr/bin';    # or whatever you need
# $ENV{'SHELL'} = '/usr/bin/sh' if exists $ENV{SHELL};
# delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
# umask(077);

###############################################################################
##                 L I B R A R I E S / M O D U L E S
###############################################################################

use strict;
use Getopt::Std;
use lib "!! CHANGE ME TO YOUR RT LIB DIRECTORY !!";
use lib "!! CHANGE ME TO YOUR RT ETC DIRECTORY !!";
use RT::Interface::CLI  qw(CleanEnv LoadConfig DBConnect 
                           GetCurrentUser GetMessageContent);
use Date::Format;
use MIME::Entity;
use MIME::Parser;
use RT::Attachment;
use DBI;


###############################################################################
##                  G L O B A L   V A R I A B L E S
###############################################################################

use vars qw($VERSION); # pre-declare the version no.
$VERSION = do { my @r=(q$Revision: 1.2 $=~/\d+/g); sprintf "%d."."%02d"x$#r, at r};

###############################################################################
##                          M E T H O D S
###############################################################################
sub usage() 
{

	print "$0 $VERSION, Copyright (C) 2002, Bruce Campbell\n";
	print "$0 comes with ABSOLUTELY NO WARRANTY; This is free software,\n";
	print "and you are welcome to redistribute it under certain conditions\n";
	print "\nusage: $0 [-hcw]\n";

	print << 'EOM' ;

 -c	  Display Copyright message.
 -w	  Display Warranty message.
 -h	  Display this message.
 -l <num> Limit this run to this many matches (default 50)

Will attempt to connect to an RT database, and add MessageId fields for all
parent attachments without a MessageId set.

EOM

exit 0;
}

sub warranty() 
{
	print << 'EOM' ;

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

EOM

exit 0;
}

sub copyright()
{
	print << 'EOM' ;

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.

EOM

exit 0;
}

###############################################################################
##                       E N V I R O N M E N T
###############################################################################

my %opts;

&getopts('hwcl:', \%opts);	# get any command line arguments
				# arguments that have data (-a foo) use 'a:'

###############################################################################
##                             M A I N
###############################################################################


# standard command line argument
warranty  if $opts{'w'};
usage     if $opts{'h'};
copyright if $opts{'c'};

# Our default chunk size
$opts{'l'} = "50" if( !defined( $opts{'l'} ) );
# Trust not user input.
$opts{'l'} = "50" if( $opts{'l'} !~ /^\s*\d+\s*$/ );

usage     if( !defined( $opts{'l'} ) );


# We're here, lets do it.


# Clean the environment
CleanEnv();

# Load RT config.
LoadConfig();

# Connect
DBConnect() || die "Unable to connect to RT\n";

# Get the attachments variable.
my $attachments = new RT::Attachments( $RT::SystemUser );

# Start limiting it.
$attachments->UnLimit;

# We want the first attachment (Parent == 0)
$attachments->Limit ( FIELD => 'Parent',
                      VALUE => '0',
                      OPERATOR => '=',
                      ENTRYAGGREGATOR => 'AND' );

# We want the MessageId to be NULL.
$attachments->Limit ( FIELD => 'MessageId',
                      VALUE => 'NULL',
                      OPERATOR => 'IS',
                      ENTRYAGGREGATOR => 'AND' );
# $attachments->LimitMessageId( VALUE => 'NULL', OPERATOR => 'is' );

# We want a Message-Id field in the Headers
$attachments->Limit ( FIELD => 'Headers',
                      VALUE => "(^|\n)Message-Id:",
                      OPERATOR => 'regexp binary',
                      ENTRYAGGREGATOR => 'AND' );
# $attachments->LimitHeaders( VALUE => '(^|\n)Message-Id:', OPERATOR => 'regexp binary' );

# We want to limit the number returned.
$attachments->RowsPerPage( $opts{'l'} );

# And lets go.
while( my $attachment = $attachments->Next() ){
	# Lets just print the results for now.

	# Copied from UpdateMessageId - could have invoked the ScripAction
	# to do code re-use, but its getting a bit awkward.
        my @Headers = split( /\n/, $attachment->Headers);

        my $ln_mtch = undef;

	my $retval = "-1";
        foreach my $ln( @Headers ){
                if( $ln =~ m/^(Message-Id):\s+(.*)$/ ){
                        my $mtmp = $1;
                        my $mtch = $2;
                        $mtch =~ s/^\s*//g;
                        $mtch =~ s/\s*$//g;
                        $mtch =~ s/^<//g;
                        $mtch =~ s/>$//g;
                        # Only grab the first one.
                        if( ! defined( $ln_mtch ) ){
                                $ln_mtch = $mtch;
                        }
                }
        }

        if( defined( $ln_mtch ) ){                              

                $retval = $attachment->SetMessageId( $ln_mtch );
		print "$ln_mtch - ";
	}
	
	print "Found: " . $attachment->id . " and result $retval\n";
}

# Disconnect.
$RT::Handle->Disconnect();


More information about the Rt-devel mailing list