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;
-}
-
binmode STDOUT, ":utf8";
our ($jid, $pw, $inputfile, $redis_host, $redis_port);
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 );
$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+)/ ) {
+
+ $sub->psubscribe( "channel $1", sub {
+ my ( $message, $from ) = @_;
+ return unless $from !~ m/\Q$jid\E/; # skip our messages
+ warn "#Q<< $from | $message\n";
+ my ( undef, $channal, $user ) = split(/ /,$from,3);
+ $cl->send_message( "$channel <$user> $message" => $msg->from, $jid, 'chat' );
+ });
+
+ $response = "subscribed to $1";
+
+ }
- $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) = @_;