#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;
use Data::Dumper;
use Mail::Karmasphere::Client qw(:all);
# use Carp qw(cluck);
# $SIG{__WARN__} = sub { cluck @_; };

$Data::Dumper::Indent = 1;

my ($username, $password);
my ($host, $port, $tcp, $single, $debug, $version);
my (@feeds, @composites, @combiners);
my (@ip4, @ip6, @domain, @url, @email);
my $flags = FL_FACTS;

my $result = GetOptions(
	"debug"			=> \$debug,
	"host=s"		=> \$host,
	"port=i"		=> \$port,
	"feed=s"		=> \@feeds,
	"composite=s"	=> \@composites,
	"combiner=s"	=> \@combiners,
	"ip4=s"			=> \@ip4,
	"ip6=s"			=> \@ip6,
	"domain=s"		=> \@domain,
	"url=s"			=> \@url,
	"email=s"		=> \@email,
	"username=s"	=> \$username,
	"password=s"	=> \$password,
	"flags=i"		=> \$flags,
	"tcp"			=> \$tcp,
	"single"		=> \$single,
	"version"		=> \$version,
);

if (!$result){
	print << "EOH";
Usage: $0
	[--username=xxx --password=yyy]
    [--host=<query.karmasphere.com>] [--port=<portnum>]
    [--tcp]
	[--feed=<feedname> ...]
	[--composite=<compositename> ...]
	[--combiner=<combinername> ...]
    [--ip4=<ip4_addr>[=tag] ...] [--ip6=<ip6_addr>[=tag] ...]
    [--domain=<domain_name>[=tag] ...]
	[--url=<url>[=tag] ...] [--email=<email_addr>[=tag] ...]
	[--version]

Example:
  Query for an IP4 address and a domain in one packet:
    $0 --username=guest --password=guest --ip4=123.45.6.7 --domain=spammer.com
EOH
	exit 1;
}

if ($version) {
	print "Mail::Karmasphere::Client $Mail::Karmasphere::Client::VERSION\n";
	exit;
}

@feeds = map { split(/[,:\s]/, $_) } @feeds;
@combiners = map { split(/[,:\s]/, $_) } @combiners;
@composites = map { split(/[,:\s]/, $_) } @composites;

my @ids = ();
my %ids = (
	IDT_IP4()		=> \@ip4,
	IDT_IP6()		=> \@ip6,
	IDT_DOMAIN()	=> \@domain,
	IDT_URL()		=> \@url,
	IDT_EMAIL()		=> \@email,
);

for my $type (keys %ids) {
	for my $id (@{ $ids{$type} }) {
	    if ($id =~ /^(.*)=([a-z\.-]+)$/) {
			push(@ids, [ $1, $type, $2 ]);
		}
		else {
			push(@ids, [ $id, $type ]);
		}
	}
}

push(@ids, [ '127.0.0.2', IDT_IP4 ]) unless @ids;
unless (@composites || @feeds) {
	push(@composites, "karmasphere.email-sender");
}

my %args = ();
$args{PeerHost} = $host if $host;
$args{PeerPort} = $port if $port;
$args{Proto} = 'tcp' if $tcp;
$args{Debug} = 1 if $debug;
$args{Principal} = $username if $username;
$args{Credentials} = $password if $password;
my $client = new Mail::Karmasphere::Client(%args);
if ($single) {
	for (@ids) {
		my $query = new Mail::Karmasphere::Query(
				Identities	=> [ $_ ],	# $_ is an ARRAY ref.
				Feeds		=> \@feeds,
				Composites	=> \@composites,
				Combiners	=> \@combiners,
				Flags		=> $flags,
					);
		my $response = $client->ask($query);
		if ($response) {
			print $response->as_string();
		}
		else {
			print "Timeout or error\n";
		}
	}
}
else {
	my $query = new Mail::Karmasphere::Query(
			Identities	=> \@ids,
			Feeds		=> \@feeds,
			Composites	=> \@composites,
			Combiners	=> \@combiners,
			Flags		=> $flags,
				);
	my $response = $client->ask($query);
	if ($response) {
		print $response->as_string();
	}
	else {
		print "Timeout or error\n";
	}
}

__END__

=head1 NAME

karmaclient - Karmasphere commandline query client

=head1 DESCRIPTION

A commandline query client which allows access to most of the
features of L<Mail::Karmasphere::Client>.

=head1 USAGE

To write.

=head1 BUGS

This document is incomplete.

=head1 SEE ALSO

L<Mail::Karmasphere::Client>
L<Mail::Karmasphere::Query>
L<Mail::Karmasphere::Response>
L<karmad>
http://www.karmasphere.com/

=head1 COPYRIGHT

Copyright (c) 2005 Shevek, Karmasphere. All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut
