source: protocols/msn/ns.c @ 6048744

Last change on this file since 6048744 was 1ad104a, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-03-21T08:11:10Z

ns.c now passess the first group a buddy is in.

  • Property mode set to 100644
File size: 15.9 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                        char *group = NULL;
291                        int num;
292                       
293                        if( cmd[4] != NULL && sscanf( cmd[4], "%d", &num ) == 1 )
294                                group = md->grouplist[num];
295                       
296                        add_buddy( gc, group, cmd[1], cmd[2] );
297                }
298                if( list & 2 ) /* AL */
299                {
300                        gc->permit = g_slist_append( gc->permit, g_strdup( cmd[1] ) );
301                }
302                if( list & 4 ) /* BL */
303                {
304                        gc->deny = g_slist_append( gc->deny, g_strdup( cmd[1] ) );
305                }
306                if( list & 8 ) /* RL */
307                {
308                        if( ( list & 6 ) == 0 )
309                                msn_buddy_ask( gc, cmd[1], cmd[2] );
310                }
311               
312                if( --md->buddycount == 0 )
313                {
314                        if( gc->flags & OPT_LOGGED_IN )
315                        {
316                                serv_got_crap( gc, "Successfully transferred to different server" );
317                                g_snprintf( buf, sizeof( buf ), "CHG %d %s %d\r\n", ++md->trId, md->away_state->code, 0 );
318                                return( msn_write( gc, buf, strlen( buf ) ) );
319                        }
320                        else
321                        {
322                                msn_logged_in( gc );
323                        }
324                }
325        }
326        else if( strcmp( cmd[0], "LSG" ) == 0 )
327        {
328                int num;
329               
330                if( num_parts != 4 )
331                {
332                        hide_login_progress_error( gc, "Syntax error" );
333                        signoff( gc );
334                        return( 0 );
335                }
336               
337                http_decode( cmd[2] );
338                num = atoi( cmd[1] );
339               
340                if( num < md->groupcount )
341                        md->grouplist[num] = g_strdup( cmd[2] );
342        }
343        else if( strcmp( cmd[0], "CHL" ) == 0 )
344        {
345                md5_state_t state;
346                md5_byte_t digest[16];
347                int i;
348               
349                if( num_parts != 3 )
350                {
351                        hide_login_progress_error( gc, "Syntax error" );
352                        signoff( gc );
353                        return( 0 );
354                }
355               
356                md5_init( &state );
357                md5_append( &state, (const md5_byte_t *) cmd[2], strlen( cmd[2] ) );
358                md5_append( &state, (const md5_byte_t *) QRY_CODE, strlen( QRY_CODE ) );
359                md5_finish( &state, digest );
360               
361                g_snprintf( buf, sizeof( buf ), "QRY %d %s %d\r\n", ++md->trId, QRY_NAME, 32 );
362                for( i = 0; i < 16; i ++ )
363                        g_snprintf( buf + strlen( buf ), 3, "%02x", digest[i] );
364               
365                return( msn_write( gc, buf, strlen( buf ) ) );
366        }
367        else if( strcmp( cmd[0], "ILN" ) == 0 )
368        {
369                const struct msn_away_state *st;
370               
371                if( num_parts != 6 )
372                {
373                        hide_login_progress_error( gc, "Syntax error" );
374                        signoff( gc );
375                        return( 0 );
376                }
377               
378                http_decode( cmd[4] );
379                serv_buddy_rename( gc, cmd[3], cmd[4] );
380               
381                st = msn_away_state_by_code( cmd[2] );
382                if( !st )
383                {
384                        /* FIXME: Warn/Bomb about unknown away state? */
385                        st = msn_away_state_list;
386                }
387               
388                serv_got_update( gc, cmd[3], 1, 0, 0, 0, st->number, 0 );
389        }
390        else if( strcmp( cmd[0], "FLN" ) == 0 )
391        {
392                if( cmd[1] )
393                        serv_got_update( gc, cmd[1], 0, 0, 0, 0, 0, 0 );
394        }
395        else if( strcmp( cmd[0], "NLN" ) == 0 )
396        {
397                const struct msn_away_state *st;
398               
399                if( num_parts != 5 )
400                {
401                        hide_login_progress_error( gc, "Syntax error" );
402                        signoff( gc );
403                        return( 0 );
404                }
405               
406                http_decode( cmd[3] );
407                serv_buddy_rename( gc, cmd[2], cmd[3] );
408               
409                st = msn_away_state_by_code( cmd[1] );
410                if( !st )
411                {
412                        /* FIXME: Warn/Bomb about unknown away state? */
413                        st = msn_away_state_list;
414                }
415               
416                serv_got_update( gc, cmd[2], 1, 0, 0, 0, st->number, 0 );
417        }
418        else if( strcmp( cmd[0], "RNG" ) == 0 )
419        {
420                struct msn_switchboard *sb;
421                char *server;
422                int session, port;
423               
424                if( num_parts != 7 )
425                {
426                        hide_login_progress_error( gc, "Syntax error" );
427                        signoff( gc );
428                        return( 0 );
429                }
430               
431                session = atoi( cmd[1] );
432               
433                server = strchr( cmd[2], ':' );
434                if( !server )
435                {
436                        hide_login_progress_error( gc, "Syntax error" );
437                        signoff( gc );
438                        return( 0 );
439                }
440                *server = 0;
441                port = atoi( server + 1 );
442                server = cmd[2];
443               
444                if( strcmp( cmd[3], "CKI" ) != 0 )
445                {
446                        hide_login_progress_error( gc, "Unknown authentication method for switchboard" );
447                        signoff( gc );
448                        return( 0 );
449                }
450               
451                debug( "Got a call from %s (session %d). Key = %s", cmd[5], session, cmd[4] );
452               
453                sb = msn_sb_create( gc, server, port, cmd[4], session );
454                sb->who = g_strdup( cmd[5] );
455        }
456        else if( strcmp( cmd[0], "ADD" ) == 0 )
457        {
458                if( num_parts == 6 && strcmp( cmd[2], "RL" ) == 0 )
459                {
460                        GSList *l;
461                       
462                        http_decode( cmd[5] );
463                       
464                        if( strchr( cmd[4], '@' ) == NULL )
465                        {
466                                hide_login_progress_error( gc, "Syntax error" );
467                                signoff( gc );
468                                return( 0 );
469                        }
470                       
471                        /* We got added by someone. If we don't have this person in permit/deny yet, inform the user. */
472                        for( l = gc->permit; l; l = l->next )
473                                if( g_strcasecmp( l->data, cmd[4] ) == 0 )
474                                        return( 1 );
475                       
476                        for( l = gc->deny; l; l = l->next )
477                                if( g_strcasecmp( l->data, cmd[4] ) == 0 )
478                                        return( 1 );
479                       
480                        msn_buddy_ask( gc, cmd[4], cmd[5] );
481                }
482        }
483        else if( strcmp( cmd[0], "OUT" ) == 0 )
484        {
485                if( cmd[1] && strcmp( cmd[1], "OTH" ) == 0 )
486                {
487                        hide_login_progress_error( gc, "Someone else logged in with your account" );
488                        gc->wants_to_die = 1;
489                }
490                else if( cmd[1] && strcmp( cmd[1], "SSD" ) == 0 )
491                {
492                        hide_login_progress_error( gc, "Terminating session because of server shutdown" );
493                }
494                else
495                {
496                        hide_login_progress_error( gc, "Session terminated by remote server (reason unknown)" );
497                }
498               
499                signoff( gc );
500                return( 0 );
501        }
502        else if( strcmp( cmd[0], "REA" ) == 0 )
503        {
504                if( num_parts != 5 )
505                {
506                        hide_login_progress_error( gc, "Syntax error" );
507                        signoff( gc );
508                        return( 0 );
509                }
510               
511                if( g_strcasecmp( cmd[3], gc->username ) == 0 )
512                {
513                        http_decode( cmd[4] );
514                        strncpy( gc->displayname, cmd[4], sizeof( gc->displayname ) );
515                        gc->displayname[sizeof(gc->displayname)-1] = 0;
516                }
517                else
518                {
519                        /* This is not supposed to happen, but let's handle it anyway... */
520                        http_decode( cmd[4] );
521                        serv_buddy_rename( gc, cmd[3], cmd[4] );
522                }
523        }
524        else if( strcmp( cmd[0], "IPG" ) == 0 )
525        {
526                do_error_dialog( gc, "Received IPG command, we don't handle them yet.", "MSN" );
527               
528                md->handler->msglen = atoi( cmd[1] );
529               
530                if( md->handler->msglen <= 0 )
531                {
532                        hide_login_progress_error( gc, "Syntax error" );
533                        signoff( gc );
534                        return( 0 );
535                }
536        }
537        else if( isdigit( cmd[0][0] ) )
538        {
539                int num = atoi( cmd[0] );
540                const struct msn_status_code *err = msn_status_by_number( num );
541               
542                g_snprintf( buf, sizeof( buf ), "Error reported by MSN server: %s", err->text );
543                do_error_dialog( gc, buf, "MSN" );
544               
545                if( err->flags & STATUS_FATAL )
546                {
547                        signoff( gc );
548                        return( 0 );
549                }
550        }
551        else
552        {
553                debug( "Received unknown command from main server: %s", cmd[0] );
554        }
555       
556        return( 1 );
557}
558
559static int msn_ns_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts )
560{
561        struct gaim_connection *gc = data;
562        char *body;
563        int blen = 0;
564       
565        if( !num_parts )
566                return( 1 );
567       
568        if( ( body = strstr( msg, "\r\n\r\n" ) ) )
569        {
570                body += 4;
571                blen = msglen - ( body - msg );
572        }
573       
574        if( strcmp( cmd[0], "MSG" ) == 0 )
575        {
576                if( g_strcasecmp( cmd[1], "Hotmail" ) == 0 )
577                {
578                        char *ct = msn_findheader( msg, "Content-Type:", msglen );
579                       
580                        if( !ct )
581                                return( 1 );
582                       
583                        if( g_strncasecmp( ct, "application/x-msmsgssystemmessage", 33 ) == 0 )
584                        {
585                                char *mtype;
586                                char *arg1;
587                               
588                                if( !body )
589                                        return( 1 );
590                               
591                                mtype = msn_findheader( body, "Type:", blen );
592                                arg1 = msn_findheader( body, "Arg1:", blen );
593                               
594                                if( mtype && strcmp( mtype, "1" ) == 0 )
595                                {
596                                        if( arg1 )
597                                                serv_got_crap( gc, "The server is going down for maintenance in %s minutes.", arg1 );
598                                }
599                               
600                                if( arg1 ) g_free( arg1 );
601                                if( mtype ) g_free( mtype );
602                        }
603                        else if( g_strncasecmp( ct, "text/x-msmsgsprofile", 20 ) == 0 )
604                        {
605                                /* We don't care about this profile for now... */
606                        }
607                        else if( g_strncasecmp( ct, "text/x-msmsgsinitialemailnotification", 37 ) == 0 )
608                        {
609                                char *inbox = msn_findheader( body, "Inbox-Unread:", blen );
610                                char *folders = msn_findheader( body, "Folders-Unread:", blen );
611                               
612                                if( inbox && folders )
613                                {
614                                        serv_got_crap( gc, "INBOX contains %s new messages, plus %s messages in other folders.", inbox, folders );
615                                }
616                        }
617                        else if( g_strncasecmp( ct, "text/x-msmsgsemailnotification", 30 ) == 0 )
618                        {
619                                char *from = msn_findheader( body, "From-Addr:", blen );
620                                char *fromname = msn_findheader( body, "From:", blen );
621                               
622                                if( from && fromname )
623                                {
624                                        serv_got_crap( gc, "Received an e-mail message from %s <%s>.", fromname, from );
625                                }
626                        }
627                        else if( g_strncasecmp( ct, "text/x-msmsgsactivemailnotification", 35 ) == 0 )
628                        {
629                                /* Sorry, but this one really is *USELESS* */
630                        }
631                        else
632                        {
633                                debug( "Can't handle %s packet from notification server", ct );
634                        }
635                       
636                        g_free( ct );
637                }
638        }
639       
640        return( 1 );
641}
642
643static void msn_auth_got_passport_id( struct passport_reply *rep )
644{
645        struct gaim_connection *gc = rep->data;
646        struct msn_data *md = gc->proto_data;
647        char *key = rep->result;
648        char buf[1024];
649       
650        if( key == NULL )
651        {
652                hide_login_progress( gc, "Error during Passport authentication" );
653                signoff( gc );
654        }
655        else
656        {
657                g_snprintf( buf, sizeof( buf ), "USR %d TWN S %s\r\n", ++md->trId, key );
658                msn_write( gc, buf, strlen( buf ) );
659        }
660}
Note: See TracBrowser for help on using the repository browser.