source: protocols/msn/ns.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: 16.1 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2004 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* MSN module - Notification server callbacks                           */
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_ns_callback( gpointer data, gint source, b_input_condition cond );
33static int msn_ns_command( gpointer data, char **cmd, int num_parts );
34static int msn_ns_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts );
35
36static void msn_auth_got_passport_id( struct passport_reply *rep );
37
38gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond )
39{
40        struct gaim_connection *gc = data;
41        struct msn_data *md;
42        char s[1024];
43       
44        if( !g_slist_find( msn_connections, gc ) )
45                return FALSE;
46       
47        if( source == -1 )
48        {
49                hide_login_progress( gc, "Could not connect to server" );
50                signoff( gc );
51                return FALSE;
52        }
53       
54        md = gc->proto_data;
55       
56        if( !md->handler )
57        {
58                md->handler = g_new0( struct msn_handler_data, 1 );
59                md->handler->data = gc;
60                md->handler->exec_command = msn_ns_command;
61                md->handler->exec_message = msn_ns_message;
62        }
63        else
64        {
65                if( md->handler->rxq )
66                        g_free( md->handler->rxq );
67               
68                md->handler->rxlen = 0;
69        }
70       
71        md->handler->fd = md->fd;
72        md->handler->rxq = g_new0( char, 1 );
73       
74        g_snprintf( s, sizeof( s ), "VER %d MSNP8 CVR0\r\n", ++md->trId );
75        if( msn_write( gc, s, strlen( s ) ) )
76        {
77                gc->inpa = b_input_add( md->fd, GAIM_INPUT_READ, msn_ns_callback, gc );
78                set_login_progress( gc, 1, "Connected to server, waiting for reply" );
79        }
80       
81        return FALSE;
82}
83
84static gboolean msn_ns_callback( gpointer data, gint source, b_input_condition cond )
85{
86        struct gaim_connection *gc = data;
87        struct msn_data *md = gc->proto_data;
88       
89        if( msn_handler( md->handler ) == -1 ) /* Don't do this on ret == 0, it's already done then. */
90        {
91                hide_login_progress( gc, "Error while reading from server" );
92                signoff( gc );
93               
94                return FALSE;
95        }
96        else
97                return TRUE;
98}
99
100static int msn_ns_command( gpointer data, char **cmd, int num_parts )
101{
102        struct gaim_connection *gc = data;
103        struct msn_data *md = gc->proto_data;
104        char buf[1024];
105       
106        if( num_parts == 0 )
107        {
108                /* Hrrm... Empty command...? Ignore? */
109                return( 1 );
110        }
111       
112        if( strcmp( cmd[0], "VER" ) == 0 )
113        {
114                if( cmd[2] && strncmp( cmd[2], "MSNP8", 5 ) != 0 )
115                {
116                        hide_login_progress( gc, "Unsupported protocol" );
117                        signoff( gc );
118                        return( 0 );
119                }
120               
121                g_snprintf( buf, sizeof( buf ), "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s\r\n",
122                                                ++md->trId, gc->username );
123                return( msn_write( gc, buf, strlen( buf ) ) );
124        }
125        else if( strcmp( cmd[0], "CVR" ) == 0 )
126        {
127                /* We don't give a damn about the information we just received */
128                g_snprintf( buf, sizeof( buf ), "USR %d TWN I %s\r\n", ++md->trId, gc->username );
129                return( msn_write( gc, buf, strlen( buf ) ) );
130        }
131        else if( strcmp( cmd[0], "XFR" ) == 0 )
132        {
133                char *server;
134                int port;
135               
136                if( num_parts == 6 && strcmp( cmd[2], "NS" ) == 0 )
137                {
138                        b_event_remove( gc->inpa );
139                        gc->inpa = 0;
140                        closesocket( md->fd );
141                       
142                        server = strchr( cmd[3], ':' );
143                        if( !server )
144                        {
145                                hide_login_progress_error( gc, "Syntax error" );
146                                signoff( gc );
147                                return( 0 );
148                        }
149                        *server = 0;
150                        port = atoi( server + 1 );
151                        server = cmd[3];
152                       
153                        set_login_progress( gc, 1, "Transferring to other server" );
154                       
155                        md->fd = proxy_connect( server, port, msn_ns_connected, gc );
156                }
157                else if( num_parts == 6 && strcmp( cmd[2], "SB" ) == 0 )
158                {
159                        struct msn_switchboard *sb;
160                       
161                        server = strchr( cmd[3], ':' );
162                        if( !server )
163                        {
164                                hide_login_progress_error( gc, "Syntax error" );
165                                signoff( gc );
166                                return( 0 );
167                        }
168                        *server = 0;
169                        port = atoi( server + 1 );
170                        server = cmd[3];
171                       
172                        if( strcmp( cmd[4], "CKI" ) != 0 )
173                        {
174                                hide_login_progress_error( gc, "Unknown authentication method for switchboard" );
175                                signoff( gc );
176                                return( 0 );
177                        }
178                       
179                        debug( "Connecting to a new switchboard with key %s", cmd[5] );
180                        sb = msn_sb_create( gc, server, port, cmd[5], MSN_SB_NEW );
181                       
182                        if( md->msgq )
183                        {
184                                struct msn_message *m = md->msgq->data;
185                                GSList *l;
186                               
187                                sb->who = g_strdup( m->who );
188                               
189                                /* Move all the messages to the first user in the message
190                                   queue to the switchboard message queue. */
191                                l = md->msgq;
192                                while( l )
193                                {
194                                        m = l->data;
195                                        l = l->next;
196                                        if( strcmp( m->who, sb->who ) == 0 )
197                                        {
198                                                sb->msgq = g_slist_append( sb->msgq, m );
199                                                md->msgq = g_slist_remove( md->msgq, m );
200                                        }
201                                }
202                        }
203                }
204                else
205                {
206                        hide_login_progress_error( gc, "Syntax error" );
207                        signoff( gc );
208                        return( 0 );
209                }
210        }
211        else if( strcmp( cmd[0], "USR" ) == 0 )
212        {
213                if( num_parts == 5 && strcmp( cmd[2], "TWN" ) == 0 && strcmp( cmd[3], "S" ) == 0 )
214                {
215                        /* Time for some Passport black magic... */
216                        if( !passport_get_id( msn_auth_got_passport_id, gc, gc->username, gc->password, cmd[4] ) )
217                        {
218                                hide_login_progress_error( gc, "Error while contacting Passport server" );
219                                signoff( gc );
220                                return( 0 );
221                        }
222                }
223                else if( num_parts == 7 && strcmp( cmd[2], "OK" ) == 0 )
224                {
225                        http_decode( cmd[4] );
226                       
227                        strncpy( gc->displayname, cmd[4], sizeof( gc->displayname ) );
228                        gc->displayname[sizeof(gc->displayname)-1] = 0;
229                       
230                        set_login_progress( gc, 1, "Authenticated, getting buddy list" );
231                       
232                        g_snprintf( buf, sizeof( buf ), "SYN %d 0\r\n", ++md->trId );
233                        return( msn_write( gc, buf, strlen( buf ) ) );
234                }
235                else
236                {
237                        hide_login_progress( gc, "Unknown authentication type" );
238                        signoff( gc );
239                        return( 0 );
240                }
241        }
242        else if( strcmp( cmd[0], "MSG" ) == 0 )
243        {
244                if( num_parts != 4 )
245                {
246                        hide_login_progress_error( gc, "Syntax error" );
247                        signoff( gc );
248                        return( 0 );
249                }
250               
251                md->handler->msglen = atoi( cmd[3] );
252               
253                if( md->handler->msglen <= 0 )
254                {
255                        hide_login_progress_error( gc, "Syntax error" );
256                        signoff( gc );
257                        return( 0 );
258                }
259        }
260        else if( strcmp( cmd[0], "SYN" ) == 0 )
261        {
262                if( num_parts == 5 )
263                {
264                        md->buddycount = atoi( cmd[3] );
265                        md->groupcount = atoi( cmd[4] );
266                        if( md->groupcount > 0 )
267                                md->grouplist = g_new0( char *, md->groupcount );
268                       
269                        if( !*cmd[3] || md->buddycount == 0 )
270                                msn_logged_in( gc );
271                }
272                else
273                {
274                        /* Hrrm... This SYN reply doesn't really look like something we expected.
275                           Let's assume everything is okay. */
276                       
277                        msn_logged_in( gc );
278                }
279        }
280        else if( strcmp( cmd[0], "LST" ) == 0 )
281        {
282                int list;
283               
284                if( num_parts != 4 && num_parts != 5 )
285                {
286                        hide_login_progress( gc, "Syntax error" );
287                        signoff( gc );
288                        return( 0 );
289                }
290               
291                http_decode( cmd[2] );
292                list = atoi( cmd[3] );
293               
294                if( list & 1 ) /* FL */
295                {
296                        char *group = NULL;
297                        int num;
298                       
299                        if( cmd[4] != NULL && sscanf( cmd[4], "%d", &num ) == 1 )
300                                group = md->grouplist[num];
301                       
302                        add_buddy( gc, group, cmd[1], cmd[2] );
303                }
304                if( list & 2 ) /* AL */
305                {
306                        gc->permit = g_slist_append( gc->permit, g_strdup( cmd[1] ) );
307                }
308                if( list & 4 ) /* BL */
309                {
310                        gc->deny = g_slist_append( gc->deny, g_strdup( cmd[1] ) );
311                }
312                if( list & 8 ) /* RL */
313                {
314                        if( ( list & 6 ) == 0 )
315                                msn_buddy_ask( gc, cmd[1], cmd[2] );
316                }
317               
318                if( --md->buddycount == 0 )
319                {
320                        if( gc->flags & OPT_LOGGED_IN )
321                        {
322                                serv_got_crap( gc, "Successfully transferred to different server" );
323                                g_snprintf( buf, sizeof( buf ), "CHG %d %s %d\r\n", ++md->trId, md->away_state->code, 0 );
324                                return( msn_write( gc, buf, strlen( buf ) ) );
325                        }
326                        else
327                        {
328                                msn_logged_in( gc );
329                        }
330                }
331        }
332        else if( strcmp( cmd[0], "LSG" ) == 0 )
333        {
334                int num;
335               
336                if( num_parts != 4 )
337                {
338                        hide_login_progress_error( gc, "Syntax error" );
339                        signoff( gc );
340                        return( 0 );
341                }
342               
343                http_decode( cmd[2] );
344                num = atoi( cmd[1] );
345               
346                if( num < md->groupcount )
347                        md->grouplist[num] = g_strdup( cmd[2] );
348        }
349        else if( strcmp( cmd[0], "CHL" ) == 0 )
350        {
351                md5_state_t state;
352                md5_byte_t digest[16];
353                int i;
354               
355                if( num_parts != 3 )
356                {
357                        hide_login_progress_error( gc, "Syntax error" );
358                        signoff( gc );
359                        return( 0 );
360                }
361               
362                md5_init( &state );
363                md5_append( &state, (const md5_byte_t *) cmd[2], strlen( cmd[2] ) );
364                md5_append( &state, (const md5_byte_t *) QRY_CODE, strlen( QRY_CODE ) );
365                md5_finish( &state, digest );
366               
367                g_snprintf( buf, sizeof( buf ), "QRY %d %s %d\r\n", ++md->trId, QRY_NAME, 32 );
368                for( i = 0; i < 16; i ++ )
369                        g_snprintf( buf + strlen( buf ), 3, "%02x", digest[i] );
370               
371                return( msn_write( gc, buf, strlen( buf ) ) );
372        }
373        else if( strcmp( cmd[0], "ILN" ) == 0 )
374        {
375                const struct msn_away_state *st;
376               
377                if( num_parts != 6 )
378                {
379                        hide_login_progress_error( gc, "Syntax error" );
380                        signoff( gc );
381                        return( 0 );
382                }
383               
384                http_decode( cmd[4] );
385                serv_buddy_rename( gc, cmd[3], cmd[4] );
386               
387                st = msn_away_state_by_code( cmd[2] );
388                if( !st )
389                {
390                        /* FIXME: Warn/Bomb about unknown away state? */
391                        st = msn_away_state_list;
392                }
393               
394                serv_got_update( gc, cmd[3], 1, 0, 0, 0, st->number, 0 );
395        }
396        else if( strcmp( cmd[0], "FLN" ) == 0 )
397        {
398                if( cmd[1] )
399                        serv_got_update( gc, cmd[1], 0, 0, 0, 0, 0, 0 );
400        }
401        else if( strcmp( cmd[0], "NLN" ) == 0 )
402        {
403                const struct msn_away_state *st;
404               
405                if( num_parts != 5 )
406                {
407                        hide_login_progress_error( gc, "Syntax error" );
408                        signoff( gc );
409                        return( 0 );
410                }
411               
412                http_decode( cmd[3] );
413                serv_buddy_rename( gc, cmd[2], cmd[3] );
414               
415                st = msn_away_state_by_code( cmd[1] );
416                if( !st )
417                {
418                        /* FIXME: Warn/Bomb about unknown away state? */
419                        st = msn_away_state_list;
420                }
421               
422                serv_got_update( gc, cmd[2], 1, 0, 0, 0, st->number, 0 );
423        }
424        else if( strcmp( cmd[0], "RNG" ) == 0 )
425        {
426                struct msn_switchboard *sb;
427                char *server;
428                int session, port;
429               
430                if( num_parts != 7 )
431                {
432                        hide_login_progress_error( gc, "Syntax error" );
433                        signoff( gc );
434                        return( 0 );
435                }
436               
437                session = atoi( cmd[1] );
438               
439                server = strchr( cmd[2], ':' );
440                if( !server )
441                {
442                        hide_login_progress_error( gc, "Syntax error" );
443                        signoff( gc );
444                        return( 0 );
445                }
446                *server = 0;
447                port = atoi( server + 1 );
448                server = cmd[2];
449               
450                if( strcmp( cmd[3], "CKI" ) != 0 )
451                {
452                        hide_login_progress_error( gc, "Unknown authentication method for switchboard" );
453                        signoff( gc );
454                        return( 0 );
455                }
456               
457                debug( "Got a call from %s (session %d). Key = %s", cmd[5], session, cmd[4] );
458               
459                sb = msn_sb_create( gc, server, port, cmd[4], session );
460                sb->who = g_strdup( cmd[5] );
461        }
462        else if( strcmp( cmd[0], "ADD" ) == 0 )
463        {
464                if( num_parts == 6 && strcmp( cmd[2], "RL" ) == 0 )
465                {
466                        GSList *l;
467                       
468                        http_decode( cmd[5] );
469                       
470                        if( strchr( cmd[4], '@' ) == NULL )
471                        {
472                                hide_login_progress_error( gc, "Syntax error" );
473                                signoff( gc );
474                                return( 0 );
475                        }
476                       
477                        /* We got added by someone. If we don't have this person in permit/deny yet, inform the user. */
478                        for( l = gc->permit; l; l = l->next )
479                                if( g_strcasecmp( l->data, cmd[4] ) == 0 )
480                                        return( 1 );
481                       
482                        for( l = gc->deny; l; l = l->next )
483                                if( g_strcasecmp( l->data, cmd[4] ) == 0 )
484                                        return( 1 );
485                       
486                        msn_buddy_ask( gc, cmd[4], cmd[5] );
487                }
488        }
489        else if( strcmp( cmd[0], "OUT" ) == 0 )
490        {
491                if( cmd[1] && strcmp( cmd[1], "OTH" ) == 0 )
492                {
493                        hide_login_progress_error( gc, "Someone else logged in with your account" );
494                        gc->wants_to_die = 1;
495                }
496                else if( cmd[1] && strcmp( cmd[1], "SSD" ) == 0 )
497                {
498                        hide_login_progress_error( gc, "Terminating session because of server shutdown" );
499                }
500                else
501                {
502                        hide_login_progress_error( gc, "Session terminated by remote server (reason unknown)" );
503                }
504               
505                signoff( gc );
506                return( 0 );
507        }
508        else if( strcmp( cmd[0], "REA" ) == 0 )
509        {
510                if( num_parts != 5 )
511                {
512                        hide_login_progress_error( gc, "Syntax error" );
513                        signoff( gc );
514                        return( 0 );
515                }
516               
517                if( g_strcasecmp( cmd[3], gc->username ) == 0 )
518                {
519                        http_decode( cmd[4] );
520                        strncpy( gc->displayname, cmd[4], sizeof( gc->displayname ) );
521                        gc->displayname[sizeof(gc->displayname)-1] = 0;
522                }
523                else
524                {
525                        /* This is not supposed to happen, but let's handle it anyway... */
526                        http_decode( cmd[4] );
527                        serv_buddy_rename( gc, cmd[3], cmd[4] );
528                }
529        }
530        else if( strcmp( cmd[0], "IPG" ) == 0 )
531        {
532                do_error_dialog( gc, "Received IPG command, we don't handle them yet.", "MSN" );
533               
534                md->handler->msglen = atoi( cmd[1] );
535               
536                if( md->handler->msglen <= 0 )
537                {
538                        hide_login_progress_error( gc, "Syntax error" );
539                        signoff( gc );
540                        return( 0 );
541                }
542        }
543        else if( isdigit( cmd[0][0] ) )
544        {
545                int num = atoi( cmd[0] );
546                const struct msn_status_code *err = msn_status_by_number( num );
547               
548                g_snprintf( buf, sizeof( buf ), "Error reported by MSN server: %s", err->text );
549                do_error_dialog( gc, buf, "MSN" );
550               
551                if( err->flags & STATUS_FATAL )
552                {
553                        signoff( gc );
554                        return( 0 );
555                }
556        }
557        else
558        {
559                debug( "Received unknown command from main server: %s", cmd[0] );
560        }
561       
562        return( 1 );
563}
564
565static int msn_ns_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts )
566{
567        struct gaim_connection *gc = data;
568        char *body;
569        int blen = 0;
570       
571        if( !num_parts )
572                return( 1 );
573       
574        if( ( body = strstr( msg, "\r\n\r\n" ) ) )
575        {
576                body += 4;
577                blen = msglen - ( body - msg );
578        }
579       
580        if( strcmp( cmd[0], "MSG" ) == 0 )
581        {
582                if( g_strcasecmp( cmd[1], "Hotmail" ) == 0 )
583                {
584                        char *ct = msn_findheader( msg, "Content-Type:", msglen );
585                       
586                        if( !ct )
587                                return( 1 );
588                       
589                        if( g_strncasecmp( ct, "application/x-msmsgssystemmessage", 33 ) == 0 )
590                        {
591                                char *mtype;
592                                char *arg1;
593                               
594                                if( !body )
595                                        return( 1 );
596                               
597                                mtype = msn_findheader( body, "Type:", blen );
598                                arg1 = msn_findheader( body, "Arg1:", blen );
599                               
600                                if( mtype && strcmp( mtype, "1" ) == 0 )
601                                {
602                                        if( arg1 )
603                                                serv_got_crap( gc, "The server is going down for maintenance in %s minutes.", arg1 );
604                                }
605                               
606                                if( arg1 ) g_free( arg1 );
607                                if( mtype ) g_free( mtype );
608                        }
609                        else if( g_strncasecmp( ct, "text/x-msmsgsprofile", 20 ) == 0 )
610                        {
611                                /* We don't care about this profile for now... */
612                        }
613                        else if( g_strncasecmp( ct, "text/x-msmsgsinitialemailnotification", 37 ) == 0 )
614                        {
615                                char *inbox = msn_findheader( body, "Inbox-Unread:", blen );
616                                char *folders = msn_findheader( body, "Folders-Unread:", blen );
617                               
618                                if( inbox && folders )
619                                {
620                                        serv_got_crap( gc, "INBOX contains %s new messages, plus %s messages in other folders.", inbox, folders );
621                                }
622                        }
623                        else if( g_strncasecmp( ct, "text/x-msmsgsemailnotification", 30 ) == 0 )
624                        {
625                                char *from = msn_findheader( body, "From-Addr:", blen );
626                                char *fromname = msn_findheader( body, "From:", blen );
627                               
628                                if( from && fromname )
629                                {
630                                        serv_got_crap( gc, "Received an e-mail message from %s <%s>.", fromname, from );
631                                }
632                        }
633                        else if( g_strncasecmp( ct, "text/x-msmsgsactivemailnotification", 35 ) == 0 )
634                        {
635                                /* Sorry, but this one really is *USELESS* */
636                        }
637                        else
638                        {
639                                debug( "Can't handle %s packet from notification server", ct );
640                        }
641                       
642                        g_free( ct );
643                }
644        }
645       
646        return( 1 );
647}
648
649static void msn_auth_got_passport_id( struct passport_reply *rep )
650{
651        struct gaim_connection *gc = rep->data;
652        struct msn_data *md = gc->proto_data;
653        char *key = rep->result;
654        char buf[1024];
655       
656        if( key == NULL )
657        {
658                char *err;
659               
660                err = g_strdup_printf( "Error during Passport authentication (%s)",
661                                       rep->error_string ? rep->error_string : "Unknown error" );
662               
663                hide_login_progress( gc, err );
664                signoff( gc );
665               
666                g_free( err );
667        }
668        else
669        {
670                g_snprintf( buf, sizeof( buf ), "USR %d TWN S %s\r\n", ++md->trId, key );
671                msn_write( gc, buf, strlen( buf ) );
672        }
673}
Note: See TracBrowser for help on using the repository browser.