[Bps-public-commit] rt-extension-rewritesubject branch pass-mimeobject-to-subroutine created. 126544df57eed778e3a049ff567b33fb8b502194

BPS Git Server git at git.bestpractical.com
Wed Dec 20 00:35:09 UTC 2023


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt-extension-rewritesubject".

The branch, pass-mimeobject-to-subroutine has been created
        at  126544df57eed778e3a049ff567b33fb8b502194 (commit)

- Log -----------------------------------------------------------------
commit 126544df57eed778e3a049ff567b33fb8b502194
Author: Ronaldo Richieri <ronaldo at bestpractical.com>
Date:   Tue Dec 19 21:34:30 2023 -0300

    Update POD

diff --git a/README b/README
index e88b61f..a401a6b 100644
--- a/README
+++ b/README
@@ -1,15 +1,18 @@
 NAME
-    RT-Extension-RewriteSubject - [One line description of module's purpose
-    here]
+    RT-Extension-RewriteSubject - A mail plugin for rewriting the subject of
+    incoming mail
 
 DESCRIPTION
-    [Why would someone install this extension? What does it do? What problem
-    does it solve?]
+    This extension provides a mail plugin that can rewrite the subject of
+    incoming mail based on patterns matched against the sender, subject, and
+    body of the email.
 
-RT VERSION
-    Works with RT [What versions of RT is this known to work with?]
+    It can also be used to add additional headers to the email, such as
+    X-RT-Command that can be used to trigger additional actions by other
+    mail plugins such as RT::Extension::CommandByMail.
 
-    [Make sure to use requires_rt and rt_too_new in Makefile.PL]
+RT VERSION
+    Works with RT 5.
 
 INSTALLATION
     perl Makefile.PL
@@ -17,16 +20,148 @@ INSTALLATION
     make install
         May need root permissions
 
-    Edit your /opt/rt4/etc/RT_SiteConfig.pm
+    Edit your /opt/rt5/etc/RT_SiteConfig.pm
         Add this line:
 
             Plugin('RT::Extension::RewriteSubject');
 
-    Clear your mason cache
-            rm -rf /opt/rt4/var/mason_data/obj
+        You also need to add RewriteSubject to your list of @MailPlugins.
+
+            Set(@MailPlugins, 'RewriteSubject', 'Auth::MailFrom');
+
+        This extension operates at the BeforeDecode hook point, so you need
+        to consider other mail plugins that also operate at that hook point
+        when deciding where in the list to put RewriteSubject.
 
     Restart your webserver
 
