[ create a new paste ] login | about

Link: http://codepad.org/pSthWrxD    [ raw code | fork | 1 comment ]

Perl, pasted on Feb 2:
no warnings qw(redefine);

sub ParseTicketId {
    my $Subject = shift;

    my $rtname = RT->Config->Get('rtname');
    my $test_name = RT->Config->Get('EmailSubjectTagRegex') || qr/\Q$rtname\E/i;

    my $id;
    if ( $Subject =~ s/\[$test_name\s+(?:\w+\s+)?\#(\d+)\s*\]//i ) {
        $id = $1;
    } else {
        foreach my $tag ( RT->System->SubjectTag ) {
            next unless $Subject =~ s/\[\Q$tag\E\s+\(?:\w+\s+)?#(\d+)\s*\]//i;
            $id = $1;
            last;
        }
    }
    return undef unless $id;

    $RT::Logger->debug("Found a ticket ID. It's $id");
    return $id;
}

sub AddSubjectTag{
  my $subject = shift;
  my $ticket  = shift;
  unless ( ref $ticket ) {
    my $tmp = RT::Ticket->new( $RT::SystemUser );
    $tmp->Load( $ticket );
    $ticket = $tmp;
  }
  my $id = $ticket->id;
  my $queue_tag = $ticket->QueueObj->SubjectTag;
  
  my $tag_re = RT->Config->Get('EmailSubjectTagRegex');
  unless ( $tag_re ) {
    my $tag = $queue_tag || RT->Config->Get('rtname');
    $tag_re = qr/\Q$tag\E/;
  } elsif ( $queue_tag ) {
      $tag_re = qr/$tag_re|\Q$queue_tag\E/;
    }

  #The parenthetical phrase may not be necessary...
  return $subject if $subject =~ /\[$tag_re\s+(?:\w+\s+)?#$id\]/;
  
  $subject =~ s/(\r\n|\n|\s)/ /gi;
  chomp $subject;
  return '['. ($queue_tag || RT->Config->Get('rtname').' '.$ticket->QueueObj->Name) ." #$id] $subject";
}

1;
__END__
diff -u
@@ -5,11 +7,11 @@
     my $test_name = RT->Config->Get('EmailSubjectTagRegex') || qr/\Q$rtname\E/i;
 
     my $id;
-    if ( $Subject =~ s/\[$test_name\s+\#(\d+)\s*\]//i ) {
+    if ( $Subject =~ s/\[$test_name\s+(?:\w+\s+)?\#(\d+)\s*\]//i ) {
         $id = $1;
     } else {
         foreach my $tag ( RT->System->SubjectTag ) {
-            next unless $Subject =~ s/\[\Q$tag\E\s+\#(\d+)\s*\]//i;
+            next unless $Subject =~ s/\[\Q$tag\E\s+\(?:\w+\s+)?#(\d+)\s*\]//i;
             $id = $1;
             last;
         }
@@ -20,7 +22,7 @@
     return $id;
 }
 
-sub AddSubjectTag {
+sub AddSubjectTag{
     my $subject = shift;
     my $ticket  = shift;
     unless ( ref $ticket ) {
@@ -38,9 +40,13 @@
     } elsif ( $queue_tag ) {
         $tag_re = qr/$tag_re|\Q$queue_tag\E/;
     }
-    return $subject if $subject =~ /\[$tag_re\s+#$id\]/;
+
+  #The parenthetical phrase may not be necessary...
+  return $subject if $subject =~ /\[$tag_re\s+(?:\w+\s+)?#$id\]/;
 
     $subject =~ s/(\r\n|\n|\s)/ /gi;
     chomp $subject;
-    return "[". ($queue_tag || RT->Config->Get('rtname')) ." #$id] $subject";
+  return '['. ($queue_tag || RT->Config->Get('rtname').' '.$ticket->QueueObj->Name) ." #$id] $subject";
 }
+
+1;


Create a new paste based on this one


Comments:
posted by spamalot on Mar 4
The edit to line 19 inside the foreach over RT->System->SubjectTag shouldn't be necessary. That's meant to capture custom queue tags configured in RT.
reply