File Coverage

lib/App/TimeTracker/Command/TellRemote.pm
Criterion Covered Total %
statement 29 39 74.3
branch 0 6 0.0
condition n/a
subroutine 10 11 90.9
pod n/a
total 39 56 69.6


line stmt bran cond sub pod time code
1             package App::TimeTracker::Command::TellRemote;
2 1     1   544 use strict;
  1         4  
  1         26  
3 1     1   5 use warnings;
  1         1  
  1         19  
4 1     1   13 use 5.010;
  1         3  
5              
6             our $VERSION = "3.000";
7             # ABSTRACT: App::TimeTracker plugin for telling generic remotes
8              
9 1     1   384 use Moose::Role;
  1         384407  
  1         3  
10 1     1   5290 use LWP::UserAgent;
  1         35311  
  1         31  
11 1     1   416 use Digest::SHA qw(sha1_hex);
  1         2515  
  1         62  
12 1     1   6 use URI::Escape;
  1         2  
  1         54  
13 1     1   398 use App::TimeTracker::Utils qw(error_message);
  1         8151  
  1         47  
14 1     1   5 use Encode;
  1         2  
  1         305  
15              
16             has 'tell_remote' => (
17             is => 'ro',
18             isa => 'Bool',
19             default => 1,
20             documentation => 'TellRemote: tell generic remote',
21             traits => ['Getopt'],
22             );
23              
24             after [ 'cmd_start', 'cmd_continue' ] => sub {
25             my $self = shift;
26             return if $self->irc_quiet;
27             my $task = $self->_current_task;
28             $self->_tell_remote( start => $task );
29             };
30              
31             after 'cmd_stop' => sub {
32             my $self = shift;
33             return if $self->irc_quiet;
34             return unless $self->_current_command eq 'cmd_stop';
35             my $task = App::TimeTracker::Data::Task->previous( $self->home );
36             $self->_tell_remote( stop => $task );
37             };
38              
39             sub _tell_remote {
40 0     0     my ( $self, $status, $task ) = @_;
41 0           my $cfg = $self->config->{tell_remote};
42 0 0         return unless $cfg;
43              
44 0           my $ua = LWP::UserAgent->new( timeout => 3 );
45 0 0         my $message
46             = $task->user
47             . ( $status eq 'start' ? ' is now' : ' stopped' )
48             . ' working on '
49             . $task->say_project_tags;
50             # Use bytes for creating the digest, otherwise we'll get into trouble
51             # https://rt.cpan.org/Public/Bug/Display.html?id=93139
52 0           my $token = sha1_hex( encode_utf8($message), $cfg->{secret} );
53              
54             my $url
55             = $cfg->{host}
56 0           . '?message='
57             . uri_escape_utf8($message)
58             . '&token='
59             . $token;
60 0           my $res = $ua->get($url);
61 0 0         unless ( $res->is_success ) {
62 0           error_message( 'Could not post to remote status via %s: %s',
63             $url, $res->status_line );
64             }
65             }
66              
67 1     1   6 no Moose::Role;
  1         1  
  1         8  
68             1;
69              
70             __END__
71              
72             =pod
73              
74             =encoding UTF-8
75              
76             =head1 NAME
77              
78             App::TimeTracker::Command::TellRemote - App::TimeTracker plugin for telling generic remotes
79              
80             =head1 VERSION
81              
82             version 3.000
83              
84             =head1 DESCRIPTION
85              
86             We use an internal IRC channel for internal communication. And we all want (need) to know what other team members are currently doing. This plugin helps us making sharing this information easy.
87              
88             After running some commands, this plugin prepares a short message and
89             sends it (together with an authentification token) to a small
90             webserver-cum-irc-bot (C<Bot::FromHTTP>, not yet on CPAN, but basically
91             just a slightly customized/enhanced pastebin).
92              
93             The messages is transfered as a GET-Request like this:
94              
95             http://yourserver/?message=some message&token=a58875d576e8c09a...
96              
97             =head1 CONFIGURATION
98              
99             =head2 plugins
100              
101             add C<TellRemote> to your list of plugins
102              
103             =head2 tell_remote
104              
105             add a hash named C<tell_remote>, containing the following keys:
106              
107             =head3 host
108              
109             The hostname of the server C<Bot::FromHTTP> is running on. Might also contain a special port number (C<http://ircbox.vpn.yourcompany.com:9090>)
110              
111             =head3 secret
112              
113             A shared secret used to calculate the authentification token. The token is calculated like this:
114              
115             my $token = Digest::SHA::sha1_hex($message, $secret);
116              
117             =head1 NEW COMMANDS
118              
119             none
120              
121             =head1 CHANGES TO OTHER COMMANDS
122              
123             =head2 start, stop, continue
124              
125             After running the respective command, a message is sent to the
126             remote that could for example post the message to IRC.
127              
128             =head3 New Options
129              
130             =head4 --tell_remote
131              
132             Defaults to true, but you can use:
133              
134             ~/perl/Your-Secret-Project$ tracker start --no_tell_remote
135              
136             to B<not> send a message
137              
138             =head1 AUTHOR
139              
140             Thomas Klausner <domm@cpan.org>
141              
142             =head1 COPYRIGHT AND LICENSE
143              
144             This software is copyright (c) 2014 - 2019 by Thomas Klausner.
145              
146             This is free software; you can redistribute it and/or modify it under
147             the same terms as the Perl 5 programming language system itself.
148              
149             =cut