our
$VERSION
=
'0.3.0'
;
has
'logger'
=> (
is
=>
'ro'
,
isa
=>
'Log::Log4perl::Logger'
,
lazy
=> 1,
default
=>
sub
{
return
Log::Log4perl::get_logger( __PACKAGE__ );
},
);
has
'icon_dir'
=> (
is
=>
'ro'
,
isa
=>
'Str'
,
lazy
=> 1,
default
=>
sub
{
return
"$ENV{HOME}/.icons"
;
},
);
sub
react {
my
(
$self
,
$message
,
$config
) =
@_
;
my
$image_dir
=
$config
->{image_dir} ||
$self
->icon_dir;
if
(
$message
->{image} ) {
if
(
my
$icon
=
$self
->_check_for_image(
$image_dir
,
$message
->{image},
$config
,
'image'
) ) {
$message
->{icon} =
$icon
;
return
$message
;
}
}
if
(
$message
->{username} &&
$message
->{username} ne
"wubot"
) {
if
(
my
$icon
=
$self
->_check_for_image(
$image_dir
,
$message
->{username},
$config
,
'username'
) ) {
$message
->{icon} =
$icon
;
return
$message
;
}
}
if
(
$message
->{key} ) {
if
(
my
$icon
=
$self
->_check_for_image(
$image_dir
,
$message
->{key},
$config
,
'key'
) ) {
$message
->{icon} =
$icon
;
return
$message
;
}
$message
->{key} =~ m|^(.*?)\-(.*)$|;
my
(
$plugin
,
$instance
) = ( $1, $2 );
if
(
my
$icon
=
$self
->_check_for_image(
$image_dir
,
$instance
,
$config
,
'instance'
) ) {
$message
->{icon} =
$icon
;
return
$message
;
}
if
(
my
$icon
=
$self
->_check_for_image(
$image_dir
,
$plugin
,
$config
,
'plugin'
, ) ) {
$message
->{icon} =
$icon
;
return
$message
;
}
}
$message
->{icon} =
$self
->_check_for_image(
$image_dir
,
"wubot"
,
$config
,
'wubot'
);
return
$message
;
}
sub
_check_for_image {
my
(
$self
,
$image_dir
,
$image
,
$config
,
$key
) =
@_
;
if
(
$config
) {
if
(
$config
->{custom}->{
$key
} ) {
if
(
$config
->{custom}->{
$key
}->{
$image
} ) {
$image
=
$config
->{custom}->{
$key
}->{
$image
};
$self
->logger->debug(
"Image custom for $key: $image"
);
}
}
}
unless
(
$image
=~ m/\.(png|jpg|gif)$/ ) {
$image
.=
".png"
;
}
$image
=
lc
(
$image
);
$image
=~ s|^.*\/||;
$self
->logger->trace(
"Looking for icon: $image"
);
$image
=
join
(
"/"
,
$image_dir
,
$image
);
return
unless
-r
$image
;
$self
->logger->debug(
"Found image: $image"
);
return
$image
;
}
1;