#!/usr/bin/perl -w
# Tough skin and sharp criticism ahead
use strict;
use warnings;
# Dependencies
use File::Slurp;
use Getopt::Std;
require RPC::XML;
require RPC::XML::Client;

my $satisfied = 0;
my $address = "";
my $email = "";
my $port = 2443;

my $options=();
getopts("ha:k:s:e:p:", \%main::options);

if (defined $main::options{h}) {
	print "This program checks InterWorx mail queue\n";
	print "Required arguments:\n";
	print "    -h    see this message\n";
	print "    -a    address of the NodeWorx server\n";
	print "    -s    file where the super secret password is kept\n";
	print "    -e    email of the authorized staffer or bot\n";
	print "    -k    file where API key is held\n";
	print "Provide either API key or email+password\n";
	print "Optional settings:\n";
	print "    -p    sets a port number, if not set, defaults to 2443\n";
	exit 0;
}

if (defined $main::options{a}) {
	$satisfied += 1;
	$address = $main::options{a};
}
if (defined $main::options{s}) {
	$satisfied += 2;
	$main::secret_file = $main::options{s};
}
if (defined $main::options{e}) {
	$satisfied += 4;
	$email = $main::options{e};
}
if (defined $main::options{k}) {
	$satisfied += 8;
	$main::key_file = $main::options{k};
}
if (defined $main::options{p}) {
	$port = $main::options{p};
}
if (($satisfied != 7 ) && ( $satisfied != 9)) {
	print "ls_mailq perl script is missing argument(s)\n";
	print "Try with -h to see what is required\n";
	exit -1;
} elsif (($satisfied & 14) == 14) {
	print "ls_mailq can only work with email+pass or API key\n";
	exit -2;
}

my $cli = RPC::XML::Client->new("https://$address:$port/xmlrpc");
# Let's prepare $apikey for when we use email+address
if(($satisfied & 6) == 6) {
	open my $secret_file_info, $main::secret_file or die "Could not open $main::secret_file: $!\n";
	my $password = <$secret_file_info>;
	close $secret_file_info;
	chomp $password;

	$main::WishIWroteThisInC = RPC::XML::struct->new({
		'email' => RPC::XML::string->new("$email"),
		'password' => RPC::XML::string->new("$password")
	});
} elsif(($satisfied & 8) == 8) {
# Let's prepare $apikey for when we use an actual API key
	open my $key_file_info, $main::key_file or die "Could not open $main::key_file: $!\n";
	$main::WishIWroteThisInC = read_file($main::key_file);
	close $key_file_info;
	chomp $main::WishIWroteThisInC;
}

my $apikey = $main::WishIWroteThisInC;
my $ctrl_name = RPC::XML::string->new('/nodeworx/mail/queue');
my $action = RPC::XML::string->new('listStats');
my $input = RPC::XML::struct->new({});
my $resp = $cli->send_request('iworx.route',
	$apikey,
	$ctrl_name,
	$action,
	$input);
my $results = $resp->value();
if(ref($resp) ne 'RPC::XML::struct') {
	print "Failed to send valid call\n";
	print "Got: ",ref($resp),"\n";
	print "Expected: RPC::XML::struct\n";
	if (ref($results->{payload}) eq 'ARRAY') {
		my @payload = @{$results->{payload}};
		foreach (@payload) {
			my @key = @{$_};
			print "@key" . "\n";
		}
	} else {
		print $results->{payload}, "\n";
	}
	exit -4;
} elsif ($results->{status} != 0) {
	print "Bad response status\n";
	print "Got: ",$results->{stats},"\n";
	print "Expected: 0\n";
	if (ref($results->{payload}) eq 'ARRAY') {
		my @payload = @{$results->{payload}};
		foreach (@payload) {
			my @key = @{$_};
			print "@key" . "\n";
		}
	} else {
		print $results->{payload}, "\n";
	}
	exit -8;
}
print $address," mail queue summary with InterWorx API\n";
print "Remote: ",$results->{payload}[0]{count},"\n";
print "Local: ",$results->{payload}[1]{count},"\n";
print "Todo: ",$results->{payload}[2]{count},"\n";