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