# Initial data for a fresh RTIR Installation.

@Initial = (
    sub {
        # put fake lifecycles for initiialization to succed
        my $lc = RT->Config->Get('Lifecycles') || {};
        foreach my $name (qw(incidents incident_reports investigations blocks)) {
            $lc->{ $name } ||= $lc->{'default'} || {};
        }
        RT->Config->Set(Lifecycles => %$lc);
        RT::Lifecycle->FillCache;
        return 1;
    },
);

@Queues = (
    {
        Name              => 'Incidents',
        Lifecycle         => 'incidents',
        CorrespondAddress => "",
        CommentAddress    => "",
        InitialPriority   => 50,
    },
    {
        Name              => 'Incident Reports',
        Lifecycle         => 'incident_reports',
        CorrespondAddress => "",
        CommentAddress    => "",
    },
    {
        Name              => 'Investigations',
        Lifecycle         => 'investigations',
        CorrespondAddress => "",
        CommentAddress    => "",
    },
    {
        Name              => 'Blocks',
        Lifecycle         => 'blocks',
        CorrespondAddress => "",
        CommentAddress    => "",
    },
);

@CustomFields = (
    {
        Name        => 'Constituency',
        Type        => 'SelectSingle',
        Queue       => ['Incidents', 'Incident Reports', 'Investigations', 'Blocks'],
        Disabled    => 0,
        Description => 'Constituency for RTIR queues',
        Values      => [
            { Name      => "EDUNET", SortOrder => 1 },
            { Name      => "GOVNET", SortOrder => 2 },
        ],
    },
    {
        Name        => 'Description',
        Type        => 'FreeformSingle',
        Queue       => 'Incidents',
        Disabled    => 0,
        Description => 'Description for Incidents RTIR queue',
    },
    {
        Name        => 'Resolution',
        Type        => 'SelectSingle',
        Queue       => 'Incidents',
        Disabled    => 0,
        Description => 'Resolution for Incidents RTIR queue',
        Values      => [
            { Name => 'successfully resolved',      SortOrder => 1 },
            { Name => 'no resolution reached',      SortOrder => 2 },
            { Name => 'no response from customer',  SortOrder => 3 },
            { Name => 'no response from other ISP', SortOrder => 4 },
        ],
    },
    {
        Name        => 'Function',
        Type        => 'SelectSingle',
        Queue       => 'Incidents',
        Disabled    => 0,
        Description => 'Function for Incidents RTIR queue',
        Values      => [
            { Name      => "AbuseDesk",     SortOrder => 1 },
            { Name      => "IncidentCoord", SortOrder => 2 },
        ],
    },
    {
        Name        => 'Classification',
        Type        => 'SelectSingle',
        Queue       => 'Incidents',
        Disabled    => 0,
        Description => 'Classification for Incidents RTIR queue',
        Values      => [
            { Name      => "Spam",              SortOrder => 1 },
            { Name      => "System Compromise", SortOrder => 2 },
            { Name      => "Query",             SortOrder => 3 },
            { Name      => "Scan",              SortOrder => 4 },
            { Name      => "Denial of Service", SortOrder => 5 },
            { Name      => "Piracy",            SortOrder => 6 },
        ],
    },
    {
        Name        => 'How Reported',
        Type        => 'SelectSingle',
        Queue       => 'Incident Reports',
        Disabled    => 0,
        Description => 'How the incident was reported for Incident Reports RTIR queue',
        Values => [
            { Name      => "Email",      SortOrder => 1 },
            { Name      => "Telephone", SortOrder => 2 },
            { Name      => "Other",     SortOrder => 3 },
        ]
    },
    {
        Name        => 'Reporter Type',
        Type        => 'SelectSingle',
        Queue       => 'Incident Reports',
        Disabled    => 0,
        Description => 'Reporter type for Incident Reports RTIR queue',
        Values      => [
            { Name => "customer",            SortOrder => 1 },
            { Name => "external individual", SortOrder => 2 },
            { Name => "other ISP",           SortOrder => 3 },
            { Name => "police",              SortOrder => 4 },
            { Name => "other IRT",           SortOrder => 5 },
            { Name => "other",               SortOrder => 6 },
        ]
    },
    {
        Name        => 'IP',
        Type        => 'IPAddressRangeMultiple',
        Queue       => ['Incidents', 'Incident Reports', 'Investigations', 'Blocks'],
        Disabled    => 0,
        Description => 'IP address for RTIR queues',
    },
    {
        Name        => 'Netmask',
        Type        => 'FreeformSingle',
        Queue       => 'Blocks',
        Disabled    => 0,
        Description => 'Network mask for Blocks RTIR queue',
    },
    {
        Name        => 'Port',
        Type        => 'FreeformSingle',
        Queue       => 'Blocks',
        Disabled    => 0,
        Description => 'Port for Blocks RTIR queue',
    },
    {
        Name        => 'Where Blocked',
        Type        => 'FreeformSingle',
        Queue       => 'Blocks',
        Disabled    => 0,
        Description => 'Where the block is placed for Blocks RTIR queue',
    },
    {
        Name        => 'Customer',
        Type        => 'SelectMultiple',
        Queue       => 'Incident Reports',
        Disabled    => 0,
        Values      => [ ],
        Description => 'Customer for Incident Reports RTIR queue',
    },
    {
        Name        => 'Customer',
        Type        => 'SelectSingle',
        Queue       => 'Investigations',
        Disabled    => 0,
        Values      => [ ],
        Description => 'Customer for Investigations RTIR queue',
    },
);

