store subscriptions on disk
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 28 Aug 2010 21:58:44 +0000 (23:58 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 28 Aug 2010 21:58:44 +0000 (23:58 +0200)
scripts/mqr-xmpp-client.pl

index b07cb93..06d4fda 100755 (executable)
@@ -8,6 +8,8 @@ use AnyEvent::XMPP::Ext::Disco;
 use AnyEvent::XMPP::Ext::Version;
 use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
 use AnyEvent::Redis;
+use Storable;
+use Data::Dump qw(dump);
 
 binmode STDOUT, ":utf8";
 
@@ -32,6 +34,31 @@ warn "connecting to $jid...\n";
 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 {
       my ($cl, $acc) = @_;
@@ -49,16 +76,8 @@ $cl->reg_cb (
                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";
+                       subscribe_channel $msg->from => $1;
+                       $response = "subscribed to: " . dump( $subscriptions->{$msg->from} );
 
                }