source: protocols/msn/sb.c @ 4ff0966

Last change on this file since 4ff0966 was 4ff0966, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-05-28T23:13:47Z

Merging from main/jelmer.

  • Property mode set to 100644
File size: 15.4 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 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 ) 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
247gboolean 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
289static 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
304static 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                else if( err->flags & STATUS_FATAL )
534                {
535                        signoff( gc );
536                        return 0;
537                }
538                else 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                        /* Do NOT return 0 here, we want to keep this sb. */
563                }
564        }
565        else
566        {
567                debug( "Received unknown command from switchboard server: %s", cmd[0] );
568        }
569       
570        return( 1 );
571}
572
573static int msn_sb_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts )
574{
575        struct msn_switchboard *sb = data;
576        struct gaim_connection *gc = sb->gc;
577        char *body;
578        int blen = 0;
579       
580        if( !num_parts )
581                return( 1 );
582       
583        if( ( body = strstr( msg, "\r\n\r\n" ) ) )
584        {
585                body += 4;
586                blen = msglen - ( body - msg );
587        }
588       
589        if( strcmp( cmd[0], "MSG" ) == 0 )
590        {
591                char *ct = msn_findheader( msg, "Content-Type:", msglen );
592               
593                if( !ct )
594                        return( 1 );
595               
596                if( g_strncasecmp( ct, "text/plain", 10 ) == 0 )
597                {
598                        g_free( ct );
599                       
600                        if( !body )
601                                return( 1 );
602                       
603                        if( sb->who )
604                        {
605                                serv_got_im( gc, cmd[1], body, 0, 0, blen );
606                        }
607                        else if( sb->chat )
608                        {
609                                serv_got_chat_in( gc, sb->chat->id, cmd[1], 0, body, 0 );
610                        }
611                        else
612                        {
613                                /* PANIC! */
614                        }
615                }
616                else if( g_strncasecmp( ct, "text/x-msmsgsinvite", 19 ) == 0 )
617                {
618                        char *itype = msn_findheader( body, "Application-GUID:", blen );
619                        char buf[1024];
620                       
621                        g_free( ct );
622                       
623                        *buf = 0;
624                       
625                        if( !itype )
626                                return( 1 );
627                       
628                        /* File transfer. */
629                        if( strcmp( itype, "{5D3E02AB-6190-11d3-BBBB-00C04F795683}" ) == 0 )
630                        {
631                                char *name = msn_findheader( body, "Application-File:", blen );
632                                char *size = msn_findheader( body, "Application-FileSize:", blen );
633                               
634                                if( name && size )
635                                {
636                                        g_snprintf( buf, sizeof( buf ), "<< \x02""BitlBee\x02"" - Filetransfer: `%s', %s bytes >>\n"
637                                                    "Filetransfers are not supported by BitlBee for now...", name, size );
638                                }
639                                else
640                                {
641                                        strcpy( buf, "<< \x02""BitlBee\x02"" - Corrupted MSN filetransfer invitation message >>" );
642                                }
643                               
644                                if( name ) g_free( name );
645                                if( size ) g_free( size );
646                        }
647                        else
648                        {
649                                char *iname = msn_findheader( body, "Application-Name:", blen );
650                               
651                                g_snprintf( buf, sizeof( buf ), "<< \x02""BitlBee\x02"" - Unknown MSN invitation - %s (%s) >>",
652                                                                itype, iname ? iname : "no name" );
653                               
654                                if( iname ) g_free( iname );
655                        }
656                       
657                        g_free( itype );
658                       
659                        if( !*buf )
660                                return( 1 );
661                       
662                        if( sb->who )
663                        {
664                                serv_got_im( gc, cmd[1], buf, 0, 0, strlen( buf ) );
665                        }
666                        else if( sb->chat )
667                        {
668                                serv_got_chat_in( gc, sb->chat->id, cmd[1], 0, buf, 0 );
669                        }
670                        else
671                        {
672                                /* PANIC! */
673                        }
674                }
675                else if( g_strncasecmp( ct, "text/x-msmsgscontrol", 20 ) == 0 )
676                {
677                        char *who = msn_findheader( msg, "TypingUser:", msglen );
678                       
679                        if( who )
680                        {
681                                serv_got_typing( gc, who, 5, 1 );
682                                g_free( who );
683                        }
684                       
685                        g_free( ct );
686                }
687                else
688                {
689                        g_free( ct );
690                }
691        }
692       
693        return( 1 );
694}
Note: See TracBrowser for help on using the repository browser.