#!/usr/bin/env perl
# BEGIN BPS TAGGED BLOCK {{{
#
# COPYRIGHT:
#
# This software is Copyright (c) 2020-2022 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 }}}

use v5.10;
use strict;
use Fcntl ':flock';
use FindBin;
use lib "$FindBin::Bin/../lib";
use JSON;
use App::wsgetmail;
use File::Slurp;
use Pod::Usage;
use Getopt::Long;

my ($config_file, $options, $dry_run);
my ($debug, $help, $quiet) = (0,0,0);
my $response_matrix = {
    delete_message => { '400' => 'ignore', '404' => 'ignore' },

    default => { '5xx' => 'ignore' },
};

my $response_handling = '';

{
    my @buffer;

    sub maybe_print {
        if($quiet) {
            push @buffer, @_;
        }
        else {
            print @_;
        }
    }

    sub flush_and_die {
        print STDERR @buffer;
        @buffer = ();
        die @_;
    }
}

GetOptions('h|help|?' => \$help,
           "q|quiet" => \$quiet,
           "v|verbose|debug" => \$debug,
           'dry-run' => \$dry_run,
           "c|config|configuration=s" => \$config_file,
           "response-handling=s" => \$response_handling,
           'options=s' => \$options);

pod2usage(1) if $help;
pod2usage(1) unless ($config_file);
die "Can't find config file $config_file" unless (-f $config_file);

# parse options, over-ride config if provided extra options
my $config_json = read_file($config_file);
my $config = decode_json($config_json);
my $extra_options = (defined($options) && $options ) ? decode_json($options) : { };
foreach my $option ( keys %$extra_options ) {
    $config->{$option} = $extra_options->{$option};
}
$config->{dry_run} = $dry_run if (defined $dry_run);
$config->{debug} = $debug if (defined $debug);

# [
#   {
#       'value':'ignore',
#       'methods':{
#           'delete_message': ['400','404'],
#           'default':        ['5xx']
#       }
#   }
# ]
if ($response_handling) {
    my $response_overrides = decode_json($response_handling);

    foreach my $section (@$response_overrides) {
        my $value = $section->{value};
        my $methods = $section->{methods};

        foreach my $method (keys %$methods) {
            my $method_codes = $methods->{$method};

            foreach my $code (@$method_codes) {
                if ($value eq 'delete') {
                    delete $response_matrix->{$method}{$code};
                }
                else {
                    $response_matrix->{$method}{$code} = $value;
                }
            }
        }
    }
}

$config->{response_matrix} = $response_matrix;

my $foldername = $config->{folder};

$foldername =~ s{/}{_}g;

my $lock_file_name = '/tmp/' . join( '.', 'wsgetmail', $config->{username}, $foldername, 'lock' );

open my $lock_file_fh, '>', $lock_file_name or die "unable to open lock file $lock_file_name ($!)";

if ( !flock $lock_file_fh, LOCK_EX | LOCK_NB ) {
    print "$0 is already running for $config->{username}/$config->{folder} ($!)\n" unless $quiet;
    exit;
}

my $getmail = App::wsgetmail->new({config => $config});

maybe_print("\nfetching mail using configuration $config_file\n");

my $done_count = 0;
my $error_count = 0;

while (my $message = $getmail->get_next_message()) {
    my $ok = $getmail->process_message($message);
    if ($ok) {
        $done_count++;
    }
    else {
        $error_count++;
    }
}

maybe_print("\nprocessed $done_count messages\n");

flush_and_die("there were errors with $error_count messages\n") if $error_count;

__END__

=head1 NAME

wsgetmail - get mail from cloud webservices

=head1 SYNOPSIS

Run:

    wsgetmail [options] --config=wsgetmail.json

where C<wsgetmail.json> looks like:

    {
    "client_id": "abcd1234-xxxx-xxxx-xxxx-1234abcdef99",
    "tenant_id": "abcd1234-xxxx-xxxx-xxxx-123abcde1234",
    "secret": "abcde1fghij2klmno3pqrst4uvwxy5~0",
    "global_access": 1,
    "username": "rt-comment@example.com",
    "folder": "Inbox",
    "command": "/opt/rt5/bin/rt-mailgate",
    "command_args": "--url=http://rt.example.com/ --queue=General --action=comment",
    "command_timeout": 30,
    "action_on_fetched": "mark_as_read"
    }

=head1 DESCRIPTION

