source: protocols/msn/ns.c @ 64768d4

Last change on this file since 64768d4 was 64768d4, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-09-02T09:15:44Z

Replace msn*write functions with saner versions that accept format strings.
Also preparing for additional temporary NS connections (auth token renewal).

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