/usr/share/perl5/Transmission/Client.pm is in libtransmission-client-perl 0.0803-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 | # ex:ts=4:sw=4:sts=4:et
package Transmission::Client;
# Copyright 2009-2013, Jan Henning Thorsen <jhthorsen@cpan.org>
# and contributors
#
# All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
=head1 NAME
Transmission::Client - Interface to Transmission
=head1 VERSION
0.0803
=head1 DESCRIPTION
L<Transmission::Client> is the main module in a collection of modules to
communicate with Transmission. Transmission is:
Transmission is a cross-platform BitTorrent client that is:
* Easy
* Lean
* Native
* Powerful
* Free
If you want to communicate with "transmission-daemon", this is a module
which can help you with that.
The documentation is half copy/paste from the Transmission RPC spec:
L<https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt>
This module differs from L<P2P::Transmission> in (at least) two ways:
This one use L<Moose> and it won't die. The latter is especially
annoying in the constructor.
=head1 SYNOPSIS
use Transmission::Client;
my $client = Transmission::Client->new;
my $torrent_id = 2;
my $data = base64_encoded_data();
$client->add(metainfo => $data) or confess $client->error;
$client->remove($torrent_id) or confess $client->error;
for my $torrent (@{ $client->torrents }) {
print $torrent->name, "\n";
for my $file (@{ $torrent->files }) {
print "> ", $file->name, "\n";
}
}
print $client->session->download_dir, "\n";
=head1 FAULT HANDLING
In C<0.06> L<Transmission::Client> can be constructed with "autodie" set
to true, to make this object confess instead of just setting L</error>.
Example:
my $client = Transmission::Client->new(autodie => 1);
eval {
$self->add(filename => 'foo.torrent');
} or do {
# add() failed...
};
=head1 SEE ALSO
L<Transmission::AttributeRole>
L<Transmission::Session>
L<Transmission::Torrent>
L<Transmission::Utils>
=cut
use Moose;
use DateTime;
use DateTime::Duration;
use JSON::Any;
use LWP::UserAgent;
use MIME::Base64;
use Transmission::Torrent;
use Transmission::Session;
use constant RPC_DEBUG => $ENV{'TC_RPC_DEBUG'};
our $VERSION = '0.0803';
our $SESSION_ID_HEADER_NAME = 'X-Transmission-Session-Id';
my $JSON = JSON::Any->new;
with 'Transmission::AttributeRole';
=head1 ATTRIBUTES
=head2 url
$str = $self->url;
Returns an URL to where the Transmission rpc api is.
Default value is "http://localhost:9091/transmission/rpc";
=cut
has url => (
is => 'ro',
isa => 'Str',
default => 'http://localhost:9091/transmission/rpc',
);
# this is subject for change!
has _url => (
is => 'ro',
isa => 'Str',
lazy_build => 1,
);
sub _build__url {
my $self = shift;
my $url = $self->url;
if($self->username or $self->password) {
my $auth = join ':', $self->username, $self->password;
$url =~ s,://,://$auth@,;
}
return $url;
}
=head2 error
$str = $self->error;
Returns the last error known to the object. All methods can return
empty list in addition to what specified. Check this attribute if so happens.
Like L</autodie>? Create your object with C<autodie> set to true and this
module will throw exceptions in addition to setting this variable.
=cut
has error => (
is => 'rw',
isa => 'Str',
default => '',
clearer => '_clear_error',
trigger => sub { $_[0]->_autodie and confess $_[1] },
);
has _autodie => (
is => 'ro',
init_arg => 'autodie',
isa => 'Bool',
default => 0,
);
=head2 username
$str = $self->username;
Used to authenticate against Transmission.
=cut
has username => (
is => 'ro',
isa => 'Str',
default => '',
);
=head2 password
$str = $self->password;
Used to authenticate against Transmission.
=cut
has password => (
is => 'ro',
isa => 'Str',
default => '',
);
=head2 timeout
$int = $self->timeout;
Number of seconds to wait for RPC response.
=cut
has _ua => (
is => 'rw',
isa => 'LWP::UserAgent',
lazy => 1,
handles => [qw/timeout/],
default => sub {
LWP::UserAgent->new( agent => 'Transmission-Client' );
},
);
=head2 session
$session_obj = $self->session;
$stats_obj = $self->stats;
Returns an instance of L<Transmission::Session>.
C<stats()> is a proxy method on L</session>.
=cut
has session => (
is => 'ro',
lazy => 1,
predicate => 'has_session',
handles => [qw/stats/],
default => sub {
Transmission::Session->new( client => $_[0] );
},
);
=head2 torrents
$array_ref = $self->torrents;
$self->clear_torrents;
Returns an array-ref of L<Transmission::Torrent> objects. Default value
is a full list of all known torrents, with as little data as possible read
from Transmission. This means that each request on a attribute on an object
will require a new request to Transmission. See L</read_torrents> for more
information.
=cut
has torrents => (
is => 'rw',
traits => ['Array'],
lazy => 1,
clearer => "clear_torrents",
builder => "read_torrents",
predicate => 'has_torrents',
handles => {
torrent_list => 'elements',
},
);
=head2 version
$str = $self->version;
Get Transmission version.
=cut
has version => (
is => 'ro',
isa => 'Str',
lazy_build => 1,
);
sub _build_version {
my $self = shift;
if(my $data = $self->rpc('session-get')) {
return $data->{'version'} || q();
}
return q();
}
=head2 session_id
$self->session_id($str);
$str = $self->session_id;
The session ID used to communicate with Transmission.
=cut
has session_id => (
is => 'rw',
isa => 'Str',
default => '',
trigger => sub {
$_[0]->_ua->default_header($SESSION_ID_HEADER_NAME => $_[1]);
},
);
=head1 METHODS
=head2 add
$bool = $self->add(%args);
key | value type & description
-----------------+-------------------------------------------------
download_dir | string path to download the torrent to
filename | string filename or URL of the .torrent file
metainfo | string torrent content
paused | boolean if true, don't start the torrent
peer_limit | number maximum number of peers
Either "filename" or "metainfo" MUST be included. All other arguments are
optional.
See "3.4 Adding a torrent" from
L<https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt>
=cut
sub add {
my $self = shift;
my %args = @_;
if($args{'filename'} and $args{'metainfo'}) {
$self->error("Filename and metainfo argument crash");
return;
}
elsif($args{'filename'}) {
return $self->rpc('torrent-add', %args);
}
elsif($args{'metainfo'}) {
$args{'metainfo'} = encode_base64($args{'metainfo'});
return $self->rpc('torrent-add', %args);
}
else {
$self->error("Need either filename or metainfo argument");
return;
}
}
=head2 remove
$bool = $self->remove(%args);
key | value type & description
-------------------+-------------------------------------------------
ids | array torrent list, as described in 3.1
delete_local_data | boolean delete local data. (default: false)
C<ids> can also be the string "all". C<ids> is required.
See "3.4 Removing a torrent" from
L<https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt>
=cut
sub remove {
my $self = shift;
if($self->_do_ids_action('torrent-remove' => @_)) {
$self->clear_torrents; # torrent list might be out of sync
return 1;
}
else {
return 0;
}
}
=head2 move
$bool = $self->move(%args);
string | value type & description
------------+-------------------------------------------------
ids | array torrent list, as described in 3.1
location | string the new torrent location
move | boolean if true, move from previous location.
| otherwise, search "location" for files
C<ids> can also be the string "all". C<ids> and C<location> is required.
See "3.5 moving a torrent" from
L<https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt>
=cut
sub move {
my $self = shift;
my %args = @_;
if(!defined $args{'location'}) {
$self->error("location argument is required");
return;
}
return $self->_do_ids_action('torrent-set-location' => %args);
}
=head2 start
$bool = $self->start($ids);
Will start one or more torrents.
C<$ids> can be a single int, an array of ints or the string "all".
=head2 stop
$bool = $self->stop($ids);
Will stop one or more torrents.
C<$ids> can be a single int, an array of ints or the string "all".
=head2 verify
$bool = $self->stop($ids);
Will verify one or more torrents.
C<$ids> can be a single int, an array of ints or the string "all".
=cut
sub start {
return shift->_do_ids_action('torrent-start' => @_);
}
sub stop {
return shift->_do_ids_action('torrent-stop' => @_);
}
sub verify {
return shift->_do_ids_action('torrent-verify' => @_);
}
sub _do_ids_action {
my $self = shift;
my $method = shift;
my %args = @_ == 1 ? (ids => $_[0]) : @_;
my $ids;
unless(defined $args{'ids'}) {
$self->error('ids is required as argument');
return;
}
unless(ref $args{'ids'} eq 'ARRAY') {
if($args{'ids'} eq 'all') {
delete $args{'ids'};
}
else {
$args{'ids'} = [$args{'ids'}];
}
}
return $self->rpc($method, %args) ? 1 : 0;
}
=head2 read_torrents
@list = $self->read_torrents(%args);
$array_ref = $self->read_torrents(%args);
key | value type & description
------------+-------------------------------------------------
ids | array torrent list, as described in 3.1
| this is optional
lazy_read | will create objects with as little data as possible.
=over 4
=item List context
Returns a list of L<Transmission::Torrent> objects and sets the L</torrents>
attribute.
=item Scalar context
Returns an array-ref of L<Transmission::Torrent>.
=back
=cut
sub read_torrents {
my $self = shift;
my %args = @_ == 1 ? (ids => $_[0]) : @_;
my $list;
# set fields...
if(exists $args{'fields'}) { # ... based on user input
# We should always request id
push @{$args{'fields'}}, 'id' unless
grep {'id' eq $_} @{$args{'fields'}};
}
elsif($args{'lazy_read'}) { # ... as few fields as possible
$args{'fields'} = ['id'];
}
else { # ... all fields
$args{'fields'} = [
keys %Transmission::Torrent::READ,
keys %Transmission::Torrent::BOTH,
];
}
# set ids
if($args{'ids'}) {
if($args{'ids'} eq 'all') {
delete $args{'ids'};
}
elsif(ref $args{'ids'} eq "") {
$args{'ids'} = [ $args{'ids'} ];
}
}
if(my $data = $self->rpc('torrent-get' => %args)) {
$list = $data->{'torrents'};
}
else {
$list = [];
}
for my $torrent (@$list) {
$torrent = Transmission::Torrent->new(
client => $self,
id => $torrent->{'id'},
%$torrent,
);
}
if(wantarray) {
$self->torrents($list);
return @$list;
}
else {
return $list;
}
}
=head2 rpc
$any = $self->rpc($method, %args);
Communicate with backend. This methods is meant for internal use.
=cut
sub rpc {
my $self = shift;
my $method = shift or return;
my %args = @_;
my $nested = delete $args{'_nested'}; # internal flag
my($tag, $res, $post);
$method = $self->_normal2Camel($method);
# The keys need to be dashes as well
# _normal2Camel modifies a hashref in places
$self->_normal2Camel( \%args );
# make sure ids are numeric
if(ref $args{'ids'} eq 'ARRAY') {
for my $id (@{ $args{'ids'} }) {
# Need to convert string integer to "real" integer
# FLAGS = (IOK,POK,pIOK,pPOK)
# IV = 42
# ...to...
# FLAGS = (PADTMP,IOK,pIOK)
# IV = 42
$id += 0 if($id =~ /^\d+$/);
}
}
$tag = int rand 2*16 - 1;
$post = $JSON->encode({
method => $method,
tag => $tag,
arguments => \%args,
});
$res = $self->_ua->post($self->_url, Content => $post);
if(RPC_DEBUG) {
print "post: $post\n";
print "status_line: ", $res->status_line, "\n";
}
unless($res->is_success) {
if($res->code == 409 and !$nested) {
$self->session_id($res->header($SESSION_ID_HEADER_NAME));
return $self->rpc($method => %args, _nested => 1);
}
else {
$self->error($res->status_line);
return;
}
}
$res = $JSON->decode($res->content);
unless($res->{'tag'} == $tag) {
$self->error("Tag mismatch");
return;
}
unless($res->{'result'} eq 'success') {
$self->error($res->{'result'});
return;
}
$self->_clear_error;
return $res->{'arguments'};
}
=head2 read_all
1 == $self->read_all;
This method will try to populate ALL torrent, session and stats information,
using three requests.
=cut
sub read_all {
my $self = shift;
$self->session->read_all;
$self->stats->read_all;
() = $self->read_torrents;
return 1;
}
=head1 LICENSE
This library is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=head1 COPYRIGHT AND AUTHORS
Copyright 2009-2013, Jan Henning Thorsen <jhthorsen@cpan.org> and contributors
Current maintainer: Olof Johansson - C<olof@cpan.org>
=head2 CONTRIBUTORS
=over
=item Andrew Fresh
=back
=cut
no MIME::Base64;
no Moose;
1;
|