source: protocols/msn/ns.c @ eb54f56

Last change on this file since eb54f56 was eb54f56, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-02-11T13:01:20Z

Add missing newslines to debugging output. Bug #896.

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