@ScripActions = (
    {  Name        => 'RTIR Set Incident Due',    # loc
       Description => 'Set the due date of parent Incident' ,                                            # loc
       ExecModule => 'RTIR_SetDueIncident',
    },
    {  Name        => 'RTIR Set How Reported',    # loc
       Description => 'Set how the Incident Report was reported' ,                                            # loc
       ExecModule => 'RTIR_SetHowReported',
    },
    {  Name        => 'RTIR Resolve Children',    # loc
       Description => "Resolve an Incident's children",                                            # loc
       ExecModule => 'RTIR_ResolveChildren',
    },
    {  Name        => 'RTIR Change Child Ownership',    # loc
       Description => "Change the ownership of Incident's children",                                            # loc
       ExecModule => 'RTIR_ChangeChildOwnership',
    },
    {  Name        => 'RTIR Change Parent Ownership',    # loc
       Description => 'Change the ownership of the parent Incident' ,                                            # loc
       ExecModule => 'RTIR_ChangeParentOwnership',
    },
    {  Name        => 'RTIR Open Parent',    # loc
       Description => 'Open the parent Incident when a child reopens' ,                                            # loc
       ExecModule => 'RTIR_OpenParent',
    },
    {  Name        => 'RTIR Set Block Status',    # loc
       Description => 'Set the status of a Block' ,                                            # loc
       ExecModule => 'RTIR_SetBlockStatus',
    },
    {  Name        => 'RTIR Set Incident Resolution',    # loc
       Description => 'Set the default resolution of an Incident' ,                                            # loc
       ExecModule => 'RTIR_SetIncidentResolution',
    },
    {  Name        => 'RTIR parse message for IPs',    # loc
       Description => 'Set IP custom field from message content' ,                                            # loc
       ExecModule  => 'RTIR_FindIP',
    },
    {  Name        => 'RTIR merge IPs',    # loc
       Description => 'Merge multiple IPs on ticket merge' ,                                            # loc
       ExecModule  => 'RTIR_MergeIPs',
    },
    {  Name        => 'RTIR Set Constituency',    # loc
       Description => 'Set and cascade Constituency custom field' ,                                            # loc
       ExecModule  => 'RTIR_SetConstituency',
    },
    {  Name        => 'RTIR Set Constituency Group',    # loc
       Description => 'Set group responsible for constituency' ,                                            # loc
       ExecModule  => 'RTIR_SetConstituencyGroup',
    },
    {  Name        => 'RTIR Activate Ticket',    # loc
       Description => 'Set status to first active possible',            # loc
       ExecModule  => 'RTIR_Activate',
    },
);

