| 1 |
elmex |
1.1 |
#!/opt/perl/bin/perl |
| 2 |
|
|
use strict; |
| 3 |
|
|
use utf8; |
| 4 |
|
|
use Event; |
| 5 |
|
|
use AnyEvent; |
| 6 |
|
|
use Net::XMPP2::Client; |
| 7 |
|
|
use Net::XMPP2::Ext::OOB; |
| 8 |
|
|
use Net::XMPP2::Ext::Disco; |
| 9 |
|
|
|
| 10 |
|
|
my $j = AnyEvent->condvar; |
| 11 |
|
|
my $cl = Net::XMPP2::Client->new (debug => 1); |
| 12 |
|
|
|
| 13 |
|
|
$cl->add_account ('net_xmpp2@jabber.org/oobtest', 'test'); |
| 14 |
|
|
|
| 15 |
|
|
my $disco = Net::XMPP2::Ext::Disco->new; |
| 16 |
|
|
my $oob = Net::XMPP2::Ext::OOB->new; |
| 17 |
|
|
|
| 18 |
|
|
$cl->add_extension ($disco); |
| 19 |
|
|
$cl->add_extension ($oob); |
| 20 |
|
|
|
| 21 |
|
|
$disco->enable_feature ($oob->disco_feature); |
| 22 |
|
|
|
| 23 |
|
|
unless (-d 'oobdata') { |
| 24 |
|
|
mkdir "oobdata" or die "Couldn't make directory oobdata/: $!"; |
| 25 |
|
|
} |
| 26 |
|
|
|
| 27 |
|
|
my $file = "aaaaaaa"; |
| 28 |
|
|
while (-e "oobdata/$file") { $file++ } |
| 29 |
|
|
|
| 30 |
|
|
$oob->reg_cb (oob_recv => sub { |
| 31 |
|
|
my ($oob, $con, $node, $url) = @_; |
| 32 |
|
|
print "receiving url $url->{url} from ".$node->attr ('from')."\n"; |
| 33 |
|
|
my $cont = `curl -f \Q$url->{url}\E 2> /dev/null`; |
| 34 |
|
|
my $exitv = $? >> 8; |
| 35 |
|
|
|
| 36 |
|
|
if ($exitv) { |
| 37 |
|
|
$oob->reply_failure ($con, $node, 'not-found'); |
| 38 |
|
|
} else { |
| 39 |
|
|
if (open OUT, ">oobdata/$file") { |
| 40 |
|
|
print OUT $cont; |
| 41 |
|
|
close OUT; |
| 42 |
|
|
$file++; |
| 43 |
|
|
} else { |
| 44 |
|
|
warn "Couldn't write to oobdata/$file: $!\n"; |
| 45 |
|
|
} |
| 46 |
|
|
|
| 47 |
|
|
$oob->reply_success ($con, $node); |
| 48 |
|
|
} |
| 49 |
|
|
}); |
| 50 |
|
|
|
| 51 |
|
|
$cl->reg_cb ( |
| 52 |
|
|
session_ready => sub { |
| 53 |
|
|
my ($cl, $acc) = @_; |
| 54 |
|
|
print "session ready, waiting...\n"; |
| 55 |
|
|
}, |
| 56 |
|
|
disconnect => sub { |
| 57 |
|
|
my ($cl, $acc, $h, $p, $reas) = @_; |
| 58 |
|
|
print "disconnect ($h:$p): $reas\n"; |
| 59 |
|
|
}, |
| 60 |
|
|
error => sub { |
| 61 |
|
|
my ($cl, $acc, $err) = @_; |
| 62 |
|
|
print "ERROR: " . $err->string . "\n"; |
| 63 |
|
|
}, |
| 64 |
|
|
message => sub { |
| 65 |
|
|
my ($cl, $acc, $msg) = @_; |
| 66 |
|
|
print "message from: " . $msg->from . ": " . $msg->any_body . "\n"; |
| 67 |
|
|
if ($msg->any_body =~ /^\s*send\s+(.+?)\s*$/) { |
| 68 |
|
|
$oob->send_url ( |
| 69 |
|
|
$acc->connection, $msg->from, "http://localhost/$1", "This is file $1", |
| 70 |
|
|
sub { |
| 71 |
|
|
my ($e) = @_; |
| 72 |
|
|
if ($e) { |
| 73 |
|
|
print "Couldn't send file to " . $msg->from . ": $e\n"; |
| 74 |
|
|
} else { |
| 75 |
|
|
print "Submitted file to " . $msg->from . "\n"; |
| 76 |
|
|
} |
| 77 |
|
|
} |
| 78 |
|
|
) |
| 79 |
|
|
} |
| 80 |
|
|
}, |
| 81 |
|
|
contact_request_subscribe => sub { # we are automatically subscribing! |
| 82 |
|
|
my ($cl, $acc, $roster, $contact) = @_; |
| 83 |
|
|
$contact->send_subscribe; |
| 84 |
|
|
1 |
| 85 |
|
|
} |
| 86 |
|
|
); |
| 87 |
|
|
$cl->start; |
| 88 |
|
|
$j->wait; |