source: protocols/msn/sb.c @ 348c11b

Last change on this file since 348c11b was bd28e6a, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-10-24T10:40:28Z

MSN message packets are now sent at once instead of separately. Probably
the MSN servers don't care, but it looks a bit prettier in wireshark. ;-)

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