source: protocols/msn/sb.c @ e7f46c5

Last change on this file since e7f46c5 was e7f46c5, checked in by Matt Sparks <root@…>, at 2005-12-04T12:58:41Z

Implements solution to typing notifications in ticket #45

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