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