1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2005 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* MSN module - Switchboard server callbacks and utilities */ |
---|
8 | |
---|
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 with |
---|
21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
23 | Suite 330, Boston, MA 02111-1307 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #include <ctype.h> |
---|
27 | #include "nogaim.h" |
---|
28 | #include "msn.h" |
---|
29 | #include "passport.h" |
---|
30 | #include "md5.h" |
---|
31 | |
---|
32 | static gboolean msn_sb_callback( gpointer data, gint source, b_input_condition cond ); |
---|
33 | static int msn_sb_command( gpointer data, char **cmd, int num_parts ); |
---|
34 | static int msn_sb_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts ); |
---|
35 | |
---|
36 | int msn_sb_write( struct msn_switchboard *sb, char *s, int len ) |
---|
37 | { |
---|
38 | int st; |
---|
39 | |
---|
40 | st = write( sb->fd, s, len ); |
---|
41 | if( st != len ) |
---|
42 | { |
---|
43 | msn_sb_destroy( sb ); |
---|
44 | return( 0 ); |
---|
45 | } |
---|
46 | |
---|
47 | return( 1 ); |
---|
48 | } |
---|
49 | |
---|
50 | struct msn_switchboard *msn_sb_create( struct gaim_connection *gc, char *host, int port, char *key, int session ) |
---|
51 | { |
---|
52 | struct msn_data *md = gc->proto_data; |
---|
53 | struct msn_switchboard *sb = g_new0( struct msn_switchboard, 1 ); |
---|
54 | |
---|
55 | sb->fd = proxy_connect( host, port, msn_sb_connected, sb ); |
---|
56 | if( sb->fd < 0 ) |
---|
57 | { |
---|
58 | g_free( sb ); |
---|
59 | return( NULL ); |
---|
60 | } |
---|
61 | |
---|
62 | sb->gc = gc; |
---|
63 | sb->key = g_strdup( key ); |
---|
64 | sb->session = session; |
---|
65 | |
---|
66 | msn_switchboards = g_slist_append( msn_switchboards, sb ); |
---|
67 | md->switchboards = g_slist_append( md->switchboards, sb ); |
---|
68 | |
---|
69 | return( sb ); |
---|
70 | } |
---|
71 | |
---|
72 | struct msn_switchboard *msn_sb_by_handle( struct gaim_connection *gc, char *handle ) |
---|
73 | { |
---|
74 | struct msn_data *md = gc->proto_data; |
---|
75 | struct msn_switchboard *sb; |
---|
76 | GSList *l; |
---|
77 | |
---|
78 | for( l = md->switchboards; l; l = l->next ) |
---|
79 | { |
---|
80 | sb = l->data; |
---|
81 | if( sb->who && strcmp( sb->who, handle ) == 0 ) |
---|
82 | return( sb ); |
---|
83 | } |
---|
84 | |
---|
85 | return( NULL ); |
---|
86 | } |
---|
87 | |
---|
88 | struct msn_switchboard *msn_sb_by_id( struct gaim_connection *gc, int id ) |
---|
89 | { |
---|
90 | struct msn_data *md = gc->proto_data; |
---|
91 | struct msn_switchboard *sb; |
---|
92 | GSList *l; |
---|
93 | |
---|
94 | for( l = md->switchboards; l; l = l->next ) |
---|
95 | { |
---|
96 | sb = l->data; |
---|
97 | if( sb->chat && sb->chat->id == id ) |
---|
98 | return( sb ); |
---|
99 | } |
---|
100 | |
---|
101 | return( NULL ); |
---|
102 | } |
---|
103 | |
---|
104 | struct msn_switchboard *msn_sb_spare( struct gaim_connection *gc ) |
---|
105 | { |
---|
106 | struct msn_data *md = gc->proto_data; |
---|
107 | struct msn_switchboard *sb; |
---|
108 | GSList *l; |
---|
109 | |
---|
110 | for( l = md->switchboards; l; l = l->next ) |
---|
111 | { |
---|
112 | sb = l->data; |
---|
113 | if( !sb->who && !sb->chat ) |
---|
114 | return( sb ); |
---|
115 | } |
---|
116 | |
---|
117 | return( NULL ); |
---|
118 | } |
---|
119 | |
---|
120 | int msn_sb_sendmessage( struct msn_switchboard *sb, char *text ) |
---|
121 | { |
---|
122 | if( sb->ready ) |
---|
123 | { |
---|
124 | char cmd[1024], *buf; |
---|
125 | int i, j; |
---|
126 | |
---|
127 | if( strcmp( text, TYPING_NOTIFICATION_MESSAGE ) != 0 ) |
---|
128 | { |
---|
129 | buf = g_new0( char, sizeof( MSN_MESSAGE_HEADERS ) + strlen( text ) * 2 ); |
---|
130 | i = strlen( MSN_MESSAGE_HEADERS ); |
---|
131 | |
---|
132 | strcpy( buf, MSN_MESSAGE_HEADERS ); |
---|
133 | for( j = 0; text[j]; j ++ ) |
---|
134 | { |
---|
135 | if( text[j] == '\n' ) |
---|
136 | buf[i++] = '\r'; |
---|
137 | |
---|
138 | buf[i++] = text[j]; |
---|
139 | } |
---|
140 | } |
---|
141 | else |
---|
142 | { |
---|
143 | i = strlen( MSN_TYPING_HEADERS ) + strlen( sb->gc->username ); |
---|
144 | buf = g_new0( char, strlen( MSN_TYPING_HEADERS ) + strlen( sb->gc->username ) ); |
---|
145 | i = g_snprintf( buf, i, MSN_TYPING_HEADERS, sb->gc->username ); |
---|
146 | } |
---|
147 | |
---|
148 | g_snprintf( cmd, sizeof( cmd ), "MSG %d N %d\r\n", ++sb->trId, i ); |
---|
149 | if( msn_sb_write( sb, cmd, strlen( cmd ) ) && msn_sb_write( sb, buf, i ) ) |
---|
150 | { |
---|
151 | g_free( buf ); |
---|
152 | return( 1 ); |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | g_free( buf ); |
---|
157 | return( 0 ); |
---|
158 | } |
---|
159 | } |
---|
160 | else if( sb->who ) |
---|
161 | { |
---|
162 | struct msn_message *m = g_new0( struct msn_message, 1 ); |
---|
163 | |
---|
164 | m->who = g_strdup( "" ); |
---|
165 | m->text = g_strdup( text ); |
---|
166 | sb->msgq = g_slist_append( sb->msgq, m ); |
---|
167 | |
---|
168 | return( 1 ); |
---|
169 | } |
---|
170 | else |
---|
171 | { |
---|
172 | return( 0 ); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | void msn_sb_to_chat( struct msn_switchboard *sb ) |
---|
177 | { |
---|
178 | struct gaim_connection *gc = sb->gc; |
---|
179 | char buf[1024]; |
---|
180 | |
---|
181 | /* Create the groupchat structure. */ |
---|
182 | g_snprintf( buf, sizeof( buf ), "MSN groupchat session %d", sb->session ); |
---|
183 | sb->chat = serv_got_joined_chat( gc, ++msn_chat_id, buf ); |
---|
184 | |
---|
185 | /* Populate the channel. */ |
---|
186 | if( sb->who ) add_chat_buddy( sb->chat, sb->who ); |
---|
187 | add_chat_buddy( sb->chat, gc->username ); |
---|
188 | |
---|
189 | /* And make sure the switchboard doesn't look like a regular chat anymore. */ |
---|
190 | if( sb->who ) |
---|
191 | { |
---|
192 | g_free( sb->who ); |
---|
193 | sb->who = NULL; |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | void msn_sb_destroy( struct msn_switchboard *sb ) |
---|
198 | { |
---|
199 | struct gaim_connection *gc = sb->gc; |
---|
200 | struct msn_data *md = gc->proto_data; |
---|
201 | |
---|
202 | debug( "Destroying switchboard: %s", sb->who ? sb->who : sb->key ? sb->key : "" ); |
---|
203 | |
---|
204 | if( sb->msgq ) |
---|
205 | { |
---|
206 | struct msn_message *m; |
---|
207 | GSList *l; |
---|
208 | |
---|
209 | for( l = sb->msgq; l; l = l->next ) |
---|
210 | { |
---|
211 | m = l->data; |
---|
212 | |
---|
213 | g_free( m->who ); |
---|
214 | g_free( m->text ); |
---|
215 | g_free( m ); |
---|
216 | } |
---|
217 | g_slist_free( sb->msgq ); |
---|
218 | |
---|
219 | serv_got_crap( gc, "Warning: Closing down MSN switchboard connection with " |
---|
220 | "unsent message to %s, you'll have to resend it.", |
---|
221 | sb->who ? sb->who : "(unknown)" ); |
---|
222 | } |
---|
223 | |
---|
224 | if( sb->key ) g_free( sb->key ); |
---|
225 | if( sb->who ) g_free( sb->who ); |
---|
226 | |
---|
227 | if( sb->chat ) |
---|
228 | { |
---|
229 | serv_got_chat_left( gc, sb->chat->id ); |
---|
230 | } |
---|
231 | |
---|
232 | if( sb->handler ) |
---|
233 | { |
---|
234 | if( sb->handler->rxq ) g_free( sb->handler->rxq ); |
---|
235 | if( sb->handler->cmd_text ) g_free( sb->handler->cmd_text ); |
---|
236 | g_free( sb->handler ); |
---|
237 | } |
---|
238 | |
---|
239 | if( sb->inp ) b_event_remove( sb->inp ); |
---|
240 | closesocket( sb->fd ); |
---|
241 | |
---|
242 | msn_switchboards = g_slist_remove( msn_switchboards, sb ); |
---|
243 | md->switchboards = g_slist_remove( md->switchboards, sb ); |
---|
244 | g_free( sb ); |
---|
245 | } |
---|
246 | |
---|
247 | gboolean msn_sb_connected( gpointer data, gint source, b_input_condition cond ) |
---|
248 | { |
---|
249 | struct msn_switchboard *sb = data; |
---|
250 | struct gaim_connection *gc; |
---|
251 | struct msn_data *md; |
---|
252 | char buf[1024]; |
---|
253 | |
---|
254 | /* Are we still alive? */ |
---|
255 | if( !g_slist_find( msn_switchboards, sb ) ) |
---|
256 | return FALSE; |
---|
257 | |
---|
258 | gc = sb->gc; |
---|
259 | md = gc->proto_data; |
---|
260 | |
---|
261 | if( source != sb->fd ) |
---|
262 | { |
---|
263 | debug( "ERROR %d while connecting to switchboard server", 1 ); |
---|
264 | msn_sb_destroy( sb ); |
---|
265 | return FALSE; |
---|
266 | } |
---|
267 | |
---|
268 | /* Prepare the callback */ |
---|
269 | sb->handler = g_new0( struct msn_handler_data, 1 ); |
---|
270 | sb->handler->fd = sb->fd; |
---|
271 | sb->handler->rxq = g_new0( char, 1 ); |
---|
272 | sb->handler->data = sb; |
---|
273 | sb->handler->exec_command = msn_sb_command; |
---|
274 | sb->handler->exec_message = msn_sb_message; |
---|
275 | |
---|
276 | if( sb->session == MSN_SB_NEW ) |
---|
277 | g_snprintf( buf, sizeof( buf ), "USR %d %s %s\r\n", ++sb->trId, gc->username, sb->key ); |
---|
278 | else |
---|
279 | g_snprintf( buf, sizeof( buf ), "ANS %d %s %s %d\r\n", ++sb->trId, gc->username, sb->key, sb->session ); |
---|
280 | |
---|
281 | if( msn_sb_write( sb, buf, strlen( buf ) ) ) |
---|
282 | sb->inp = b_input_add( sb->fd, GAIM_INPUT_READ, msn_sb_callback, sb ); |
---|
283 | else |
---|
284 | debug( "ERROR %d while connecting to switchboard server", 2 ); |
---|
285 | |
---|
286 | return FALSE; |
---|
287 | } |
---|
288 | |
---|
289 | static gboolean msn_sb_callback( gpointer data, gint source, b_input_condition cond ) |
---|
290 | { |
---|
291 | struct msn_switchboard *sb = data; |
---|
292 | |
---|
293 | if( msn_handler( sb->handler ) == -1 ) |
---|
294 | { |
---|
295 | debug( "ERROR: Switchboard died" ); |
---|
296 | msn_sb_destroy( sb ); |
---|
297 | |
---|
298 | return FALSE; |
---|
299 | } |
---|
300 | else |
---|
301 | return TRUE; |
---|
302 | } |
---|
303 | |
---|
304 | static int msn_sb_command( gpointer data, char **cmd, int num_parts ) |
---|
305 | { |
---|
306 | struct msn_switchboard *sb = data; |
---|
307 | struct gaim_connection *gc = sb->gc; |
---|
308 | char buf[1024]; |
---|
309 | |
---|
310 | if( !num_parts ) |
---|
311 | { |
---|
312 | /* Hrrm... Empty command...? Ignore? */ |
---|
313 | return( 1 ); |
---|
314 | } |
---|
315 | |
---|
316 | if( strcmp( cmd[0], "XFR" ) == 0 ) |
---|
317 | { |
---|
318 | hide_login_progress_error( gc, "Received an XFR from a switchboard server, unable to comply! This is likely to be a bug, please report it!" ); |
---|
319 | signoff( gc ); |
---|
320 | return( 0 ); |
---|
321 | } |
---|
322 | else if( strcmp( cmd[0], "USR" ) == 0 ) |
---|
323 | { |
---|
324 | if( num_parts != 5 ) |
---|
325 | { |
---|
326 | msn_sb_destroy( sb ); |
---|
327 | return( 0 ); |
---|
328 | } |
---|
329 | |
---|
330 | if( strcmp( cmd[2], "OK" ) != 0 ) |
---|
331 | { |
---|
332 | msn_sb_destroy( sb ); |
---|
333 | return( 0 ); |
---|
334 | } |
---|
335 | |
---|
336 | if( sb->who ) |
---|
337 | { |
---|
338 | g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, sb->who ); |
---|
339 | return( msn_sb_write( sb, buf, strlen( buf ) ) ); |
---|
340 | } |
---|
341 | else |
---|
342 | { |
---|
343 | debug( "Just created a switchboard, but I don't know what to do with it." ); |
---|
344 | } |
---|
345 | } |
---|
346 | else if( strcmp( cmd[0], "IRO" ) == 0 ) |
---|
347 | { |
---|
348 | int num, tot; |
---|
349 | |
---|
350 | if( num_parts != 6 ) |
---|
351 | { |
---|
352 | msn_sb_destroy( sb ); |
---|
353 | return( 0 ); |
---|
354 | } |
---|
355 | |
---|
356 | num = atoi( cmd[2] ); |
---|
357 | tot = atoi( cmd[3] ); |
---|
358 | |
---|
359 | if( tot <= 0 ) |
---|
360 | { |
---|
361 | msn_sb_destroy( sb ); |
---|
362 | return( 0 ); |
---|
363 | } |
---|
364 | else if( tot > 1 ) |
---|
365 | { |
---|
366 | char buf[1024]; |
---|
367 | |
---|
368 | if( num == 1 ) |
---|
369 | { |
---|
370 | g_snprintf( buf, sizeof( buf ), "MSN groupchat session %d", sb->session ); |
---|
371 | sb->chat = serv_got_joined_chat( gc, ++msn_chat_id, buf ); |
---|
372 | |
---|
373 | g_free( sb->who ); |
---|
374 | sb->who = NULL; |
---|
375 | } |
---|
376 | |
---|
377 | add_chat_buddy( sb->chat, cmd[4] ); |
---|
378 | |
---|
379 | if( num == tot ) |
---|
380 | { |
---|
381 | add_chat_buddy( sb->chat, gc->username ); |
---|
382 | } |
---|
383 | } |
---|
384 | } |
---|
385 | else if( strcmp( cmd[0], "ANS" ) == 0 ) |
---|
386 | { |
---|
387 | if( num_parts != 3 ) |
---|
388 | { |
---|
389 | msn_sb_destroy( sb ); |
---|
390 | return( 0 ); |
---|
391 | } |
---|
392 | |
---|
393 | if( strcmp( cmd[2], "OK" ) != 0 ) |
---|
394 | { |
---|
395 | debug( "Switchboard server sent a negative ANS reply" ); |
---|
396 | msn_sb_destroy( sb ); |
---|
397 | return( 0 ); |
---|
398 | } |
---|
399 | |
---|
400 | sb->ready = 1; |
---|
401 | } |
---|
402 | else if( strcmp( cmd[0], "CAL" ) == 0 ) |
---|
403 | { |
---|
404 | if( num_parts != 4 || !isdigit( cmd[3][0] ) ) |
---|
405 | { |
---|
406 | msn_sb_destroy( sb ); |
---|
407 | return( 0 ); |
---|
408 | } |
---|
409 | |
---|
410 | sb->session = atoi( cmd[3] ); |
---|
411 | } |
---|
412 | else if( strcmp( cmd[0], "JOI" ) == 0 ) |
---|
413 | { |
---|
414 | if( num_parts != 3 ) |
---|
415 | { |
---|
416 | msn_sb_destroy( sb ); |
---|
417 | return( 0 ); |
---|
418 | } |
---|
419 | |
---|
420 | if( sb->who && g_strcasecmp( cmd[1], sb->who ) == 0 ) |
---|
421 | { |
---|
422 | /* The user we wanted to talk to is finally there, let's send the queued messages then. */ |
---|
423 | struct msn_message *m; |
---|
424 | GSList *l; |
---|
425 | int st = 1; |
---|
426 | |
---|
427 | debug( "%s arrived in the switchboard session, now sending queued message(s)", cmd[1] ); |
---|
428 | |
---|
429 | /* Without this, sendmessage() will put everything back on the queue... */ |
---|
430 | sb->ready = 1; |
---|
431 | |
---|
432 | while( ( l = sb->msgq ) ) |
---|
433 | { |
---|
434 | m = l->data; |
---|
435 | if( st ) |
---|
436 | { |
---|
437 | /* This hack is meant to convert a regular new chat into a groupchat */ |
---|
438 | if( strcmp( m->text, GROUPCHAT_SWITCHBOARD_MESSAGE ) == 0 ) |
---|
439 | msn_sb_to_chat( sb ); |
---|
440 | else |
---|
441 | st = msn_sb_sendmessage( sb, m->text ); |
---|
442 | } |
---|
443 | g_free( m->text ); |
---|
444 | g_free( m->who ); |
---|
445 | g_free( m ); |
---|
446 | |
---|
447 | sb->msgq = g_slist_remove( sb->msgq, m ); |
---|
448 | } |
---|
449 | |
---|
450 | return( st ); |
---|
451 | } |
---|
452 | else if( sb->who ) |
---|
453 | { |
---|
454 | debug( "Converting chat with %s to a groupchat because %s joined the session.", sb->who, cmd[1] ); |
---|
455 | |
---|
456 | /* This SB is a one-to-one chat right now, but someone else is joining. */ |
---|
457 | msn_sb_to_chat( sb ); |
---|
458 | |
---|
459 | add_chat_buddy( sb->chat, cmd[1] ); |
---|
460 | } |
---|
461 | else if( sb->chat ) |
---|
462 | { |
---|
463 | add_chat_buddy( sb->chat, cmd[1] ); |
---|
464 | sb->ready = 1; |
---|
465 | } |
---|
466 | else |
---|
467 | { |
---|
468 | /* PANIC! */ |
---|
469 | } |
---|
470 | } |
---|
471 | else if( strcmp( cmd[0], "MSG" ) == 0 ) |
---|
472 | { |
---|
473 | if( num_parts != 4 ) |
---|
474 | { |
---|
475 | msn_sb_destroy( sb ); |
---|
476 | return( 0 ); |
---|
477 | } |
---|
478 | |
---|
479 | sb->handler->msglen = atoi( cmd[3] ); |
---|
480 | |
---|
481 | if( sb->handler->msglen <= 0 ) |
---|
482 | { |
---|
483 | debug( "Received a corrupted message on the switchboard, the switchboard will be closed" ); |
---|
484 | msn_sb_destroy( sb ); |
---|
485 | return( 0 ); |
---|
486 | } |
---|
487 | } |
---|
488 | else if( strcmp( cmd[0], "BYE" ) == 0 ) |
---|
489 | { |
---|
490 | if( num_parts < 2 ) |
---|
491 | { |
---|
492 | msn_sb_destroy( sb ); |
---|
493 | return( 0 ); |
---|
494 | } |
---|
495 | |
---|
496 | /* if( cmd[2] && *cmd[2] == '1' ) -=> Chat is being cleaned up because of idleness */ |
---|
497 | |
---|
498 | if( sb->who ) |
---|
499 | { |
---|
500 | /* This is a single-person chat, and the other person is leaving. */ |
---|
501 | g_free( sb->who ); |
---|
502 | sb->who = NULL; |
---|
503 | sb->ready = 0; |
---|
504 | |
---|
505 | debug( "Person %s left the one-to-one switchboard connection. Keeping it around as a spare...", cmd[1] ); |
---|
506 | |
---|
507 | /* We could clean up the switchboard now, but keeping it around |
---|
508 | as a spare for a next conversation sounds more sane to me. |
---|
509 | The server will clean it up when it's idle for too long. */ |
---|
510 | } |
---|
511 | else if( sb->chat ) |
---|
512 | { |
---|
513 | remove_chat_buddy( sb->chat, cmd[1], "" ); |
---|
514 | } |
---|
515 | else |
---|
516 | { |
---|
517 | /* PANIC! */ |
---|
518 | } |
---|
519 | } |
---|
520 | else if( isdigit( cmd[0][0] ) ) |
---|
521 | { |
---|
522 | int num = atoi( cmd[0] ); |
---|
523 | const struct msn_status_code *err = msn_status_by_number( num ); |
---|
524 | |
---|
525 | g_snprintf( buf, sizeof( buf ), "Error reported by switchboard server: %s", err->text ); |
---|
526 | do_error_dialog( gc, buf, "MSN" ); |
---|
527 | |
---|
528 | if( err->flags & STATUS_SB_FATAL ) |
---|
529 | { |
---|
530 | msn_sb_destroy( sb ); |
---|
531 | return( 0 ); |
---|
532 | } |
---|
533 | if( err->flags & STATUS_FATAL ) |
---|
534 | { |
---|
535 | signoff( gc ); |
---|
536 | return( 0 ); |
---|
537 | } |
---|
538 | if( err->flags & STATUS_SB_IM_SPARE ) |
---|
539 | { |
---|
540 | if( sb->who ) |
---|
541 | { |
---|
542 | struct msn_message *m; |
---|
543 | GSList *l; |
---|
544 | |
---|
545 | /* Apparently some invitation failed. We might want to use this |
---|
546 | board later, so keep it as a spare. */ |
---|
547 | g_free( sb->who ); |
---|
548 | sb->who = NULL; |
---|
549 | |
---|
550 | /* Also clear the msgq, otherwise someone else might get them. */ |
---|
551 | for( l = sb->msgq; l; l = l->next ) |
---|
552 | { |
---|
553 | m = l->data; |
---|
554 | g_free( m->who ); |
---|
555 | g_free( m->text ); |
---|
556 | g_free( m ); |
---|
557 | } |
---|
558 | g_slist_free( sb->msgq ); |
---|
559 | sb->msgq = NULL; |
---|
560 | } |
---|
561 | } |
---|
562 | } |
---|
563 | else |
---|
564 | { |
---|
565 | debug( "Received unknown command from switchboard server: %s", cmd[0] ); |
---|
566 | } |
---|
567 | |
---|
568 | return( 1 ); |
---|
569 | } |
---|
570 | |
---|
571 | static int msn_sb_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts ) |
---|
572 | { |
---|
573 | struct msn_switchboard *sb = data; |
---|
574 | struct gaim_connection *gc = sb->gc; |
---|
575 | char *body; |
---|
576 | int blen = 0; |
---|
577 | |
---|
578 | if( !num_parts ) |
---|
579 | return( 1 ); |
---|
580 | |
---|
581 | if( ( body = strstr( msg, "\r\n\r\n" ) ) ) |
---|
582 | { |
---|
583 | body += 4; |
---|
584 | blen = msglen - ( body - msg ); |
---|
585 | } |
---|
586 | |
---|
587 | if( strcmp( cmd[0], "MSG" ) == 0 ) |
---|
588 | { |
---|
589 | char *ct = msn_findheader( msg, "Content-Type:", msglen ); |
---|
590 | |
---|
591 | if( !ct ) |
---|
592 | return( 1 ); |
---|
593 | |
---|
594 | if( g_strncasecmp( ct, "text/plain", 10 ) == 0 ) |
---|
595 | { |
---|
596 | g_free( ct ); |
---|
597 | |
---|
598 | if( !body ) |
---|
599 | return( 1 ); |
---|
600 | |
---|
601 | if( sb->who ) |
---|
602 | { |
---|
603 | serv_got_im( gc, cmd[1], body, 0, 0, blen ); |
---|
604 | } |
---|
605 | else if( sb->chat ) |
---|
606 | { |
---|
607 | serv_got_chat_in( gc, sb->chat->id, cmd[1], 0, body, 0 ); |
---|
608 | } |
---|
609 | else |
---|
610 | { |
---|
611 | /* PANIC! */ |
---|
612 | } |
---|
613 | } |
---|
614 | else if( g_strncasecmp( ct, "text/x-msmsgsinvite", 19 ) == 0 ) |
---|
615 | { |
---|
616 | char *itype = msn_findheader( body, "Application-GUID:", blen ); |
---|
617 | char buf[1024]; |
---|
618 | |
---|
619 | g_free( ct ); |
---|
620 | |
---|
621 | *buf = 0; |
---|
622 | |
---|
623 | if( !itype ) |
---|
624 | return( 1 ); |
---|
625 | |
---|
626 | /* File transfer. */ |
---|
627 | if( strcmp( itype, "{5D3E02AB-6190-11d3-BBBB-00C04F795683}" ) == 0 ) |
---|
628 | { |
---|
629 | char *name = msn_findheader( body, "Application-File:", blen ); |
---|
630 | char *size = msn_findheader( body, "Application-FileSize:", blen ); |
---|
631 | |
---|
632 | if( name && size ) |
---|
633 | { |
---|
634 | g_snprintf( buf, sizeof( buf ), "<< \x02""BitlBee\x02"" - Filetransfer: `%s', %s bytes >>\n" |
---|
635 | "Filetransfers are not supported by BitlBee for now...", name, size ); |
---|
636 | } |
---|
637 | else |
---|
638 | { |
---|
639 | strcpy( buf, "<< \x02""BitlBee\x02"" - Corrupted MSN filetransfer invitation message >>" ); |
---|
640 | } |
---|
641 | |
---|
642 | if( name ) g_free( name ); |
---|
643 | if( size ) g_free( size ); |
---|
644 | } |
---|
645 | else |
---|
646 | { |
---|
647 | char *iname = msn_findheader( body, "Application-Name:", blen ); |
---|
648 | |
---|
649 | g_snprintf( buf, sizeof( buf ), "<< \x02""BitlBee\x02"" - Unknown MSN invitation - %s (%s) >>", |
---|
650 | itype, iname ? iname : "no name" ); |
---|
651 | |
---|
652 | if( iname ) g_free( iname ); |
---|
653 | } |
---|
654 | |
---|
655 | g_free( itype ); |
---|
656 | |
---|
657 | if( !*buf ) |
---|
658 | return( 1 ); |
---|
659 | |
---|
660 | if( sb->who ) |
---|
661 | { |
---|
662 | serv_got_im( gc, cmd[1], buf, 0, 0, strlen( buf ) ); |
---|
663 | } |
---|
664 | else if( sb->chat ) |
---|
665 | { |
---|
666 | serv_got_chat_in( gc, sb->chat->id, cmd[1], 0, buf, 0 ); |
---|
667 | } |
---|
668 | else |
---|
669 | { |
---|
670 | /* PANIC! */ |
---|
671 | } |
---|
672 | } |
---|
673 | else if( g_strncasecmp( ct, "text/x-msmsgscontrol", 20 ) == 0 ) |
---|
674 | { |
---|
675 | char *who = msn_findheader( msg, "TypingUser:", msglen ); |
---|
676 | |
---|
677 | if( who ) |
---|
678 | { |
---|
679 | serv_got_typing( gc, who, 5, 1 ); |
---|
680 | g_free( who ); |
---|
681 | } |
---|
682 | |
---|
683 | g_free( ct ); |
---|
684 | } |
---|
685 | else |
---|
686 | { |
---|
687 | g_free( ct ); |
---|
688 | } |
---|
689 | } |
---|
690 | |
---|
691 | return( 1 ); |
---|
692 | } |
---|