# cpanel - /var/cpanel/perl/Cpanel/DAV/Config/CCS.pm
#                                                  Copyright 2019 cPanel, L.L.C.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

package Cpanel::DAV::Config::CCS;

use strict;
use warnings;

use parent "Cpanel::DAV::Config::Base";

use lib '/var/cpanel/perl5/lib/';

use Cpanel                 ();
use Cpanel::AdminBin::Call ();
use Cpanel::Version        ();

sub HTTP_PORT {
    my $greater_than_v84 = Cpanel::Version::compare( Cpanel::Version::getversionnumber, ">=", "11.85", );
    return $greater_than_v84 ? 2079 : 8080;
}

sub HTTPS_PORT {
    my $greater_than_v84 = Cpanel::Version::compare( Cpanel::Version::getversionnumber, ">=", "11.85", );
    $greater_than_v84 ? 2080 : 8443;
}

=head1 NAME

Cpanel::DAV::Config::CCS

=head1 SYNOPSIS

Module for defining the routes to getting Calendar and Contacts information for the CCS backend.

=head1 DESCRIPTION

Used only within the context of Cpanel::DAV::Config::get_calendar_contacts_config, see example usage there.
Basically this is how to tell the system what URLs, etc you need to return for a calendar in order for
it to properly display in the UI.

=head1 SEE ALSO

Cpanel::DAV::Config::Base

=head1 METHODS

=head2 new

Overrides base constructor in order to get us the UUID for the user.

=cut

sub new {
    my ( $class, $user ) = @_;
    my $obj = $class->SUPER::new($user);
    $obj->{uid} = $obj->_get_uid_for_user($user);
    return $obj;
}

sub _get_uid_for_user {
    my ( $self, $user ) = @_;

    # We don't wanna die quite yet if this is blank, as we don't catch that in Cpanel::DAV::Config.
    # Instead, die in the calendar or contacts getter in this case, as those are try/catch'd.
    # Also, note we have to fetch this via adminbin, as the user won't have access to the persistence DB to fetch the UID otherwise.
    my $uid = '';
    if ( $< != 0 ) {
        local $@;
        eval { $uid = Cpanel::AdminBin::Call::call( 'Cpanel', 'ccs', 'GET_UID', $user ); };
    }
    else {

        # We are already root, so we don't need to invoke adminbin
        local $@;
        eval {

            # Apparently some execution contexts for this engage in
            # "forgetting" about @INC that I set above, so I get to push
            # this to @INC in this context to get around it.
            push( @INC, "/var/cpanel/perl" ) if !grep { m{/var/cpanel/perl} } @INC;
            require Cpanel::CCS::Userdata;
            $uid = Cpanel::CCS::Userdata::get_user_uid( $user, 'autocreate' => 0 ) || '';
        };

    }

    return $uid;
}

=head2 PRINCIPAL_PATH

Returns a STRING which is the path to the principal data URL (relative to the server's root).

=cut

sub PRINCIPAL_PATH {
    return "principals/__uids__/$_[0]->{uid}/";
}

=head2 FREEBUSY_PATH

Returns a STRING which is the path to the free/busy data URL (relative to the server's root).

=cut

sub FREEBUSY_PATH {
    return "calendars/__uids__/$_[0]->{uid}/freebusy";
}

=head2 get_principal

Returns the UID of the principal, as that's all we actually need to figure out calendar and contact urls in this context

=cut

sub get_principal {
    return $_[0]->{uid};
}

=head2 get_contacts

Returns an ARRAY of contact information HASHREFs.

=cut

sub get_contacts {
    my ( $self, $principal ) = @_;
    return [
        {
            'name'        => 'Address Book',
            'description' => 'The default address book',
            'path'        => "/addressbooks/__uids__/$self->{uid}/addressbook",
        },
    ];
}

=head2 get_calendars

Returns an ARRAY of calendar information HASHREFs.

=cut

sub get_calendars {
    my ( $self, $principal ) = @_;
    return [
        {
            'name'        => 'Calendar',
            'description' => 'The default calendar',
            'path'        => "/calendars/__uids__/$self->{uid}/calendar",
        },
        {
            'name'        => 'Task List',
            'description' => 'The default task list',
            'path'        => "/calendars/__uids__/$self->{uid}/tasks",
        },
    ];
}

1;
