%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2026 Best Practical Solutions, LLC
%#                                          <sales@bestpractical.com>
%#
%# (Except where explicitly superseded by other copyright notices)
%#
%#
%# LICENSE:
%#
%# This work is made available to you under the terms of Version 2 of
%# the GNU General Public License. A copy of that license should have
%# been provided with this software, but in any event can be snarfed
%# from www.gnu.org.
%#
%# This work 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., 51 Franklin Street, Fifth Floor, Boston, MA
%# 02110-1301 or visit their web page on the internet at
%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
%#
%#
%# CONTRIBUTION SUBMISSION POLICY:
%#
%# (The following paragraph is not intended to limit the rights granted
%# to you to modify and distribute this software under the terms of
%# the GNU General Public License and is only of importance to you if
%# you choose to contribute your changes and enhancements to the
%# community by submitting them to Best Practical Solutions, LLC.)
%#
%# By intentionally submitting any modifications, corrections or
%# derivatives to this work, or any other work intended for use with
%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
%# you are the copyright holder for those contributions and you grant
%# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
%# royalty-free, perpetual, license to use, copy, create derivative
%# works based on those contributions, and sublicense and distribute
%# those contributions and any derivatives thereof.
%#
%# END BPS TAGGED BLOCK }}}
% if ( $q ) {
<h2>WHOIS Results</h2>
% if ( $TicketObj && RT::IR::TicketType( Ticket => $TicketObj ) eq 'Incident' ) {
<form method="post" action="<% RT::IR->HREFTo("Tools/Lookup.html")|n%>">
<input type="hidden" name="incidentid" value="<% $TicketObj->id %>" />
<input type="hidden" name="Subject" value="<% $TicketObj->Subject %>" />
% }
% }
% if ( $WhoisError ) {
<b><% $WhoisError %></b>
% }
<%PERL>
my $DoInvestigate = 0;
if ($WhoisIterator) {
    while (my $obj = $WhoisIterator->next) {
        $SavedContent .= $obj->content . "\n";
        my @lines_starting_with_space = grep /^(\s+)(\w+)/, $obj->content;
        if ($handparse || $#lines_starting_with_space >= 4) {    #we couldn't parse that. suck
            my $content = join "", $obj->content;
            $m->comp('/Elements/MakeClicky',
                     object        => $TicketObj,
                     lookup_params => "ticket=" . ($TicketObj ? $TicketObj->id : 0) . "&server=$WhoisServer",
                     content       => \$content,);
            $DoInvestigate = 1 if $content =~ /Requestorbox/ig;
</%PERL>
<pre><% $content |n %></pre><br />
%       } else {
Structured RIPE whois data returned.
Click <a href="Lookup.html?q=<% $q |u %>&server=<% $WhoisServer |u %>&handparse=1">here</a> to manually parse this data.
<br />Warnings <% $obj->warnings %>
<br />errors <% $obj->errors %>
<hr />
<%perl>
foreach my $attribute ( $obj->attributes ) {
   foreach my $value ( $obj->$attribute() ) {
</%perl>
     <b><%$attribute%></b>: 
<& /Elements/MakeClicky, 
    ticket => $TicketObj, 
    lookup_params => "ticket=".$TicketObj->id, 
    content => \$value &>
<% $value |n %><br />
<%perl>
     }
   }

  }
 }
 }
</%perl>
%# Don't offer the option of Investigating to unless there are addresses
% if ( $DoInvestigate ) {
<& /Elements/Submit,
    Name => "InvestigateTo",
    Caption => loc("Investigate to selected addresses"),
    Label => loc("Investigate"),
    FullWidth => 1,
&>
<& /Elements/Submit,
    Name => "InvestigateWithScriptedActions",
    Caption => loc("Investigate to selected addresses"),
    Label => loc("Investigate using scripted action"),
    FullWidth => 1,
&>
% }
</form>

% if ($TicketObj and $WhoisIterator and $SavedContent) {
%   my $url = RT::IR->HREFTo(
%       "Tools/Lookup.html?"
%       . $m->comp(
%           '/Elements/QueryString',
%           ticket => $TicketObj->id,
%           q => $q,
%       )
%   );
    <div class="container">
      <div class="row justify-content-center">
        <div class="col-auto">
          <form method="post" action="<% $url %>">
            <input type="hidden" name="LookupResults" value="<% $SavedContent %>" />
            <input class="btn btn-primary" type="submit" value="<% loc('Save these results to the ticket') %>" id="save-results-to-ticket" />
          </form>
        </div>
      </div>
    </div>
% }

<%args>
$q =>  undef
$handparse => 1
$TicketObj => undef
$WhoisServer => undef
$server => undef
</%args>
<%init>

$WhoisServer = $server unless defined $WhoisServer;
unless ($WhoisServer) {
    return unless RT->Config->Get('RunWhoisRequestByDefault');

    $WhoisServer = RT::IR->FirstWhoisServer;

    if ( $TicketObj and $TicketObj->QueueObj->FirstCustomFieldValue('RTIR default WHOIS server') ){
        $WhoisServer = $TicketObj->QueueObj->FirstCustomFieldValue('RTIR default WHOIS server');
    }
}

return unless $q;

my ($WhoisIterator, $WhoisError) = RT::IR->WhoisLookup(
    Server => $WhoisServer,
    Query => $q,
    CurrentUser => $session{'CurrentUser'},
);

my $SavedContent = '';
</%init>
