melo / codebits

Presentations and projects done at SAPO Codebits

This URL has Read+Write access

codebits / 2008 / xmpp-hands-on / bots / http2xmpp / http2xmpp-3.pl
100755 80 lines (69 sloc) 2.226 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env perl
#
# Raw XML
#
# Testar com
# ./http2xmpp-send.pl --to=melo@test.simplicidade.org --body='olas' --xml='<n xmlns="ola"><campo>valor</campo></n>'
#
 
use strict;
use warnings;
use Net::XMPP2::Client;
use AnyEvent::Mojo;
use Encode 'decode';
 
# Start a small bot
my $bot = Net::XMPP2::Client->new;
$bot->add_account('http2xmpp@test.simplicidade.org', 'teste', '127.0.0.1', 5222);
$bot->start;
 
 
# Start a HTTP server
my $server = mojo_server undef, 3001, sub {
  my (undef, $tx) = @_;
  my $req = $tx->req;
  my $url = $req->url;
  my $meth = $req->method;
  my $res = $tx->res;
  
  if ($url eq '/send') {
    my $body = decode('utf8', $req->param('body') || '');
    my $to = decode('utf8', $req->param('to') || '');
    my $xml = decode('utf8', $req->param('xml') || '');
 
    my $extra = '';
    if ($meth eq 'POST' && $body) {
      my $msg = Net::XMPP2::IM::Message->new(
          body => $body,
          to => $to,
          type => 'chat',
      );
      $msg->append_creation(sub {
        my ($w) = @_;
        $w->raw($xml);
      }) if $xml;
      
      $bot->send_message($msg);
      $extra = '<p style="color: red;">Thank you sir, can I have another?</p>';
    }
 
    $to ||= 'melo@test.simplicidade.org';
    
    $res->code(200)->headers->content_type('text/html');
    $res->body(<<" EOH")
      <html>
      <head>
         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
         <title>Send a message!</title>
      </head>
      <body>
        <h1>Send a message</h1>
        $extra
        <form method="post" action="/send" accept-charset="utf-8">
          <label for="to">JID:</label><input type="text" name="to" id="to" value="$to" size="60"/><br />
          <label for="body">Body:</label><textarea name="body" id="body" cols="50" rows="4">$body</textarea><br />
          <label for="xml">XML:</label><textarea name="xml" id="xml" cols="50" rows="4">$xml</textarea><br />
          <input type="submit" value="Send!" />
        </form>
      </body>
    </html>
    EOH
  }
  else {
    $res->code(404);
    $res->body('These are not the droids you are looking for...');
    $res->headers->content_type('text/plain');
  }
};
 
$server->run;