@ScripConditions = (

    {  Name        => 'RTIR Customer Response',    # loc
       Description => 'Detect an external response' ,                                            # loc
       ApplicableTransTypes => 'Correspond',
       ExecModule => 'RTIR_CustomerResponse',
    },
    {  Name        => 'RTIR Staff Response',    # loc
       Description => 'Detect an internal response' ,                                            # loc
       ApplicableTransTypes => 'Correspond',
       ExecModule => 'RTIR_StaffResponse',
    },
    {  Name        => 'RTIR Close Ticket',    # loc
       Description => 'A ticket is rejected or resolved' ,                                            # loc
       ApplicableTransTypes => 'Any',
       ExecModule  => 'StatusChange',
       Argument    => 'old: initial, active; new: inactve',
    },
    {  Name        => 'RTIR Reopen Ticket',    # loc
       Description => 'A closed ticket is reopened' ,                                            # loc
       ApplicableTransTypes => 'Any',
       ExecModule  => 'StatusChange',
       Argument    => 'old: inactive; new: initial, active',
    },
    {  Name        => 'RTIR Require Due Change',    # loc
       Description => 'The due date of the parent incident must be changed',                                            # loc
       ApplicableTransTypes => 'Any',
       ExecModule => 'RTIR_RequireDueChange',
    },
    {  Name        => 'RTIR Require Constituency Change',    # loc
       Description => 'The constituency must be changed',                                            # loc
       ApplicableTransTypes => 'Any',
       ExecModule => 'RTIR_RequireConstituencyChange',
    },
    {  Name        => 'RTIR Require Constituency Group Change',    # loc
       Description => 'A group responsible for constituency must be changed',                                            # loc
       ApplicableTransTypes => 'Any',
       ExecModule => 'RTIR_RequireConstituencyGroupChange',
    },
    {  Name        => 'RTIR Linking To Incident',    # loc
       Description => 'Whenever ticket is linked to incident or created with link',                                 # loc
       ApplicableTransTypes => 'Create,AddLink',
       ExecModule => 'RTIR_LinkingToIncident',
    },
    {  Name        => 'RTIR Merge',    # loc
       Description => 'Whenever ticket is merged into another one',                                 # loc
       ApplicableTransTypes => 'AddLink',
       ExecModule => 'RTIR_Merge',
    },
    {  Name                 => 'RTIR Require IRs Activation',               # loc
       Description          => "Whenever it's time to activate report",                                        # loc
       ApplicableTransTypes => 'Correspond',
       ExecModule           => 'RTIR_RequireReportActivation',
    },
);