+CONFIGURATION
+    The extension expects $SubjectRewrites to be set to a reference to a
+    complex data structure.
+
+    The top layer is a hash of patterns that are matched against the sender
+    email address, as extracted from the From header.
+
+    The next layer is a hash of patterns that are matched against the
+    Subject header.
+
+    The third layer is a hash of patterns that are matched against the main
+    email content.
+
+    The fourth layer is a hash with two keys: body_patterns and new_subject.
+
+    The body_patterns value is an ARRAYREF of additional patterns that are
+    matched against the email content. It is intended that these patterns
+    are primarily for selecting data out of the body rather than determining
+    which innermost block applies to the incoming email, but a failure to
+    match with any of these patterns will also cause the extension to move
+    on to the next block.
+
+    The new_subject value may be either a string or a CODEREF.
+
+    If it is a string, it will be localized according to the system default
+    language using all positional captures from any of the patterns that led
+    to the selection of this subject, up to and including the email address
+    pattern.
+
+    If it is a CODEREF, it will be passed a HASHREF containing all of the
+    capture information for all of those patterns, including both positional
+    and named captures organized by origin: email, subject, and body, with
+    the body section being an ARRAYREF. It will also be passed a reference
+    to the original RT::Interface::Email::Message object, which you can
+    optionally use to set additional headers on the email.
+
+    The CODEREF should return the new subject as a string, or an undefined
+    value on failure.
+
+EXAMPLE CONFIGURATION
+        Set($SubjectRewrites,
+            {
+                # matched against from email address
+                qr'no-reply at sns.amazonaws.com' => {
+                    # matched against the subject
+                    qr/^RDS Notification Message$/ => {
+                        # matched against the body to "identify" it
+                        qr/Multi-AZ Instance (failover \w+)/i => {
+                            # matched against the body to collect data
+                            body_patterns => [ qr/dbinstance:id=([-\w]+)/ ],
+                            new_subject => sub {
+                                my $capture_info = shift;
+                                my $event_type = $capture_info->{body}[0]{positional}[0];
+                                my $instance_id = $capture_info->{body}[1]{positional}[0];
+
+                                if ($event_type =~ /failover completed/i) {
+                                    my $tickets = RT::Tickets->new( RT->SystemUser );
+                                    $tickets->LimitToActiveStatus;
+                                    $tickets->LimitSubject(OPERATOR => '=', VALUE => "RDS failover started ($instance_id)");
+
+                                    if ($tickets->Count and $tickets->Count == 1) {
+                                        my $start_ticket = $tickets->First;
+                                        my $id = $start_ticket->Id;
+                                        my $tag = $start_ticket->SubjectTag;
+
+                                        $start_ticket->SetSubject("RDS failover started/complete ($instance_id)");
+
+                                        return "$tag RDS $event_type ($instance_id)";
+                                    }
+                                }
+
+                                return "RDS $event_type ($instance_id)";
+                            },
+                        },
+                        # matched against the body to "identify" it
+                        qr/: An?\s+(.*\S) is available for your DB instance/ => {
+                            # matched against the body to collect data
+                            body_patterns => [ qr/dbinstance:id=([-\w]+)/ ],
+                            new_subject => 'RDS [_1] ([_2])',
+                        },
+                        # matched against the body to "identify" it
+                        qr/ free storage capacity .* is low at (\S+) / => {
+                            # matched against the body to collect data
+                            body_patterns => [ qr/dbinstance:id=([-\w]+)/, qr/Provisioned Storage: ([^B]+B)/, qr/Free Storage: ([^B]+B)/ ],
+                            new_subject => 'RDS [_1] free storage ([_4]/[_3]) ([_2])',
+                        },
+                    },
+                    # matched against the subject
+                    qr/^AWS Notification Message$/ => {
+                        # matched against the body to "identify" it
+                        qr/AWS GuardDuty/ => {
+                            # matched against the body to collect data
+                            body_patterns => [ qr/ in the ([-\w]+) region/, qr/ instance ([-\w]+)/ ],
+                            new_subject => sub {
+                                my ($capture_info, $original_message) = @_;
+                                my $region = $capture_info->{body}[1]{positional}[0];
+                                my $instance_id = $capture_info->{body}[2]{positional}[0];
+
+                                my $instance_name = $lookup_instance_name_by_region_and_id->( $region, $instance_id );
+                                $original_message->head->set('X-RT-Command', "AddRequestor: manager at example.com");
+
+                                return "AWS Notification Message - GuardDuty ($instance_name)" if $instance_name;
+                            },
+                        },
+                        # matched against the body to "identify" it
+                        qr/AWS Health Event/ => {
+                            # all of these notices we've gotten have either been for one instance or for an entire region.
+                            # this handles the single-instance case, leaving region-wide untouched.
+
+                            # matched against the body to collect data
+                            body_patterns => [ qr/"region":"([^"]+)"/, qr/"entityValue":"[^"]+"/, qr/"eventTypeCode":"([^"]+)/ ],
+                            new_subject => sub {
+                                my $capture_info = shift;
+                                my $region = $capture_info->{body}[1]{positional}[0];
+                                my $instance_id = $capture_info->{body}[2]{positional}[0];
+                                my $event_type = $capture_info->{body}[3]{positional}[0];
+
+                                my $instance_name = $lookup_instance_name_by_region_and_id->( $region, $instance_id );
+
+                                return "AWS Health Event - $event_type ($instance_name)" if $instance_name;
+                            },
+                        },
+                    },
+                },
+            }
+        );
+
 AUTHOR
     Best Practical Solutions, LLC <modules at bestpractical.com>
 
