use AnyEvent::XMPP::Ext::Version;
use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
use AnyEvent::Redis;
-
-my @msgs;
-
-sub read_messages {
- my ($msgs_file) = @_;
- open my $f, $msgs_file
- or die "Couldn't open messages file: '$msgs_file'\n";
- (@msgs) = map { chomp; $_ } <$f>;
- close $f;
-}
+use Storable;
+use Data::Dump qw(dump);
binmode STDOUT, ":utf8";
warn "# $jid <- $inputfile\n";
-read_messages ($inputfile);
-
my $j = AnyEvent->condvar;
my $cl = AnyEvent::XMPP::Client->new (debug => 1);
my $disco = AnyEvent::XMPP::Ext::Disco->new;
$cl->add_account ($jid, $pw);
warn "connecting to $jid...\n";
-my $redis = AnyEvent::Redis->new( host => $redis_host, port => $redis_port );
+my $pub = AnyEvent::Redis->new( host => $redis_host, port => $redis_port );
+my $sub = AnyEvent::Redis->new( host => $redis_host, port => $redis_port );
+
+my $subscriptions = eval { retrieve '/tmp/subs' };
+
+sub subscribe_channel {
+ my ( $who, $to ) = @_;
+ warn "# subscribe_channel $who $to\n";
+
+ $subscriptions->{$who}->{$to} ||= time;
+
+ $sub->psubscribe( "channel $to", sub {
+ my ( $message, $from ) = @_;
+ return unless $from !~ m/\Q($to|$jid)\E/; # skip our messages
+ warn "#Q<< $from [$jid] | $message\n";
+ my ( undef, $channel, $user ) = split(/ /,$from,3);
+ $cl->send_message( "$channel <$user> $message", $who => $jid, 'chat' );
+ });
+
+ store $subscriptions, '/tmp/subs';
+}
+
+foreach my $who ( keys %$subscriptions ) {
+ foreach my $to ( keys %{ $subscriptions->{$who} } ) {
+ subscribe_channel $who => $to;
+ }
+}
$cl->reg_cb (
session_ready => sub {
warn "connected!\n";
},
message => sub {
- my ($cl, $acc, $msg) = @_;
- my $talkmsg = $msgs[int (rand (@msgs))];
- my $repl = $msg->make_reply;
- $repl->add_body ("You said '".$msg->any_body."' but... " . $talkmsg);
- warn "Got message: '".$msg->any_body."' from ".$msg->from."\n";
- warn "Answered: $talkmsg\n";
+ my ($cl, $acc, $msg) = @_;
+
+ my $response = '...';
+
+ my $body = $msg->any_body;
+
+ my $channel = join(' ', 'channel', $msg->from);
+ $pub->publish( $channel, $body );
+ warn "#X<< $channel | $body\n";
+
+ if ( $msg =~ m/!subscribe\s+(\S+)/ ) {
+ subscribe_channel $msg->from => $1;
+ $response = "subscribed to: " . dump( $subscriptions->{$msg->from} );
+
+ }
- $redis->publish( join(' ', 'channel', $msg->from), $msg->any_body );
+ my $repl = $msg->make_reply;
+ $repl->add_body( $response );
+ $repl->send;
+ warn "#>>> $response\n";
- $repl->send;
},
contact_request_subscribe => sub {
my ($cl, $acc, $roster, $contact) = @_;