@Scrips = (
    {  Description       => "SetHowReported",
       Queue             => 'Incident Reports',
       ScripCondition    => 'On Create',
       ScripAction       => 'RTIR Set How Reported',
       Template          => 'Blank' },

    {  Description       => "On Correspond Change Status of the Block",
       Queue             => 'Blocks',
       ScripCondition    => 'On Correspond',
       ScripAction       => 'RTIR Set Block Status',
       Template          => 'Blank', },
    {  Description       => "On Linking To Incident Activate Report",
       Queue             => 'Incident Reports',
       ScripCondition    => 'RTIR Linking To Incident',
       ScripAction       => 'RTIR Activate Ticket',
       Template          => 'Blank' },
    {  Description       => "On Correspond (not every) Activate Report",
       Queue             => 'Incident Reports',
       ScripCondition    => 'RTIR Require IRs Activation',
       ScripAction       => 'RTIR Activate Ticket',
       Template          => 'Blank' },

    {  Description       => "Set Due Date On Incident",
       Queue             => ['Incident Reports', 'Investigations', 'Blocks' ],
       ScripCondition    => 'RTIR Require Due Change',
       ScripAction       => 'RTIR Set Incident Due',
       Template          => 'Blank', },

    {  Description       => "ResolveAllChildren",
       Queue             => 'Incidents',
       ScripCondition    => 'On Status Change',
       ScripAction       => 'RTIR Resolve Children',
       Template          => 'Blank', },

    {  Description       => "FixOwnership",
       Queue             => 'Incidents',
       ScripCondition    => 'On Owner Change',
       ScripAction       => 'RTIR Change Child Ownership',
       Template          => 'Blank', },
    {  Description       => "FixOwnership",
       Queue             => [ 'Incident Reports', 'Investigations', 'Blocks' ],
       ScripCondition    => 'On Owner Change',
       ScripAction       => 'RTIR Change Parent Ownership',
       Template          => 'Blank', },

    {  Description       => "ReopenIncident",
       Queue             => [ 'Incident Reports', 'Investigations', 'Blocks' ],
       ScripCondition    => 'On Status Change',
       ScripAction       => 'RTIR Open Parent',
       Template          => 'Blank', },

    {  Description       => "SetDefaultIncidentResolution",
       Queue             => 'Incidents',
       ScripCondition    => 'On Status Change',
       ScripAction       => 'RTIR Set Incident Resolution',
       Template          => 'Blank', },

    {  Description       => "NotifyOnClose",
       Queue             => 'Blocks',
       ScripCondition    => 'RTIR Close Ticket',
       ScripAction       => 'Notify Requestors',
       Template          => 'BlockRemoved' },

    {  Description       => "SetIPFromContent",
       Queue             => ['Incidents', 'Incident Reports', 'Investigations', 'Blocks'],
       ScripCondition    => 'On Correspond',
       ScripAction       => 'RTIR parse message for IPs',
       Template          => 'Blank' },
    {  Description       => "SetIPFromContent",
       Queue             => ['Incidents', 'Incident Reports', 'Investigations', 'Blocks'],
       ScripCondition    => 'On Create',
       ScripAction       => 'RTIR parse message for IPs',
       Template          => 'Blank' },

    {  Description       => "MergeIPs",
       Queue             => ['Incidents', 'Incident Reports', 'Investigations', 'Blocks'],
       ScripCondition    => 'RTIR Merge',
       ScripAction       => 'RTIR merge IPs',
       Template          => 'Blank' },

    {  Description       => "SetConstituency",
       Queue             => ['Incidents', 'Incident Reports', 'Investigations', 'Blocks'],
       ScripCondition    => 'RTIR Require Constituency Change',
       ScripAction       => 'RTIR Set Constituency',
       Template          => 'Blank' },
    {  Description       => "SetConstituencyGroup",
       Queue             => ['Incidents', 'Incident Reports', 'Investigations', 'Blocks'],
       ScripCondition    => 'RTIR Require Constituency Group Change',
       ScripAction       => 'RTIR Set Constituency Group',
       Template          => 'Blank' },
);

# WARNING: If you change content of the templates, don't forget to
# mention it in the UPGRADING file

