Go to ConfigurationGlobalScripNew Scrip
In the tab for new scrip select / type following options:
Description: AddWatchersOnCorrespond
Condition: On Correspond
Action: User Defined
Template: Global Template: Blank
Stage: TransactionCreate
Custom condition: (keep empty)
Custom action preparation code: return 1;
Custom action cleanup code:
# Get some info:
my $scrip = 'Scrip:AddWatchersOnCorrespond';
my $Transaction = $self->TransactionObj;
my $EmailAddr = $self->TransactionObj->CreatorObj->EmailAddress;
my $Queue = $self->TicketObj->QueueObj;
my $Ticket = $self->TicketObj;
my $Id = $self->TicketObj->id;
# Extract a list of people associated with this transaction:
# - including the transaction creator, and if it is an email, the sender and recipients of that email message.
my @People = ($EmailAddr);
foreach my $h (qw(From To Cc)) {
my $header = $Transaction->Attachments->First->GetHeader($h);
my @addr = Mail::Address->parse($header);
foreach my $addrobj (@addr) {
my $addr = lc $RT::Nobody->UserObj->CanonicalizeEmailAddress($addrobj->address);
# Ignore the specific addresses for this queue:
next if lc $Queue->CorrespondAddress eq $addr or lc $Queue->CommentAddress eq $addr;
# Ignore any email address that looks like one for ANY of our queues:
next if RT::EmailParser::IsRTAddress('', $addr);
$RT::Logger->debug("$scrip: Ticket #$Id correspondence contains header - $h: $addr");
push @People, $addr;
}
}
# Lookup the 'experts' (general) group to use below:
# my $Experts = RT::Group->new($self->CurrentUser);
# $Experts->LoadUserDefinedGroup('general');
# Now check if each user is already watching the ticket or queue:
foreach my $addr (@People) {
my $User = RT::User->new($RT::SystemUser);
$User->LoadByEmail($addr);
my $Name = $User->Name;
my $Principal = $User->PrincipalId if $User->Id;
if (not ($Queue->IsWatcher(Type => 'Cc', PrincipalId => $Principal) or
$Queue->IsWatcher(Type => 'AdminCc', PrincipalId => $Principal) or
$Queue->IsWatcher(Type => 'Owner', PrincipalId => $Principal) or
$Ticket->IsWatcher(Type => 'Cc', PrincipalId => $Principal) or
$Ticket->IsWatcher(Type => 'AdminCc', PrincipalId => $Principal) or
$Ticket->IsWatcher(Type => 'Owner', PrincipalId => $Principal) or
$Ticket->IsWatcher(Type => 'Requestor', PrincipalId => $Principal)
)) {
# If the user is a member of the experts group, then add them as an AdminCc, otherwise as a normal Cc:
# my $type = $Experts->HasMember($User->PrincipalObj) ? 'AdminCc' : 'Cc';
my $type = 'Cc';
# Add the new watcher now and check for errors:
my ($ret, $msg) = $Ticket->AddWatcher(Type => $type,
Email => $addr,
PrincipalId => $Principal,);
if ($ret) {
$RT::Logger->info("$scrip: New $type watcher added to ticket #$Id: $addr (#$Principal)");
} else {
$RT::Logger->error("$scrip: Failed to add new $type watcher to ticket #$Id: $addr (#$Principal) - $msg");
}
}
}
return 1;
|