wsgetmail retrieves mail from a folder available through a web services API
and delivers it to another system. Currently, it only knows how to retrieve
mail from the Microsoft Graph API, and deliver it by running another command
on the local system.

=head1 CONFIGURATION

For full setup and configuration instructions, see L<App::wsgetmail>.

    perldoc App::wsgetmail

=head1 ARGUMENTS

=over 4

=item --config, --configuration, -c

Path of the primary wsgetmail JSON configuration file to read. This argument
is required. The configuration file is documented in the next section.

=item --options

A string with a JSON object in the same format as the configuration
file. Configuration in this object will override the configuration file. You
can use this to extend a base configuration. For example, given the
configuration in the synopsis above, you can process a second folder the
same way by running:

    wsgetmail --config=wsgetmail.json --options='{"folder": "Other Folder"}'

=item --verbose, --debug, -v

Log additional information about each mail API request and any problems
delivering mail.

=item --quiet, -q

In "quiet" mode wsgetmail suppresses its normal output on stdout.  This output
identifies the configuration file used and how many messages were processed.

If there are any errors this information is sent to stderr after the error
messages.

This does not affect the additional logging generated by the verbose/debug
flag.

The purpose for "quiet" mode is so that, when running wsgetmail multiple times
on a schedule using multiple configuration files, no output is produced unless
there are errors, and that output identifies the configuration file used.

=item --dry-run

Read mail and deliver it to the configured command, but don't run the
configured C<action_on_fetched> like deleting messages or marking them as
read.

=item --help, -h

Show this help documentation.

=item --response-handling

This option shouldn't have to exist.

This option exists because most of the MS Graph API calls can return
non-success responses sporadically that aren't related to any visible or
actionable issue, and may not even mean that the attempted action failed.

Previous versions of wsgetmail treated all of these as first-class errors,
complete with warning messages and aborting the run with a failure status.
For an administrator this creates a trickle of non-actionable alerts, unless
they choose to ignore all errors and output.

To complicate matters it isn't quite as simple as always ignoring specific
error codes. For example, one may want to treat a C<404 Not Found> status
code as success when deleting a message and still consider it to be failure
when trying to list the messages in a folder.

Version C<0.09> of wsgetmail changed the default handling of some non-success
status codes across the board (C<5xx - server-side errors>) and for some
specific non-success codes when deleting messages (C<400> and C<404>).

This seems to be both safe and comprehensive, but this is based on our
observations of operational behavior and does not seem to be supported by
documentation.

This option exists to allow administrators to modify or augment these new
default behaviors precisely.  This option is not intended to be used without
at least some understanding of the MS Graph API and how the
L<App::wsgetmail::MS365> module uses it.

See the C<response_matrix> section of L<App::wsgetmail::MS365> for more
information, including a list of recognized labels and a description of the
data in the matrix is used.

In addition to the C<ignore> value recognized by C<App::wsgetmail::MS365> this
option also makes use the special value C<delete>, which can be used to
remove the built-in defaults.

The value of this option is a JSON list of objects that are used to modify and
augment the default response matrix, where each element of the list directs
that one or more values be added to, or removed from, the matrix.

The default response matrix could be represented with the JSON:

  [
    {
      'value':'ignore',
      'labels':{
        'delete_message': ['400','404'],
        'default':        ['5xx']
      }
    }
  ]

or, more compactly, as:

  --response-matrix='[{"value":"ignore","labels":{"delete_message":["400","404"],"default":["5xx"]}}]'

This directs C<App::wsgetmail::MS365> to C<ignore> (that is, to treat as
closely as possible to success) HTTP status codes C<400> and C<404> when
deleting messages as the post-fetch action, and to ignore all C<5xx> HTTP
status codes (server errors) for all calls.

One case worth mentioning specifically is the API call referred to as
C<get_message_mime_content>. When this fails there is no content to process
so neither the C<command> nor the follow-up action will be performed.
However, in order to maintain the illusion of success for the caller, the
method returns a value that indicates that the message was processed, which
would cause it to be reflected in the counts if they are emitted.

=back


=head1 AUTHOR

Best Practical Solutions, LLC <modules@bestpractical.com>

=head1 LICENSE AND COPYRIGHT

This software is Copyright (c) 2015-2020 by Best Practical Solutions, LLC.

This is free software, licensed under:

The GNU General Public License, Version 2, June 1991

=cut
