File Coverage

blib/lib/App/MatrixTool/Command/client.pm
Criterion Covered Total %
statement 15 30 50.0
branch 0 8 0.0
condition 0 9 0.0
subroutine 5 7 71.4
pod 0 2 0.0
total 20 56 35.7


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2016 -- leonerd@leonerd.org.uk
5              
6             package App::MatrixTool::Command::client;
7              
8 1     1   975 use strict;
  1         1  
  1         31  
9 1     1   5 use warnings;
  1         1  
  1         33  
10 1     1   5 use base qw( App::MatrixTool::SubCommands );
  1         1  
  1         515  
11              
12             our $VERSION = '0.07';
13              
14 1     1   6 use constant DESCRIPTION => "Commandline client utilities";
  1         1  
  1         69  
15 1         456 use constant OPTIONS => (
16             's|server=s' => "Server",
17             'u|user-id=s' => "User ID",
18 1     1   4 );
  1         2  
19              
20             =head1 NAME
21              
22             matrixtool client - Commandline client utilities for a Matrix homeserver
23              
24             =head1 SYNOPSIS
25              
26             $ matrixtool client login @username:example.com
27              
28             =head1 DESCRIPTION
29              
30             This command provides a commandline interface to various client APIs on a
31             Matrix homeserver. It has various sub-commands that provide particular
32             functionality, along with some common options between them.
33              
34             These sub-commands offers a convenience over using simpler tools like F
35             directly because they will automatically fill in details like access tokens
36             and server hostnames, when given a C argument. This works by storing
37             a cache of access tokens in the user's home directory, under
38             F<.matrix/client-tokens>.
39              
40             By using the C command you can add an access token
41             for your user account into this cache:
42              
43             $ matrixtool client login @me:example.com
44             Password:
45             [OK] Obtained access token
46              
47             Having logged in, you can now use the C<-u> option to other
48             C subcommands, causing it to automatically supply this
49             access token on every request:
50              
51             $ matrixtool client -u @me:example.com json /_matrix/client/r0/publicRooms
52             ...
53              
54             =head1 OPTIONS
55              
56             The following additional options are recognised
57              
58             =over 4
59              
60             =item C<--user-id>, C<-u>
61              
62             User ID to supply the C of.
63              
64             =item C<--server>, C<-s>
65              
66             Server to connect to. Usually optional, as it is implied by the C if
67             provided.
68              
69             =back
70              
71             =head1 COMMANDS
72              
73             The following sub-commands are recognised
74              
75             =head2 json
76              
77             Perform a direct JSON request
78              
79             $ matrixtool client json PATH [DATA]
80              
81             See also L.
82              
83             =head2 login
84              
85             Obtain a client authentication token
86              
87             $ matrixtool client login USER-ID
88              
89             See also L.
90              
91             =head2 upload
92              
93             Upload a file to the media repository
94              
95             $ matrixtool client upload FILE [TYPE]
96              
97             See also L.
98              
99             =cut
100              
101             sub run
102             {
103 0     0 0   my $self = shift;
104 0           my ( $opts, @args ) = @_;
105              
106 0   0       $self->{$_} //= $opts->{$_} for qw( server user_id );
107              
108 0           return $self->SUPER::run( @args );
109             }
110              
111             sub do_json
112             {
113 0     0 0   my $self = shift;
114 0           my ( $method, $path, %opts ) = @_;
115              
116 0           my $client = $self->http_client;
117              
118 0 0         if( my $user_id = $self->{user_id} ) {
119 0           my ( $server ) = $user_id =~ m/^@.*?:(.*)$/;
120 0   0       $self->{server} //= $server;
121             }
122              
123             defined $self->{server} or
124 0 0         die "Not sure what --server to use\n";
125              
126 0 0 0       if( $self->{server} && $self->{user_id} ) {
127             my $token = $self->client_token_store->get(
128             server => $self->{server},
129             id => $self->{user_id},
130 0           );
131              
132 0 0         $opts{params}{access_token} = $token if defined $token;
133             }
134              
135             $client->request_json(
136             server => $self->{server},
137 0           method => $method,
138             path => $path,
139             %opts,
140             );
141             }
142              
143             =head1 AUTHOR
144              
145             Paul Evans
146              
147             =cut
148              
149             0x55AA;