ICQ process_message must also call escapeHTML.
[thrasher.git] / perl / lib / Thrasher / Avatar.pm
blob93e0662473b257d09e110de06a127699fa5fcbc5
1 package Thrasher::Avatar;
2 use strict;
3 use warnings;
5 =pod
7 =head1 NAME
9 Thrasher::Avatar - common avatar manipulation code, including setting
10 them
12 =head1 DESCRIPTION
14 Thrasher::Avatar manages the common bits of avatar code, including
15 setting them. It takes care of converting them into PNG avatars,
16 and firing a callback for plugins to catch.
18 The callback 'avatar_changed' is called, with: ($component, $user_jid,
19 $legacy_username, $raw_binary_data, $base64_data, $image_magick_image).
21 =cut
23 use Thrasher::Callbacks qw(callbacks);
24 use Thrasher::Log qw(:all);
26 use MIME::Base64 qw(encode_base64);
27 use Image::Magick;
28 use Carp qw(confess);
30 use base 'Exporter';
32 our @EXPORT_OK = qw(set_avatar);
33 our %EXPORT_TAGS = (all => \@EXPORT_OK);
35 # I realize later versions of Image::Magick can do this with the
36 # 'mime' attribute, but I don't want to require that recent of
37 # a version.
38 my %MAGICK_TO_MIME =
40 'PNG' => 'image/png',
41 'GIF' => 'image/gif'
44 # After $component, you give it a $binary_avatar_data
45 sub set_avatar {
46 my $component = shift;
47 my $jid = shift;
48 my $legacy_jid = shift;
49 my $avatar_binary = shift;
51 if (!defined($avatar_binary)) {
52 confess "Undefined avatar being passed to set_avatar.";
55 my $image = Image::Magick->new;
56 if (my $res = $image->BlobToImage($avatar_binary)) {
57 log("While trying to load avatar for $jid, " .
58 "Image::Magick complained: $res. Skipped because "
59 ."we failed to determine a type for the image.");
60 return;
63 if (!$image->Get('magick')) {
64 log("While trying to load avatar for $jid, "
65 ."Image::Magick failed to determine a type for "
66 ."the image. Skipped.");
67 return;
70 if ($image->Get('magick') ne 'PNG') {
71 $image->Set(magick => 'PNG');
72 $avatar_binary = $image->ImageToBlob();
75 my $base64_binary = encode_base64($avatar_binary);
77 $component->{protocol}->{backend}->set_avatar($jid, $legacy_jid,
78 $base64_binary);
80 callbacks('avatar_changed', $component, undef,
81 $jid, $legacy_jid,
82 $avatar_binary, $base64_binary, $image);