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);
. 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) = @_;
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);
},
);
-$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;
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);
$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 {
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 {
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;