File Coverage

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