source: protocols/msn/ns.c @ bae0617

Last change on this file since bae0617 was bae0617, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-09-03T21:24:58Z

Rearrange things a bit to support multiple NS connections. This is apparently
needed for refreshing auth. tokens.

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