Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move XMPP services to a separate dist

  • Loading branch information...
commit 93ebfdd2442b48175799475be108578696b074d2 1 parent 26f8597
@miyagawa authored
View
49 eg/xmppbot/app.psgi
@@ -1,49 +0,0 @@
-#!/usr/bin/perl
-use strict;
-use warnings;
-use Tatsumaki::Error;
-use Tatsumaki::Application;
-use Tatsumaki::HTTPClient;
-use JSON;
-
-package XMPPHandler;
-use base qw(Tatsumaki::Handler::XMPP);
-__PACKAGE__->asynchronous(1);
-
-use JSON;
-use URI;
-
-sub post {
- my $self = shift;
-
- my $message = $self->xmpp_message;
-
- my $uri = URI->new("http://ajax.googleapis.com/ajax/services/language/translate");
- $uri->query_form(v => "1.0", langpair => "en|ja", q => $message->body);
-
- my $client = Tatsumaki::HTTPClient->new;
- $client->get($uri, $self->async_cb(sub { $self->on_response($message, @_) }));
-}
-
-sub on_response {
- my($self, $message, $res) = @_;
- my $result = JSON::decode_json($res->content);
- my $text = $result->{responseData}{translatedText};
-
- $message->reply($text);
- $self->finish;
-}
-
-package main;
-use Tatsumaki::Service::XMPP;
-
-my $svc = Tatsumaki::Service::XMPP->new(
- $ENV{XMPP_JID}, $ENV{XMPP_PASSWORD},
-);
-
-my $app = Tatsumaki::Application->new([
- '/_services/xmpp/chat' => 'XMPPHandler',
-]);
-
-$app->add_service($svc);
-$app;
View
55 lib/Tatsumaki/Handler/XMPP.pm
@@ -1,55 +0,0 @@
-package Tatsumaki::Handler::XMPP;
-use Moose;
-extends 'Tatsumaki::Handler';
-
-use Tatsumaki::Service::XMPP::Message;
-
-sub xmpp_message {
- my $self = shift;
-
- my $params = $self->request->parameters;
- my $env = $self->request->env->{'tatsumaki.xmpp'};
-
- Tatsumaki::Service::XMPP::Message->new(
- from => $params->{from},
- to => $params->{to},
- body => $params->{body},
- xmpp_message => $env->{message},
- );
-}
-
-sub prepare {
- my $self = shift;
-
- unless (exists $self->request->env->{'tatsumaki.xmpp'}) {
- Tatsumaki::Error::HTTP->throw(400);
- }
-}
-
-sub post {
- my $self = shift;
-
- my $msg = $self->xmpp_message;
- if ($msg->body =~ s!^/(\w+)\s+!!) {
- my $cmd = $1;
- my $arg = $msg->body;
-
- my $handler = $cmd . "_command";
- if (my $method = $self->can($handler)) {
- $msg->command($1);
- $msg->arg($msg->body);
- $self->$method($msg);
- } else {
- $self->unhandled_command($msg, $cmd);
- }
- } else {
- # what to do?
- }
-}
-
-sub unhandled_command {
- my($self, $msg, $cmd) = @_;
- $msg->reply("Command /$cmd not found");
-}
-
-1;
View
80 lib/Tatsumaki/Service/XMPP.pm
@@ -1,80 +0,0 @@
-package Tatsumaki::Service::XMPP;
-use Moose;
-extends 'Tatsumaki::Service';
-
-use constant DEBUG => $ENV{TATSUMAKI_XMPP_DEBUG};
-
-use AnyEvent::XMPP::Client;
-use Carp ();
-use HTTP::Request::Common;
-use HTTP::Message::PSGI;
-use namespace::clean -except => 'meta';
-
-has jid => (is => 'rw', isa => 'Str');
-has password => (is => 'rw', isa => 'Str');
-has xmpp => (is => 'rw', isa => 'AnyEvent::XMPP::Client', lazy_build => 1);
-
-around BUILDARGS => sub {
- my $orig = shift;
- my $class = shift;
-
- if (@_ == 2) {
- $class->$orig(jid => $_[0], password => $_[1]);
- } else {
- $class->$orig(@_);
- }
-};
-
-sub _build_xmpp {
- my $self = shift;
- my $xmpp = AnyEvent::XMPP::Client->new(debug => DEBUG);
- $xmpp->add_account($self->jid, $self->password);
- $xmpp->reg_cb(
- error => sub { Carp::croak @_ },
- message => sub {
- my($client, $acct, $msg) = @_;
-
- return unless $msg->any_body;
-
- # TODO refactor this
- my $req = POST "/_services/xmpp/chat", [ from => $msg->from, to => $acct->jid, body => $msg->body ];
- my $env = $req->to_psgi;
- $env->{'tatsumaki.xmpp'} = {
- client => $client,
- account => $acct,
- message => $msg,
- };
- $env->{'psgi.streaming'} = 1;
-
- my $res = $self->application->($env);
- $res->(sub { my $res = shift }) if ref $res eq 'CODE';
- },
- contact_request_subscribe => sub {
- my($client, $acct, $roster, $contact) = @_;
- $contact->send_subscribed;
-
- my $req = POST "/_services/xmpp/subscribe", [ from => $contact->jid, to => $acct->jid ];
- my $env = $req->to_psgi;
- $env->{'tatsumaki.xmpp'} = {
- client => $client,
- account => $acct,
- contact => $contact,
- };
- $env->{'psgi.streaming'} = 1;
-
- my $res = $self->application->($env);
- $res->(sub { my $res = shift }) if ref $res eq 'CODE';
- },
- );
- $xmpp;
-}
-
-sub start {
- my($self, $application) = @_;
- $self->xmpp->start;
-}
-
-no Moose;
-__PACKAGE__->meta->make_immutable;
-
-1;
View
21 lib/Tatsumaki/Service/XMPP/Message.pm
@@ -1,21 +0,0 @@
-package Tatsumaki::Service::XMPP::Message;
-use Moose;
-
-has from => (is => 'rw', isa => 'Str');
-has to => (is => 'rw', isa => 'Str');
-has body => (is => 'rw', isa => 'Str');
-has command => (is => 'rw', isa => 'Str');
-has arg => (is => 'rw', isa => 'Str');
-
-has xmpp_message => (is => 'ro', isa => 'AnyEvent::XMPP::IM::Message');
-
-sub reply {
- my $self = shift;
- my($body) = @_;
-
- my $reply = $self->xmpp_message->make_reply;
- $reply->add_body($body);
- $reply->send;
-}
-
-1;
Please sign in to comment.
Something went wrong with that request. Please try again.