use
5.014;
$App::SpreadRevolutionaryDate::MsgMaker::PromptUser::VERSION
=
'0.47'
;
use
open
qw(:std :encoding(UTF-8)
);
has
'default'
=> (
is
=>
'ro'
,
isa
=>
'Str'
,
required
=> 1,
default
=>
'Goodbye old world, hello revolutionary worlds'
,
);
has
'img_path'
=> (
is
=>
'rw'
,
isa
=>
'Str'
,
default
=>
''
,
);
has
'img_alt'
=> (
is
=>
'rw'
,
isa
=>
'Str'
,
default
=>
''
,
);
has
'img_url'
=> (
is
=>
'rw'
,
isa
=>
'Str'
,
default
=>
''
,
);
around
BUILDARGS
=>
sub
{
my
(
$orig
,
$class
,
%args
) =
@_
;
if
(
$args
{locale}) {
my
(
$volume
,
$directory
,
$file
) = File::Spec->splitpath(__FILE__);
my
$locale_mo
= File::Spec->catfile(
$directory
,
'..'
,
'..'
,
'..'
,
'LocaleData'
,
$args
{locale},
'LC_MESSAGES'
,
'App-SpreadRevolutionaryDate.mo'
);
$args
{locale} =
'en'
unless
-f
$locale_mo
;
}
delete
$args
{
default
}
if
exists
$args
{
default
} && !
defined
$args
{
default
};
return
$class
->
$orig
(
%args
);
};
sub
compute {
my
$self
=
shift
;
my
$question
= __
"Please, enter message to spread"
;
my
$confirm_ok
=
lc
(
substr
(__(
"yes"
), 0, 1));
my
$confirm_nok
=
lc
(
substr
(__(
"no"
), 0, 1));
my
$confirm_abort
=
substr
(__(
"Abort"
), 0, 1);
my
$confirm_abort_text
= __x(
"or {abort} to abort"
,
abort
=>
$confirm_abort
);
my
$confirm_intro
= __
"Spread"
;
my
$confirm_question
= __x(
"confirm ({confirm_ok}/{confirm_nok} {confirm_abort_text})?"
,
confirm_ok
=>
$confirm_ok
,
confirm_nok
=>
$confirm_nok
,
confirm_abort_text
=>
$confirm_abort_text
);
my
$confirm_error
= __x(
"Input must be \"{confirm_ok}\" or \"{confirm_nok}\"\n"
,
confirm_ok
=>
$confirm_ok
,
confirm_nok
=>
$confirm_nok
);
my
$abort
= __
"OK not spreading"
;
if
(
$self
->img_path) {
$self
->img_alt(
ucfirst
(fileparse(
$self
->img_path,
qr/\.[^.]*/
)))
unless
$self
->img_alt;
$confirm_question
= __x(
"with image file"
).
' '
.
$self
->{img_path} .
' (alt:'
.
$self
->img_alt .
'), '
.
$confirm_question
;
}
elsif
(
$self
->img_url) {
$self
->img_alt(
ucfirst
(fileparse(
$self
->img_url,
qr/\.[^.]*/
)))
unless
$self
->img_alt;
$confirm_question
= __x(
"with image from url:"
).
' '
.
$self
->{img_url} .
' (alt:'
.
$self
->img_alt .
'), '
.
$confirm_question
;
}
my
$confirm
=
$confirm_nok
;
my
$msg
;
while
(
defined
$confirm
&&
$confirm
!~
qr($confirm_ok)
) {
$msg
= prompt(
$question
,
$self
->
default
);
$confirm
= prompt(
message
=>
$confirm_intro
.
' "'
.
$msg
.
'", '
.
$confirm_question
,
default
=>
$confirm_ok
,
validate
=>
qr/^[$confirm_ok$confirm_nok]$/
i,
escape
=>
qr/^$confirm_abort$/
,
error
=>
$confirm_error
,
tries
=> 2,
);
}
die
"$abort\n"
unless
defined
$confirm
&&
$confirm
=~
qr($confirm_ok)
;
if
(
$self
->img_path) {
return
(
$msg
, {
path
=>
$self
->img_path,
alt
=>
$self
->img_alt});
}
elsif
(
$self
->img_url) {
my
$ua
= LWP::UserAgent->new(
env_proxy
=> 1,
timeout
=> 10,
agent
=>
'App::SpreadRevolutionaryDate bot'
);
my
$response
=
$ua
->get(
$self
->img_url);
die
"Cannot download image from "
.
$self
->img_url .
": "
.
$response
->status_line .
"\n"
unless
$response
->is_success;
my
(
$fh
,
$filename
) = tempfile(
UNLINK
=> 1);
print
$fh
$response
->content;
close
$fh
;
return
(
$msg
, {
path
=>
$filename
,
alt
=>
$self
->img_alt});
}
else
{
return
(
$msg
,
undef
);
}
}
no
Moose;
__PACKAGE__->meta->make_immutable;
"Quand le gouvernement viole les droits du peuple,
l'insurrection est pour le peuple le plus sacré
et le plus indispensable des devoirs";