Mercurial > pidgin
annotate libgaim/protocols/msn/slp.c @ 14723:d18b9360ba54
[gaim-migrate @ 17477]
Patch from Richard Nelson (wabz) to fix a crash in the statusbox.
committer: Tailor Script <tailor@pidgin.im>
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Sat, 14 Oct 2006 01:52:33 +0000 |
parents | d85f326af28d |
children | dc9ed6c44c48 |
rev | line source |
---|---|
14192 | 1 /** |
2 * @file msnslp.c MSNSLP support | |
3 * | |
4 * gaim | |
5 * | |
6 * Gaim is the legal property of its developers, whose names are too numerous | |
7 * to list here. Please refer to the COPYRIGHT file distributed with this | |
8 * source distribution. | |
9 * | |
10 * This program is free software; you can redistribute it and/or modify | |
11 * it under the terms of the GNU General Public License as published by | |
12 * the Free Software Foundation; either version 2 of the License, or | |
13 * (at your option) any later version. | |
14 * | |
15 * This program is distributed in the hope that it will be useful, | |
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 * GNU General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program; if not, write to the Free Software | |
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
23 */ | |
24 #include "msn.h" | |
25 #include "slp.h" | |
26 #include "slpcall.h" | |
27 #include "slpmsg.h" | |
28 #include "slpsession.h" | |
29 | |
30 #include "object.h" | |
31 #include "user.h" | |
32 #include "switchboard.h" | |
33 | |
34 /* ms to delay between sending buddy icon requests to the server. */ | |
35 #define BUDDY_ICON_DELAY 20000 | |
36 | |
37 static void send_ok(MsnSlpCall *slpcall, const char *branch, | |
38 const char *type, const char *content); | |
39 | |
40 static void send_decline(MsnSlpCall *slpcall, const char *branch, | |
41 const char *type, const char *content); | |
42 | |
43 void msn_request_user_display(MsnUser *user); | |
44 | |
45 /************************************************************************** | |
46 * Util | |
47 **************************************************************************/ | |
48 | |
49 static char * | |
50 get_token(const char *str, const char *start, const char *end) | |
51 { | |
52 const char *c, *c2; | |
53 | |
54 if ((c = strstr(str, start)) == NULL) | |
55 return NULL; | |
56 | |
57 c += strlen(start); | |
58 | |
59 if (end != NULL) | |
60 { | |
61 if ((c2 = strstr(c, end)) == NULL) | |
62 return NULL; | |
63 | |
64 return g_strndup(c, c2 - c); | |
65 } | |
66 else | |
67 { | |
68 /* This has to be changed */ | |
69 return g_strdup(c); | |
70 } | |
71 | |
72 } | |
73 | |
74 /************************************************************************** | |
75 * Xfer | |
76 **************************************************************************/ | |
77 | |
78 static void | |
79 msn_xfer_init(GaimXfer *xfer) | |
80 { | |
81 MsnSlpCall *slpcall; | |
82 /* MsnSlpLink *slplink; */ | |
83 char *content; | |
84 | |
85 gaim_debug_info("msn", "xfer_init\n"); | |
86 | |
87 slpcall = xfer->data; | |
88 | |
89 /* Send Ok */ | |
90 content = g_strdup_printf("SessionID: %lu\r\n\r\n", | |
91 slpcall->session_id); | |
92 | |
93 send_ok(slpcall, slpcall->branch, "application/x-msnmsgr-sessionreqbody", | |
94 content); | |
95 | |
96 g_free(content); | |
97 msn_slplink_unleash(slpcall->slplink); | |
98 } | |
99 | |
100 void | |
101 msn_xfer_cancel(GaimXfer *xfer) | |
102 { | |
103 MsnSlpCall *slpcall; | |
104 char *content; | |
105 | |
106 g_return_if_fail(xfer != NULL); | |
107 g_return_if_fail(xfer->data != NULL); | |
108 | |
109 slpcall = xfer->data; | |
110 | |
111 if (gaim_xfer_get_status(xfer) == GAIM_XFER_STATUS_CANCEL_LOCAL) | |
112 { | |
113 if (slpcall->started) | |
114 { | |
115 msn_slp_call_close(slpcall); | |
116 } | |
117 else | |
118 { | |
119 content = g_strdup_printf("SessionID: %lu\r\n\r\n", | |
120 slpcall->session_id); | |
121 | |
122 send_decline(slpcall, slpcall->branch, "application/x-msnmsgr-sessionreqbody", | |
123 content); | |
124 | |
125 g_free(content); | |
126 msn_slplink_unleash(slpcall->slplink); | |
127 | |
128 msn_slp_call_destroy(slpcall); | |
129 } | |
130 } | |
131 } | |
132 | |
133 void | |
134 msn_xfer_progress_cb(MsnSlpCall *slpcall, gsize total_length, gsize len, gsize offset) | |
135 { | |
136 GaimXfer *xfer; | |
137 | |
138 xfer = slpcall->xfer; | |
139 | |
140 xfer->bytes_sent = (offset + len); | |
141 xfer->bytes_remaining = total_length - (offset + len); | |
142 | |
143 gaim_xfer_update_progress(xfer); | |
144 } | |
145 | |
146 void | |
147 msn_xfer_end_cb(MsnSlpCall *slpcall, MsnSession *session) | |
148 { | |
149 if ((gaim_xfer_get_status(slpcall->xfer) != GAIM_XFER_STATUS_DONE) && | |
150 (gaim_xfer_get_status(slpcall->xfer) != GAIM_XFER_STATUS_CANCEL_REMOTE) && | |
151 (gaim_xfer_get_status(slpcall->xfer) != GAIM_XFER_STATUS_CANCEL_LOCAL)) | |
152 { | |
153 gaim_xfer_cancel_remote(slpcall->xfer); | |
154 } | |
155 } | |
156 | |
157 void | |
158 msn_xfer_completed_cb(MsnSlpCall *slpcall, const guchar *body, | |
159 gsize size) | |
160 { | |
161 gaim_xfer_set_completed(slpcall->xfer, TRUE); | |
162 } | |
163 | |
164 /************************************************************************** | |
165 * SLP Control | |
166 **************************************************************************/ | |
167 | |
168 #if 0 | |
169 static void | |
170 got_transresp(MsnSlpCall *slpcall, const char *nonce, | |
171 const char *ips_str, int port) | |
172 { | |
173 MsnDirectConn *directconn; | |
174 char **ip_addrs, **c; | |
175 | |
176 directconn = msn_directconn_new(slpcall->slplink); | |
177 | |
178 directconn->initial_call = slpcall; | |
179 | |
180 /* msn_directconn_parse_nonce(directconn, nonce); */ | |
181 directconn->nonce = g_strdup(nonce); | |
182 | |
183 ip_addrs = g_strsplit(ips_str, " ", -1); | |
184 | |
185 for (c = ip_addrs; *c != NULL; c++) | |
186 { | |
187 gaim_debug_info("msn", "ip_addr = %s\n", *c); | |
188 if (msn_directconn_connect(directconn, *c, port)) | |
189 break; | |
190 } | |
191 | |
192 g_strfreev(ip_addrs); | |
193 } | |
194 #endif | |
195 | |
196 static void | |
197 send_ok(MsnSlpCall *slpcall, const char *branch, | |
198 const char *type, const char *content) | |
199 { | |
200 MsnSlpLink *slplink; | |
201 MsnSlpMessage *slpmsg; | |
202 | |
203 slplink = slpcall->slplink; | |
204 | |
205 /* 200 OK */ | |
206 slpmsg = msn_slpmsg_sip_new(slpcall, 1, | |
207 "MSNSLP/1.0 200 OK", | |
208 branch, type, content); | |
209 | |
210 #ifdef MSN_DEBUG_SLP | |
211 slpmsg->info = "SLP 200 OK"; | |
212 slpmsg->text_body = TRUE; | |
213 #endif | |
214 | |
215 msn_slplink_queue_slpmsg(slplink, slpmsg); | |
216 | |
217 msn_slp_call_session_init(slpcall); | |
218 } | |
219 | |
220 static void | |
221 send_decline(MsnSlpCall *slpcall, const char *branch, | |
222 const char *type, const char *content) | |
223 { | |
224 MsnSlpLink *slplink; | |
225 MsnSlpMessage *slpmsg; | |
226 | |
227 slplink = slpcall->slplink; | |
228 | |
229 /* 603 Decline */ | |
230 slpmsg = msn_slpmsg_sip_new(slpcall, 1, | |
231 "MSNSLP/1.0 603 Decline", | |
232 branch, type, content); | |
233 | |
234 #ifdef MSN_DEBUG_SLP | |
235 slpmsg->info = "SLP 603 Decline"; | |
236 slpmsg->text_body = TRUE; | |
237 #endif | |
238 | |
239 msn_slplink_queue_slpmsg(slplink, slpmsg); | |
240 } | |
241 | |
242 #define MAX_FILE_NAME_LEN 0x226 | |
243 | |
244 static void | |
245 got_sessionreq(MsnSlpCall *slpcall, const char *branch, | |
246 const char *euf_guid, const char *context) | |
247 { | |
248 if (!strcmp(euf_guid, "A4268EEC-FEC5-49E5-95C3-F126696BDBF6")) | |
249 { | |
250 /* Emoticon or UserDisplay */ | |
251 MsnSlpSession *slpsession; | |
252 MsnSlpLink *slplink; | |
253 MsnSlpMessage *slpmsg; | |
254 MsnObject *obj; | |
255 char *msnobj_data; | |
256 const char *file_name; | |
257 char *content; | |
258 gsize len; | |
259 int type; | |
260 | |
261 /* Send Ok */ | |
262 content = g_strdup_printf("SessionID: %lu\r\n\r\n", | |
263 slpcall->session_id); | |
264 | |
265 send_ok(slpcall, branch, "application/x-msnmsgr-sessionreqbody", | |
266 content); | |
267 | |
268 g_free(content); | |
269 | |
270 slplink = slpcall->slplink; | |
271 | |
272 msnobj_data = (char *)gaim_base64_decode(context, &len); | |
273 obj = msn_object_new_from_string(msnobj_data); | |
274 type = msn_object_get_type(obj); | |
275 g_free(msnobj_data); | |
276 | |
277 if (!(type == MSN_OBJECT_USERTILE)) | |
278 { | |
279 gaim_debug_error("msn", "Wrong object?\n"); | |
280 msn_object_destroy(obj); | |
281 g_return_if_reached(); | |
282 } | |
283 | |
284 file_name = msn_object_get_real_location(obj); | |
285 | |
286 if (file_name == NULL) | |
287 { | |
288 gaim_debug_error("msn", "Wrong object.\n"); | |
289 msn_object_destroy(obj); | |
290 g_return_if_reached(); | |
291 } | |
292 | |
293 msn_object_destroy(obj); | |
294 | |
295 slpsession = msn_slplink_find_slp_session(slplink, | |
296 slpcall->session_id); | |
297 | |
298 /* DATA PREP */ | |
299 slpmsg = msn_slpmsg_new(slplink); | |
300 slpmsg->slpcall = slpcall; | |
301 slpmsg->slpsession = slpsession; | |
302 slpmsg->session_id = slpsession->id; | |
303 msn_slpmsg_set_body(slpmsg, NULL, 4); | |
304 #ifdef MSN_DEBUG_SLP | |
305 slpmsg->info = "SLP DATA PREP"; | |
306 #endif | |
307 msn_slplink_queue_slpmsg(slplink, slpmsg); | |
308 | |
309 /* DATA */ | |
310 slpmsg = msn_slpmsg_new(slplink); | |
311 slpmsg->slpcall = slpcall; | |
312 slpmsg->slpsession = slpsession; | |
313 slpmsg->flags = 0x20; | |
314 #ifdef MSN_DEBUG_SLP | |
315 slpmsg->info = "SLP DATA"; | |
316 #endif | |
317 msn_slpmsg_open_file(slpmsg, file_name); | |
318 msn_slplink_queue_slpmsg(slplink, slpmsg); | |
319 } | |
320 else if (!strcmp(euf_guid, "5D3E02AB-6190-11D3-BBBB-00C04F795683")) | |
321 { | |
322 /* File Transfer */ | |
323 GaimAccount *account; | |
324 GaimXfer *xfer; | |
325 char *bin; | |
326 gsize bin_len; | |
327 guint32 file_size; | |
328 char *file_name; | |
329 gunichar2 *uni_name; | |
330 | |
331 account = slpcall->slplink->session->account; | |
332 | |
333 slpcall->cb = msn_xfer_completed_cb; | |
334 slpcall->end_cb = msn_xfer_end_cb; | |
335 slpcall->progress_cb = msn_xfer_progress_cb; | |
336 slpcall->branch = g_strdup(branch); | |
337 | |
338 slpcall->pending = TRUE; | |
339 | |
340 xfer = gaim_xfer_new(account, GAIM_XFER_RECEIVE, | |
341 slpcall->slplink->remote_user); | |
342 | |
343 bin = (char *)gaim_base64_decode(context, &bin_len); | |
344 file_size = GUINT32_FROM_LE(*((gsize *)bin + 2)); | |
345 | |
346 uni_name = (gunichar2 *)(bin + 20); | |
347 while(*uni_name != 0 && ((char *)uni_name - (bin + 20)) < MAX_FILE_NAME_LEN) { | |
348 *uni_name = GUINT16_FROM_LE(*uni_name); | |
349 uni_name++; | |
350 } | |
351 | |
352 file_name = g_utf16_to_utf8((const gunichar2 *)(bin + 20), -1, | |
353 NULL, NULL, NULL); | |
354 | |
355 g_free(bin); | |
356 | |
357 gaim_xfer_set_filename(xfer, file_name); | |
358 gaim_xfer_set_size(xfer, file_size); | |
359 gaim_xfer_set_init_fnc(xfer, msn_xfer_init); | |
360 gaim_xfer_set_request_denied_fnc(xfer, msn_xfer_cancel); | |
361 gaim_xfer_set_cancel_recv_fnc(xfer, msn_xfer_cancel); | |
362 | |
363 slpcall->xfer = xfer; | |
364 xfer->data = slpcall; | |
365 | |
366 gaim_xfer_request(xfer); | |
367 } | |
368 } | |
369 | |
370 void | |
371 send_bye(MsnSlpCall *slpcall, const char *type) | |
372 { | |
373 MsnSlpLink *slplink; | |
374 MsnSlpMessage *slpmsg; | |
375 char *header; | |
376 | |
377 slplink = slpcall->slplink; | |
378 | |
379 g_return_if_fail(slplink != NULL); | |
380 | |
381 header = g_strdup_printf("BYE MSNMSGR:%s MSNSLP/1.0", | |
382 slplink->local_user); | |
383 | |
384 slpmsg = msn_slpmsg_sip_new(slpcall, 0, header, | |
385 "A0D624A6-6C0C-4283-A9E0-BC97B4B46D32", | |
386 type, | |
387 "\r\n"); | |
388 g_free(header); | |
389 | |
390 #ifdef MSN_DEBUG_SLP | |
391 slpmsg->info = "SLP BYE"; | |
392 slpmsg->text_body = TRUE; | |
393 #endif | |
394 | |
395 msn_slplink_queue_slpmsg(slplink, slpmsg); | |
396 } | |
397 | |
398 static void | |
399 got_invite(MsnSlpCall *slpcall, | |
400 const char *branch, const char *type, const char *content) | |
401 { | |
402 MsnSlpLink *slplink; | |
403 | |
404 slplink = slpcall->slplink; | |
405 | |
406 if (!strcmp(type, "application/x-msnmsgr-sessionreqbody")) | |
407 { | |
408 char *euf_guid, *context; | |
409 char *temp; | |
410 | |
411 euf_guid = get_token(content, "EUF-GUID: {", "}\r\n"); | |
412 | |
413 temp = get_token(content, "SessionID: ", "\r\n"); | |
414 if (temp != NULL) | |
415 slpcall->session_id = atoi(temp); | |
416 g_free(temp); | |
417 | |
418 temp = get_token(content, "AppID: ", "\r\n"); | |
419 if (temp != NULL) | |
420 slpcall->app_id = atoi(temp); | |
421 g_free(temp); | |
422 | |
423 context = get_token(content, "Context: ", "\r\n"); | |
424 | |
14433 | 425 if (context != NULL) |
426 got_sessionreq(slpcall, branch, euf_guid, context); | |
14192 | 427 |
428 g_free(context); | |
429 g_free(euf_guid); | |
430 } | |
431 else if (!strcmp(type, "application/x-msnmsgr-transreqbody")) | |
432 { | |
433 /* A direct connection? */ | |
434 | |
435 char *listening, *nonce; | |
436 char *content; | |
437 | |
438 if (FALSE) | |
439 { | |
440 #if 0 | |
441 MsnDirectConn *directconn; | |
442 /* const char *ip_addr; */ | |
443 char *ip_port; | |
444 int port; | |
445 | |
446 /* ip_addr = gaim_prefs_get_string("/core/ft/public_ip"); */ | |
447 ip_port = "5190"; | |
448 listening = "true"; | |
449 nonce = rand_guid(); | |
450 | |
451 directconn = msn_directconn_new(slplink); | |
452 | |
453 /* msn_directconn_parse_nonce(directconn, nonce); */ | |
454 directconn->nonce = g_strdup(nonce); | |
455 | |
456 msn_directconn_listen(directconn); | |
457 | |
458 port = directconn->port; | |
459 | |
460 content = g_strdup_printf( | |
461 "Bridge: TCPv1\r\n" | |
462 "Listening: %s\r\n" | |
463 "Nonce: {%s}\r\n" | |
464 "Ipv4Internal-Addrs: 192.168.0.82\r\n" | |
465 "Ipv4Internal-Port: %d\r\n" | |
466 "\r\n", | |
467 listening, | |
468 nonce, | |
469 port); | |
470 #endif | |
471 } | |
472 else | |
473 { | |
474 listening = "false"; | |
475 nonce = g_strdup("00000000-0000-0000-0000-000000000000"); | |
476 | |
477 content = g_strdup_printf( | |
478 "Bridge: TCPv1\r\n" | |
479 "Listening: %s\r\n" | |
480 "Nonce: {%s}\r\n" | |
481 "\r\n", | |
482 listening, | |
483 nonce); | |
484 } | |
485 | |
486 send_ok(slpcall, branch, | |
487 "application/x-msnmsgr-transrespbody", content); | |
488 | |
489 g_free(content); | |
490 g_free(nonce); | |
491 } | |
492 else if (!strcmp(type, "application/x-msnmsgr-transrespbody")) | |
493 { | |
494 #if 0 | |
495 char *ip_addrs; | |
496 char *temp; | |
497 char *nonce; | |
498 int port; | |
499 | |
500 nonce = get_token(content, "Nonce: {", "}\r\n"); | |
501 ip_addrs = get_token(content, "IPv4Internal-Addrs: ", "\r\n"); | |
502 | |
503 temp = get_token(content, "IPv4Internal-Port: ", "\r\n"); | |
504 if (temp != NULL) | |
505 port = atoi(temp); | |
506 else | |
507 port = -1; | |
508 g_free(temp); | |
509 | |
510 if (ip_addrs == NULL) | |
511 return; | |
512 | |
513 if (port > 0) | |
514 got_transresp(slpcall, nonce, ip_addrs, port); | |
515 | |
516 g_free(nonce); | |
517 g_free(ip_addrs); | |
518 #endif | |
519 } | |
520 } | |
521 | |
522 static void | |
523 got_ok(MsnSlpCall *slpcall, | |
524 const char *type, const char *content) | |
525 { | |
526 g_return_if_fail(slpcall != NULL); | |
527 g_return_if_fail(type != NULL); | |
528 | |
529 if (!strcmp(type, "application/x-msnmsgr-sessionreqbody")) | |
530 { | |
531 #if 0 | |
532 if (slpcall->type == MSN_SLPCALL_DC) | |
533 { | |
534 /* First let's try a DirectConnection. */ | |
535 | |
536 MsnSlpLink *slplink; | |
537 MsnSlpMessage *slpmsg; | |
538 char *header; | |
539 char *content; | |
540 char *branch; | |
541 | |
542 slplink = slpcall->slplink; | |
543 | |
544 branch = rand_guid(); | |
545 | |
546 content = g_strdup_printf( | |
547 "Bridges: TRUDPv1 TCPv1\r\n" | |
548 "NetID: 0\r\n" | |
549 "Conn-Type: Direct-Connect\r\n" | |
550 "UPnPNat: false\r\n" | |
551 "ICF: false\r\n" | |
552 ); | |
553 | |
554 header = g_strdup_printf("INVITE MSNMSGR:%s MSNSLP/1.0", | |
555 slplink->remote_user); | |
556 | |
557 slpmsg = msn_slp_sipmsg_new(slpcall, 0, header, branch, | |
558 "application/x-msnmsgr-transreqbody", | |
559 content); | |
560 | |
561 #ifdef MSN_DEBUG_SLP | |
562 slpmsg->info = "SLP INVITE"; | |
563 slpmsg->text_body = TRUE; | |
564 #endif | |
565 msn_slplink_send_slpmsg(slplink, slpmsg); | |
566 | |
567 g_free(header); | |
568 g_free(content); | |
569 | |
570 g_free(branch); | |
571 } | |
572 else | |
573 { | |
574 msn_slp_call_session_init(slpcall); | |
575 } | |
576 #else | |
577 msn_slp_call_session_init(slpcall); | |
578 #endif | |
579 } | |
580 else if (!strcmp(type, "application/x-msnmsgr-transreqbody")) | |
581 { | |
582 /* Do we get this? */ | |
583 gaim_debug_info("msn", "OK with transreqbody\n"); | |
584 } | |
585 else if (!strcmp(type, "application/x-msnmsgr-transrespbody")) | |
586 { | |
587 #if 0 | |
588 char *ip_addrs; | |
589 char *temp; | |
590 char *nonce; | |
591 int port; | |
592 | |
593 nonce = get_token(content, "Nonce: {", "}\r\n"); | |
594 ip_addrs = get_token(content, "IPv4Internal-Addrs: ", "\r\n"); | |
595 | |
596 temp = get_token(content, "IPv4Internal-Port: ", "\r\n"); | |
597 if (temp != NULL) | |
598 port = atoi(temp); | |
599 else | |
600 port = -1; | |
601 g_free(temp); | |
602 | |
603 if (ip_addrs == NULL) | |
604 return; | |
605 | |
606 if (port > 0) | |
607 got_transresp(slpcall, nonce, ip_addrs, port); | |
608 | |
609 g_free(nonce); | |
610 g_free(ip_addrs); | |
611 #endif | |
612 } | |
613 } | |
614 | |
615 MsnSlpCall * | |
616 msn_slp_sip_recv(MsnSlpLink *slplink, const char *body) | |
617 { | |
618 MsnSlpCall *slpcall; | |
619 | |
620 if (body == NULL) | |
621 { | |
622 gaim_debug_warning("msn", "received bogus message\n"); | |
623 return NULL; | |
624 } | |
625 | |
626 if (!strncmp(body, "INVITE", strlen("INVITE"))) | |
627 { | |
628 char *branch; | |
629 char *content; | |
630 char *content_type; | |
631 | |
632 slpcall = msn_slp_call_new(slplink); | |
633 | |
634 /* From: <msnmsgr:buddy@hotmail.com> */ | |
635 #if 0 | |
636 slpcall->remote_user = get_token(body, "From: <msnmsgr:", ">\r\n"); | |
637 #endif | |
638 | |
639 branch = get_token(body, ";branch={", "}"); | |
640 | |
641 slpcall->id = get_token(body, "Call-ID: {", "}"); | |
642 | |
643 #if 0 | |
644 long content_len = -1; | |
645 | |
646 temp = get_token(body, "Content-Length: ", "\r\n"); | |
647 if (temp != NULL) | |
648 content_len = atoi(temp); | |
649 g_free(temp); | |
650 #endif | |
651 content_type = get_token(body, "Content-Type: ", "\r\n"); | |
652 | |
653 content = get_token(body, "\r\n\r\n", NULL); | |
654 | |
655 got_invite(slpcall, branch, content_type, content); | |
656 | |
657 g_free(branch); | |
658 g_free(content_type); | |
659 g_free(content); | |
660 } | |
661 else if (!strncmp(body, "MSNSLP/1.0 ", strlen("MSNSLP/1.0 "))) | |
662 { | |
663 char *content; | |
664 char *content_type; | |
665 /* Make sure this is "OK" */ | |
666 const char *status = body + strlen("MSNSLP/1.0 "); | |
667 char *call_id; | |
668 | |
669 call_id = get_token(body, "Call-ID: {", "}"); | |
670 slpcall = msn_slplink_find_slp_call(slplink, call_id); | |
671 g_free(call_id); | |
672 | |
673 g_return_val_if_fail(slpcall != NULL, NULL); | |
674 | |
675 if (strncmp(status, "200 OK", 6)) | |
676 { | |
677 /* It's not valid. Kill this off. */ | |
678 char temp[32]; | |
679 const char *c; | |
680 | |
681 /* Eww */ | |
682 if ((c = strchr(status, '\r')) || (c = strchr(status, '\n')) || | |
683 (c = strchr(status, '\0'))) | |
684 { | |
685 size_t offset = c - status; | |
686 if (offset >= sizeof(temp)) | |
687 offset = sizeof(temp) - 1; | |
688 | |
689 strncpy(temp, status, offset); | |
690 temp[offset] = '\0'; | |
691 } | |
692 | |
693 gaim_debug_error("msn", "Received non-OK result: %s\n", temp); | |
694 | |
695 slpcall->wasted = TRUE; | |
696 | |
697 /* msn_slp_call_destroy(slpcall); */ | |
698 return slpcall; | |
699 } | |
700 | |
701 content_type = get_token(body, "Content-Type: ", "\r\n"); | |
702 | |
703 content = get_token(body, "\r\n\r\n", NULL); | |
704 | |
705 got_ok(slpcall, content_type, content); | |
706 | |
707 g_free(content_type); | |
708 g_free(content); | |
709 } | |
710 else if (!strncmp(body, "BYE", strlen("BYE"))) | |
711 { | |
712 char *call_id; | |
713 | |
714 call_id = get_token(body, "Call-ID: {", "}"); | |
715 slpcall = msn_slplink_find_slp_call(slplink, call_id); | |
716 g_free(call_id); | |
717 | |
718 if (slpcall != NULL) | |
719 slpcall->wasted = TRUE; | |
720 | |
721 /* msn_slp_call_destroy(slpcall); */ | |
722 } | |
723 else | |
724 slpcall = NULL; | |
725 | |
726 return slpcall; | |
727 } | |
728 | |
729 /************************************************************************** | |
730 * Msg Callbacks | |
731 **************************************************************************/ | |
732 | |
733 void | |
734 msn_p2p_msg(MsnCmdProc *cmdproc, MsnMessage *msg) | |
735 { | |
736 MsnSession *session; | |
737 MsnSlpLink *slplink; | |
738 | |
739 session = cmdproc->servconn->session; | |
740 slplink = msn_session_get_slplink(session, msg->remote_user); | |
741 | |
742 if (slplink->swboard == NULL) | |
743 { | |
744 /* We will need this in order to change its flags. */ | |
745 slplink->swboard = (MsnSwitchBoard *)cmdproc->data; | |
746 /* If swboard is NULL, something has probably gone wrong earlier on | |
747 * I didn't want to do this, but MSN 7 is somehow causing us to crash | |
748 * here, I couldn't reproduce it to debug more, and people are | |
749 * reporting bugs. Hopefully this doesn't cause more crashes. Stu. | |
750 */ | |
751 if (slplink->swboard != NULL) | |
752 slplink->swboard->slplinks = g_list_prepend(slplink->swboard->slplinks, slplink); | |
753 else | |
754 gaim_debug_error("msn", "msn_p2p_msg, swboard is NULL, ouch!\n"); | |
755 } | |
756 | |
757 msn_slplink_process_msg(slplink, msg); | |
758 } | |
759 | |
760 static void | |
761 got_emoticon(MsnSlpCall *slpcall, | |
762 const guchar *data, gsize size) | |
763 { | |
764 | |
765 GaimConversation *conv; | |
766 GaimConnection *gc; | |
767 const char *who; | |
768 | |
769 gc = slpcall->slplink->session->account->gc; | |
770 who = slpcall->slplink->remote_user; | |
771 | |
772 if ((conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_ANY, who, gc->account))) { | |
773 | |
774 /* FIXME: it would be better if we wrote the data as we received it | |
775 instead of all at once, calling write multiple times and | |
776 close once at the very end | |
777 */ | |
778 gaim_conv_custom_smiley_write(conv, slpcall->data_info, data, size); | |
779 gaim_conv_custom_smiley_close(conv, slpcall->data_info); | |
780 } | |
781 #ifdef MSN_DEBUG_UD | |
782 gaim_debug_info("msn", "Got smiley: %s\n", slpcall->data_info); | |
783 #endif | |
784 } | |
785 | |
786 void | |
787 msn_emoticon_msg(MsnCmdProc *cmdproc, MsnMessage *msg) | |
788 { | |
789 MsnSession *session; | |
790 MsnSlpLink *slplink; | |
791 MsnObject *obj; | |
792 char **tokens; | |
793 char *smile, *body_str; | |
794 const char *body, *who, *sha1c; | |
795 guint tok; | |
796 size_t body_len; | |
797 | |
798 GaimConversation *conv; | |
799 | |
800 session = cmdproc->servconn->session; | |
801 | |
802 if (!gaim_account_get_bool(session->account, "custom_smileys", TRUE)) | |
803 return; | |
804 | |
805 body = msn_message_get_bin_data(msg, &body_len); | |
806 body_str = g_strndup(body, body_len); | |
807 | |
808 /* MSN Messenger 7 may send more than one MSNObject in a single message... | |
809 * Maybe 10 tokens is a reasonable max value. */ | |
810 tokens = g_strsplit(body_str, "\t", 10); | |
811 | |
812 g_free(body_str); | |
813 | |
814 for (tok = 0; tok < 9; tok += 2) { | |
815 if (tokens[tok] == NULL || tokens[tok + 1] == NULL) { | |
816 break; | |
817 } | |
818 | |
819 smile = tokens[tok]; | |
820 obj = msn_object_new_from_string(gaim_url_decode(tokens[tok + 1])); | |
821 | |
822 who = msn_object_get_creator(obj); | |
823 sha1c = msn_object_get_sha1c(obj); | |
824 | |
825 slplink = msn_session_get_slplink(session, who); | |
826 | |
827 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_ANY, who, | |
828 session->account); | |
829 | |
830 /* If the conversation doesn't exist then this is a custom smiley | |
831 * used in the first message in a MSN conversation: we need to create | |
832 * the conversation now, otherwise the custom smiley won't be shown. | |
833 * This happens because every GtkIMHtml has its own smiley tree: if | |
834 * the conversation doesn't exist then we cannot associate the new | |
835 * smiley with its GtkIMHtml widget. */ | |
836 if (!conv) { | |
837 conv = gaim_conversation_new(GAIM_CONV_TYPE_IM, session->account, who); | |
838 } | |
839 | |
840 if (gaim_conv_custom_smiley_add(conv, smile, "sha1", sha1c, TRUE)) { | |
841 msn_slplink_request_object(slplink, smile, got_emoticon, NULL, obj); | |
842 } | |
843 | |
844 msn_object_destroy(obj); | |
845 obj = NULL; | |
846 who = NULL; | |
847 sha1c = NULL; | |
848 } | |
849 g_strfreev(tokens); | |
850 } | |
851 | |
852 static gboolean | |
853 buddy_icon_cached(GaimConnection *gc, MsnObject *obj) | |
854 { | |
855 GaimAccount *account; | |
856 GaimBuddy *buddy; | |
857 const char *old; | |
858 const char *new; | |
859 | |
860 g_return_val_if_fail(obj != NULL, FALSE); | |
861 | |
862 account = gaim_connection_get_account(gc); | |
863 | |
864 buddy = gaim_find_buddy(account, msn_object_get_creator(obj)); | |
865 if (buddy == NULL) | |
866 return FALSE; | |
867 | |
868 old = gaim_blist_node_get_string((GaimBlistNode *)buddy, "icon_checksum"); | |
869 new = msn_object_get_sha1c(obj); | |
870 | |
871 if (new == NULL) | |
872 return FALSE; | |
873 | |
874 /* If the old and new checksums are the same, and the file actually exists, | |
875 * then return TRUE */ | |
876 if (old != NULL && !strcmp(old, new) && (gaim_buddy_icons_find(account, gaim_buddy_get_name(buddy)) != NULL)) | |
877 return TRUE; | |
878 | |
879 return FALSE; | |
880 } | |
881 | |
882 static void | |
883 msn_release_buddy_icon_request(MsnUserList *userlist) | |
884 { | |
885 MsnUser *user; | |
886 | |
887 g_return_if_fail(userlist != NULL); | |
888 | |
889 #ifdef MSN_DEBUG_UD | |
890 gaim_debug_info("msn", "Releasing buddy icon request\n"); | |
891 #endif | |
892 | |
893 if (userlist->buddy_icon_window > 0) | |
894 { | |
895 GQueue *queue; | |
896 GaimAccount *account; | |
897 const char *username; | |
898 | |
899 queue = userlist->buddy_icon_requests; | |
900 | |
901 if (g_queue_is_empty(userlist->buddy_icon_requests)) | |
902 return; | |
903 | |
904 user = g_queue_pop_head(queue); | |
905 | |
906 account = userlist->session->account; | |
907 username = user->passport; | |
908 | |
909 userlist->buddy_icon_window--; | |
910 msn_request_user_display(user); | |
911 | |
912 #ifdef MSN_DEBUG_UD | |
913 gaim_debug_info("msn", "msn_release_buddy_icon_request(): buddy_icon_window-- yields =%d\n", | |
914 userlist->buddy_icon_window); | |
915 #endif | |
916 } | |
917 } | |
918 | |
919 /* | |
920 * Called on a timeout from end_user_display(). Frees a buddy icon window slow and dequeues the next | |
921 * buddy icon request if there is one. | |
922 */ | |
923 static gboolean | |
924 msn_release_buddy_icon_request_timeout(gpointer data) | |
925 { | |
926 MsnUserList *userlist = (MsnUserList *)data; | |
927 | |
928 /* Free one window slot */ | |
929 userlist->buddy_icon_window++; | |
930 | |
931 /* Clear the tag for our former request timer */ | |
932 userlist->buddy_icon_request_timer = 0; | |
933 | |
934 msn_release_buddy_icon_request(userlist); | |
935 | |
936 return FALSE; | |
937 } | |
938 | |
939 void | |
940 msn_queue_buddy_icon_request(MsnUser *user) | |
941 { | |
942 GaimAccount *account; | |
943 MsnObject *obj; | |
944 GQueue *queue; | |
945 | |
946 g_return_if_fail(user != NULL); | |
947 | |
948 account = user->userlist->session->account; | |
949 | |
950 obj = msn_user_get_object(user); | |
951 | |
952 if (obj == NULL) | |
953 { | |
954 /* It seems the user has not set a msnobject */ | |
955 GSList *sl, *list; | |
956 | |
957 list = gaim_find_buddies(account, user->passport); | |
958 | |
959 for (sl = list; sl != NULL; sl = sl->next) | |
960 { | |
961 GaimBuddy *buddy = (GaimBuddy *)sl->data; | |
14486
d85f326af28d
[gaim-migrate @ 17205]
Richard Laager <rlaager@wiktel.com>
parents:
14433
diff
changeset
|
962 if (buddy->icon) |
d85f326af28d
[gaim-migrate @ 17205]
Richard Laager <rlaager@wiktel.com>
parents:
14433
diff
changeset
|
963 gaim_blist_node_remove_setting((GaimBlistNode*)buddy, "icon_checksum"); |
14192 | 964 } |
965 g_slist_free(list); | |
966 | |
14486
d85f326af28d
[gaim-migrate @ 17205]
Richard Laager <rlaager@wiktel.com>
parents:
14433
diff
changeset
|
967 /* TODO: I think we need better buddy icon core functions. */ |
d85f326af28d
[gaim-migrate @ 17205]
Richard Laager <rlaager@wiktel.com>
parents:
14433
diff
changeset
|
968 gaim_buddy_icons_set_for_user(account, user->passport, NULL, 0); |
d85f326af28d
[gaim-migrate @ 17205]
Richard Laager <rlaager@wiktel.com>
parents:
14433
diff
changeset
|
969 |
14192 | 970 return; |
971 } | |
972 | |
973 if (!buddy_icon_cached(account->gc, obj)) | |
974 { | |
975 MsnUserList *userlist; | |
976 | |
977 userlist = user->userlist; | |
978 queue = userlist->buddy_icon_requests; | |
979 | |
980 #ifdef MSN_DEBUG_UD | |
981 gaim_debug_info("msn", "Queueing buddy icon request for %s (buddy_icon_window = %i)\n", | |
982 user->passport, userlist->buddy_icon_window); | |
983 #endif | |
984 | |
985 g_queue_push_tail(queue, user); | |
986 | |
987 if (userlist->buddy_icon_window > 0) | |
988 msn_release_buddy_icon_request(userlist); | |
989 } | |
990 } | |
991 | |
992 static void | |
993 got_user_display(MsnSlpCall *slpcall, | |
994 const guchar *data, gsize size) | |
995 { | |
996 MsnUserList *userlist; | |
997 const char *info; | |
998 GaimAccount *account; | |
999 GSList *sl, *list; | |
1000 | |
1001 g_return_if_fail(slpcall != NULL); | |
1002 | |
1003 info = slpcall->data_info; | |
1004 #ifdef MSN_DEBUG_UD | |
1005 gaim_debug_info("msn", "Got User Display: %s\n", slpcall->slplink->remote_user); | |
1006 #endif | |
1007 | |
1008 userlist = slpcall->slplink->session->userlist; | |
1009 account = slpcall->slplink->session->account; | |
1010 | |
1011 /* TODO: I think we need better buddy icon core functions. */ | |
1012 gaim_buddy_icons_set_for_user(account, slpcall->slplink->remote_user, | |
1013 (void *)data, size); | |
1014 | |
1015 list = gaim_find_buddies(account, slpcall->slplink->remote_user); | |
1016 | |
1017 for (sl = list; sl != NULL; sl = sl->next) | |
1018 { | |
1019 GaimBuddy *buddy = (GaimBuddy *)sl->data; | |
1020 gaim_blist_node_set_string((GaimBlistNode*)buddy, "icon_checksum", info); | |
1021 } | |
1022 g_slist_free(list); | |
1023 | |
1024 #if 0 | |
1025 /* Free one window slot */ | |
1026 userlist->buddy_icon_window++; | |
1027 | |
1028 gaim_debug_info("msn", "got_user_display(): buddy_icon_window++ yields =%d\n", | |
1029 userlist->buddy_icon_window); | |
1030 | |
1031 msn_release_buddy_icon_request(userlist); | |
1032 #endif | |
1033 } | |
1034 | |
1035 static void | |
1036 end_user_display(MsnSlpCall *slpcall, MsnSession *session) | |
1037 { | |
1038 MsnUserList *userlist; | |
1039 | |
1040 g_return_if_fail(session != NULL); | |
1041 | |
1042 #ifdef MSN_DEBUG_UD | |
1043 gaim_debug_info("msn", "End User Display\n"); | |
1044 #endif | |
1045 | |
1046 userlist = session->userlist; | |
1047 | |
1048 /* If the session is being destroyed we better stop doing anything. */ | |
1049 if (session->destroying) | |
1050 return; | |
1051 | |
1052 /* Delay before freeing a buddy icon window slot and requesting the next icon, if appropriate. | |
1053 * If we don't delay, we'll rapidly hit the MSN equivalent of AIM's rate limiting; the server will | |
1054 * send us an error 800 like so: | |
1055 * | |
1056 * C: NS 000: XFR 21 SB | |
1057 * S: NS 000: 800 21 | |
1058 */ | |
1059 if (userlist->buddy_icon_request_timer) { | |
1060 /* Free the window slot used by this previous request */ | |
1061 userlist->buddy_icon_window++; | |
1062 | |
1063 /* Clear our pending timeout */ | |
1064 gaim_timeout_remove(userlist->buddy_icon_request_timer); | |
1065 } | |
1066 | |
1067 /* Wait BUDDY_ICON_DELAY ms before freeing our window slot and requesting the next icon. */ | |
1068 userlist->buddy_icon_request_timer = gaim_timeout_add(BUDDY_ICON_DELAY, | |
1069 msn_release_buddy_icon_request_timeout, userlist); | |
1070 } | |
1071 | |
1072 void | |
1073 msn_request_user_display(MsnUser *user) | |
1074 { | |
1075 GaimAccount *account; | |
1076 MsnSession *session; | |
1077 MsnSlpLink *slplink; | |
1078 MsnObject *obj; | |
1079 const char *info; | |
1080 | |
1081 session = user->userlist->session; | |
1082 account = session->account; | |
1083 | |
1084 slplink = msn_session_get_slplink(session, user->passport); | |
1085 | |
1086 obj = msn_user_get_object(user); | |
1087 | |
1088 info = msn_object_get_sha1c(obj); | |
1089 | |
1090 if (g_ascii_strcasecmp(user->passport, | |
1091 gaim_account_get_username(account))) | |
1092 { | |
1093 msn_slplink_request_object(slplink, info, got_user_display, | |
1094 end_user_display, obj); | |
1095 } | |
1096 else | |
1097 { | |
1098 MsnObject *my_obj = NULL; | |
1099 gchar *data = NULL; | |
1100 gsize len = 0; | |
1101 GSList *sl, *list; | |
1102 | |
1103 #ifdef MSN_DEBUG_UD | |
1104 gaim_debug_info("msn", "Requesting our own user display\n"); | |
1105 #endif | |
1106 | |
1107 my_obj = msn_user_get_object(session->user); | |
1108 | |
1109 if (my_obj != NULL) | |
1110 { | |
1111 const char *filename = msn_object_get_real_location(my_obj); | |
1112 | |
1113 if (filename != NULL) | |
1114 g_file_get_contents(filename, &data, &len, NULL); | |
1115 } | |
1116 | |
1117 /* TODO: I think we need better buddy icon core functions. */ | |
1118 gaim_buddy_icons_set_for_user(account, user->passport, (void *)data, len); | |
1119 g_free(data); | |
1120 | |
1121 list = gaim_find_buddies(account, user->passport); | |
1122 | |
1123 for (sl = list; sl != NULL; sl = sl->next) | |
1124 { | |
1125 GaimBuddy *buddy = (GaimBuddy *)sl->data; | |
1126 gaim_blist_node_set_string((GaimBlistNode*)buddy, "icon_checksum", info); | |
1127 } | |
1128 g_slist_free(list); | |
1129 | |
1130 /* Free one window slot */ | |
1131 session->userlist->buddy_icon_window++; | |
1132 | |
1133 #ifdef MSN_DEBUG_UD | |
1134 gaim_debug_info("msn", "msn_request_user_display(): buddy_icon_window++ yields =%d\n", | |
1135 session->userlist->buddy_icon_window); | |
1136 #endif | |
1137 | |
1138 msn_release_buddy_icon_request(session->userlist); | |
1139 } | |
1140 } |