source: protocols/msn/ns.c @ bc736cfa

Last change on this file since bc736cfa was bc736cfa, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-03-21T08:00:12Z

Basic handling of LSG responses.

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