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