#!/usr/bin/perl -W
# Simple tool which listens to rabbit.opensuse.org and prints all messages in a human friendly format.
# Copyright 2024, Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
#
# Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence").
# You may not use this work except in compliance with the Licence.
# An English copy of the Licence is shipped in a file called LICENSE along with this applications source code.
# You may obtain copies of the Licence in any of the official languages at https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12.

use 5.26.0;  # Leap 15.6

use Net::AMQP::RabbitMQ;
use JSON::PP;
use Try::Tiny;

my $mq = Net::AMQP::RabbitMQ->new();

$mq->connect(
	'rabbit.opensuse.org', {
		user => 'opensuse',
		password => 'opensuse',
		ssl => 1,
		ssl_cacert => '/etc/ssl/ca-bundle.pem',
	}
);

$mq->channel_open(1);
my $exchange = $mq->exchange_declare(1, 'pubsub', {
		durable => 1,
		exclusive => 1,
		passive => 1,
	}
);

my $queue = $mq->queue_declare(1, '');
$mq->queue_bind(1, $queue, 'pubsub', '#');
$mq->consume(1, $queue);


while ( my $message = $mq->recv() )
{
	print "\n=== MESSAGE ===\n";
	try {
		my $json = 'JSON::PP'->new->pretty;
		print($json->encode($json->decode($message->{'body'})));
	} catch {
		print "$message->{'body'}\n";
	};
}

$mq->disconnect();