@Templates = (
    { Queue       => 'Blocks',
      Name        => 'Autoreply',
      Description => 'Sent when a block is created',
      Content     => 'RT-Attach-Message: yes
Subject: { $Ticket->Subject }

{ $Transaction->Content }

{ my $output = "";
  my @mailfields = ( "IP", "Netmask", "Port", "Where Blocked" );

  my $CustomFields = $Ticket->QueueObj->TicketCustomFields;
  while ( my $CustomField = $CustomFields->Next ) {
    my $name = $CustomField->Name;
    next unless grep lc $_ eq lc $name, @mailfields;

    my $Values = $Ticket->CustomFieldValues( $CustomField->Id );
    while ( my $Value = $Values->Next ) {
      $output .= $name .": ". $Value->Content ."\n";
    }
  }
  return $output;
}
-------------------------------------------------------------------------
Please include the string:

         [{ $Ticket->QueueObj->SubjectTag || $rtname } #{ $Ticket->id }]

in the subject line of all future correspondence about this issue. To do so, 
you may reply to this message.

                        Thank you,
                        { $Ticket->QueueObj->CorrespondAddress }', },
    { Queue       => 'Blocks',
      Name        => 'BlockRemoved',
      Description => 'Sent when a block is removed',
      Content     => 'Subject: {$Ticket->Subject}

Block #{$Ticket->id} was removed.

{ my $output = "";
  my @mailfields = ( "IP", "Netmask", "Port", "Where Blocked" );

  my $CustomFields = $Ticket->QueueObj->TicketCustomFields;
  while ( my $CustomField = $CustomFields->Next ) {
    my $name = $CustomField->Name;
    next unless grep lc $_ eq lc $name, @mailfields;

    my $Values = $Ticket->CustomFieldValues( $CustomField->Id );
    while ( my $Value = $Values->Next ) {
      $output .= $name .": ". $Value->Content ."\n";
    }
  }
  return $output;
}
-------------------------------------------------------------------------
Please include the string:

         [{ $Ticket->QueueObj->SubjectTag || $rtname } #{$Ticket->id}]

in the subject line of all future correspondence about this issue. To do so, 
you may reply to this message.

                        Thank you,
                        {$Ticket->QueueObj->CorrespondAddress()}', },
    { Queue       => 'Investigations',
      Name        => 'Autoreply',
      Description => 'Sent when an investigation is launched',
      Content     => 'RT-Attach-Message: yes
Subject: {$Ticket->Subject}

{$Transaction->Content()}

-------------------------------------------------------------------------
Please include the string:

         [{ $Ticket->QueueObj->SubjectTag || $rtname } #{$Ticket->id}]

in the subject line of all future correspondence about this issue. To do so, 
you may reply to this message.

                        Thank you,
                        {$Ticket->QueueObj->CorrespondAddress()}', },
);

@Groups = (
    { Name        => 'DutyTeam',
      Type        => 'Privileged',
      Domain      => 'UserDefined',
      Instance    => '',
      Description => 'Duty Team Members',    # loc
    },
    { Name        => 'DutyTeam EDUNET',
      Type        => 'Privileged',
      Domain      => 'UserDefined',
      Instance    => '',
      Description => 'Duty Team responsible for EDUNET constituency',    # loc
    },
    { Name        => 'DutyTeam GOVNET',
      Type        => 'Privileged',
      Domain      => 'UserDefined',
      Instance    => '',
      Description => 'Duty Team responsible for GOVNET constituency',    # loc
    },
);

@ACL = ();
for my $queue (map {$_->{Name}} @Queues) {
    push @ACL, (
    { GroupDomain => 'RT::Queue-Role',
      GroupType => 'Owner',
      Queue => $queue,
      Right  => 'ModifyTicket', },
    { GroupId => 'DutyTeam',      # - principalId
      GroupDomain => 'UserDefined',
      Queue => $queue,
      Right  => 'ShowTemplate', },
    { GroupId => 'DutyTeam',      # - principalId
      GroupDomain => 'UserDefined',
      Queue => $queue,
      Right  => 'CreateTicket', },
    { GroupId => 'DutyTeam',      # - principalId
      GroupDomain => 'UserDefined',
      Queue => $queue,
      Right  => 'OwnTicket', },
    { GroupId => 'DutyTeam',      # - principalId
      GroupDomain => 'UserDefined',
      Queue => $queue,
      Right  => 'CommentOnTicket', },
    { GroupId => 'DutyTeam',      # - principalId
      GroupDomain => 'UserDefined',
      Queue => $queue,
      Right  => 'SeeQueue', },
    { GroupId => 'DutyTeam',      # - principalId
      GroupDomain => 'UserDefined',
      Queue => $queue,
      Right  => 'ShowTicket', },
    { GroupId => 'DutyTeam',      # - principalId
      GroupDomain => 'UserDefined',
      Queue => $queue,
      Right  => 'ShowTicketComments', },
    { GroupId => 'DutyTeam',      # - principalId
      GroupDomain => 'UserDefined',
      Queue => $queue,
      Right  => 'StealTicket', },
    { GroupId => 'DutyTeam',      # - principalId
      GroupDomain => 'UserDefined',
      Queue => $queue,
      Right  => 'TakeTicket', },
    { GroupId => 'DutyTeam',      # - principalId
      GroupDomain => 'UserDefined',
      Queue => $queue,
      Right  => 'Watch', },
);}

push @ACL, (
    { GroupType => 'Everyone', 
      GroupDomain => 'SystemInternal',
      Queue => 'Incident Reports',
      Right  => 'CreateTicket', },
    { GroupType => 'Everyone', 
      GroupDomain => 'SystemInternal',
      Queue => 'Incident Reports',
      Right  => 'ReplyToTicket', },

    { GroupType => 'Everyone', 
      GroupDomain => 'SystemInternal',
      Queue => 'Investigations',
      Right  => 'ReplyToTicket', },

    { GroupType => 'Everyone', 
      GroupDomain => 'SystemInternal',
      Queue => 'Blocks',
      Right  => 'ReplyToTicket', },
);

push @ACL, (
    { GroupId => 'DutyTeam',      # - principalId
      GroupDomain => 'UserDefined',
      Queue => 0,
      Right => 'ModifySelf', },
    { GroupId => 'DutyTeam',      # - principalId
      GroupDomain => 'UserDefined',
      Queue => 0,
      Right => 'CreateSavedSearch', },
    { GroupId => 'DutyTeam',      # - principalId
      GroupDomain => 'UserDefined',
      Queue => 0,
      Right => 'EditSavedSearches', },
    { GroupId => 'DutyTeam',      # - principalId
      GroupDomain => 'UserDefined',
      Queue => 0,
      Right => 'LoadSavedSearch', },
    { GroupId => 'DutyTeam',      # - principalId
      GroupDomain => 'UserDefined',
      Queue => 0,
      Right => 'ShowSavedSearches', },
);

for my $cf (@CustomFields) {
    push @ACL, (
        { GroupId => 'DutyTeam',
          GroupDomain => 'UserDefined',
          CF => $cf->{Name},
          Queue => $cf->{Queue},
          Right => 'SeeCustomField', },
        { GroupId => 'DutyTeam',
          GroupDomain => 'UserDefined',
          CF => $cf->{Name},
          Queue => $cf->{Queue},
          Right => 'ModifyCustomField', },
    );
}

@Attributes = ( {
    Name => 'RTIR_HomepageSettings',
    Description => 'RTIR homepage settings',
    Content => { 
        body => [
            { type => 'component',  name => '/RTIR/Elements/NewReports' },
            { type => 'component',  name => '/RTIR/Elements/UserDueIncidents' },
            { type => 'component',  name => '/RTIR/Elements/NobodyDueIncidents' },
            { type => 'component',  name => '/RTIR/Elements/DueIncidents' },
        ],
        summary => [
            { type => 'component', name => 'RefreshHomepage' },
            { type => 'component', name => 'Quicksearch' },
        ]
    },
} );

@Final = ( sub {
    $| = 1;
    my $CurrentUser = RT::CurrentUser->new();
    $CurrentUser->LoadByName('RT_System');

    require RT::Class;
    my $class = RT::Class->new($CurrentUser);
    $class->Load('Templates');
    unless ($class->Id) {
        $RT::Logger->info("Creating 'Templates' Article class.");
        my ($id, $msg) = $class->Create(
            Name => 'Templates',
            Description => 'Response templates',
            HotList => 1
        );
        die $msg unless $id;

        foreach my $qname ( map $_->{'Name'}, @Queues ) {
            my $queue = RT::Queue->new( $CurrentUser );
            $queue->Load( $qname );
            next unless $queue->id;

            my ($status, $msg) = $class->AddToObject( $queue );
            $RT::Logger->error("Couldn't apply class to queue '$qname'")
                unless $status;
        }
    }

    require RT::Article;
    my $response = RT::CustomField->new($CurrentUser);
    $response->Load('Response');
    unless ($response->Id and $response->LookupType eq RT::Article->CustomFieldLookupType) {
        $RT::Logger->info("Creating 'Response' Article custom field.");
        my ($id, $msg) = $response->Create(
            Name => 'Response',
            Type => 'Text',
            MaxValues => 1,
            Description => 'Response to be inserted into the ticket',
            LookupType => RT::Article->CustomFieldLookupType,
        );
        die $msg unless $id;
    }

    $response->AddToObject($class);
    $class->SetAttribute(Name => "Skip-$_", Content => 1)
      for ("Name", "Summary", "CF-Title-".$response->Id);

    my $group = RT::Group->new($CurrentUser);
    $group->LoadUserDefinedGroup( "DutyTeam" );
    die "Can't load group" unless $group->Id;
    $group->PrincipalObj->GrantRight( Right => $_, Object => $response )
      for qw/SeeCustomField ModifyCustomField/;
    $group->PrincipalObj->GrantRight( Right => $_, Object => $class )
      for qw/AdminClass AdminTopics CreateArticle ModifyArticle ModifyArticleTopics SeeClass ShowArticle ShowArticleHistory DeleteArticle/;

} );

