source: protocols/msn/ns.c @ 5a7af1b

Last change on this file since 5a7af1b was 5a7af1b, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-15T13:02:52Z

The ADL command doesn't support >150 contacts. Split it up.

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