source: protocols/msn/ns.c @ ca7de3a

Last change on this file since ca7de3a was ca7de3a, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-12T22:13:26Z

Successful login (including contact list sync). \o/

  • Property mode set to 100644
File size: 20.2 KB
RevLine 
[b7d3cc34]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 "md5.h"
[7db65b7]30#include "soap.h"
[ca7de3a]31#include "xmltree.h"
[b7d3cc34]32
[ba9edaa]33static gboolean msn_ns_callback( gpointer data, gint source, b_input_condition cond );
[b7d3cc34]34static int msn_ns_command( gpointer data, char **cmd, int num_parts );
35static int msn_ns_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts );
36
[e3413cc]37static gboolean msn_ns_got_display_name( struct im_connection *ic, char *name );
[ca7de3a]38static void msn_ns_send_adl( struct im_connection *ic );
[b7d3cc34]39
[ba9edaa]40gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond )
[b7d3cc34]41{
[0da65d5]42        struct im_connection *ic = data;
[b7d3cc34]43        struct msn_data *md;
44        char s[1024];
45       
[0da65d5]46        if( !g_slist_find( msn_connections, ic ) )
[ba9edaa]47                return FALSE;
[b7d3cc34]48       
49        if( source == -1 )
50        {
[84b045d]51                imcb_error( ic, "Could not connect to server" );
[c2fb3809]52                imc_logout( ic, TRUE );
[ba9edaa]53                return FALSE;
[b7d3cc34]54        }
55       
[0da65d5]56        md = ic->proto_data;
[b7d3cc34]57       
58        if( !md->handler )
59        {
60                md->handler = g_new0( struct msn_handler_data, 1 );
[0da65d5]61                md->handler->data = ic;
[b7d3cc34]62                md->handler->exec_command = msn_ns_command;
63                md->handler->exec_message = msn_ns_message;
64        }
65        else
66        {
67                if( md->handler->rxq )
68                        g_free( md->handler->rxq );
69               
70                md->handler->rxlen = 0;
71        }
72       
73        md->handler->fd = md->fd;
74        md->handler->rxq = g_new0( char, 1 );
75       
[523fb23]76        g_snprintf( s, sizeof( s ), "VER %d MSNP15 CVR0\r\n", ++md->trId );
[0da65d5]77        if( msn_write( ic, s, strlen( s ) ) )
[b7d3cc34]78        {
[e046390]79                ic->inpa = b_input_add( md->fd, B_EV_IO_READ, msn_ns_callback, ic );
[84b045d]80                imcb_log( ic, "Connected to server, waiting for reply" );
[b7d3cc34]81        }
[ba9edaa]82       
83        return FALSE;
[b7d3cc34]84}
85
[ba9edaa]86static gboolean msn_ns_callback( gpointer data, gint source, b_input_condition cond )
[b7d3cc34]87{
[0da65d5]88        struct im_connection *ic = data;
89        struct msn_data *md = ic->proto_data;
[b7d3cc34]90       
91        if( msn_handler( md->handler ) == -1 ) /* Don't do this on ret == 0, it's already done then. */
92        {
[84b045d]93                imcb_error( ic, "Error while reading from server" );
[c2fb3809]94                imc_logout( ic, TRUE );
[ba9edaa]95               
96                return FALSE;
[b7d3cc34]97        }
[ba9edaa]98        else
99                return TRUE;
[b7d3cc34]100}
101
102static int msn_ns_command( gpointer data, char **cmd, int num_parts )
103{
[0da65d5]104        struct im_connection *ic = data;
105        struct msn_data *md = ic->proto_data;
[b7d3cc34]106        char buf[1024];
107       
108        if( num_parts == 0 )
109        {
110                /* Hrrm... Empty command...? Ignore? */
111                return( 1 );
112        }
113       
114        if( strcmp( cmd[0], "VER" ) == 0 )
115        {
[523fb23]116                if( cmd[2] && strncmp( cmd[2], "MSNP15", 5 ) != 0 )
[b7d3cc34]117                {
[84b045d]118                        imcb_error( ic, "Unsupported protocol" );
[c2fb3809]119                        imc_logout( ic, FALSE );
[b7d3cc34]120                        return( 0 );
121                }
122               
123                g_snprintf( buf, sizeof( buf ), "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s\r\n",
[c2fb3809]124                                                ++md->trId, ic->acc->user );
[0da65d5]125                return( msn_write( ic, buf, strlen( buf ) ) );
[b7d3cc34]126        }
127        else if( strcmp( cmd[0], "CVR" ) == 0 )
128        {
129                /* We don't give a damn about the information we just received */
[523fb23]130                g_snprintf( buf, sizeof( buf ), "USR %d SSO I %s\r\n", ++md->trId, ic->acc->user );
[0da65d5]131                return( msn_write( ic, buf, strlen( buf ) ) );
[b7d3cc34]132        }
133        else if( strcmp( cmd[0], "XFR" ) == 0 )
134        {
135                char *server;
136                int port;
137               
[ca7de3a]138                if( num_parts >= 6 && strcmp( cmd[2], "NS" ) == 0 )
[b7d3cc34]139                {
[0da65d5]140                        b_event_remove( ic->inpa );
141                        ic->inpa = 0;
[b7d3cc34]142                        closesocket( md->fd );
143                       
144                        server = strchr( cmd[3], ':' );
145                        if( !server )
146                        {
[84b045d]147                                imcb_error( ic, "Syntax error" );
[c2fb3809]148                                imc_logout( ic, TRUE );
[b7d3cc34]149                                return( 0 );
150                        }
151                        *server = 0;
152                        port = atoi( server + 1 );
153                        server = cmd[3];
154                       
[84b045d]155                        imcb_log( ic, "Transferring to other server" );
[b7d3cc34]156                       
[0da65d5]157                        md->fd = proxy_connect( server, port, msn_ns_connected, ic );
[b7d3cc34]158                }
[ca7de3a]159                else if( num_parts >= 6 && strcmp( cmd[2], "SB" ) == 0 )
[b7d3cc34]160                {
161                        struct msn_switchboard *sb;
162                       
163                        server = strchr( cmd[3], ':' );
164                        if( !server )
165                        {
[84b045d]166                                imcb_error( ic, "Syntax error" );
[c2fb3809]167                                imc_logout( ic, TRUE );
[b7d3cc34]168                                return( 0 );
169                        }
170                        *server = 0;
171                        port = atoi( server + 1 );
172                        server = cmd[3];
173                       
174                        if( strcmp( cmd[4], "CKI" ) != 0 )
175                        {
[84b045d]176                                imcb_error( ic, "Unknown authentication method for switchboard" );
[c2fb3809]177                                imc_logout( ic, TRUE );
[b7d3cc34]178                                return( 0 );
179                        }
180                       
181                        debug( "Connecting to a new switchboard with key %s", cmd[5] );
[99f929c]182
183                        if( ( sb = msn_sb_create( ic, server, port, cmd[5], MSN_SB_NEW ) ) == NULL )
184                        {
185                                /* Although this isn't strictly fatal for the NS connection, it's
186                                   definitely something serious (we ran out of file descriptors?). */
187                                imcb_error( ic, "Could not create new switchboard" );
188                                imc_logout( ic, TRUE );
189                                return( 0 );
190                        }
[b7d3cc34]191                       
192                        if( md->msgq )
193                        {
194                                struct msn_message *m = md->msgq->data;
195                                GSList *l;
196                               
197                                sb->who = g_strdup( m->who );
198                               
199                                /* Move all the messages to the first user in the message
200                                   queue to the switchboard message queue. */
201                                l = md->msgq;
202                                while( l )
203                                {
204                                        m = l->data;
205                                        l = l->next;
206                                        if( strcmp( m->who, sb->who ) == 0 )
207                                        {
208                                                sb->msgq = g_slist_append( sb->msgq, m );
209                                                md->msgq = g_slist_remove( md->msgq, m );
210                                        }
211                                }
212                        }
213                }
214                else
215                {
[84b045d]216                        imcb_error( ic, "Syntax error" );
[c2fb3809]217                        imc_logout( ic, TRUE );
[b7d3cc34]218                        return( 0 );
219                }
220        }
221        else if( strcmp( cmd[0], "USR" ) == 0 )
222        {
[523fb23]223                if( num_parts >= 6 && strcmp( cmd[2], "SSO" ) == 0 &&
224                    strcmp( cmd[3], "S" ) == 0 )
[b7d3cc34]225                {
[523fb23]226                        msn_soap_passport_sso_request( ic, cmd[4], cmd[5] );
[b7d3cc34]227                }
[5fecede]228                else if( strcmp( cmd[2], "OK" ) == 0 )
[b7d3cc34]229                {
[21d48d9]230                        if( num_parts == 7 )
[e3413cc]231                                msn_ns_got_display_name( ic, cmd[4] );
[21d48d9]232                        else
233                                imcb_log( ic, "Warning: Friendly name in server response was corrupted" );
[911f2eb]234                       
[84b045d]235                        imcb_log( ic, "Authenticated, getting buddy list" );
[b7d3cc34]236                       
[7db65b7]237                        msn_soap_memlist_request( ic );
[b7d3cc34]238                }
239                else
240                {
[84b045d]241                        imcb_error( ic, "Unknown authentication type" );
[c2fb3809]242                        imc_logout( ic, FALSE );
[b7d3cc34]243                        return( 0 );
244                }
245        }
246        else if( strcmp( cmd[0], "MSG" ) == 0 )
247        {
248                if( num_parts != 4 )
249                {
[84b045d]250                        imcb_error( ic, "Syntax error" );
[c2fb3809]251                        imc_logout( ic, TRUE );
[b7d3cc34]252                        return( 0 );
253                }
254               
255                md->handler->msglen = atoi( cmd[3] );
256               
257                if( md->handler->msglen <= 0 )
258                {
[84b045d]259                        imcb_error( ic, "Syntax error" );
[c2fb3809]260                        imc_logout( ic, TRUE );
[b7d3cc34]261                        return( 0 );
262                }
263        }
[ca7de3a]264        else if( strcmp( cmd[0], "BLP" ) == 0 )
[b7d3cc34]265        {
[ca7de3a]266                msn_ns_send_adl( ic );
[b7d3cc34]267        }
[ca7de3a]268        else if( strcmp( cmd[0], "ADL" ) == 0 )
[b7d3cc34]269        {
[ca7de3a]270                if( num_parts >= 3 && strcmp( cmd[2], "OK" ) == 0 )
[b7d3cc34]271                {
[ca7de3a]272                        char buf[1024];
273                        char *fn_raw = set_getstr( &ic->acc->set, "display_name" );
274                        char *fn;
[1ad104a]275                       
[ca7de3a]276                        if( fn_raw == NULL )
277                                fn_raw = ic->acc->user;
278                        fn = g_malloc( strlen( fn_raw ) * 3 + 1 );
279                        strcpy( fn, fn_raw );
280                        http_encode( fn );
[1ad104a]281                       
[ca7de3a]282                        g_snprintf( buf, sizeof( buf ), "PRP %d MFN %s\r\n",
283                                    ++md->trId, fn );
284                        g_free( fn );
285                       
286                        msn_write( ic, buf, strlen( buf ) );
[b7d3cc34]287                }
288        }
[ca7de3a]289        else if( strcmp( cmd[0], "PRP" ) == 0 )
[b7d3cc34]290        {
[ca7de3a]291                imcb_connected( ic );
[b7d3cc34]292        }
293        else if( strcmp( cmd[0], "CHL" ) == 0 )
294        {
295                md5_state_t state;
296                md5_byte_t digest[16];
297                int i;
298               
299                if( num_parts != 3 )
300                {
[84b045d]301                        imcb_error( ic, "Syntax error" );
[c2fb3809]302                        imc_logout( ic, TRUE );
[b7d3cc34]303                        return( 0 );
304                }
305               
306                md5_init( &state );
307                md5_append( &state, (const md5_byte_t *) cmd[2], strlen( cmd[2] ) );
308                md5_append( &state, (const md5_byte_t *) QRY_CODE, strlen( QRY_CODE ) );
309                md5_finish( &state, digest );
310               
311                g_snprintf( buf, sizeof( buf ), "QRY %d %s %d\r\n", ++md->trId, QRY_NAME, 32 );
312                for( i = 0; i < 16; i ++ )
313                        g_snprintf( buf + strlen( buf ), 3, "%02x", digest[i] );
314               
[0da65d5]315                return( msn_write( ic, buf, strlen( buf ) ) );
[b7d3cc34]316        }
317        else if( strcmp( cmd[0], "ILN" ) == 0 )
318        {
[08995b0]319                const struct msn_away_state *st;
[b7d3cc34]320               
[ca7de3a]321                if( num_parts < 6 )
[b7d3cc34]322                {
[84b045d]323                        imcb_error( ic, "Syntax error" );
[c2fb3809]324                        imc_logout( ic, TRUE );
[b7d3cc34]325                        return( 0 );
326                }
327               
[ca7de3a]328                http_decode( cmd[5] );
329                imcb_rename_buddy( ic, cmd[3], cmd[5] );
[b7d3cc34]330               
331                st = msn_away_state_by_code( cmd[2] );
332                if( !st )
333                {
334                        /* FIXME: Warn/Bomb about unknown away state? */
[b051d39]335                        st = msn_away_state_list + 1;
[b7d3cc34]336                }
337               
[b051d39]338                imcb_buddy_status( ic, cmd[3], OPT_LOGGED_IN | 
339                                   ( st != msn_away_state_list ? OPT_AWAY : 0 ),
340                                   st->name, NULL );
[b7d3cc34]341        }
342        else if( strcmp( cmd[0], "FLN" ) == 0 )
343        {
[9bf2481]344                if( cmd[1] == NULL )
345                        return 1;
346               
347                imcb_buddy_status( ic, cmd[1], 0, NULL, NULL );
348               
[bb839e8]349                msn_sb_start_keepalives( msn_sb_by_handle( ic, cmd[1] ), TRUE );
[b7d3cc34]350        }
351        else if( strcmp( cmd[0], "NLN" ) == 0 )
352        {
[08995b0]353                const struct msn_away_state *st;
[b7d3cc34]354               
355                if( num_parts != 5 )
356                {
[84b045d]357                        imcb_error( ic, "Syntax error" );
[c2fb3809]358                        imc_logout( ic, TRUE );
[b7d3cc34]359                        return( 0 );
360                }
361               
362                http_decode( cmd[3] );
[f0cb961]363                imcb_rename_buddy( ic, cmd[2], cmd[3] );
[b7d3cc34]364               
365                st = msn_away_state_by_code( cmd[1] );
366                if( !st )
367                {
368                        /* FIXME: Warn/Bomb about unknown away state? */
[b051d39]369                        st = msn_away_state_list + 1;
[b7d3cc34]370                }
371               
[b051d39]372                imcb_buddy_status( ic, cmd[2], OPT_LOGGED_IN | 
373                                   ( st != msn_away_state_list ? OPT_AWAY : 0 ),
374                                   st->name, NULL );
[9bf2481]375               
[bb839e8]376                msn_sb_stop_keepalives( msn_sb_by_handle( ic, cmd[2] ) );
[b7d3cc34]377        }
378        else if( strcmp( cmd[0], "RNG" ) == 0 )
379        {
380                struct msn_switchboard *sb;
381                char *server;
382                int session, port;
383               
384                if( num_parts != 7 )
385                {
[84b045d]386                        imcb_error( ic, "Syntax error" );
[c2fb3809]387                        imc_logout( ic, TRUE );
[b7d3cc34]388                        return( 0 );
389                }
390               
391                session = atoi( cmd[1] );
392               
393                server = strchr( cmd[2], ':' );
394                if( !server )
395                {
[84b045d]396                        imcb_error( ic, "Syntax error" );
[c2fb3809]397                        imc_logout( ic, TRUE );
[b7d3cc34]398                        return( 0 );
399                }
400                *server = 0;
401                port = atoi( server + 1 );
402                server = cmd[2];
403               
404                if( strcmp( cmd[3], "CKI" ) != 0 )
405                {
[84b045d]406                        imcb_error( ic, "Unknown authentication method for switchboard" );
[c2fb3809]407                        imc_logout( ic, TRUE );
[b7d3cc34]408                        return( 0 );
409                }
410               
411                debug( "Got a call from %s (session %d). Key = %s", cmd[5], session, cmd[4] );
412               
[99f929c]413                if( ( sb = msn_sb_create( ic, server, port, cmd[4], session ) ) == NULL )
414                {
415                        /* Although this isn't strictly fatal for the NS connection, it's
416                           definitely something serious (we ran out of file descriptors?). */
417                        imcb_error( ic, "Could not create new switchboard" );
418                        imc_logout( ic, TRUE );
419                        return( 0 );
420                }
421                else
422                {
423                        sb->who = g_strdup( cmd[5] );
424                }
[b7d3cc34]425        }
426        else if( strcmp( cmd[0], "ADD" ) == 0 )
427        {
428                if( num_parts == 6 && strcmp( cmd[2], "RL" ) == 0 )
429                {
430                        GSList *l;
431                       
432                        http_decode( cmd[5] );
433                       
434                        if( strchr( cmd[4], '@' ) == NULL )
435                        {
[84b045d]436                                imcb_error( ic, "Syntax error" );
[c2fb3809]437                                imc_logout( ic, TRUE );
[f0cb961]438                                return 0;
[b7d3cc34]439                        }
440                       
[f0cb961]441                        /* We got added by someone. If we don't have this
442                           person in permit/deny yet, inform the user. */
[0da65d5]443                        for( l = ic->permit; l; l = l->next )
[b7d3cc34]444                                if( g_strcasecmp( l->data, cmd[4] ) == 0 )
[f0cb961]445                                        return 1;
[b7d3cc34]446                       
[0da65d5]447                        for( l = ic->deny; l; l = l->next )
[b7d3cc34]448                                if( g_strcasecmp( l->data, cmd[4] ) == 0 )
[f0cb961]449                                        return 1;
[b7d3cc34]450                       
[0da65d5]451                        msn_buddy_ask( ic, cmd[4], cmd[5] );
[b7d3cc34]452                }
[f0cb961]453                else if( num_parts >= 6 && strcmp( cmd[2], "FL" ) == 0 )
454                {
[6acc033]455                        const char *group = NULL;
456                        int num;
457                       
458                        if( cmd[6] != NULL && sscanf( cmd[6], "%d", &num ) == 1 && num < md->groupcount )
459                                group = md->grouplist[num];
460                       
[f0cb961]461                        http_decode( cmd[5] );
[6acc033]462                        imcb_add_buddy( ic, cmd[4], group );
[f0cb961]463                        imcb_rename_buddy( ic, cmd[4], cmd[5] );
464                }
[b7d3cc34]465        }
466        else if( strcmp( cmd[0], "OUT" ) == 0 )
467        {
[c2fb3809]468                int allow_reconnect = TRUE;
469               
[b7d3cc34]470                if( cmd[1] && strcmp( cmd[1], "OTH" ) == 0 )
471                {
[84b045d]472                        imcb_error( ic, "Someone else logged in with your account" );
[c2fb3809]473                        allow_reconnect = FALSE;
[b7d3cc34]474                }
475                else if( cmd[1] && strcmp( cmd[1], "SSD" ) == 0 )
476                {
[84b045d]477                        imcb_error( ic, "Terminating session because of server shutdown" );
[b7d3cc34]478                }
479                else
480                {
[84b045d]481                        imcb_error( ic, "Session terminated by remote server (reason unknown)" );
[b7d3cc34]482                }
483               
[c2fb3809]484                imc_logout( ic, allow_reconnect );
[b7d3cc34]485                return( 0 );
486        }
[e3413cc]487#if 0
488        /* Discard this one completely for now since I don't care about the ack
489           and since MSN servers can apparently screw up the formatting. */
[b7d3cc34]490        else if( strcmp( cmd[0], "REA" ) == 0 )
491        {
492                if( num_parts != 5 )
493                {
[84b045d]494                        imcb_error( ic, "Syntax error" );
[c2fb3809]495                        imc_logout( ic, TRUE );
[b7d3cc34]496                        return( 0 );
497                }
498               
[c2fb3809]499                if( g_strcasecmp( cmd[3], ic->acc->user ) == 0 )
[b7d3cc34]500                {
[911f2eb]501                        set_t *s;
502                       
[b7d3cc34]503                        http_decode( cmd[4] );
[0da65d5]504                        strncpy( ic->displayname, cmd[4], sizeof( ic->displayname ) );
505                        ic->displayname[sizeof(ic->displayname)-1] = 0;
[911f2eb]506                       
[0da65d5]507                        if( ( s = set_find( &ic->acc->set, "display_name" ) ) )
[911f2eb]508                        {
509                                g_free( s->value );
510                                s->value = g_strdup( cmd[4] );
511                        }
[b7d3cc34]512                }
513                else
514                {
515                        /* This is not supposed to happen, but let's handle it anyway... */
516                        http_decode( cmd[4] );
[f0cb961]517                        imcb_rename_buddy( ic, cmd[3], cmd[4] );
[b7d3cc34]518                }
519        }
[e3413cc]520#endif
[b7d3cc34]521        else if( strcmp( cmd[0], "IPG" ) == 0 )
522        {
[84b045d]523                imcb_error( ic, "Received IPG command, we don't handle them yet." );
[b7d3cc34]524               
525                md->handler->msglen = atoi( cmd[1] );
526               
527                if( md->handler->msglen <= 0 )
528                {
[84b045d]529                        imcb_error( ic, "Syntax error" );
[c2fb3809]530                        imc_logout( ic, TRUE );
[b7d3cc34]531                        return( 0 );
532                }
533        }
[70ac477]534        else if( strcmp( cmd[0], "ADG" ) == 0 )
535        {
536                char *group = g_strdup( cmd[3] );
537                int groupnum, i;
538                GSList *l, *next;
539               
540                http_decode( group );
541                if( sscanf( cmd[4], "%d", &groupnum ) == 1 )
542                {
543                        if( groupnum >= md->groupcount )
544                        {
545                                md->grouplist = g_renew( char *, md->grouplist, groupnum + 1 );
546                                for( i = md->groupcount; i <= groupnum; i ++ )
547                                        md->grouplist[i] = NULL;
548                                md->groupcount = groupnum + 1;
549                        }
550                        g_free( md->grouplist[groupnum] );
551                        md->grouplist[groupnum] = group;
552                }
553                else
554                {
555                        /* Shouldn't happen, but if it does, give up on the group. */
556                        g_free( group );
557                        imcb_error( ic, "Syntax error" );
558                        imc_logout( ic, TRUE );
559                        return 0;
560                }
561               
562                for( l = md->grpq; l; l = next )
563                {
564                        struct msn_groupadd *ga = l->data;
565                        next = l->next;
566                        if( g_strcasecmp( ga->group, group ) == 0 )
567                        {
568                                if( !msn_buddy_list_add( ic, "FL", ga->who, ga->who, group ) )
569                                        return 0;
570                               
571                                g_free( ga->group );
572                                g_free( ga->who );
573                                g_free( ga );
574                                md->grpq = g_slist_remove( md->grpq, ga );
575                        }
576                }
577        }
[5fecede]578        else if( strcmp( cmd[0], "GCF" ) == 0 )
579        {
580                /* Coming up is cmd[2] bytes of stuff we're supposed to
581                   censore. Meh. */
582                md->handler->msglen = atoi( cmd[2] );
583        }
[b7d3cc34]584        else if( isdigit( cmd[0][0] ) )
585        {
586                int num = atoi( cmd[0] );
[08995b0]587                const struct msn_status_code *err = msn_status_by_number( num );
[b7d3cc34]588               
[84b045d]589                imcb_error( ic, "Error reported by MSN server: %s", err->text );
[b7d3cc34]590               
591                if( err->flags & STATUS_FATAL )
592                {
[c2fb3809]593                        imc_logout( ic, TRUE );
[b7d3cc34]594                        return( 0 );
595                }
596        }
597        else
598        {
[8ff0a61]599                /* debug( "Received unknown command from main server: %s", cmd[0] ); */
[b7d3cc34]600        }
601       
602        return( 1 );
603}
604
605static int msn_ns_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts )
606{
[0da65d5]607        struct im_connection *ic = data;
[b7d3cc34]608        char *body;
609        int blen = 0;
610       
611        if( !num_parts )
612                return( 1 );
613       
614        if( ( body = strstr( msg, "\r\n\r\n" ) ) )
615        {
616                body += 4;
617                blen = msglen - ( body - msg );
618        }
619       
620        if( strcmp( cmd[0], "MSG" ) == 0 )
621        {
622                if( g_strcasecmp( cmd[1], "Hotmail" ) == 0 )
623                {
624                        char *ct = msn_findheader( msg, "Content-Type:", msglen );
625                       
626                        if( !ct )
627                                return( 1 );
628                       
629                        if( g_strncasecmp( ct, "application/x-msmsgssystemmessage", 33 ) == 0 )
630                        {
631                                char *mtype;
632                                char *arg1;
633                               
634                                if( !body )
635                                        return( 1 );
636                               
637                                mtype = msn_findheader( body, "Type:", blen );
638                                arg1 = msn_findheader( body, "Arg1:", blen );
639                               
640                                if( mtype && strcmp( mtype, "1" ) == 0 )
641                                {
642                                        if( arg1 )
[84b045d]643                                                imcb_log( ic, "The server is going down for maintenance in %s minutes.", arg1 );
[b7d3cc34]644                                }
645                               
[ec29351]646                                g_free( arg1 );
647                                g_free( mtype );
[b7d3cc34]648                        }
649                        else if( g_strncasecmp( ct, "text/x-msmsgsprofile", 20 ) == 0 )
650                        {
651                                /* We don't care about this profile for now... */
652                        }
653                        else if( g_strncasecmp( ct, "text/x-msmsgsinitialemailnotification", 37 ) == 0 )
654                        {
[ec29351]655                                if( set_getbool( &ic->acc->set, "mail_notifications" ) )
[b7d3cc34]656                                {
[ec29351]657                                        char *inbox = msn_findheader( body, "Inbox-Unread:", blen );
658                                        char *folders = msn_findheader( body, "Folders-Unread:", blen );
659
660                                        if( inbox && folders )
661                                                imcb_log( ic, "INBOX contains %s new messages, plus %s messages in other folders.", inbox, folders );
662                                       
663                                        g_free( inbox );
664                                        g_free( folders );
[b7d3cc34]665                                }
666                        }
667                        else if( g_strncasecmp( ct, "text/x-msmsgsemailnotification", 30 ) == 0 )
668                        {
[ec29351]669                                if( set_getbool( &ic->acc->set, "mail_notifications" ) )
[b7d3cc34]670                                {
[ec29351]671                                        char *from = msn_findheader( body, "From-Addr:", blen );
672                                        char *fromname = msn_findheader( body, "From:", blen );
673                                       
674                                        if( from && fromname )
675                                                imcb_log( ic, "Received an e-mail message from %s <%s>.", fromname, from );
676
677                                        g_free( from );
678                                        g_free( fromname );
[b7d3cc34]679                                }
680                        }
681                        else if( g_strncasecmp( ct, "text/x-msmsgsactivemailnotification", 35 ) == 0 )
682                        {
683                                /* Sorry, but this one really is *USELESS* */
684                        }
685                        else
686                        {
687                                debug( "Can't handle %s packet from notification server", ct );
688                        }
689                       
690                        g_free( ct );
691                }
692        }
693       
694        return( 1 );
695}
696
[523fb23]697void msn_auth_got_passport_token( struct im_connection *ic, char *token )
[b7d3cc34]698{
[d84e2a9]699        struct msn_data *md;
[e6648bf]700       
[d84e2a9]701        /* Dead connection? */
702        if( g_slist_find( msn_connections, ic ) == NULL )
703                return;
704       
705        md = ic->proto_data;
[523fb23]706       
[b7d3cc34]707        {
[d84e2a9]708                char buf[1024];
709               
[523fb23]710                g_snprintf( buf, sizeof( buf ), "USR %d SSO S %s %s\r\n", ++md->trId, md->tokens[0], token );
[d84e2a9]711                msn_write( ic, buf, strlen( buf ) );
[b7d3cc34]712        }
713}
[e3413cc]714
[ca7de3a]715void msn_auth_got_contact_list( struct im_connection *ic )
716{
717        char buf[64];
718        struct msn_data *md;
719       
720        /* Dead connection? */
721        if( g_slist_find( msn_connections, ic ) == NULL )
722                return;
723       
724        md = ic->proto_data;
725       
726       
727        g_snprintf( buf, sizeof( buf ), "BLP %d %s\r\n", ++md->trId, "BL" );
728        msn_write( ic, buf, strlen( buf ) );
729}
730
731static gboolean msn_ns_send_adl_1( gpointer key, gpointer value, gpointer data )
732{
733        struct xt_node *adl = data, *d, *c;
734        struct bee_user *bu = value;
735        struct msn_buddy_data *bd = bu->data;
736        char handle[strlen(bu->handle)];
737        char *domain;
738        char l[4];
739       
740        strcpy( handle, bu->handle );
741        if( ( domain = strchr( handle, '@' ) ) == NULL ) /* WTF */
742                return FALSE; 
743        *domain = '\0';
744        domain ++;
745       
746        if( ( d = adl->children ) == NULL ||
747            g_strcasecmp( xt_find_attr( d, "n" ), domain ) != 0 )
748        {
749                d = xt_new_node( "d", NULL, NULL );
750                xt_add_attr( d, "n", domain );
751                xt_insert_child( adl, d );
752        }
753       
754        g_snprintf( l, sizeof( l ), "%d", bd->flags & 7 );
755        c = xt_new_node( "c", NULL, NULL );
756        xt_add_attr( c, "n", handle );
757        xt_add_attr( c, "l", l );
758        xt_add_attr( c, "t", "1" ); /* 1 means normal, 4 means mobile? */
759        xt_insert_child( d, c );
760       
761        return FALSE;
762}
763
764static void msn_ns_send_adl( struct im_connection *ic )
765{
766        struct xt_node *adl;
767        struct msn_data *md;
768        char *adls, buf[64];
769       
770        /* Dead connection? */
771        if( g_slist_find( msn_connections, ic ) == NULL )
772                return;
773       
774        md = ic->proto_data;
775       
776        adl = xt_new_node( "ml", NULL, NULL );
777        xt_add_attr( adl, "l", "1" );
778        g_tree_foreach( md->domaintree, msn_ns_send_adl_1, adl );
779        adls = xt_to_string( adl );
780       
781        g_snprintf( buf, sizeof( buf ), "ADL %d %zd\r\n", ++md->trId, strlen( adls ) );
782        if( msn_write( ic, buf, strlen( buf ) ) )
783                msn_write( ic, adls, strlen( adls ) );
784       
785        g_free( adls );
786        xt_free_node( adl );
787}
788
[e3413cc]789static gboolean msn_ns_got_display_name( struct im_connection *ic, char *name )
790{
791        set_t *s;
792       
793        if( ( s = set_find( &ic->acc->set, "display_name" ) ) == NULL )
794                return FALSE; /* Shouldn't happen.. */
795       
796        http_decode( name );
797       
798        if( s->value && strcmp( s->value, name ) == 0 )
799        {
800                return TRUE;
801                /* The names match, nothing to worry about. */
802        }
803        else if( s->value != NULL &&
804                 ( strcmp( name, ic->acc->user ) == 0 ||
805                   set_getbool( &ic->acc->set, "local_display_name" ) ) )
806        {
807                /* The server thinks our display name is our e-mail address
808                   which is probably wrong, or the user *wants* us to do this:
809                   Always use the locally set display_name. */
810                return msn_set_display_name( ic, s->value );
811        }
812        else
813        {
814                if( s->value && *s->value )
815                        imcb_log( ic, "BitlBee thinks your display name is `%s' but "
816                                      "the MSN server says it's `%s'. Using the MSN "
817                                      "server's name. Set local_display_name to true "
818                                      "to use the local name.", s->value, name );
819               
[7815a2b]820                if( g_utf8_validate( name, -1, NULL ) )
821                {
822                        g_free( s->value );
823                        s->value = g_strdup( name );
824                }
825                else
826                {
827                        imcb_log( ic, "Warning: Friendly name in server response was corrupted" );
828                }
829               
[e3413cc]830                return TRUE;
831        }
832}
Note: See TracBrowser for help on using the repository browser.