Skip to content

Instantly share code, notes, and snippets.

@yoshiki
Created December 7, 2011 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoshiki/1441510 to your computer and use it in GitHub Desktop.
Save yoshiki/1441510 to your computer and use it in GitHub Desktop.
Perl scripts for im.kayac.com
#!/usr/bin/env perl
use strict;
use warnings;
use AnyEvent::XMPP::Client;
use AnyEvent;
use Config::Pit;
use Encode;
use constant DEBUG => 0;
my $activation_code = $ARGV[0] or die "$0 activation_code";
chomp $activation_code;
my $dest_account = 'api@im.kayac.com';
my $gtalk_c = pit_get( 'gtalk' );
my $j = AnyEvent->condvar;
my $cl = AnyEvent::XMPP::Client->new( debug => DEBUG );
$cl->add_account( $gtalk_c->{ username }, $gtalk_c->{ password },
'talk.google.com', undef, undef );
$cl->reg_cb(
session_ready => sub {
my ( $cl, $acc ) = @_;
warn 'session ready';
},
connected => sub {
my ( $cl, $acc ) = @_;
warn 'connected';
$cl->send_message( $activation_code => $dest_account, undef, 'chat' );
},
message => sub {
my ( $cl, $acc, $msg ) = @_;
warn sprintf 'message from %s: %s'
, $msg->from
, encode( 'utf8', $msg->any_body );
},
error => sub {
my ( $cl, $acc, $error ) = @_;
warn sprintf "ERROR: %s", $error->string;
},
disconnect => sub { warn "DISCON[@_]\n"; 1 },
);
$cl->start;
$j->wait;
#!/usr/bin/env perl
use strict;
use warnings;
use AnyEvent::XMPP::Client;
use AnyEvent;
use Config::Pit;
use Encode;
use WebService::NotifyMyAndroid;
use constant DEBUG => 0;
my $dest_account = 'api@im.kayac.com';
my $nma = WebService::NotifyMyAndroid->new;
my $nma_c = pit_get( 'nma' );
my $gtalk_c = pit_get( 'gtalk' );
my $res = $nma->verify( apikey => $nma_c->{ apikey } );
defined $res->{ success } or die $res->{ error }->{ content };
my $j = AnyEvent->condvar;
my $cl = AnyEvent::XMPP::Client->new( debug => DEBUG );
$cl->add_account( $gtalk_c->{ username }, $gtalk_c->{ password },
'talk.google.com', undef, undef );
$cl->reg_cb(
session_ready => sub {
my ( $cl, $acc ) = @_;
warn 'session ready';
},
connected => sub {
my ( $cl, $acc ) = @_;
warn 'connected';
},
message => sub {
my ( $cl, $acc, $msg ) = @_;
if ( $msg->from =~ /^$dest_account/ ) {
$nma->notify(
apikey => [ $nma_c->{ apikey } ],
application => 'im.kayac.com',
event => 'im.kayac.com',
description => encode( 'utf8', $msg->any_body ),
priority => 1,
);
}
},
error => sub {
my ( $cl, $acc, $error ) = @_;
warn sprintf "ERROR: %s", $error->string;
},
disconnect => sub {
my ( $c, $acc, $h, $p, $reason ) = @_;
printf "Disconnected: %s (%s:%s)%s\n", $acc->bare_jid, $h, $p, $reason ? ": $reason" : '';
my $attempts = 0;
do {
sleep 2;
$c->update_connections;
$attempts++;
} unless $attempts >= 4 || scalar( $c->get_connected_accounts() ) == 1; # one account
},
);
$cl->start;
$j->wait;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment