source: protocols/msn/ns.c @ e0e1546

Last change on this file since e0e1546 was e0e1546, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-15T18:46:10Z

Making display name code a bit saner. Apparently PoS MSN is still suffering
from display_name amnesia a little bit though at least with Hotmail accounts.

  • Property mode set to 100644
File size: 19.6 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
[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       
[91d6e91]76        g_snprintf( s, sizeof( s ), "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER );
[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        {
[91d6e91]116                if( cmd[2] && strncmp( cmd[2], MSNP_VER, 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                {
[84b045d]230                        imcb_log( ic, "Authenticated, getting buddy list" );
[7db65b7]231                        msn_soap_memlist_request( ic );
[b7d3cc34]232                }
233                else
234                {
[84b045d]235                        imcb_error( ic, "Unknown authentication type" );
[c2fb3809]236                        imc_logout( ic, FALSE );
[b7d3cc34]237                        return( 0 );
238                }
239        }
240        else if( strcmp( cmd[0], "MSG" ) == 0 )
241        {
[b46769d]242                if( num_parts < 4 )
[b7d3cc34]243                {
[84b045d]244                        imcb_error( ic, "Syntax error" );
[c2fb3809]245                        imc_logout( ic, TRUE );
[b7d3cc34]246                        return( 0 );
247                }
248               
249                md->handler->msglen = atoi( cmd[3] );
250               
251                if( md->handler->msglen <= 0 )
252                {
[84b045d]253                        imcb_error( ic, "Syntax error" );
[c2fb3809]254                        imc_logout( ic, TRUE );
[b7d3cc34]255                        return( 0 );
256                }
257        }
[ca7de3a]258        else if( strcmp( cmd[0], "BLP" ) == 0 )
[b7d3cc34]259        {
[5a7af1b]260                msn_ns_send_adl_start( ic );
[e0e1546]261               
262                if( md->adl_todo < 0 && !( ic->flags & OPT_LOGGED_IN ) )
263                        return msn_ns_set_display_name( ic, set_getstr( &ic->acc->set, "display_name" ) );
[b7d3cc34]264        }
[ca7de3a]265        else if( strcmp( cmd[0], "ADL" ) == 0 )
[b7d3cc34]266        {
[ca7de3a]267                if( num_parts >= 3 && strcmp( cmd[2], "OK" ) == 0 )
[b7d3cc34]268                {
[5a7af1b]269                        msn_ns_send_adl( ic );
[1ad104a]270                       
[5a7af1b]271                        if( md->adl_todo < 0 && !( ic->flags & OPT_LOGGED_IN ) )
272                        {
[e0e1546]273                                msn_ns_send_adl( ic );
[5a7af1b]274                               
[e0e1546]275                                if( md->adl_todo < 0 && !( ic->flags & OPT_LOGGED_IN ) )
276                                        return msn_ns_set_display_name( ic, set_getstr( &ic->acc->set, "display_name" ) );
[5a7af1b]277                        }
[b7d3cc34]278                }
[e5854a8]279                else if( num_parts >= 3 )
280                {
281                        md->handler->msglen = atoi( cmd[2] );
282                }
[b7d3cc34]283        }
[ca7de3a]284        else if( strcmp( cmd[0], "PRP" ) == 0 )
[b7d3cc34]285        {
[ca7de3a]286                imcb_connected( ic );
[b7d3cc34]287        }
288        else if( strcmp( cmd[0], "CHL" ) == 0 )
289        {
[be7a180]290                char *resp;
[b7d3cc34]291               
[be7a180]292                if( num_parts < 3 )
[b7d3cc34]293                {
[84b045d]294                        imcb_error( ic, "Syntax error" );
[c2fb3809]295                        imc_logout( ic, TRUE );
[b7d3cc34]296                        return( 0 );
297                }
298               
[be7a180]299                resp = msn_p11_challenge( cmd[2] );
300                g_snprintf( buf, sizeof( buf ), "QRY %d %s %zd\r\n%s",
301                            ++md->trId, MSNP11_PROD_ID,
302                            strlen( resp ), resp );
303                g_free( resp );
[b7d3cc34]304               
[0da65d5]305                return( msn_write( ic, buf, strlen( buf ) ) );
[b7d3cc34]306        }
307        else if( strcmp( cmd[0], "ILN" ) == 0 )
308        {
[08995b0]309                const struct msn_away_state *st;
[b7d3cc34]310               
[ca7de3a]311                if( num_parts < 6 )
[b7d3cc34]312                {
[84b045d]313                        imcb_error( ic, "Syntax error" );
[c2fb3809]314                        imc_logout( ic, TRUE );
[b7d3cc34]315                        return( 0 );
316                }
317               
[ca7de3a]318                http_decode( cmd[5] );
319                imcb_rename_buddy( ic, cmd[3], cmd[5] );
[b7d3cc34]320               
321                st = msn_away_state_by_code( cmd[2] );
322                if( !st )
323                {
324                        /* FIXME: Warn/Bomb about unknown away state? */
[b051d39]325                        st = msn_away_state_list + 1;
[b7d3cc34]326                }
327               
[b051d39]328                imcb_buddy_status( ic, cmd[3], OPT_LOGGED_IN | 
329                                   ( st != msn_away_state_list ? OPT_AWAY : 0 ),
330                                   st->name, NULL );
[b7d3cc34]331        }
332        else if( strcmp( cmd[0], "FLN" ) == 0 )
333        {
[9bf2481]334                if( cmd[1] == NULL )
335                        return 1;
336               
337                imcb_buddy_status( ic, cmd[1], 0, NULL, NULL );
338               
[bb839e8]339                msn_sb_start_keepalives( msn_sb_by_handle( ic, cmd[1] ), TRUE );
[b7d3cc34]340        }
341        else if( strcmp( cmd[0], "NLN" ) == 0 )
342        {
[08995b0]343                const struct msn_away_state *st;
[b7d3cc34]344               
[b46769d]345                if( num_parts < 5 )
[b7d3cc34]346                {
[84b045d]347                        imcb_error( ic, "Syntax error" );
[c2fb3809]348                        imc_logout( ic, TRUE );
[b7d3cc34]349                        return( 0 );
350                }
351               
[5848675]352                http_decode( cmd[4] );
353                imcb_rename_buddy( ic, cmd[2], cmd[4] );
[b7d3cc34]354               
355                st = msn_away_state_by_code( cmd[1] );
356                if( !st )
357                {
358                        /* FIXME: Warn/Bomb about unknown away state? */
[b051d39]359                        st = msn_away_state_list + 1;
[b7d3cc34]360                }
361               
[b051d39]362                imcb_buddy_status( ic, cmd[2], OPT_LOGGED_IN | 
363                                   ( st != msn_away_state_list ? OPT_AWAY : 0 ),
364                                   st->name, NULL );
[9bf2481]365               
[bb839e8]366                msn_sb_stop_keepalives( msn_sb_by_handle( ic, cmd[2] ) );
[b7d3cc34]367        }
368        else if( strcmp( cmd[0], "RNG" ) == 0 )
369        {
370                struct msn_switchboard *sb;
371                char *server;
372                int session, port;
373               
[b46769d]374                if( num_parts < 7 )
[b7d3cc34]375                {
[84b045d]376                        imcb_error( ic, "Syntax error" );
[c2fb3809]377                        imc_logout( ic, TRUE );
[b7d3cc34]378                        return( 0 );
379                }
380               
381                session = atoi( cmd[1] );
382               
383                server = strchr( cmd[2], ':' );
384                if( !server )
385                {
[84b045d]386                        imcb_error( ic, "Syntax error" );
[c2fb3809]387                        imc_logout( ic, TRUE );
[b7d3cc34]388                        return( 0 );
389                }
390                *server = 0;
391                port = atoi( server + 1 );
392                server = cmd[2];
393               
394                if( strcmp( cmd[3], "CKI" ) != 0 )
395                {
[84b045d]396                        imcb_error( ic, "Unknown authentication method for switchboard" );
[c2fb3809]397                        imc_logout( ic, TRUE );
[b7d3cc34]398                        return( 0 );
399                }
400               
401                debug( "Got a call from %s (session %d). Key = %s", cmd[5], session, cmd[4] );
402               
[99f929c]403                if( ( sb = msn_sb_create( ic, server, port, cmd[4], session ) ) == NULL )
404                {
405                        /* Although this isn't strictly fatal for the NS connection, it's
406                           definitely something serious (we ran out of file descriptors?). */
407                        imcb_error( ic, "Could not create new switchboard" );
408                        imc_logout( ic, TRUE );
409                        return( 0 );
410                }
411                else
412                {
413                        sb->who = g_strdup( cmd[5] );
414                }
[b7d3cc34]415        }
416        else if( strcmp( cmd[0], "OUT" ) == 0 )
417        {
[c2fb3809]418                int allow_reconnect = TRUE;
419               
[b7d3cc34]420                if( cmd[1] && strcmp( cmd[1], "OTH" ) == 0 )
421                {
[84b045d]422                        imcb_error( ic, "Someone else logged in with your account" );
[c2fb3809]423                        allow_reconnect = FALSE;
[b7d3cc34]424                }
425                else if( cmd[1] && strcmp( cmd[1], "SSD" ) == 0 )
426                {
[84b045d]427                        imcb_error( ic, "Terminating session because of server shutdown" );
[b7d3cc34]428                }
429                else
430                {
[84b045d]431                        imcb_error( ic, "Session terminated by remote server (reason unknown)" );
[b7d3cc34]432                }
433               
[c2fb3809]434                imc_logout( ic, allow_reconnect );
[b7d3cc34]435                return( 0 );
436        }
437        else if( strcmp( cmd[0], "IPG" ) == 0 )
438        {
[84b045d]439                imcb_error( ic, "Received IPG command, we don't handle them yet." );
[b7d3cc34]440               
441                md->handler->msglen = atoi( cmd[1] );
442               
443                if( md->handler->msglen <= 0 )
444                {
[84b045d]445                        imcb_error( ic, "Syntax error" );
[c2fb3809]446                        imc_logout( ic, TRUE );
[b7d3cc34]447                        return( 0 );
448                }
449        }
[e5854a8]450#if 0
[70ac477]451        else if( strcmp( cmd[0], "ADG" ) == 0 )
452        {
453                char *group = g_strdup( cmd[3] );
454                int groupnum, i;
455                GSList *l, *next;
456               
457                http_decode( group );
458                if( sscanf( cmd[4], "%d", &groupnum ) == 1 )
459                {
460                        if( groupnum >= md->groupcount )
461                        {
462                                md->grouplist = g_renew( char *, md->grouplist, groupnum + 1 );
463                                for( i = md->groupcount; i <= groupnum; i ++ )
464                                        md->grouplist[i] = NULL;
465                                md->groupcount = groupnum + 1;
466                        }
467                        g_free( md->grouplist[groupnum] );
468                        md->grouplist[groupnum] = group;
469                }
470                else
471                {
472                        /* Shouldn't happen, but if it does, give up on the group. */
473                        g_free( group );
474                        imcb_error( ic, "Syntax error" );
475                        imc_logout( ic, TRUE );
476                        return 0;
477                }
478               
479                for( l = md->grpq; l; l = next )
480                {
481                        struct msn_groupadd *ga = l->data;
482                        next = l->next;
483                        if( g_strcasecmp( ga->group, group ) == 0 )
484                        {
485                                if( !msn_buddy_list_add( ic, "FL", ga->who, ga->who, group ) )
486                                        return 0;
487                               
488                                g_free( ga->group );
489                                g_free( ga->who );
490                                g_free( ga );
491                                md->grpq = g_slist_remove( md->grpq, ga );
492                        }
493                }
494        }
[e5854a8]495#endif
[5fecede]496        else if( strcmp( cmd[0], "GCF" ) == 0 )
497        {
498                /* Coming up is cmd[2] bytes of stuff we're supposed to
499                   censore. Meh. */
500                md->handler->msglen = atoi( cmd[2] );
501        }
[be7a180]502        else if( strcmp( cmd[0], "UBX" ) == 0 )
503        {
[e5854a8]504                /* Status message. */
[be7a180]505                if( num_parts >= 4 )
506                        md->handler->msglen = atoi( cmd[3] );
507        }
[d93c0eb9]508        else if( strcmp( cmd[0], "NOT" ) == 0 )
509        {
[e5854a8]510                /* Some kind of notification, poorly documented but
511                   apparently used to announce address book changes. */
512                if( num_parts >= 2 )
513                        md->handler->msglen = atoi( cmd[1] );
[d93c0eb9]514        }
[b7d3cc34]515        else if( isdigit( cmd[0][0] ) )
516        {
517                int num = atoi( cmd[0] );
[08995b0]518                const struct msn_status_code *err = msn_status_by_number( num );
[b7d3cc34]519               
[84b045d]520                imcb_error( ic, "Error reported by MSN server: %s", err->text );
[b7d3cc34]521               
522                if( err->flags & STATUS_FATAL )
523                {
[c2fb3809]524                        imc_logout( ic, TRUE );
[b7d3cc34]525                        return( 0 );
526                }
527        }
528        else
529        {
[8ff0a61]530                /* debug( "Received unknown command from main server: %s", cmd[0] ); */
[b7d3cc34]531        }
532       
533        return( 1 );
534}
535
536static int msn_ns_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts )
537{
[0da65d5]538        struct im_connection *ic = data;
[b7d3cc34]539        char *body;
540        int blen = 0;
541       
542        if( !num_parts )
543                return( 1 );
544       
545        if( ( body = strstr( msg, "\r\n\r\n" ) ) )
546        {
547                body += 4;
548                blen = msglen - ( body - msg );
549        }
550       
551        if( strcmp( cmd[0], "MSG" ) == 0 )
552        {
553                if( g_strcasecmp( cmd[1], "Hotmail" ) == 0 )
554                {
555                        char *ct = msn_findheader( msg, "Content-Type:", msglen );
556                       
557                        if( !ct )
558                                return( 1 );
559                       
560                        if( g_strncasecmp( ct, "application/x-msmsgssystemmessage", 33 ) == 0 )
561                        {
562                                char *mtype;
563                                char *arg1;
564                               
565                                if( !body )
566                                        return( 1 );
567                               
568                                mtype = msn_findheader( body, "Type:", blen );
569                                arg1 = msn_findheader( body, "Arg1:", blen );
570                               
571                                if( mtype && strcmp( mtype, "1" ) == 0 )
572                                {
573                                        if( arg1 )
[84b045d]574                                                imcb_log( ic, "The server is going down for maintenance in %s minutes.", arg1 );
[b7d3cc34]575                                }
576                               
[ec29351]577                                g_free( arg1 );
578                                g_free( mtype );
[b7d3cc34]579                        }
580                        else if( g_strncasecmp( ct, "text/x-msmsgsprofile", 20 ) == 0 )
581                        {
582                                /* We don't care about this profile for now... */
583                        }
584                        else if( g_strncasecmp( ct, "text/x-msmsgsinitialemailnotification", 37 ) == 0 )
585                        {
[ec29351]586                                if( set_getbool( &ic->acc->set, "mail_notifications" ) )
[b7d3cc34]587                                {
[ec29351]588                                        char *inbox = msn_findheader( body, "Inbox-Unread:", blen );
589                                        char *folders = msn_findheader( body, "Folders-Unread:", blen );
590
591                                        if( inbox && folders )
592                                                imcb_log( ic, "INBOX contains %s new messages, plus %s messages in other folders.", inbox, folders );
593                                       
594                                        g_free( inbox );
595                                        g_free( folders );
[b7d3cc34]596                                }
597                        }
598                        else if( g_strncasecmp( ct, "text/x-msmsgsemailnotification", 30 ) == 0 )
599                        {
[ec29351]600                                if( set_getbool( &ic->acc->set, "mail_notifications" ) )
[b7d3cc34]601                                {
[ec29351]602                                        char *from = msn_findheader( body, "From-Addr:", blen );
603                                        char *fromname = msn_findheader( body, "From:", blen );
604                                       
605                                        if( from && fromname )
606                                                imcb_log( ic, "Received an e-mail message from %s <%s>.", fromname, from );
607
608                                        g_free( from );
609                                        g_free( fromname );
[b7d3cc34]610                                }
611                        }
612                        else if( g_strncasecmp( ct, "text/x-msmsgsactivemailnotification", 35 ) == 0 )
613                        {
614                                /* Sorry, but this one really is *USELESS* */
615                        }
616                        else
617                        {
618                                debug( "Can't handle %s packet from notification server", ct );
619                        }
620                       
621                        g_free( ct );
622                }
623        }
[d93c0eb9]624        else if( strcmp( cmd[0], "UBX" ) == 0 )
625        {
626                struct xt_node *psm;
627                char *psm_text = NULL;
628               
629                psm = xt_from_string( msg );
630                if( psm && strcmp( psm->name, "Data" ) == 0 &&
631                    ( psm = xt_find_node( psm->children, "PSM" ) ) )
632                        psm_text = psm->text;
633               
634                imcb_buddy_status_msg( ic, cmd[1], psm_text );
635                xt_free_node( psm );
636        }
[e5854a8]637        else if( strcmp( cmd[0], "ADL" ) == 0 )
638        {
639                struct xt_node *adl, *d, *c;
640               
641                if( !( adl = xt_from_string( msg ) ) )
642                        return 1;
643               
644                for( d = adl->children; d; d = d->next )
645                {
646                        char *dn;
647                        if( strcmp( d->name, "d" ) != 0 ||
648                            ( dn = xt_find_attr( d, "n" ) ) == NULL )
649                                continue;
650                        for( c = d->children; c; c = c->next )
651                        {
652                                bee_user_t *bu;
653                                struct msn_buddy_data *bd;
654                                char *cn, *handle, *f, *l;
655                                int flags;
656                               
657                                if( strcmp( c->name, "c" ) != 0 ||
658                                    ( l = xt_find_attr( c, "l" ) ) == NULL ||
659                                    ( cn = xt_find_attr( c, "n" ) ) == NULL )
660                                        continue;
661                               
662                                handle = g_strdup_printf( "%s@%s", cn, dn );
663                                if( !( ( bu = bee_user_by_handle( ic->bee, ic, handle ) ) ||
664                                       ( bu = bee_user_new( ic->bee, ic, handle, 0 ) ) ) )
665                                {
666                                        g_free( handle );
667                                        continue;
668                                }
669                                g_free( handle );
670                                bd = bu->data;
671                               
672                                if( ( f = xt_find_attr( c, "f" ) ) )
673                                {
674                                        http_decode( f );
675                                        imcb_rename_buddy( ic, bu->handle, f );
676                                }
677                               
678                                flags = atoi( l ) & 15;
679                                if( bd->flags != flags )
680                                {
681                                        bd->flags = flags;
682                                        msn_buddy_ask( bu );
683                                }
684                        }
685                }
686        }
[b7d3cc34]687       
688        return( 1 );
689}
690
[660cb00]691void msn_auth_got_passport_token( struct im_connection *ic, const char *token, const char *error )
[b7d3cc34]692{
[d84e2a9]693        struct msn_data *md;
[e6648bf]694       
[d84e2a9]695        /* Dead connection? */
696        if( g_slist_find( msn_connections, ic ) == NULL )
697                return;
698       
699        md = ic->proto_data;
[523fb23]700       
[660cb00]701        if( token )
[b7d3cc34]702        {
[d97f51b]703                char buf[1536];
[d84e2a9]704               
[523fb23]705                g_snprintf( buf, sizeof( buf ), "USR %d SSO S %s %s\r\n", ++md->trId, md->tokens[0], token );
[d84e2a9]706                msn_write( ic, buf, strlen( buf ) );
[b7d3cc34]707        }
[660cb00]708        else
709        {
710                imcb_error( ic, "Error during Passport authentication: %s", error );
711                imc_logout( ic, TRUE );
712        }
[b7d3cc34]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;
[5a7af1b]736        struct msn_data *md = bu->ic->proto_data;
[ca7de3a]737        char handle[strlen(bu->handle)];
738        char *domain;
739        char l[4];
740       
[5a7af1b]741        if( ( bd->flags & 7 ) == 0 || ( bd->flags & MSN_BUDDY_ADL_SYNCED ) )
[e5854a8]742                return FALSE;
743       
[ca7de3a]744        strcpy( handle, bu->handle );
745        if( ( domain = strchr( handle, '@' ) ) == NULL ) /* WTF */
746                return FALSE; 
747        *domain = '\0';
748        domain ++;
749       
750        if( ( d = adl->children ) == NULL ||
751            g_strcasecmp( xt_find_attr( d, "n" ), domain ) != 0 )
752        {
753                d = xt_new_node( "d", NULL, NULL );
754                xt_add_attr( d, "n", domain );
755                xt_insert_child( adl, d );
756        }
757       
758        g_snprintf( l, sizeof( l ), "%d", bd->flags & 7 );
759        c = xt_new_node( "c", NULL, NULL );
760        xt_add_attr( c, "n", handle );
761        xt_add_attr( c, "l", l );
762        xt_add_attr( c, "t", "1" ); /* 1 means normal, 4 means mobile? */
763        xt_insert_child( d, c );
764       
[5a7af1b]765        /* Do this in batches of 100. */
766        bd->flags |= MSN_BUDDY_ADL_SYNCED;
767        return (--md->adl_todo % 140) == 0;
[ca7de3a]768}
769
770static void msn_ns_send_adl( struct im_connection *ic )
771{
772        struct xt_node *adl;
[5a7af1b]773        struct msn_data *md = ic->proto_data;
[ca7de3a]774        char *adls, buf[64];
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 );
[5a7af1b]779        if( adl->children == NULL )
780        {
781                /* This tells the caller that we're done now. */
782                md->adl_todo = -1;
783                xt_free_node( adl );
784                return;
785        }
[ca7de3a]786        adls = xt_to_string( adl );
787       
788        g_snprintf( buf, sizeof( buf ), "ADL %d %zd\r\n", ++md->trId, strlen( adls ) );
789        if( msn_write( ic, buf, strlen( buf ) ) )
790                msn_write( ic, adls, strlen( adls ) );
791       
792        g_free( adls );
[5a7af1b]793}
794
795static void msn_ns_send_adl_start( struct im_connection *ic )
796{
797        struct msn_data *md;
798        GSList *l;
799       
800        /* Dead connection? */
801        if( g_slist_find( msn_connections, ic ) == NULL )
802                return;
803       
804        md = ic->proto_data;
805        md->adl_todo = 0;
806        for( l = ic->bee->users; l; l = l->next )
807        {
808                bee_user_t *bu = l->data;
809                struct msn_buddy_data *bd = bu->data;
810               
811                if( bu->ic != ic || ( bd->flags & 7 ) == 0 )
812                        continue;
813               
814                bd->flags &= ~MSN_BUDDY_ADL_SYNCED;
815                md->adl_todo++;
816        }
817       
818        msn_ns_send_adl( ic );
[ca7de3a]819}
Note: See TracBrowser for help on using the repository browser.