script/electric-sheep-viber-send-message.pl untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 128 CC 1 SCAR 6.9
CC
1
Cov
0%
usage
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/env perl
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.09';
7  
8 use lib ('blib/lib');
9  
10 use Getopt::Long qw(:config no_ignore_case);
11 use Data::Roundtrip qw/perl2dump no-unicode-escape-permanently/;
12  
13 use Android::ElectricSheep::Automator::Plugins::Apps::Viber;
14  
15 my $VERBOSITY = 0; # we need verbosity of 10 (max), so this is not used
16 my ($DEVICE, %SENDPARS, $CONFIGFILE);
17 my $CLOSE_APP_AFTER = 0;
18 if( ! Getopt::Long::GetOptions(
19 'recipient=s' => sub { $SENDPARS{$_[0]} = $_[1] },
20 # 1) no unicode, 2) each space must be converted to '%s'
21 'message=s' => sub { $SENDPARS{$_[0]} = $_[1] },
22 'outbase=s' => sub { $SENDPARS{$_[0]} = $_[1] },
23 'mock' => sub { $SENDPARS{$_[0]} = 1 },
24  
25 'close-app-after' => sub { $CLOSE_APP_AFTER = 1 },
26 'device|d=s' => \$DEVICE,
27 'verbosity|v=i' => \$VERBOSITY,
28 'configfile|c=s' => \$CONFIGFILE,
29 'help|h' => sub { print STDOUT usage(); exit(0); }
30 ) ){ die usage() }
31  
32 for ('message', 'recipient'){
33 if( ! exists($SENDPARS{$_}) || ! defined($SENDPARS{$_}) ){ print STDERR usage(); print STDERR "\n$0 : error, input parameter '$_' is missing.\n"; exit(1); }
34 }
35 if( ! defined $CONFIGFILE ){ print STDERR usage(); print STDERR "\n$0 : error, a configuration file must be specified with '--configfile'.\n"; exit(1); }
36 if( ! -f $CONFIGFILE ){ die "$0 : failed to find config file '$CONFIGFILE'." }
37  
38 my $params = {
39 'configfile' => $CONFIGFILE,
40 'verbosity' => $VERBOSITY,
41 'device-connected' => 1,
42 };
43 # we assume there is a device connected which the user
44 # must specify by serial, of if just one, we connect to
45 # it without the serial
46 if( defined $DEVICE ){ $params->{'device-serial'} = $DEVICE }
47 else { $params->{'device-is-connected'} = 1 }
48  
49 my $client = Android::ElectricSheep::Automator::Plugins::Apps::Viber->new($params);
50 if( ! defined($client) ){ die "$0 : failed to instantiate the automator." }
51  
52 # navigate to the home screen, get rid of previous tests rubbish
53 $client->mother->home_screen();
54  
55 my ($res);
56 # open the app if it is not running or bring it to foreground
57 # IT IS IMPORTANT TO BE IN THE FOREGROUND
58 # open the app
59 $res = $client->open_app();
60 if( ! defined($res) ){ die "$0 : failed to open Viber app." }
61 sleep(4);
62 # is the app running?
63 $res = $client->is_app_running();
64 if( ! defined($res) ){ die "$0 : failed to check if Viber app is running." }
65 if( $res != 1 ){ die "$0 : Viber app is not running yet, perhaps increase the waiting time..." }
66  
67 # ok app is running and on the foreground
68  
69 my $ret = $client->send_message(\%SENDPARS);
70 if( ! defined($ret) ){ die "$0 : failed to send message." }
71 if( $VERBOSITY > 0 ){ print STDOUT "$0 : message sent OK.\n" }
72  
73 if( $CLOSE_APP_AFTER ){
74 if( $VERBOSITY > 0 ){ print STDOUT "$0 : closing the app ...\n" }
75 $res = $client->close_app();
76 if( ! defined($res) ){ die "$0 : failed to close Viber app (message was sent successfully)." }
77 sleep(4);
78 # is the app still running?
79 $res = $client->is_app_running();
80 if( ! defined($res) ){ die "$0 : failed to check if Viber app is running (message was sent successfully)." }
81 if( $res != 1 ){ die "$0 : Viber app is not running yet, perhaps increase the waiting time (message was sent successfully) ..." }
82 }
83  
84 if( $VERBOSITY > 0 ){ print STDOUT "$0 : done, message was sent to recipient '".$SENDPARS{'recipient'}."' successfully.\n" }
85  
86 sub usage {
87 return "Usage $0 --configfile CONFIGFILE --recipient R --message M [--mock] [--outbase OUTBASE] [--close-app-after] [--device DEVICE] [--verbosity v]"
88 . "\n\nThis script will send the specified message to the specified recipient using the Viber app.\n"
89 . "\nThe message must not have unicode characters and all spaces in it must be replaced with '%s'.\n"
90 . "\nExample:\n"
91 . "$0 --message 'hello%sthere' --recipient 'george' --configfile config/myapp.conf\n"
92 . "\nThis saves a lot of debugging information, if it fails to find a button etc. use this:\n"
93 . "$0 --outbase debug --verbosity 1 --message 'hello%sthere' --recipient 'george' --configfile config/myapp.conf\n"
94 . "\nMock it, do not send any message but do everything else (open app, find buttons etc.):\n"
95 . "$0 --mock --message 'hello%sthere' --recipient 'george' --configfile config/myapp.conf\n"
96 . "Use the flag --close-app-after in order to close the app when the message is sent.\n"
97 . "\n\nProgram by Andreas Hadjiprocopis (c) 2025 / bliako at cpan.org / andreashad2 at gmail.com\n\n"
98 ;
99 }
100  
101 1;
102