%# Note that this file is not a valid *.html file!
%# It is intended to be a bottlepy - style template
%# used for the scripting part of TheOnionBox!

<%

    try:
       from tob.log import sanitize_for_html
    except ModuleNotFoundError:
        from theonionbox.tob.log import sanitize_for_html
    end


tor = get('tor') if tor is None else tor
oo_details = get('oo_details') if oo_details is None else oo_details
oo_bw = get('oo_bw')
# oo_show = get('oo_show', False)
fp = get('family_fp', None)


%>

{{!header_row('Tor', 'Network Status', 'network')}}

% if fp is None or len(fp) < 1:
    <div class="row">
        <div class="{{box_datum_grid}}">
        </div>
        <div class="{{box_value_grid}} box_value_margin alert alert-info">
            This node is a Tor client. No network status available.
        </div>
    </div>

% else:

%   if oo_details.is_relay() and oo_details.is_bridge():
        <div class="row">
            <div class="{{box_datum_grid}}">
            </div>
            <div class="{{box_value_grid}} box_value_margin alert alert-danger">
                According to Onionoo the node with fingerprint '{{oo_details('fingerprint')}}' is a Relay <b>and</b> a Bridge.
                This shall not happen!
            </div>
        </div>
<%
    end

    if oo_details.is_relay() :
        publ = oo_details.published()
        if publ is not None:
%>
            {{!standard_row('Status Time', publ)}}
%       end

        <div class="row">
            <div class="{{box_datum_grid}}">
                Fingerprint
            </div>
            <div class="{{box_value_grid}} box_value_margin">
                % fp = oo_details('fingerprint')
                   {{fp}}
                </a>
            </div>
        </div>

        {{!standard_row('Type', 'Relay')}}
        {{!standard_row('Nickname', oo_details('nickname'))}}
        {{!standard_row('Platform', oo_details('platform'))}}
        {{!standard_row('Contact Info', oo_details('contact'))}}
        {{!standard_row('OR Addresses', ' | '.join(oo_details('or_addresses')))}}
        {{!standard_row('First Seen', oo_details('first_seen'))}}
        {{!standard_row('Last Seen', oo_details('last_seen'))}}
        % if oo_details('last_restarted'):
            {{!standard_row('Latest Restart', oo_details('last_restarted'))}}
        % end
        {{!standard_row('Latest Address Change', oo_details('last_changed_address_or_port'))}}
        % if oo_details('exit_addresses'):
            {{!standard_row('Exit Addresses', ' | '.join(oo_details('exit_addresses')))}}
        % end
        % if oo_details('dir_address'):
            {{!standard_row('DIR Address', oo_details('dir_address'))}}
        % end
        {{!standard_row('Running', oo_details('running'))}}
        % if oo_details('hibernating'):
            {{!standard_row('Hibernating', oo_details('hibernating'))}}
        % end
        % if oo_details('flags'):
            {{!standard_row('Flags', ' | '.join(oo_details('flags')))}}
        % end

<%
        def location_row(datum='', value=''):

            # If value holds unicode characters, standard processing generates an exception!
            out = ""

            # Python2
            try:
                out = unicode("""
                    <div class='row'>
                        <div class='truncate col-6 box_datum'>{}</div>
                        <div class='col-6 box_value box_value_margin'>{}</div>
                    </div>""").format(datum, value)
            except:
                # Python3
                try:
                    out = """
                        <div class='row'>
                            <div class='truncate col-6 box_datum'>{}</div>
                            <div class='col-6 box_value box_value_margin'>{}</div>
                        </div>""".format(datum, value)
                except:
                    pass
                end
            end

            return out
        end

        geoip = get('geoip')
        if len(oo_details('or_addresses')) > 0:
            ip = oo_details('or_addresses')[0]
%>

            {{!group_row('Location')}}

            <div class="row">
                <div class='col-6' id="location_data">
                    {{!location_row('Country', geoip.country(ip, oo_details('country')))}}
                    {{!location_row('Country Name', geoip.country_name(ip, oo_details('country_name')))}}
                    % rn = geoip.region_name(ip, oo_details('region_name'))
                    % if rn is not None:
                        {{!location_row('Region Name', rn)}}
                    % end
                    % cn = geoip.city_name(ip, oo_details('city_name'))
                    % if cn is not None:
                        {{!location_row('City Name', cn)}}
                    % end
                    % geoip_lat = geoip.latitude(ip, oo_details('latitude'))
                    % geoip_long = geoip.longitude(ip, oo_details('longitude'))
                    {{!location_row('Latitude', geoip_lat)}}
                    {{!location_row('Longitude', geoip_long)}}
                </div>

                <div class='col-4 map' id="location_map">

                % if geoip_lat and geoip_long:

                    <link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css">
                    <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js" type="text/javascript"></script>

                % end

                </div>
                <div class='col-2'></div>
            </div>

        % end

<%
        ep = oo_details('exit_policy')
        eps = oo_details('exit_policy_summary')
        ep6s = oo_details('exit_policy_v6_summary')

        if ep or eps or ep6s:
%>

            {{!group_row('Exit Policies')}}

            % if ep is not None:
                <div class="row">
                    <div class="{{box_datum_grid}}">
                        Policy Lines
                    </div>
                    <div class="{{box_value_grid}} box_value_margin box_value_columns">
                        % for item in ep:
                            {{item}}<br>
                        % end
                    </div>
                </div>
            % end

            % if eps is not None:
                % if 'accept' in eps:
                    % ar = 'Accept'
                    % epdata = eps['accept']
                % else:
                    % ar = 'Reject'
                    % epdata = eps['reject']
                % end
                <div class="row">
                    <div class="{{box_datum_grid}}">
                        {{'{} Summary IPv4'.format(ar)}}
                    </div>
                    <div class="{{box_value_grid}} box_value_margin box_value_columns">
                        % for item in epdata:
                            {{item}}<br>
                        % end
                    </div>
                    <div class="{{box_right_grid}}"></div>
                </div>
            % end

            % if ep6s is not None:
                % if 'accept' in ep6s:
                    % ar = 'Accept'
                    % epdata = ep6s['accept']
                % else:
                    % ar = 'Reject'
                    % epdata = ep6s['reject']
                % end
                <div class="row">
                    <div class="{{box_datum_grid}}">
                        {{'{} Summary IPv6'.format(ar)}}
                    </div>
                    <div class="{{box_value_grid}} box_value_margin box_value_columns">
                        % for item in epdata:
                            {{item}}<br>
                        % end
                    </div>
                    <div class="{{box_right_grid}}"></div>
                </div>
            % else:
                %# From Onionoo:
                %# "Missing if the relay rejects all connections to IPv6 addresses."
                {{!standard_row('Reject Summary IPv6', '1-65535')}}
            % end
        %end

        {{!group_row('Autonomous System')}}
        {{!standard_row('Host', oo_details('host_name'))}}
        {{!standard_row('Name', oo_details('as_name'))}}
        {{!standard_row('Number', oo_details('as_number'))}}

    % # 20190825: Family Members processing was removed here!

    % #TODO: Processing for a bridge

% end
