remove resource from contact jid
[MQR.git] / scripts / mqr-xmpp-client.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use utf8;
5 use AnyEvent;
6 use AnyEvent::XMPP::Client;
7 use AnyEvent::XMPP::Ext::Disco;
8 use AnyEvent::XMPP::Ext::Version;
9 use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
10 use AnyEvent::Redis;
11 use Data::Dump qw(dump);
12
13 binmode STDOUT, ":utf8";
14
15 my $jid = $ENV{XMPP_JID} || die "XMPP_JID";
16 my $pw  = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD";
17
18 my $j       = AnyEvent->condvar;
19 my $cl      = AnyEvent::XMPP::Client->new (debug => 1);
20 my $disco   = AnyEvent::XMPP::Ext::Disco->new;
21 my $version = AnyEvent::XMPP::Ext::Version->new;
22
23 $cl->add_extension ($disco);
24 $cl->add_extension ($version);
25
26 $cl->set_presence(undef, 'I\'m a talking bot.', 1);
27
28 $cl->add_account ($jid, $pw);
29 warn "connecting to $jid...\n";
30
31 my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} );
32 my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} );
33
34 our $contacts;
35
36 $sub->psubscribe( 'channel *', sub {
37         my ( $message, $from ) = @_;
38         return unless $from !~ m/\Q$jid\E/; # skip our messages
39         warn "#Q<< $from [$jid] | $message\n";
40         my ( undef, $channel, $user ) = split(/ /,$from,3);
41 warn "# contacts ",dump($contacts);
42         foreach my $contact ( keys %$contacts ) {
43                 next if $from =~ m/\Q$contact\E/;
44                 warn "# $jid [$from] -> [$contact] | $message\n";
45                 $cl->send_message( join(' ',$channel, $user, $message), $contact => $jid, 'chat' );
46         }
47 });
48
49 $cl->reg_cb (
50    session_ready => sub {
51       my ($cl, $acc) = @_;
52       warn "connected!\n";
53    },
54    message => sub {
55                 my ($cl, $acc, $msg) = @_;
56
57                 my $body = $msg->any_body;
58
59                 my $f = $msg->from;
60                 $f =~ s!/.+!!;
61                 $contacts->{ $f }++;
62
63 warn "# contacts ",dump($contacts);
64
65                 my $channel = join(' ', 'channel', $msg->from);
66                 $pub->publish( $channel, $body );
67                 warn "#X<< $channel | $body\n";
68
69 #               my $repl = $msg->make_reply;
70 #               $repl->add_body( $response );
71 #               $repl->send;
72 #               warn "#>>> $response\n";
73
74    },
75    contact_request_subscribe => sub {
76       my ($cl, $acc, $roster, $contact) = @_;
77       $contact->send_subscribed;
78       warn "Subscribed to ".$contact->jid."\n";
79    },
80    error => sub {
81       my ($cl, $acc, $error) = @_;
82       warn "Error encountered: ".$error->string."\n";
83       $j->broadcast;
84    },
85    disconnect => sub {
86       warn "Got disconnected: [@_]\n";
87       $j->broadcast;
88    },
89         roster_update => sub {
90                 my ($con,$account,$roster) = @_;
91 warn "XXXXX", ref($account), " | ", ref($roster);
92                 foreach my $contact ( $roster->get_contacts ) {
93                         $contacts->{ $contact->{jid} }++;
94                         warn "# contacts ",dump($contacts);
95                 }
96                 warn "# contacts ",dump($contacts);
97         },
98 );
99
100 $cl->start;
101
102 $j->wait;