source: protocols/msn/sb.c @ e6d6047

Last change on this file since e6d6047 was 8ba511d, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-03-17T15:39:16Z

Fixed a very stupid bug in the "Closing switchboard with unsent messages"
warning message. There's still another problem with switchboar management
somewhere.. :-(

  • Property mode set to 100644
File size: 15.3 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 void msn_sb_callback( gpointer data, gint source, GaimInputCondition 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 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
176void 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
197void 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 ) gaim_input_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
247void msn_sb_connected( gpointer data, gint source, GaimInputCondition 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;
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;
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 = gaim_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
287static void msn_sb_callback( gpointer data, gint source, GaimInputCondition cond )
288{
289        struct msn_switchboard *sb = data;
290       
291        if( msn_handler( sb->handler ) == -1 )
292        {
293                debug( "ERROR: Switchboard died" );
294                msn_sb_destroy( sb );
295        }
296}
297
298static int msn_sb_command( gpointer data, char **cmd, int num_parts )
299{
300        struct msn_switchboard *sb = data;
301        struct gaim_connection *gc = sb->gc;
302        char buf[1024];
303       
304        if( !num_parts )
305        {
306                /* Hrrm... Empty command...? Ignore? */
307                return( 1 );
308        }
309       
310        if( strcmp( cmd[0], "XFR" ) == 0 )
311        {
312                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!" );
313                signoff( gc );
314                return( 0 );
315        }
316        else if( strcmp( cmd[0], "USR" ) == 0 )
317        {
318                if( num_parts != 5 )
319                {
320                        msn_sb_destroy( sb );
321                        return( 0 );
322                }
323               
324                if( strcmp( cmd[2], "OK" ) != 0 )
325                {
326                        msn_sb_destroy( sb );
327                        return( 0 );
328                }
329               
330                if( sb->who )
331                {
332                        g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, sb->who );
333                        return( msn_sb_write( sb, buf, strlen( buf ) ) );
334                }
335                else
336                {
337                        debug( "Just created a switchboard, but I don't know what to do with it." );
338                }
339        }
340        else if( strcmp( cmd[0], "IRO" ) == 0 )
341        {
342                int num, tot;
343               
344                if( num_parts != 6 )
345                {
346                        msn_sb_destroy( sb );
347                        return( 0 );
348                }
349               
350                num = atoi( cmd[2] );
351                tot = atoi( cmd[3] );
352               
353                if( tot <= 0 )
354                {
355                        msn_sb_destroy( sb );
356                        return( 0 );
357                }
358                else if( tot > 1 )
359                {
360                        char buf[1024];
361                       
362                        if( num == 1 )
363                        {
364                                g_snprintf( buf, sizeof( buf ), "MSN groupchat session %d", sb->session );
365                                sb->chat = serv_got_joined_chat( gc, ++msn_chat_id, buf );
366                               
367                                g_free( sb->who );
368                                sb->who = NULL;
369                        }
370                       
371                        add_chat_buddy( sb->chat, cmd[4] );
372                       
373                        if( num == tot )
374                        {
375                                add_chat_buddy( sb->chat, gc->username );
376                        }
377                }
378        }
379        else if( strcmp( cmd[0], "ANS" ) == 0 )
380        {
381                if( num_parts != 3 )
382                {
383                        msn_sb_destroy( sb );
384                        return( 0 );
385                }
386               
387                if( strcmp( cmd[2], "OK" ) != 0 )
388                {
389                        debug( "Switchboard server sent a negative ANS reply" );
390                        msn_sb_destroy( sb );
391                        return( 0 );
392                }
393               
394                sb->ready = 1;
395        }
396        else if( strcmp( cmd[0], "CAL" ) == 0 )
397        {
398                if( num_parts != 4 || !isdigit( cmd[3][0] ) )
399                {
400                        msn_sb_destroy( sb );
401                        return( 0 );
402                }
403               
404                sb->session = atoi( cmd[3] );
405        }
406        else if( strcmp( cmd[0], "JOI" ) == 0 )
407        {
408                if( num_parts != 3 )
409                {
410                        msn_sb_destroy( sb );
411                        return( 0 );
412                }
413               
414                if( sb->who && g_strcasecmp( cmd[1], sb->who ) == 0 )
415                {
416                        /* The user we wanted to talk to is finally there, let's send the queued messages then. */
417                        struct msn_message *m;
418                        GSList *l;
419                        int st = 1;
420                       
421                        debug( "%s arrived in the switchboard session, now sending queued message(s)", cmd[1] );
422                       
423                        /* Without this, sendmessage() will put everything back on the queue... */
424                        sb->ready = 1;
425                       
426                        while( ( l = sb->msgq ) )
427                        {
428                                m = l->data;
429                                if( st )
430                                {
431                                        /* This hack is meant to convert a regular new chat into a groupchat */
432                                        if( strcmp( m->text, GROUPCHAT_SWITCHBOARD_MESSAGE ) == 0 )
433                                                msn_sb_to_chat( sb );
434                                        else
435                                                st = msn_sb_sendmessage( sb, m->text );
436                                }
437                                g_free( m->text );
438                                g_free( m->who );
439                                g_free( m );
440                               
441                                sb->msgq = g_slist_remove( sb->msgq, m );
442                        }
443                       
444                        return( st );
445                }
446                else if( sb->who )
447                {
448                        debug( "Converting chat with %s to a groupchat because %s joined the session.", sb->who, cmd[1] );
449                       
450                        /* This SB is a one-to-one chat right now, but someone else is joining. */
451                        msn_sb_to_chat( sb );
452                       
453                        add_chat_buddy( sb->chat, cmd[1] );
454                }
455                else if( sb->chat )
456                {
457                        add_chat_buddy( sb->chat, cmd[1] );
458                        sb->ready = 1;
459                }
460                else
461                {
462                        /* PANIC! */
463                }
464        }
465        else if( strcmp( cmd[0], "MSG" ) == 0 )
466        {
467                if( num_parts != 4 )
468                {
469                        msn_sb_destroy( sb );
470                        return( 0 );
471                }
472               
473                sb->handler->msglen = atoi( cmd[3] );
474               
475                if( sb->handler->msglen <= 0 )
476                {
477                        debug( "Received a corrupted message on the switchboard, the switchboard will be closed" );
478                        msn_sb_destroy( sb );
479                        return( 0 );
480                }
481        }
482        else if( strcmp( cmd[0], "BYE" ) == 0 )
483        {
484                if( num_parts < 2 )
485                {
486                        msn_sb_destroy( sb );
487                        return( 0 );
488                }
489               
490                /* if( cmd[2] && *cmd[2] == '1' ) -=> Chat is being cleaned up because of idleness */
491               
492                if( sb->who )
493                {
494                        /* This is a single-person chat, and the other person is leaving. */
495                        g_free( sb->who );
496                        sb->who = NULL;
497                        sb->ready = 0;
498                       
499                        debug( "Person %s left the one-to-one switchboard connection. Keeping it around as a spare...", cmd[1] );
500                       
501                        /* We could clean up the switchboard now, but keeping it around
502                           as a spare for a next conversation sounds more sane to me.
503                           The server will clean it up when it's idle for too long. */
504                }
505                else if( sb->chat )
506                {
507                        remove_chat_buddy( sb->chat, cmd[1], "" );
508                }
509                else
510                {
511                        /* PANIC! */
512                }
513        }
514        else if( isdigit( cmd[0][0] ) )
515        {
516                int num = atoi( cmd[0] );
517                const struct msn_status_code *err = msn_status_by_number( num );
518               
519                g_snprintf( buf, sizeof( buf ), "Error reported by switchboard server: %s", err->text );
520                do_error_dialog( gc, buf, "MSN" );
521               
522                if( err->flags & STATUS_SB_FATAL )
523                {
524                        msn_sb_destroy( sb );
525                        return( 0 );
526                }
527                if( err->flags & STATUS_FATAL )
528                {
529                        signoff( gc );
530                        return( 0 );
531                }
532                if( err->flags & STATUS_SB_IM_SPARE )
533                {
534                        if( sb->who )
535                        {
536                                struct msn_message *m;
537                                GSList *l;
538                               
539                                /* Apparently some invitation failed. We might want to use this
540                                   board later, so keep it as a spare. */
541                                g_free( sb->who );
542                                sb->who = NULL;
543                               
544                                /* Also clear the msgq, otherwise someone else might get them. */
545                                for( l = sb->msgq; l; l = l->next )
546                                {
547                                        m = l->data;
548                                        g_free( m->who );
549                                        g_free( m->text );
550                                        g_free( m );
551                                }
552                                g_slist_free( sb->msgq );
553                                sb->msgq = NULL;
554                        }
555                }
556        }
557        else
558        {
559                debug( "Received unknown command from switchboard server: %s", cmd[0] );
560        }
561       
562        return( 1 );
563}
564
565static int msn_sb_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts )
566{
567        struct msn_switchboard *sb = data;
568        struct gaim_connection *gc = sb->gc;
569        char *body;
570        int blen = 0;
571       
572        if( !num_parts )
573                return( 1 );
574       
575        if( ( body = strstr( msg, "\r\n\r\n" ) ) )
576        {
577                body += 4;
578                blen = msglen - ( body - msg );
579        }
580       
581        if( strcmp( cmd[0], "MSG" ) == 0 )
582        {
583                char *ct = msn_findheader( msg, "Content-Type:", msglen );
584               
585                if( !ct )
586                        return( 1 );
587               
588                if( g_strncasecmp( ct, "text/plain", 10 ) == 0 )
589                {
590                        g_free( ct );
591                       
592                        if( !body )
593                                return( 1 );
594                       
595                        if( sb->who )
596                        {
597                                serv_got_im( gc, cmd[1], body, 0, 0, blen );
598                        }
599                        else if( sb->chat )
600                        {
601                                serv_got_chat_in( gc, sb->chat->id, cmd[1], 0, body, 0 );
602                        }
603                        else
604                        {
605                                /* PANIC! */
606                        }
607                }
608                else if( g_strncasecmp( ct, "text/x-msmsgsinvite", 19 ) == 0 )
609                {
610                        char *itype = msn_findheader( body, "Application-GUID:", blen );
611                        char buf[1024];
612                       
613                        g_free( ct );
614                       
615                        *buf = 0;
616                       
617                        if( !itype )
618                                return( 1 );
619                       
620                        /* File transfer. */
621                        if( strcmp( itype, "{5D3E02AB-6190-11d3-BBBB-00C04F795683}" ) == 0 )
622                        {
623                                char *name = msn_findheader( body, "Application-File:", blen );
624                                char *size = msn_findheader( body, "Application-FileSize:", blen );
625                               
626                                if( name && size )
627                                {
628                                        g_snprintf( buf, sizeof( buf ), "<< \x02""BitlBee\x02"" - Filetransfer: `%s', %s bytes >>\n"
629                                                    "Filetransfers are not supported by BitlBee for now...", name, size );
630                                }
631                                else
632                                {
633                                        strcpy( buf, "<< \x02""BitlBee\x02"" - Corrupted MSN filetransfer invitation message >>" );
634                                }
635                               
636                                if( name ) g_free( name );
637                                if( size ) g_free( size );
638                        }
639                        else
640                        {
641                                char *iname = msn_findheader( body, "Application-Name:", blen );
642                               
643                                g_snprintf( buf, sizeof( buf ), "<< \x02""BitlBee\x02"" - Unknown MSN invitation - %s (%s) >>",
644                                                                itype, iname ? iname : "no name" );
645                               
646                                if( iname ) g_free( iname );
647                        }
648                       
649                        g_free( itype );
650                       
651                        if( !*buf )
652                                return( 1 );
653                       
654                        if( sb->who )
655                        {
656                                serv_got_im( gc, cmd[1], buf, 0, 0, strlen( buf ) );
657                        }
658                        else if( sb->chat )
659                        {
660                                serv_got_chat_in( gc, sb->chat->id, cmd[1], 0, buf, 0 );
661                        }
662                        else
663                        {
664                                /* PANIC! */
665                        }
666                }
667                else if( g_strncasecmp( ct, "text/x-msmsgscontrol", 20 ) == 0 )
668                {
669                        char *who = msn_findheader( msg, "TypingUser:", msglen );
670                       
671                        if( who )
672                        {
673                                serv_got_typing( gc, who, 5, 1 );
674                                g_free( who );
675                        }
676                       
677                        g_free( ct );
678                }
679                else
680                {
681                        g_free( ct );
682                }
683        }
684       
685        return( 1 );
686}
Note: See TracBrowser for help on using the repository browser.