move all configuration into enviroment variables
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 29 Aug 2010 00:20:51 +0000 (02:20 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 29 Aug 2010 00:20:51 +0000 (02:20 +0200)
scripts/mqr-irc-client.pl
scripts/mqr-publish.pl
scripts/mqr-subscribe.pl
scripts/mqr-xmpp-client.pl

index b5a0de4..63f8fee 100755 (executable)
@@ -9,13 +9,13 @@ use IO::Handle;
 use AnyEvent;
 use AnyEvent::IRC::Client;
 use AnyEvent::IRC::Util qw/mk_msg parse_irc_msg encode_ctcp/;
-use Data::Dumper;
+use AnyEvent::Redis;
+use Data::Dump qw(dump);
 
-my ($nick, $server, $port) = @ARGV;
+my $nick = $ENV{IRC_NICK} || die "IRC_NICK";
 
-$nick ||= 'mqr-irc';
-$server ||= 'irc.freenode.net';
-$port ||= 6667;
+my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} );
+my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} );
 
 my $c = AnyEvent->condvar;
 my $stdout = AnyEvent::Handle->new (fh => \*STDOUT);
@@ -41,12 +41,18 @@ $con->reg_cb (
          . mk_msg ($msg->{prefix}, $msg->{command}, @{$msg->{params}})
          . "\n"
       );
+
+               if ( $msg->{command} eq 'PRIVMSG' ) {
+                       my $channel = join(' ', 'channel', $msg->{params}->[0], $msg->{prefix});
+                       $pub->publish( $channel, $msg->{params}->[1] );
+               }
    },
    debug_send => sub {
       my ($con, @msg) = @_;
       $stdout->push_write (
          "> " . mk_msg (undef, @msg) . "\n"
       );
+warn dump(@msg);
    },
    registered => sub {
       my ($con) = @_;
@@ -71,7 +77,7 @@ $con->reg_cb (
                      warn "eval error: $@\n";
                   } else {
                      $Data::Dumper::Terse = 1;
-                     $stdout->push_write ("result: " . Data::Dumper::Dumper ($r));
+                     $stdout->push_write ("result: " . dump($r));
                   }
                } else {
                   my $msg = parse_irc_msg ($line);
@@ -87,8 +93,8 @@ $con->reg_cb (
    },
 );
 
-$con->ctcp_auto_reply ('VERSION', ['VERSION', 'DebugConsole:0.1:Perl']);
+$con->ctcp_auto_reply('VERSION', ['VERSION', 'MQR-IRC:0.1:Perl']);
 
-$con->connect ($server, $port || 6667);
+$con->connect ($ENV{IRC_SERVER}, $ENV{IRC_PORT} || 6667);
 
 $c->wait;
index 16e76e6..68accca 100755 (executable)
@@ -3,12 +3,9 @@ use warnings;
 use strict;
 use AnyEvent::Redis;
 
-our ( $redis_host, $redis_port );
-require 'config.pl';
+my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} );
 
-my $pub = AnyEvent::Redis->new( host => $redis_host, port => $redis_port );
-
-$pub->publish("test.1" => $ARGV[0])->recv;
+$pub->publish("channel test" => $ARGV[0])->recv;
 
 AnyEvent->condvar->recv;
 
index a7d232f..9744349 100755 (executable)
@@ -3,12 +3,9 @@ use warnings;
 use strict;
 use AnyEvent::Redis;
 
-our ( $redis_host, $redis_port );
-require 'config.pl';
-
 my $channel = $ARGV[0] || die "usage: $0 channel\n";
 
-my $sub = AnyEvent::Redis->new( host => $redis_host, port => $redis_port );
+my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} );
 
 my $sub_cv = $sub->psubscribe($channel, sub {
        my($message, $chan) = @_;
index f7379a4..84acdd1 100755 (executable)
@@ -8,15 +8,12 @@ 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";
 
-our ($jid, $pw, $inputfile, $redis_host, $redis_port);
-require 'config.pl';
-
-warn "# $jid <- $inputfile\n";
+my $jid = $ENV{XMPP_JID} || die "XMPP_JID";
+my $pw  = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD";
 
 my $j       = AnyEvent->condvar;
 my $cl      = AnyEvent::XMPP::Client->new (debug => 1);
@@ -26,45 +23,28 @@ my $version = AnyEvent::XMPP::Ext::Version->new;
 $cl->add_extension ($disco);
 $cl->add_extension ($version);
 
-$cl->set_presence (undef, 'I\'m a talking bot.', 1);
+$cl->set_presence(undef, 'I\'m a talking bot.', 1);
 
 $cl->add_account ($jid, $pw);
 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 $track = eval { retrieve '/tmp/track' };
-
-sub track {
-       my ( $who, $to, $value ) = @_;
-       warn "# ${value}track $who $to\n";
-
-       if ( $value ) {
-
-               $track->{$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' );
-               });
-
-       } else {
-               delete $track->{$who}->{$to};
-               $sub->punsubscribe( "channel $to");
-       }
-
-       store $track, '/tmp/track';
-}
-
-foreach my $who ( keys %$track ) {
-       foreach my $to ( keys %{ $track->{$who} } ) {
-               track $who => $to, time;
+my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} );
+my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} );
+
+our $contacts;
+
+$sub->psubscribe( 'channel *', sub {
+       my ( $message, $from ) = @_;
+       return unless $from !~ m/\Q$jid\E/; # skip our messages
+       warn "#Q<< $from [$jid] | $message\n";
+       my ( undef, $channel, $user ) = split(/ /,$from,3);
+warn "# contacts ",dump($contacts);
+       foreach my $contact ( keys %$contacts ) {
+               next if $from =~ m/\Q$contact\E/;
+               warn "# $jid [$from] -> [$contact] | $message\n";
+               $cl->send_message( join(' ',$channel, $user, $message), $contact => $jid, 'chat' );
        }
-}
+});
 
 $cl->reg_cb (
    session_ready => sub {
@@ -74,25 +54,20 @@ $cl->reg_cb (
    message => sub {
                my ($cl, $acc, $msg) = @_;
 
-               my $response;
-
                my $body = $msg->any_body;
 
+               $contacts->{ $msg->from }++;
+
+warn "# contacts ",dump($contacts);
+
                my $channel = join(' ', 'channel', $msg->from);
                $pub->publish( $channel, $body );
                warn "#X<< $channel | $body\n";
 
-               if ( $msg =~ m/^!(un)?track\s+(\S+)/ ) {
-                       track $msg->from => $2, $1 ? 0 : 1;
-                       $response = "track: " . dump( $track->{$msg->from} );
-               } elsif ( $msg =~ m/^!/ ) {
-                       $response = "UNKNOWN $msg";
-               }
-
-               my $repl = $msg->make_reply;
-               $repl->add_body( $response );
-               $repl->send;
-               warn "#>>> $response\n";
+#              my $repl = $msg->make_reply;
+#              $repl->add_body( $response );
+#              $repl->send;
+#              warn "#>>> $response\n";
 
    },
    contact_request_subscribe => sub {
@@ -109,6 +84,15 @@ $cl->reg_cb (
       warn "Got disconnected: [@_]\n";
       $j->broadcast;
    },
+       roster_update => sub {
+               my ($con,$account,$roster) = @_;
+warn "XXXXX", ref($account), " | ", ref($roster);
+               foreach my $contact ( $roster->get_contacts ) {
+                       $contacts->{ $contact->{jid} }++;
+                       warn "# contacts ",dump($contacts);
+               }
+               warn "# contacts ",dump($contacts);
+       },
 );
 
 $cl->start;