Mercurial > pidgin.yaz
annotate src/protocols/msn/switchboard.c @ 5284:0a38c5a00b85
[gaim-migrate @ 5656]
jabber now compiles :-)
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Fri, 02 May 2003 18:13:08 +0000 |
parents | abe4d103e300 |
children | e2e53316a21d |
rev | line source |
---|---|
4542 | 1 /** |
2 * @file switchboard.c MSN switchboard functions | |
3 * | |
4 * gaim | |
5 * | |
6 * Copyright (C) 2003, Christian Hammond <chipx86@gnupdate.org> | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 * | |
22 */ | |
23 #include "msn.h" | |
24 | |
4546
a951bb590857
[gaim-migrate @ 4825]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4542
diff
changeset
|
25 #ifdef _WIN32 |
a951bb590857
[gaim-migrate @ 4825]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4542
diff
changeset
|
26 #include "win32dep.h" |
a951bb590857
[gaim-migrate @ 4825]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4542
diff
changeset
|
27 #endif |
a951bb590857
[gaim-migrate @ 4825]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4542
diff
changeset
|
28 |
a951bb590857
[gaim-migrate @ 4825]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4542
diff
changeset
|
29 G_MODULE_IMPORT GSList *connections; |
a951bb590857
[gaim-migrate @ 4825]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4542
diff
changeset
|
30 |
4542 | 31 static char * |
32 msn_parse_format(char *mime) | |
33 { | |
34 char *cur; | |
35 GString *ret = g_string_new(NULL); | |
36 guint colorbuf; | |
37 char *colors = (char *)(&colorbuf); | |
38 | |
39 | |
40 cur = strstr(mime, "FN="); | |
41 if (cur && (*(cur = cur + 3) != ';')) { | |
42 ret = g_string_append(ret, "<FONT FACE=\""); | |
43 while (*cur && *cur != ';') { | |
44 ret = g_string_append_c(ret, *cur); | |
45 cur++; | |
46 } | |
47 ret = g_string_append(ret, "\">"); | |
48 } | |
49 | |
50 cur = strstr(mime, "EF="); | |
51 if (cur && (*(cur = cur + 3) != ';')) { | |
52 while (*cur && *cur != ';') { | |
53 ret = g_string_append_c(ret, '<'); | |
54 ret = g_string_append_c(ret, *cur); | |
55 ret = g_string_append_c(ret, '>'); | |
56 cur++; | |
57 } | |
58 } | |
59 | |
60 cur = strstr(mime, "CO="); | |
61 if (cur && (*(cur = cur + 3) != ';')) { | |
62 if (sscanf (cur, "%x;", &colorbuf) == 1) { | |
63 char tag[64]; | |
64 g_snprintf(tag, sizeof(tag), "<FONT COLOR=\"#%02hhx%02hhx%02hhx\">", colors[0], colors[1], colors[2]); | |
65 ret = g_string_append(ret, tag); | |
66 } | |
67 } | |
68 | |
69 cur = url_decode(ret->str); | |
70 g_string_free(ret, TRUE); | |
71 return cur; | |
72 } | |
73 | |
74 static int | |
75 msn_process_switch(struct msn_switchboard *ms, char *buf) | |
76 { | |
77 struct gaim_connection *gc = ms->gc; | |
78 char sendbuf[MSN_BUF_LEN]; | |
79 static int id = 0; | |
80 | |
4793 | 81 if (!g_ascii_strncasecmp(buf, "ACK", 3)) { |
82 } else if (!g_ascii_strncasecmp(buf, "ANS", 3)) { | |
4542 | 83 if (ms->chat) |
84 gaim_chat_add_user(GAIM_CHAT(ms->chat), gc->username, NULL); | |
4793 | 85 } else if (!g_ascii_strncasecmp(buf, "BYE", 3)) { |
4542 | 86 char *user, *tmp = buf; |
87 GET_NEXT(tmp); | |
88 user = tmp; | |
89 | |
90 if (ms->chat) { | |
91 gaim_chat_remove_user(GAIM_CHAT(ms->chat), user, NULL); | |
92 } else { | |
93 char msgbuf[256]; | |
94 const char *username; | |
95 struct gaim_conversation *cnv; | |
96 struct buddy *b; | |
97 | |
4687 | 98 if ((b = gaim_find_buddy(gc->account, user)) != NULL) |
99 username = gaim_get_buddy_alias(b); | |
4542 | 100 else |
101 username = user; | |
102 | |
103 g_snprintf(msgbuf, sizeof(msgbuf), | |
104 _("%s has closed the conversation window"), username); | |
105 | |
106 if ((cnv = gaim_find_conversation(user))) | |
107 gaim_conversation_write(cnv, NULL, msgbuf, -1, | |
4687 | 108 WFLAG_SYSTEM, time(NULL)); |
4542 | 109 |
110 msn_kill_switch(ms); | |
111 return 0; | |
112 } | |
4793 | 113 } else if (!g_ascii_strncasecmp(buf, "CAL", 3)) { |
114 } else if (!g_ascii_strncasecmp(buf, "IRO", 3)) { | |
4542 | 115 char *tot, *user, *tmp = buf; |
116 | |
117 GET_NEXT(tmp); | |
118 GET_NEXT(tmp); | |
119 GET_NEXT(tmp); | |
120 tot = tmp; | |
121 GET_NEXT(tmp); | |
122 ms->total = atoi(tot); | |
123 user = tmp; | |
124 GET_NEXT(tmp); | |
125 | |
126 if (ms->total > 1) { | |
127 if (!ms->chat) | |
128 ms->chat = serv_got_joined_chat(gc, ++id, "MSN Chat"); | |
129 | |
130 gaim_chat_add_user(GAIM_CHAT(ms->chat), user, NULL); | |
131 } | |
4793 | 132 } else if (!g_ascii_strncasecmp(buf, "JOI", 3)) { |
4542 | 133 char *user, *tmp = buf; |
134 GET_NEXT(tmp); | |
135 user = tmp; | |
136 GET_NEXT(tmp); | |
137 | |
138 if (ms->total == 1) { | |
139 ms->chat = serv_got_joined_chat(gc, ++id, "MSN Chat"); | |
140 gaim_chat_add_user(GAIM_CHAT(ms->chat), ms->user, NULL); | |
141 gaim_chat_add_user(GAIM_CHAT(ms->chat), gc->username, NULL); | |
142 g_free(ms->user); | |
143 ms->user = NULL; | |
144 } | |
145 if (ms->chat) | |
146 gaim_chat_add_user(GAIM_CHAT(ms->chat), user, NULL); | |
147 ms->total++; | |
148 while (ms->txqueue) { | |
149 char *send = add_cr(ms->txqueue->data); | |
150 g_snprintf(sendbuf, sizeof(sendbuf), | |
151 "MSG %u N %d\r\n%s%s", ++ms->trId, | |
152 strlen(MIME_HEADER) + strlen(send), | |
153 MIME_HEADER, send); | |
154 | |
155 g_free(ms->txqueue->data); | |
156 ms->txqueue = g_slist_remove(ms->txqueue, ms->txqueue->data); | |
157 | |
158 if (msn_write(ms->fd, sendbuf, strlen(sendbuf)) < 0) { | |
159 msn_kill_switch(ms); | |
160 return 0; | |
161 } | |
162 } | |
4793 | 163 } else if (!g_ascii_strncasecmp(buf, "MSG", 3)) { |
4542 | 164 char *user, *tmp = buf; |
165 int length; | |
166 | |
167 GET_NEXT(tmp); | |
168 user = tmp; | |
169 | |
170 GET_NEXT(tmp); | |
171 | |
172 GET_NEXT(tmp); | |
173 length = atoi(tmp); | |
174 | |
175 ms->msg = TRUE; | |
176 ms->msguser = g_strdup(user); | |
177 ms->msglen = length; | |
4793 | 178 } else if (!g_ascii_strncasecmp(buf, "NAK", 3)) { |
4542 | 179 do_error_dialog(_("An MSN message may not have been received."), NULL, GAIM_ERROR); |
4793 | 180 } else if (!g_ascii_strncasecmp(buf, "NLN", 3)) { |
181 } else if (!g_ascii_strncasecmp(buf, "OUT", 3)) { | |
4542 | 182 if (ms->chat) |
183 serv_got_chat_left(gc, gaim_chat_get_id(GAIM_CHAT(ms->chat))); | |
184 msn_kill_switch(ms); | |
185 return 0; | |
4793 | 186 } else if (!g_ascii_strncasecmp(buf, "USR", 3)) { |
4542 | 187 /* good, we got USR, now we need to find out who we want to talk to */ |
188 struct msn_switchboard *ms = msn_find_writable_switch(gc); | |
189 | |
190 if (!ms) | |
191 return 0; | |
192 | |
193 g_snprintf(sendbuf, sizeof(sendbuf), "CAL %u %s\r\n", | |
194 ++ms->trId, ms->user); | |
195 | |
196 if (msn_write(ms->fd, sendbuf, strlen(sendbuf)) < 0) { | |
197 msn_kill_switch(ms); | |
198 return 0; | |
199 } | |
200 } else if (isdigit(*buf)) { | |
201 handle_errcode(buf, TRUE); | |
202 | |
203 if (atoi(buf) == 217) | |
204 msn_kill_switch(ms); | |
205 | |
206 } else { | |
5221
abe4d103e300
[gaim-migrate @ 5591]
Christian Hammond <chipx86@chipx86.com>
parents:
4793
diff
changeset
|
207 gaim_debug(GAIM_DEBUG_WARNING, "msn", "Unhandled message!\n"); |
4542 | 208 } |
209 | |
210 return 1; | |
211 } | |
212 | |
213 static void | |
214 msn_process_switch_msg(struct msn_switchboard *ms, char *msg) | |
215 { | |
216 char *content, *agent, *format; | |
217 char *message = NULL; | |
218 int flags = 0; | |
219 | |
220 agent = strstr(msg, "User-Agent: "); | |
221 if (agent) { | |
4793 | 222 if (!g_ascii_strncasecmp(agent, "User-Agent: Gaim", |
4542 | 223 strlen("User-Agent: Gaim"))) |
224 flags |= IM_FLAG_GAIMUSER; | |
225 } | |
226 | |
227 format = strstr(msg, "X-MMS-IM-Format: "); | |
228 if (format) { | |
229 format = msn_parse_format(format); | |
230 } else { | |
231 format = NULL; | |
232 } | |
233 | |
234 content = strstr(msg, "Content-Type: "); | |
235 if (!content) | |
236 return; | |
4793 | 237 if (!g_ascii_strncasecmp(content, "Content-Type: text/x-msmsgscontrol\r\n", |
4542 | 238 strlen( "Content-Type: text/x-msmsgscontrol\r\n"))) { |
239 if (strstr(content,"TypingUser: ") && !ms->chat) { | |
240 serv_got_typing(ms->gc, ms->msguser, | |
241 MSN_TYPING_RECV_TIMEOUT, TYPING); | |
242 return; | |
243 } | |
244 | |
4793 | 245 } else if (!g_ascii_strncasecmp(content, "Content-Type: text/x-msmsgsinvite;", |
4542 | 246 strlen("Content-Type: text/x-msmsgsinvite;"))) { |
247 | |
248 /* | |
249 * NOTE: Other things, such as voice communication, would go in | |
250 * here too (since they send the same Content-Type). However, | |
251 * this is the best check for file transfer messages, so I'm | |
252 * calling msn_process_ft_invite_msg(). If anybody adds support | |
253 * for anything else that sends a text/x-msmsgsinvite, perhaps | |
254 * this should be changed. For now, it stays. | |
255 */ | |
256 msn_process_ft_msg(ms, content); | |
257 | |
4793 | 258 } else if (!g_ascii_strncasecmp(content, "Content-Type: text/plain", |
4542 | 259 strlen("Content-Type: text/plain"))) { |
260 | |
261 char *skiphead = strstr(msg, "\r\n\r\n"); | |
262 | |
263 if (!skiphead || !skiphead[4]) { | |
264 return; | |
265 } | |
266 | |
267 skiphead += 4; | |
268 strip_linefeed(skiphead); | |
269 | |
270 if (format) { | |
271 message = g_strdup_printf("%s%s", format, skiphead); | |
272 } else { | |
273 message = g_strdup(skiphead); | |
274 } | |
275 | |
276 if (ms->chat) | |
277 serv_got_chat_in(ms->gc, gaim_chat_get_id(GAIM_CHAT(ms->chat)), | |
278 ms->msguser, flags, message, time(NULL)); | |
279 else | |
280 serv_got_im(ms->gc, ms->msguser, message, flags, time(NULL), -1); | |
281 | |
282 g_free(message); | |
283 } | |
284 } | |
285 | |
286 static void | |
287 msn_switchboard_callback(gpointer data, gint source, GaimInputCondition cond) | |
288 { | |
289 struct msn_switchboard *ms = data; | |
290 char buf[MSN_BUF_LEN]; | |
291 int cont = 1; | |
292 int len; | |
293 | |
294 ms->fd = source; | |
295 len = read(ms->fd, buf, sizeof(buf)); | |
296 if (len <= 0) { | |
297 msn_kill_switch(ms); | |
298 return; | |
299 } | |
300 | |
301 ms->rxqueue = g_realloc(ms->rxqueue, len + ms->rxlen); | |
302 memcpy(ms->rxqueue + ms->rxlen, buf, len); | |
303 ms->rxlen += len; | |
304 | |
305 while (cont) { | |
306 if (!ms->rxlen) | |
307 return; | |
308 | |
309 if (ms->msg) { | |
310 char *msg; | |
311 if (ms->msglen > ms->rxlen) | |
312 return; | |
313 msg = ms->rxqueue; | |
314 ms->rxlen -= ms->msglen; | |
315 if (ms->rxlen) { | |
316 ms->rxqueue = g_memdup(msg + ms->msglen, ms->rxlen); | |
317 } else { | |
318 ms->rxqueue = NULL; | |
319 msg = g_realloc(msg, ms->msglen + 1); | |
320 } | |
321 msg[ms->msglen] = 0; | |
322 ms->msglen = 0; | |
323 ms->msg = FALSE; | |
324 | |
325 msn_process_switch_msg(ms, msg); | |
326 | |
327 g_free(ms->msguser); | |
328 g_free(msg); | |
329 } else { | |
330 char *end = ms->rxqueue; | |
331 int cmdlen; | |
332 char *cmd; | |
333 int i = 0; | |
334 | |
335 while (i + 1 < ms->rxlen) { | |
336 if (*end == '\r' && end[1] == '\n') | |
337 break; | |
338 end++; i++; | |
339 } | |
340 if (i + 1 == ms->rxlen) | |
341 return; | |
342 | |
343 cmdlen = end - ms->rxqueue + 2; | |
344 cmd = ms->rxqueue; | |
345 ms->rxlen -= cmdlen; | |
346 if (ms->rxlen) { | |
347 ms->rxqueue = g_memdup(cmd + cmdlen, ms->rxlen); | |
348 } else { | |
349 ms->rxqueue = NULL; | |
350 cmd = g_realloc(cmd, cmdlen + 1); | |
351 } | |
352 cmd[cmdlen] = 0; | |
353 | |
5221
abe4d103e300
[gaim-migrate @ 5591]
Christian Hammond <chipx86@chipx86.com>
parents:
4793
diff
changeset
|
354 gaim_debug(GAIM_DEBUG_MISC, "msn", "S: %s", cmd); |
4542 | 355 g_strchomp(cmd); |
356 cont = msn_process_switch(ms, cmd); | |
357 | |
358 g_free(cmd); | |
359 } | |
360 } | |
361 } | |
362 | |
363 void | |
364 msn_rng_connect(gpointer data, gint source, GaimInputCondition cond) | |
365 { | |
366 struct msn_switchboard *ms = data; | |
367 struct gaim_connection *gc = ms->gc; | |
368 struct msn_data *md; | |
369 char buf[MSN_BUF_LEN]; | |
370 | |
371 if (source == -1 || !g_slist_find(connections, gc)) { | |
372 close(source); | |
373 g_free(ms->sessid); | |
374 g_free(ms->auth); | |
375 g_free(ms); | |
376 return; | |
377 } | |
378 | |
379 md = gc->proto_data; | |
380 | |
381 if (ms->fd != source) | |
382 ms->fd = source; | |
383 | |
384 g_snprintf(buf, sizeof(buf), "ANS %u %s %s %s\r\n", ++ms->trId, gc->username, ms->auth, ms->sessid); | |
385 if (msn_write(ms->fd, buf, strlen(buf)) < 0) { | |
386 close(ms->fd); | |
387 g_free(ms->sessid); | |
388 g_free(ms->auth); | |
389 g_free(ms); | |
390 return; | |
391 } | |
392 | |
393 md->switches = g_slist_append(md->switches, ms); | |
394 ms->inpa = gaim_input_add(ms->fd, GAIM_INPUT_READ, | |
395 msn_switchboard_callback, ms); | |
396 } | |
397 | |
398 static void | |
399 msn_ss_xfr_connect(gpointer data, gint source, GaimInputCondition cond) | |
400 { | |
401 struct msn_switchboard *ms = data; | |
402 struct gaim_connection *gc = ms->gc; | |
403 char buf[MSN_BUF_LEN]; | |
404 | |
405 if (source == -1 || !g_slist_find(connections, gc)) { | |
406 close(source); | |
407 if (g_slist_find(connections, gc)) { | |
408 msn_kill_switch(ms); | |
409 do_error_dialog(_("Gaim was unable to send an MSN message"), | |
410 _("Gaim encountered an error communicating with the " | |
411 "MSN switchboard server. Please try again later."), | |
412 GAIM_ERROR); | |
413 } | |
414 | |
415 return; | |
416 } | |
417 | |
418 if (ms->fd != source) | |
419 ms->fd = source; | |
420 | |
421 g_snprintf(buf, sizeof(buf), "USR %u %s %s\r\n", | |
422 ++ms->trId, gc->username, ms->auth); | |
423 | |
424 if (msn_write(ms->fd, buf, strlen(buf)) < 0) { | |
425 g_free(ms->auth); | |
426 g_free(ms); | |
427 return; | |
428 } | |
429 | |
430 ms->inpa = gaim_input_add(ms->fd, GAIM_INPUT_READ, | |
431 msn_switchboard_callback, ms); | |
432 } | |
433 | |
434 struct msn_switchboard * | |
435 msn_find_switch(struct gaim_connection *gc, const char *username) | |
436 { | |
437 struct msn_data *md = (struct msn_data *)gc->proto_data; | |
438 GSList *m = md->switches; | |
439 | |
440 for (m = md->switches; m != NULL; m = m->next) { | |
441 struct msn_switchboard *ms = (struct msn_switchboard *)m->data; | |
442 | |
4793 | 443 if (ms->total <= 1 && !gaim_utf8_strcasecmp(ms->user, username)) |
4542 | 444 return ms; |
445 } | |
446 | |
447 return NULL; | |
448 } | |
449 | |
450 struct msn_switchboard * | |
451 msn_find_switch_by_id(struct gaim_connection *gc, int chat_id) | |
452 { | |
453 struct msn_data *md = (struct msn_data *)gc->proto_data; | |
454 GSList *m; | |
455 | |
456 for (m = md->switches; m != NULL; m = m->next) { | |
457 struct msn_switchboard *ms = (struct msn_switchboard *)m->data; | |
458 | |
459 if (ms->chat && gaim_chat_get_id(GAIM_CHAT(ms->chat)) == chat_id) | |
460 return ms; | |
461 } | |
462 | |
463 return NULL; | |
464 } | |
465 | |
466 struct msn_switchboard * | |
467 msn_find_writable_switch(struct gaim_connection *gc) | |
468 { | |
469 struct msn_data *md = (struct msn_data *)gc->proto_data; | |
470 GSList *m; | |
471 | |
472 for (m = md->switches; m != NULL; m = m->next) { | |
473 struct msn_switchboard *ms = (struct msn_switchboard *)m->data; | |
474 | |
475 if (ms->txqueue != NULL) | |
476 return ms; | |
477 } | |
478 | |
479 return NULL; | |
480 } | |
481 | |
482 void | |
483 msn_kill_switch(struct msn_switchboard *ms) | |
484 { | |
485 struct gaim_connection *gc = ms->gc; | |
486 struct msn_data *md = gc->proto_data; | |
487 | |
488 if (ms->inpa) | |
489 gaim_input_remove(ms->inpa); | |
490 | |
491 close(ms->fd); | |
492 g_free(ms->rxqueue); | |
493 | |
494 if (ms->msg) g_free(ms->msguser); | |
495 if (ms->user) g_free(ms->user); | |
496 if (ms->sessid) g_free(ms->sessid); | |
497 | |
498 g_free(ms->auth); | |
499 | |
500 while (ms->txqueue) { | |
501 g_free(ms->txqueue->data); | |
502 ms->txqueue = g_slist_remove(ms->txqueue, ms->txqueue->data); | |
503 } | |
504 | |
505 if (ms->chat) | |
506 serv_got_chat_left(gc, gaim_chat_get_id(GAIM_CHAT(ms->chat))); | |
507 | |
508 md->switches = g_slist_remove(md->switches, ms); | |
509 | |
510 g_free(ms); | |
511 } | |
512 | |
513 struct msn_switchboard * | |
514 msn_switchboard_connect(struct gaim_connection *gc, const char *host, int port) | |
515 { | |
516 struct msn_switchboard *ms; | |
517 | |
518 if (host == NULL || port == 0) | |
519 return NULL; | |
520 | |
521 ms = msn_find_writable_switch(gc); | |
522 | |
523 if (ms == NULL) | |
524 return NULL; | |
525 | |
4634 | 526 if (proxy_connect(gc->account, (char *)host, port, msn_ss_xfr_connect, |
527 ms) != 0) { | |
4542 | 528 msn_kill_switch(ms); |
529 | |
530 return NULL; | |
531 } | |
532 | |
533 return ms; | |
534 } |