0
|
1 |
/* |
|
2 |
* avatar.c -- Pep avatar events |
|
3 |
* |
|
4 |
* Copyrigth (C) 2009 Myhailo Danylenko <isbear@ukrpost.net> |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU General Public License as published by |
|
8 |
* the Free Software Foundation; either version 2 of the License, or (at |
|
9 |
* your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but |
|
12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
* General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|
19 |
* USA |
|
20 |
*/ |
|
21 |
|
|
22 |
#define PNG_USER_MEM_SUPPORTED |
|
23 |
|
|
24 |
#include <glib.h> |
|
25 |
#include <gmodule.h> |
|
26 |
#include <loudmouth/loudmouth.h> |
|
27 |
#include <aalib.h> |
|
28 |
#include <png.h> |
|
29 |
#include <string.h> |
|
30 |
#include <unistd.h> |
|
31 |
#include <sys/types.h> |
|
32 |
#include <sys/stat.h> |
|
33 |
#include <fcntl.h> |
|
34 |
#include <stdlib.h> |
|
35 |
#include <errno.h> |
|
36 |
|
|
37 |
#include "compl.h" |
|
38 |
#include "commands.h" |
|
39 |
#include "utils.h" |
|
40 |
#include "xmpp.h" |
|
41 |
#include "screen.h" |
|
42 |
#include "hbuf.h" |
|
43 |
#include "hooks.h" |
|
44 |
#include "settings.h" |
|
45 |
#include "pep.h" |
|
46 |
|
|
47 |
#define NS_AVATAR_DATA ( "urn:xmpp:avatar:data" ) |
|
48 |
#define NS_AVATAR_METADATA ( "urn:xmpp:avatar:metadata" ) |
|
49 |
#define NS_AVATAR_METADATA_NOTIFY ( "urn:xmpp:avatar:metadata+notify" ) |
|
50 |
|
|
51 |
static guint avatar_cid = 0; |
|
52 |
|
|
53 |
GSList *reply_handlers = NULL; |
|
54 |
|
|
55 |
// png stuff |
|
56 |
|
|
57 |
static png_voidp png_glib_malloc (png_structp ignore, png_size_t size) |
|
58 |
{ |
|
59 |
return g_malloc (size); |
|
60 |
} |
|
61 |
|
|
62 |
static void png_glib_free (png_structp ignore, png_voidp chunk) |
|
63 |
{ |
|
64 |
g_free (chunk); |
|
65 |
} |
|
66 |
|
|
67 |
// reads file and returns array of pointers to image rows in |
|
68 |
// grayscale 8-bit with alpha-channel format (i.e. two bytes per pixel) |
|
69 |
// after use just do g_free of this pointer - all data are in one memory chunk |
|
70 |
static png_bytep *png_read_file (const char *file, int *rheight, int *rwidth) |
|
71 |
{ |
|
72 |
FILE *fd = fopen (file, "rb"); |
|
73 |
png_infop info_ptr; |
|
74 |
png_structp png_ptr; |
|
75 |
int rowbytes; |
|
76 |
png_bytep *row_pointers; |
|
77 |
int width; |
|
78 |
int height; |
|
79 |
|
|
80 |
if (!fd) |
|
81 |
return NULL; |
|
82 |
|
|
83 |
{ // check signature |
|
84 |
char header[8]; |
|
85 |
|
|
86 |
fread (header, 1, 8, fd); |
|
87 |
|
|
88 |
if (png_sig_cmp (header, 0, 8)) { |
|
89 |
fclose (fd); |
|
90 |
return NULL; |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
// initialize reader |
|
95 |
png_ptr = png_create_read_struct_2 (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL, NULL, png_glib_malloc, png_glib_free); |
|
96 |
|
|
97 |
if (!png_ptr) { |
|
98 |
fclose (fd); |
|
99 |
return NULL; |
|
100 |
} |
|
101 |
|
|
102 |
info_ptr = png_create_info_struct (png_ptr); |
|
103 |
if (!info_ptr) { |
|
104 |
png_destroy_read_struct (&png_ptr, NULL, NULL); |
|
105 |
fclose (fd); |
|
106 |
return NULL; |
|
107 |
} |
|
108 |
|
|
109 |
if (setjmp (png_jmpbuf (png_ptr))) { |
|
110 |
png_destroy_read_struct (&png_ptr, &info_ptr, NULL); |
|
111 |
fclose (fd); |
|
112 |
return NULL; |
|
113 |
} |
|
114 |
|
|
115 |
png_init_io (png_ptr, fd); |
|
116 |
png_set_sig_bytes (png_ptr, 8); |
|
117 |
|
|
118 |
// get information |
|
119 |
png_read_info (png_ptr, info_ptr); |
|
120 |
|
|
121 |
{ // set up transformations |
|
122 |
png_byte color_type = info_ptr->color_type; |
|
123 |
png_byte bit_depth = info_ptr->bit_depth; |
|
124 |
|
|
125 |
// trying to convert anything to grayscale 8-bit color with alpha channel |
|
126 |
if (color_type == PNG_COLOR_TYPE_PALETTE) // XXX |
|
127 |
png_set_palette_to_rgb (png_ptr); |
|
128 |
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) |
|
129 |
png_set_expand_gray_1_2_4_to_8 (png_ptr); |
|
130 |
if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_PALETTE /* ??? */) // XXX |
|
131 |
png_set_add_alpha (png_ptr, 0xff, PNG_FILLER_AFTER); |
|
132 |
if (bit_depth == 16) |
|
133 |
png_set_strip_16 (png_ptr); |
|
134 |
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) |
|
135 |
png_set_tRNS_to_alpha (png_ptr); |
|
136 |
if (color_type == PNG_COLOR_TYPE_PALETTE /* ??? */ || color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_RGB_ALPHA) // XXX |
|
137 |
png_set_rgb_to_gray_fixed (png_ptr, 1, -1, -1); |
|
138 |
|
|
139 |
// renew information in info structure |
|
140 |
png_read_update_info (png_ptr, info_ptr); |
|
141 |
} |
|
142 |
|
|
143 |
width = info_ptr->width; |
|
144 |
height = info_ptr->height; |
|
145 |
rowbytes = info_ptr->rowbytes; |
|
146 |
|
|
147 |
{ // allocate buffer for image data |
|
148 |
png_bytep chunk = g_malloc ((sizeof(png_bytep) + rowbytes) * height); |
|
149 |
png_bytep start = chunk + sizeof(png_bytep) * height; |
|
150 |
int y; |
|
151 |
|
|
152 |
row_pointers = (png_bytep *) chunk; |
|
153 |
|
|
154 |
for (y = 0; y < height; ++y) |
|
155 |
row_pointers[y] = start + y * rowbytes; |
|
156 |
} |
|
157 |
|
|
158 |
// read image |
|
159 |
if (setjmp (png_jmpbuf (png_ptr))) { |
|
160 |
g_free (row_pointers); |
|
161 |
png_destroy_read_struct (&png_ptr, &info_ptr, NULL); |
|
162 |
fclose (fd); |
|
163 |
return NULL; |
|
164 |
} |
|
165 |
|
|
166 |
png_read_image (png_ptr, row_pointers); |
|
167 |
png_read_end (png_ptr, NULL); |
|
168 |
|
|
169 |
// free resources |
|
170 |
png_destroy_read_struct (&png_ptr, &info_ptr, NULL); |
|
171 |
fclose (fd); |
|
172 |
|
|
173 |
*rheight = height; |
|
174 |
*rwidth = width; |
|
175 |
|
|
176 |
return row_pointers; |
|
177 |
} |
|
178 |
|
|
179 |
// aa stuff |
|
180 |
|
|
181 |
// converts image in format, returned by png_read_file into ascii, |
|
182 |
// scaling it to fit specified max values. If font parameters are |
|
183 |
// set properly, should result in image on screen with original png |
|
184 |
// aspect ratio. |
|
185 |
static gchar *aa_convert_image (png_bytep *row_pointers, int height, int width, int maxcharh, int maxcharw, int fonth, int fontw) |
|
186 |
{ |
|
187 |
gchar *result; |
|
188 |
struct aa_hardware_params hwparams = { |
|
189 |
.font = NULL, |
|
190 |
.supported = AA_NORMAL_MASK, |
|
191 |
.width = 0, |
|
192 |
.height = 0, |
|
193 |
}; |
|
194 |
|
|
195 |
// params for zooming |
|
196 |
int finalcharh; |
|
197 |
int finalcharw; |
|
198 |
int finalh; |
|
199 |
int finalw; |
|
200 |
int starty; |
|
201 |
int startx; |
|
202 |
int finalratioh; |
|
203 |
int finalratiow; |
|
204 |
|
|
205 |
// process aa parameters from environment variables |
|
206 |
aa_parseoptions (&hwparams, NULL, NULL, NULL); |
|
207 |
|
|
208 |
{ // calculate parameters for image zooming into aa surface |
|
209 |
// this always makes me a hard time, thus such a mess :/ |
|
210 |
// I hope, compiler will optimize out all these variables... |
|
211 |
|
|
212 |
// font size in font pixels (real?) |
|
213 |
//int fonth = 16; |
|
214 |
//int fontw = 8; |
|
215 |
float fontaspect = (float) fontw / fonth; |
|
216 |
// aa pixels per character |
|
217 |
const int aappch = 2; |
|
218 |
const int aappcw = 2; |
|
219 |
const float aaaspect = (float) aappcw / aappch; |
|
220 |
// font pixels per aa pixel |
|
221 |
int aappph = (float) fonth / aappch; |
|
222 |
int aapppw = (float) fontw / aappcw; |
|
223 |
float aapixelaspect = (float) aapppw / aappph; |
|
224 |
// original image size in real pixels |
|
225 |
int originh = height; |
|
226 |
int originw = width; |
|
227 |
float originaspect = (float) originw / originh; |
|
228 |
// maximal resulting image size in chars |
|
229 |
//int maxcharh = res_height; |
|
230 |
//int maxcharw = res_width; |
|
231 |
// maximal resulting image size in aa pixels |
|
232 |
int maxh = maxcharh * aappch; |
|
233 |
int maxw = maxcharw * aappcw; |
|
234 |
// maximal resulting image size in real pixels |
|
235 |
int maxh_rp; |
|
236 |
int maxw_rp; |
|
237 |
if (aapixelaspect > 1) { |
|
238 |
maxh_rp = maxh; |
|
239 |
maxw_rp = (float) maxw * aapixelaspect; |
|
240 |
} else { |
|
241 |
maxh_rp = (float) maxh / aapixelaspect; |
|
242 |
maxw_rp = maxw; |
|
243 |
} |
|
244 |
float maxaspect_rp = (float) maxw_rp / maxh_rp; |
|
245 |
// resulting image size in real pixels |
|
246 |
float ratio_rp; |
|
247 |
int resh_rp; |
|
248 |
int resw_rp; |
|
249 |
if (originh > maxh_rp || originw > maxw_rp) { |
|
250 |
// boundaries crossed, will zoom |
|
251 |
if (originaspect > maxaspect_rp) { |
|
252 |
// width is bigger |
|
253 |
ratio_rp = (float) maxw_rp / originw; |
|
254 |
resh_rp = (float) originh * ratio_rp; |
|
255 |
resw_rp = maxw_rp; |
|
256 |
} else { |
|
257 |
// height is bigger |
|
258 |
ratio_rp = (float) maxh_rp / originh; |
|
259 |
resh_rp = maxh_rp; |
|
260 |
resw_rp = (float) originw * ratio_rp; |
|
261 |
} |
|
262 |
} else { |
|
263 |
// image fits, no zooming |
|
264 |
ratio_rp = 1; |
|
265 |
resh_rp = originh; |
|
266 |
resw_rp = originw; |
|
267 |
} |
|
268 |
// resulting image size in aa pixels |
|
269 |
int resh; |
|
270 |
int resw; |
|
271 |
if (aapixelaspect > 1) { |
|
272 |
resh = resh_rp; |
|
273 |
resw = (float) resw_rp / aapixelaspect; |
|
274 |
} else { |
|
275 |
resh = (float) resh_rp * aapixelaspect; |
|
276 |
resw = resw_rp; |
|
277 |
} |
|
278 |
// number of original image pixels per one resulting image pixel |
|
279 |
int ratioh = (float) originh / resh; |
|
280 |
int ratiow = (float) originw / resw; |
|
281 |
// resulting image size in chars |
|
282 |
int rescharh = (float) resh / aappch; |
|
283 |
int rescharw = (float) resw / aappcw; |
|
284 |
|
|
285 |
// re-calculate parameters, as they may change due to rounding (?) |
|
286 |
finalcharh = rescharh; |
|
287 |
finalcharw = rescharw; |
|
288 |
finalh = rescharh * aappch; |
|
289 |
finalw = rescharw * aappcw; |
|
290 |
int finalh_rp; |
|
291 |
int finalw_rp; |
|
292 |
if (aapixelaspect > 1) { |
|
293 |
finalh_rp = finalh; |
|
294 |
finalw_rp = (float) finalw * aapixelaspect; |
|
295 |
} else { |
|
296 |
finalh_rp = (float) finalh / aapixelaspect; |
|
297 |
finalw_rp = finalw; |
|
298 |
} |
|
299 |
finalratioh = (float) originh / finalh; |
|
300 |
finalratiow = (float) originw / finalw; |
|
301 |
|
|
302 |
// center image part actually used |
|
303 |
int areah = (float) finalh * finalratioh; |
|
304 |
int areaw = (float) finalw * finalratiow; |
|
305 |
int losth = originh - areah; |
|
306 |
int lostw = originw - areaw; |
|
307 |
starty = losth / 2; |
|
308 |
startx = lostw / 2; |
|
309 |
} |
|
310 |
|
|
311 |
// create aa image |
|
312 |
hwparams.height = finalcharh; |
|
313 |
hwparams.width = finalcharw; |
|
314 |
|
|
315 |
aa_context *context = aa_init (&mem_d, &hwparams, NULL); |
|
316 |
|
|
317 |
if (context == NULL) |
|
318 |
return NULL; |
|
319 |
|
|
320 |
{ // fill aa image from png buffer with scaling |
|
321 |
int height, width; |
|
322 |
const int bpp = 2; |
|
323 |
int y; |
|
324 |
|
|
325 |
height = aa_imgheight (context); |
|
326 |
width = aa_imgwidth (context); |
|
327 |
|
|
328 |
if (height > finalh) |
|
329 |
height = finalh; |
|
330 |
if (width > finalw) |
|
331 |
width = finalw; |
|
332 |
|
|
333 |
for (y = 0; y < height; ++y) { |
|
334 |
int x; |
|
335 |
|
|
336 |
for (x = 0; x < width; ++x) { |
|
337 |
png_bytep *row_pointer = row_pointers + starty + y * finalratioh; |
|
338 |
unsigned int color = 0; |
|
339 |
unsigned int cy; |
|
340 |
|
|
341 |
// just arithmetic average |
|
342 |
for (cy = 0; cy < finalratioh; ++cy) { |
|
343 |
png_bytep row = row_pointer[cy] + (startx + x * finalratiow) * bpp; |
|
344 |
int cx; |
|
345 |
|
|
346 |
for (cx = 0; cx < finalratiow; ++cx) |
|
347 |
color += row[cx * bpp] * row[cx * bpp + 1] / 255; |
|
348 |
} |
|
349 |
|
|
350 |
color /= finalratioh * finalratiow; |
|
351 |
|
|
352 |
aa_putpixel (context, x, y, color); |
|
353 |
} |
|
354 |
} |
|
355 |
} |
|
356 |
|
|
357 |
// render ascii image |
|
358 |
aa_render (context, &aa_defrenderparams, 0, 0, aa_scrwidth (context), aa_scrheight (context)); |
|
359 |
aa_flush (context); |
|
360 |
|
|
361 |
{ // format the result |
|
362 |
char *text = aa_text (context); |
|
363 |
int width = aa_scrwidth (context); |
|
364 |
int height = aa_scrheight (context); |
|
365 |
GString *avatar = g_string_new (NULL); |
|
366 |
char *end = text + width * height; |
|
367 |
|
|
368 |
while (text < end) { |
|
369 |
g_string_append_len (avatar, text, width); |
|
370 |
g_string_append_c (avatar, '\n'); |
|
371 |
text += width; |
|
372 |
} |
|
373 |
|
|
374 |
// strip last '\n' |
|
375 |
g_string_truncate (avatar, avatar->len - 1); |
|
376 |
|
|
377 |
result = g_string_free (avatar, FALSE); |
|
378 |
|
|
379 |
} |
|
380 |
|
|
381 |
// free resources |
|
382 |
aa_close (context); |
|
383 |
|
|
384 |
return result; |
|
385 |
} |
|
386 |
|
|
387 |
// avatar stuff |
|
388 |
|
|
389 |
// returns filename to save this avatar. NULL on error. g_free result. |
|
390 |
static gchar *avatar_id_filename (const gchar *id) |
|
391 |
{ |
|
392 |
gchar *escid = g_strdup (id); |
|
393 |
gchar *dir = (gchar *) settings_opt_get ("avatar_directory"); |
|
394 |
gchar *file; |
|
395 |
|
|
396 |
if (!dir) |
|
397 |
return NULL; |
|
398 |
|
|
399 |
{ // neutralize id |
|
400 |
const gchar *valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
|
401 |
|
|
402 |
g_strcanon (escid, valid, '_'); |
|
403 |
} |
|
404 |
|
|
405 |
dir = expand_filename (dir); |
|
406 |
|
|
407 |
file = g_strdup_printf ("%s/%s.png", dir, escid); |
|
408 |
|
|
409 |
g_free (dir); |
|
410 |
g_free (escid); |
|
411 |
|
|
412 |
return file; |
|
413 |
} |
|
414 |
|
|
415 |
// returns name of symlink to avatar image file for this jid |
|
416 |
static gchar *avatar_jid_filename (const gchar *jid) |
|
417 |
{ |
|
418 |
gchar *dir = (gchar *) settings_opt_get ("avatar_directory"); |
|
419 |
gchar *file = NULL; |
|
420 |
gchar *bjid; |
|
421 |
|
|
422 |
if (!dir) |
|
423 |
return NULL; |
|
424 |
|
|
425 |
// if jid validity alone is enough for mcabber to use as |
|
426 |
// names for history logs, it's fine for us too. |
|
427 |
if (check_jid_syntax (jid)) |
|
428 |
return NULL; |
|
429 |
|
|
430 |
bjid = jidtodisp (jid); |
|
431 |
mc_strtolower (bjid); |
|
432 |
dir = expand_filename (dir); |
|
433 |
file = g_strdup_printf ("%s/%s", dir, bjid); |
|
434 |
|
|
435 |
g_free (bjid); |
|
436 |
return file; |
|
437 |
} |
|
438 |
|
|
439 |
// symlinks specified jid to specified file. performs jid |
|
440 |
// checks, but not file checks. |
|
441 |
static void set_jid_avatar (const char *jid, const char *file) |
|
442 |
{ |
|
443 |
gchar *jfile; |
|
444 |
|
|
445 |
if (!jid || !file) |
|
446 |
return; |
|
447 |
|
|
448 |
jfile = avatar_jid_filename (jid); |
|
449 |
|
|
450 |
if (!jfile) |
|
451 |
return; |
|
452 |
|
|
453 |
unlink (jfile); |
|
454 |
if (symlink (file, jfile) == -1) |
|
455 |
scr_LogPrint (LPRINT_LOGNORM, "avatar: Cannot symlink jid-file to avatar: %s.", strerror (errno)); |
|
456 |
} |
|
457 |
|
|
458 |
// reply handler for metadata publish request |
|
459 |
// just prints error message if unsuccessful |
|
460 |
LmHandlerResult avatar_publish_metadata_reply_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer userdata) |
|
461 |
{ |
|
462 |
reply_handlers = g_slist_remove (reply_handlers, handler); |
|
463 |
|
|
464 |
switch (lm_message_get_sub_type (message)) { |
|
465 |
case LM_MESSAGE_SUB_TYPE_RESULT: |
|
466 |
break; |
|
467 |
|
|
468 |
case LM_MESSAGE_SUB_TYPE_ERROR: |
|
469 |
|
|
470 |
{ |
|
471 |
LmMessageNode *node = lm_message_get_node (message); |
|
472 |
const gchar *type; |
|
473 |
const gchar *reason; |
|
474 |
|
|
475 |
node = lm_message_node_get_child (node, "error"); |
|
476 |
type = lm_message_node_get_attribute (node, "type"); |
|
477 |
if (node->children) |
|
478 |
reason = node->children->name; |
|
479 |
else |
|
480 |
reason = "undefined"; |
|
481 |
|
|
482 |
scr_LogPrint (LPRINT_LOGNORM, "avatar: Metadata publishing failed: %s - %s.", type, reason); |
|
483 |
} |
|
484 |
|
|
485 |
break; |
|
486 |
|
|
487 |
default: |
|
488 |
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
489 |
break; |
|
490 |
} |
|
491 |
|
|
492 |
return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
|
493 |
} |
|
494 |
|
|
495 |
// reply handler for data publish request |
|
496 |
// sends metadata update request or prints error message |
|
497 |
LmHandlerResult avatar_publish_data_reply_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer userdata) |
|
498 |
{ |
|
499 |
LmMessage *request = (LmMessage *) userdata; |
|
500 |
|
|
501 |
reply_handlers = g_slist_remove (reply_handlers, handler); |
|
502 |
|
|
503 |
switch (lm_message_get_sub_type (message)) { |
|
504 |
case LM_MESSAGE_SUB_TYPE_RESULT: |
|
505 |
|
|
506 |
{ |
|
507 |
// XXX we may use only one instance of this... |
|
508 |
LmMessageHandler *mhandler = lm_message_handler_new (avatar_publish_metadata_reply_handler, NULL, NULL); |
|
509 |
|
|
510 |
reply_handlers = g_slist_append (reply_handlers, mhandler); |
|
511 |
|
|
512 |
lm_connection_send_with_reply (connection, request, mhandler, NULL); |
|
513 |
|
|
514 |
lm_message_handler_unref (mhandler); |
|
515 |
lm_message_unref (request); |
|
516 |
} |
|
517 |
|
|
518 |
break; |
|
519 |
|
|
520 |
case LM_MESSAGE_SUB_TYPE_ERROR: |
|
521 |
|
|
522 |
{ |
|
523 |
LmMessageNode *node = lm_message_get_node (message); |
|
524 |
const gchar *type; |
|
525 |
const gchar *reason; |
|
526 |
|
|
527 |
node = lm_message_node_get_child (node, "error"); |
|
528 |
type = lm_message_node_get_attribute (node, "type"); |
|
529 |
if (node->children) |
|
530 |
reason = node->children->name; |
|
531 |
else |
|
532 |
reason = "undefined"; |
|
533 |
|
|
534 |
scr_LogPrint (LPRINT_LOGNORM, "avatar: Data publishing failed: %s - %s.", type, reason); |
|
535 |
} |
|
536 |
|
|
537 |
break; |
|
538 |
|
|
539 |
default: |
|
540 |
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
541 |
break; |
|
542 |
} |
|
543 |
|
|
544 |
return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
|
545 |
} |
|
546 |
|
|
547 |
// prints avatar (if available) for jid into jid's buffer |
|
548 |
// id can be omitted |
|
549 |
static gboolean print_avatar (const gchar *file, const gchar *jid, const gchar *id) |
|
550 |
{ |
|
551 |
png_bytep *row_pointers; |
|
552 |
gchar *avatar; |
|
553 |
int width; |
|
554 |
int height; |
|
555 |
|
|
556 |
if (!file) |
|
557 |
return FALSE; |
|
558 |
|
|
559 |
{ // check if file exists to not trigger unnecessary error messages |
|
560 |
struct stat buf; |
|
561 |
|
|
562 |
if (stat (file, &buf) == -1) { |
|
563 |
gchar *bjid = jidtodisp (jid); |
|
564 |
|
|
565 |
scr_WriteIncomingMessage (bjid, "No avatar for this buddy yet.", 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0); // NO conversion from utf-8 |
|
566 |
|
|
567 |
g_free (bjid); |
|
568 |
} |
|
569 |
} |
|
570 |
|
|
571 |
row_pointers = png_read_file (file, &width, &height); |
|
572 |
|
|
573 |
if (!row_pointers) { |
|
574 |
scr_LogPrint (LPRINT_LOGNORM, "avatar: Cannot decode png data from file %s.", file); |
|
575 |
return FALSE; |
|
576 |
} |
|
577 |
|
|
578 |
{ // convert to ascii |
|
579 |
int maxcharh = settings_opt_get_int ("avatar_max_height"); |
|
580 |
int maxcharw = settings_opt_get_int ("avatar_max_width"); |
|
581 |
int fonth = settings_opt_get_int ("avatar_font_height"); |
|
582 |
int fontw = settings_opt_get_int ("avatar_font_width"); |
|
583 |
|
|
584 |
// if not set explicitly, try to calculate available space |
|
585 |
if (!maxcharh) { |
|
586 |
char *var = getenv ("LINES"); |
|
587 |
|
|
588 |
if (var) |
|
589 |
maxcharh = atoi (var); |
|
590 |
else |
|
591 |
maxcharh = 25; |
|
592 |
|
|
593 |
maxcharh -= settings_opt_get_int ("log_win_height") + 3 + 1; // +1 due to avatar header line |
|
594 |
} |
|
595 |
|
|
596 |
if (!maxcharw) { |
|
597 |
char *var = getenv ("COLUMNS"); |
|
598 |
|
|
599 |
if (var) |
|
600 |
maxcharw = atoi (var); |
|
601 |
else |
|
602 |
maxcharw = 80; |
|
603 |
|
|
604 |
maxcharw -= settings_opt_get_int ("roster_width") + scr_getprefixwidth (); |
|
605 |
} |
|
606 |
|
|
607 |
if (!fonth) |
|
608 |
fonth = 16; |
|
609 |
if (!fontw) |
|
610 |
fontw = 8; |
|
611 |
|
|
612 |
avatar = aa_convert_image (row_pointers, width, height, maxcharh, maxcharw, fonth, fontw); |
|
613 |
} |
|
614 |
|
|
615 |
g_free (row_pointers); |
|
616 |
|
|
617 |
if (!avatar) { |
|
618 |
scr_LogPrint (LPRINT_LOGNORM, "avatar: Error converting image to ascii."); |
|
619 |
return FALSE; |
|
620 |
} |
|
621 |
|
|
622 |
{ // print out avatar |
|
623 |
gchar *bjid = jidtodisp (jid); |
|
624 |
gchar *mesg; |
|
625 |
if (id) |
|
626 |
mesg = g_strdup_printf ("Avatar [%s]:\n%s", id, avatar); |
|
627 |
else |
|
628 |
mesg = g_strdup_printf ("Avatar:\n%s", avatar); |
|
629 |
|
|
630 |
scr_WriteIncomingMessage (bjid, mesg, 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0); // NO conversion from utf-8 |
|
631 |
|
|
632 |
g_free (bjid); |
|
633 |
g_free (mesg); |
|
634 |
} |
|
635 |
|
|
636 |
g_free (avatar); |
|
637 |
|
|
638 |
return TRUE; |
|
639 |
} |
|
640 |
|
|
641 |
// reply handler for image/png data request |
|
642 |
// saves image and prints it to buddy buffer |
|
643 |
LmHandlerResult avatar_retrieve_data_reply_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer userdata) |
|
644 |
{ |
|
645 |
gchar *from = (gchar *) userdata; |
|
646 |
|
|
647 |
reply_handlers = g_slist_remove (reply_handlers, handler); |
|
648 |
|
|
649 |
switch (lm_message_get_sub_type (message)) { |
|
650 |
case LM_MESSAGE_SUB_TYPE_RESULT: |
|
651 |
|
|
652 |
{ |
|
653 |
LmMessageNode *node = lm_message_get_node (message); |
|
654 |
node = lm_message_node_get_child (node, "pubsub"); |
|
655 |
if (!node) |
|
656 |
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
657 |
|
|
658 |
node = lm_message_node_get_child (node, "items"); |
|
659 |
if (!node) |
|
660 |
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
661 |
|
|
662 |
{ |
|
663 |
LmMessageNode *item; |
|
664 |
|
|
665 |
for (item = node->children; item; item = item->next) { |
|
666 |
if (!strcmp (item->name, "item")) { |
|
667 |
LmMessageNode *data = lm_message_node_get_child (item, "data"); |
|
668 |
const gchar *id = lm_message_node_get_attribute (item, "id"); |
|
669 |
|
|
670 |
if (!data) |
|
671 |
continue; |
|
672 |
|
|
673 |
{ // save to file and display in ascii |
|
674 |
const gchar *base64 = lm_message_node_get_value (data); |
|
675 |
gchar *png; |
|
676 |
gsize len; |
|
677 |
gchar *file; |
|
678 |
|
|
679 |
if (!base64) |
|
680 |
continue; |
|
681 |
|
|
682 |
png = g_base64_decode (base64, &len); |
|
683 |
|
|
684 |
if (!png) |
|
685 |
continue; |
|
686 |
|
|
687 |
file = avatar_id_filename (id); |
|
688 |
|
|
689 |
if (!file) { |
|
690 |
g_free (png); |
|
691 |
continue; |
|
692 |
} |
|
693 |
|
|
694 |
{ // write image to file |
|
695 |
int fd = open (file, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); |
|
696 |
|
|
697 |
if (fd == -1) { |
|
698 |
scr_LogPrint (LPRINT_LOGNORM, "avatar: Cannot create file: %s.", strerror (errno)); |
|
699 |
g_free (png); |
|
700 |
g_free (file); |
|
701 |
continue; |
|
702 |
} |
|
703 |
|
|
704 |
write (fd, png, len); |
|
705 |
close (fd); |
|
706 |
} |
|
707 |
|
|
708 |
// create jid symlink to avatar image |
|
709 |
set_jid_avatar (from, file); |
|
710 |
|
|
711 |
g_free (png); |
|
712 |
|
|
713 |
// read it back :/ |
|
714 |
print_avatar (file, from, id); |
|
715 |
|
|
716 |
g_free (file); |
|
717 |
} |
|
718 |
} |
|
719 |
} |
|
720 |
} |
|
721 |
} |
|
722 |
|
|
723 |
break; |
|
724 |
|
|
725 |
case LM_MESSAGE_SUB_TYPE_ERROR: |
|
726 |
|
|
727 |
{ |
|
728 |
LmMessageNode *node = lm_message_get_node (message); |
|
729 |
const gchar *type; |
|
730 |
const gchar *reason; |
|
731 |
|
|
732 |
node = lm_message_node_get_child (node, "error"); |
|
733 |
type = lm_message_node_get_attribute (node, "type"); |
|
734 |
if (node->children) |
|
735 |
reason = node->children->name; |
|
736 |
else |
|
737 |
reason = "undefined"; |
|
738 |
|
|
739 |
scr_LogPrint (LPRINT_LOGNORM, "avatar: Obtaining avatar data for %s failed: %s - %s.", from, type, reason); |
|
740 |
} |
|
741 |
|
|
742 |
break; |
|
743 |
|
|
744 |
default: |
|
745 |
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
746 |
break; |
|
747 |
} |
|
748 |
|
|
749 |
return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
|
750 |
} |
|
751 |
|
|
752 |
// data is image/png data of length len |
|
753 |
// call with len = 0 to publish empty avatar |
|
754 |
static void avatar_publish (const gchar *data, gsize len) |
|
755 |
{ |
|
756 |
LmMessage *datarequest; |
|
757 |
LmMessageHandler *handler; |
|
758 |
gchar *id; |
|
759 |
LmMessage *request; |
|
760 |
LmMessageNode *node; |
|
761 |
|
|
762 |
request = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_SET); |
|
763 |
node = lm_message_get_node (request); |
|
764 |
|
|
765 |
if (len) { |
|
766 |
// create data publish request |
|
767 |
lm_message_node_set_attribute (node, "from", lm_connection_get_jid (lconnection)); |
|
768 |
|
|
769 |
node = lm_message_node_add_child (node, "pubsub", NULL); |
|
770 |
lm_message_node_set_attribute (node, "xmlns", NS_PUBSUB); |
|
771 |
|
|
772 |
node = lm_message_node_add_child (node, "publish", NULL); |
|
773 |
lm_message_node_set_attribute (node, "node", NS_AVATAR_DATA); |
|
774 |
|
|
775 |
node = lm_message_node_add_child (node, "item", NULL); |
|
776 |
id = g_compute_checksum_for_data (G_CHECKSUM_SHA1, data, len); |
|
777 |
lm_message_node_set_attribute (node, "id", id); |
|
778 |
|
|
779 |
{ |
|
780 |
gchar *base64 = g_base64_encode (data, len); |
|
781 |
|
|
782 |
node = lm_message_node_add_child (node, "data", base64); |
|
783 |
lm_message_node_set_attribute (node, "xmlns", NS_AVATAR_DATA); |
|
784 |
|
|
785 |
g_free (base64); |
|
786 |
} |
|
787 |
|
|
788 |
// then create metadata publish request to be supplied to reply handler as userdata |
|
789 |
datarequest = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_SET); |
|
790 |
node = lm_message_get_node (datarequest); |
|
791 |
} |
|
792 |
|
|
793 |
lm_message_node_set_attribute (node, "from", lm_connection_get_jid (lconnection)); |
|
794 |
|
|
795 |
node = lm_message_node_add_child (node, "pubsub", NULL); |
|
796 |
lm_message_node_set_attribute (node, "xmlns", NS_PUBSUB); |
|
797 |
|
|
798 |
node = lm_message_node_add_child (node, "publish", NULL); |
|
799 |
lm_message_node_set_attribute (node, "node", NS_AVATAR_METADATA); |
|
800 |
|
|
801 |
node = lm_message_node_add_child (node, "item", NULL); |
|
802 |
if (len) |
|
803 |
lm_message_node_set_attribute (node, "id", id); |
|
804 |
|
|
805 |
node = lm_message_node_add_child (node, "metadata", NULL); |
|
806 |
lm_message_node_set_attribute (node, "xmlns", NS_AVATAR_METADATA); |
|
807 |
|
|
808 |
if (len) { |
|
809 |
gchar *bytes = g_strdup_printf ("%d", len); |
|
810 |
|
|
811 |
node = lm_message_node_add_child (node, "info", NULL); |
|
812 |
lm_message_node_set_attributes (node, "bytes", bytes, "id", id, "type", "image/png", NULL); |
|
813 |
|
|
814 |
g_free (bytes); |
|
815 |
} |
|
816 |
|
|
817 |
// create handler |
|
818 |
if (len) |
|
819 |
handler = lm_message_handler_new (avatar_publish_data_reply_handler, (gpointer) datarequest, (GDestroyNotify) lm_message_unref /* custom, remove from queue? */); |
|
820 |
else |
|
821 |
handler = lm_message_handler_new (avatar_publish_metadata_reply_handler, NULL, NULL); |
|
822 |
reply_handlers = g_slist_append (reply_handlers, handler); |
|
823 |
|
|
824 |
// send |
|
825 |
lm_connection_send_with_reply (lconnection, request, handler, NULL); |
|
826 |
|
|
827 |
lm_message_handler_unref (handler); |
|
828 |
lm_message_unref (request); |
|
829 |
} |
|
830 |
|
|
831 |
// pep node handler |
|
832 |
// if avatar is not yet saved, sends data request, else updates jid's symlink |
|
833 |
static void avatar_handler (const gchar *from, const gchar *enode, LmMessageNode *n, const gchar *id, gpointer ignore) |
|
834 |
{ |
|
835 |
LmMessageNode *node; |
|
836 |
|
|
837 |
// well, anyway we now do not store information about jid-avatar relationship, |
|
838 |
// thus "no avatar" publishes are meaningless to us |
|
839 |
for (node = n->children; node; node = node->next) { |
|
840 |
const gchar *name = node->name; |
|
841 |
|
|
842 |
if (!strcmp (name, "info")) { |
|
843 |
gchar *jid = jidtodisp (from); |
|
844 |
const gchar *url = lm_message_node_get_attribute (node, "url"); |
|
845 |
const gchar *type = lm_message_node_get_attribute (node, "type"); |
|
846 |
const gchar *id = lm_message_node_get_attribute (node, "id"); |
|
847 |
const gchar *bytes = lm_message_node_get_attribute (node, "bytes"); |
|
848 |
const gchar *height = lm_message_node_get_attribute (node, "height"); |
|
849 |
const gchar *width = lm_message_node_get_attribute (node, "width"); |
|
850 |
|
|
851 |
{ // print to buddy's buffer |
|
852 |
GString *mesg = g_string_new ("Avatar:"); |
|
853 |
gchar *text; |
|
854 |
|
|
855 |
// [id] type w x h (# bytes) url |
|
856 |
g_string_append_printf (mesg, " [%s]", id ? id : "(none)"); |
|
857 |
|
|
858 |
if (type) { |
|
859 |
g_string_append_c (mesg, ' '); |
|
860 |
g_string_append (mesg, type); |
|
861 |
} |
|
862 |
|
|
863 |
if (width && height) |
|
864 |
g_string_append_printf (mesg, " %s x %s", width, height); |
|
865 |
|
|
866 |
if (bytes) |
|
867 |
g_string_append_printf (mesg, " (%s bytes)", bytes); |
|
868 |
|
|
869 |
if (url) { |
|
870 |
g_string_append_c (mesg, ' '); |
|
871 |
g_string_append (mesg, url); |
|
872 |
} |
|
873 |
|
|
874 |
text = g_string_free (mesg, FALSE); |
|
875 |
|
|
876 |
scr_WriteIncomingMessage (jid, text, 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0); // NO conversion from utf-8 |
|
877 |
|
|
878 |
g_free (text); |
|
879 |
} |
|
880 |
|
|
881 |
if (!url) { |
|
882 |
// it is server-published png, go ahead and request data |
|
883 |
LmMessage *request; |
|
884 |
LmMessageNode *node; |
|
885 |
LmMessageHandler *dhandler; |
|
886 |
|
|
887 |
{ // check, if file already exists |
|
888 |
gchar *file = avatar_id_filename (id); |
|
889 |
struct stat buf; |
|
890 |
|
|
891 |
if (!file) { |
|
892 |
scr_LogPrint (LPRINT_LOGNORM, "avatar: Cannot obtain filename to save file, probably avatar_directory is not set."); |
|
893 |
g_free (jid); |
|
894 |
continue; |
|
895 |
} |
|
896 |
|
|
897 |
if (stat (file, &buf) != -1) { |
|
898 |
scr_WriteIncomingMessage (jid, "Avatar file exists, will not download.", 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0); |
|
899 |
// link jid to this file |
|
900 |
set_jid_avatar (jid, file); |
|
901 |
g_free (file); |
|
902 |
g_free (jid); |
|
903 |
continue; |
|
904 |
} |
|
905 |
|
|
906 |
g_free (file); |
|
907 |
} |
|
908 |
|
|
909 |
// create data request |
|
910 |
request = lm_message_new_with_sub_type (jid, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_GET); |
|
911 |
node = lm_message_get_node (request); |
|
912 |
lm_message_node_set_attribute (node, "from", lm_connection_get_jid (lconnection)); |
|
913 |
|
|
914 |
node = lm_message_node_add_child (node, "pubsub", NULL); |
|
915 |
lm_message_node_set_attribute (node, "xmlns", NS_PUBSUB); |
|
916 |
|
|
917 |
node = lm_message_node_add_child (node, "items", NULL); |
|
918 |
lm_message_node_set_attribute (node, "node", NS_AVATAR_DATA); |
|
919 |
|
|
920 |
node = lm_message_node_add_child (node, "item", NULL); |
|
921 |
lm_message_node_set_attribute (node, "id", id); |
|
922 |
|
|
923 |
// create handler |
|
924 |
dhandler = lm_message_handler_new (avatar_retrieve_data_reply_handler, jid, g_free); |
|
925 |
reply_handlers = g_slist_append (reply_handlers, dhandler); |
|
926 |
|
|
927 |
// send |
|
928 |
lm_connection_send_with_reply (lconnection, request, dhandler, NULL); |
|
929 |
|
|
930 |
lm_message_handler_unref (dhandler); |
|
931 |
lm_message_unref (request); |
|
932 |
|
|
933 |
// NOT free jid here - it will be freed on handler destruction |
|
934 |
} else |
|
935 |
g_free (jid); |
|
936 |
} |
|
937 |
} |
|
938 |
} |
|
939 |
|
|
940 |
// /AVATAR [filename|-] |
|
941 |
static void do_avatar (char *arg) |
|
942 |
{ |
|
943 |
gchar *fname; |
|
944 |
gchar *data; |
|
945 |
gsize len; |
|
946 |
|
|
947 |
if (!*arg) { // print current byddy's avatar |
|
948 |
const gchar *jid = CURRENT_JID; |
|
949 |
|
|
950 |
if (!jid) { |
|
951 |
scr_LogPrint (LPRINT_NORMAL, "This item cannot have avatar."); |
|
952 |
return; |
|
953 |
} |
|
954 |
|
|
955 |
fname = avatar_jid_filename (jid); |
|
956 |
|
|
957 |
print_avatar (fname, jid, NULL); |
|
958 |
|
|
959 |
g_free (fname); |
|
960 |
return; |
|
961 |
} |
|
962 |
|
|
963 |
// publish |
|
964 |
if (arg[0] == '-' && arg[1] == '\0') { |
|
965 |
// no avatar |
|
966 |
avatar_publish (NULL, 0); |
|
967 |
return; |
|
968 |
} |
|
969 |
|
|
970 |
fname = expand_filename (arg); |
|
971 |
|
|
972 |
{ // read file |
|
973 |
char buffer[1024]; |
|
974 |
GString *datastring; |
|
975 |
int fd = open (fname, O_RDONLY); |
|
976 |
|
|
977 |
if (!fd) { |
|
978 |
scr_LogPrint (LPRINT_NORMAL, "Cannot open file '%s': %s.", fname, strerror (errno)); |
|
979 |
g_free (fname); |
|
980 |
return; |
|
981 |
} |
|
982 |
|
|
983 |
datastring = g_string_new (NULL); |
|
984 |
|
|
985 |
{ // read data |
|
986 |
int ret; |
|
987 |
while ((ret = read (fd, buffer, 1024)) > 0) |
|
988 |
g_string_append_len (datastring, buffer, ret); |
|
989 |
} |
|
990 |
|
|
991 |
close (fd); |
|
992 |
|
|
993 |
len = datastring->len; |
|
994 |
data = g_string_free (datastring, FALSE); |
|
995 |
} |
|
996 |
|
|
997 |
g_free (fname); |
|
998 |
|
|
999 |
avatar_publish (data, len); |
|
1000 |
|
|
1001 |
g_free (data); |
|
1002 |
} |
|
1003 |
|
|
1004 |
static void avatar_free_reply_handlers (void) |
|
1005 |
{ |
|
1006 |
GSList *hel; |
|
1007 |
|
|
1008 |
// let's hope, that after invalidation, lm will remove and free unreffed by us handler |
|
1009 |
for (hel = reply_handlers; hel; hel = hel->next) { |
|
1010 |
LmMessageHandler *handler = (LmMessageHandler *) hel->data; |
|
1011 |
lm_message_handler_invalidate (handler); |
|
1012 |
} |
|
1013 |
|
|
1014 |
g_slist_free (reply_handlers); |
|
1015 |
reply_handlers = NULL; |
|
1016 |
} |
|
1017 |
|
|
1018 |
// release handlers before reconnect |
|
1019 |
static void avatar_hh (guint32 hid, hk_arg_t *args, gpointer userdata) |
|
1020 |
{ |
|
1021 |
hk_arg_t *arg; |
|
1022 |
|
|
1023 |
for (arg = args; arg->name; arg++) { |
|
1024 |
if (!strcmp (arg->name, "hook") && !strcmp (arg->value, "hook-pre-disconnect")) { |
|
1025 |
avatar_free_reply_handlers (); |
|
1026 |
return; |
|
1027 |
} |
|
1028 |
} |
|
1029 |
} |
|
1030 |
|
|
1031 |
const gchar *g_module_check_init (GModule *module) |
|
1032 |
{ |
|
1033 |
pep_register_xmlns_handler (NS_AVATAR_METADATA, avatar_handler, NULL, NULL); |
|
1034 |
|
|
1035 |
cmd_add ("avatar", "", COMPL_FILENAME, 0, do_avatar, NULL); |
|
1036 |
|
|
1037 |
hk_add_handler (avatar_hh, HOOK_INTERNAL, NULL); |
|
1038 |
|
|
1039 |
xmpp_add_feature (NS_AVATAR_METADATA); |
|
1040 |
xmpp_add_feature (NS_AVATAR_METADATA_NOTIFY); |
|
1041 |
|
|
1042 |
return NULL; |
|
1043 |
} |
|
1044 |
|
|
1045 |
void g_module_unload (GModule *module) |
|
1046 |
{ |
|
1047 |
pep_unregister_xmlns_handler (NS_AVATAR_METADATA); |
|
1048 |
|
|
1049 |
avatar_free_reply_handlers (); |
|
1050 |
|
|
1051 |
hk_del_handler (avatar_hh, NULL); |
|
1052 |
|
|
1053 |
cmd_del ("avatar"); |
|
1054 |
|
|
1055 |
xmpp_del_feature (NS_AVATAR_METADATA); |
|
1056 |
xmpp_del_feature (NS_AVATAR_METADATA_NOTIFY); |
|
1057 |
} |
|
1058 |
|
|
1059 |
/* vim: se ts=4: */ |