source: protocols/msn/sb.c @ 43671b9

Last change on this file since 43671b9 was e35d1a1, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-04-22T20:44:27Z

Read-only support for Jabber conferences (non-anonymous rooms only).
Just don't use this, you're really not going to like it. :-)

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