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