source: protocols/msn/ns.c @ 080c43a

Last change on this file since 080c43a was 080c43a, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-09-16T13:52:19Z

Some more tweaks: Should show up online now, and be able to send and receive
messages. Seeing online state is still a problem due to the protocol#: gunk.

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