diff --git a/lib/RT/Extension/RewriteSubject.pm b/lib/RT/Extension/RewriteSubject.pm
index b1b753f..584264b 100644
--- a/lib/RT/Extension/RewriteSubject.pm
+++ b/lib/RT/Extension/RewriteSubject.pm
@@ -10,12 +10,17 @@ RT-Extension-RewriteSubject - A mail plugin for rewriting the subject of incomin
 
 =head1 DESCRIPTION
 
-[Why would someone install this extension? What does it do? What problem
-does it solve?]
+This extension provides a mail plugin that can rewrite the subject of incoming
+mail based on patterns matched against the sender, subject, and body of the
+email.
+
+It can also be used to add additional headers to the email, such as
+C<X-RT-Command> that can be used to trigger additional actions by other mail
+plugins such as L<RT::Extension::CommandByMail>.
 
 =head1 RT VERSION
 
-Works with RT 4.4+
+Works with RT 5.
 
 =head1 INSTALLATION
 
@@ -29,7 +34,7 @@ Works with RT 4.4+
 
 May need root permissions
 
-=item Edit your F</opt/rt4/etc/RT_SiteConfig.pm>
+=item Edit your F</opt/rt5/etc/RT_SiteConfig.pm>
 
 Add this line:
 
@@ -78,8 +83,12 @@ selection of this subject, up to and including the email address pattern.
 If it is a C<CODEREF>, it will be passed a C<HASHREF> containing all of the
 capture information for all of those patterns, including both positional and
 named captures organized by origin: C<email>, C<subject>, and C<body>, with the
-C<body> section being an C<ARRAYREF>.  The C<CODEREF> should return the new subject
-as a string, or an undefined value on failure.
+C<body> section being an C<ARRAYREF>. It will also be passed a reference to the
+original C<RT::Interface::Email::Message> object, which you can optionally use
+to set additional headers on the email.
+
+The C<CODEREF> should return the new subject as a string, or an undefined value
+on failure.
 
 =head1 EXAMPLE CONFIGURATION
 
@@ -137,11 +146,12 @@ as a string, or an undefined value on failure.
                         # matched against the body to collect data
                         body_patterns => [ qr/ in the ([-\w]+) region/, qr/ instance ([-\w]+)/ ],
                         new_subject => sub {
-                            my $capture_info = shift;
+                            my ($capture_info, $original_message) = @_;
                             my $region = $capture_info->{body}[1]{positional}[0];
                             my $instance_id = $capture_info->{body}[2]{positional}[0];
 
                             my $instance_name = $lookup_instance_name_by_region_and_id->( $region, $instance_id );
+                            $original_message->head->set('X-RT-Command', "AddRequestor: manager at example.com");
 
                             return "AWS Notification Message - GuardDuty ($instance_name)" if $instance_name;
                         },
commit a611a2a4a9c54b6f1b100fc603bfd950b5eda1d8
Author: Ronaldo Richieri <ronaldo at bestpractical.com>
Date:   Tue Dec 19 21:23:30 2023 -0300

    Pass MIME::Entity object to RewriteSubject subroutine
    
    This allows the RewriteSubject subroutine to update also headers
    other than Subject.  For example, it can be used to add a special header
    to be used by another extension such as RT::Extension::CommandByMail.

diff --git a/lib/RT/Interface/Email/RewriteSubject.pm b/lib/RT/Interface/Email/RewriteSubject.pm
index 78e6cb5..b3da4fb 100644
--- a/lib/RT/Interface/Email/RewriteSubject.pm
+++ b/lib/RT/Interface/Email/RewriteSubject.pm
@@ -160,7 +160,7 @@ BODY_IDENTITY_PATTERN:
 
                             if (ref $subject_proto) {
                                 if (ref($subject_proto) eq 'CODE') {
-                                    $new_subject = $subject_proto->( \%capture_info );
+                                    $new_subject = $subject_proto->( \%capture_info, $message );
                                 }
                             }
                             else {
-----------------------------------------------------------------------


hooks/post-receive
-- 
rt-extension-rewritesubject


More information about the Bps-public-commit mailing list