source: protocols/msn/soap.c @ 27053b5

Last change on this file since 27053b5 was 27053b5, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-09-04T17:13:55Z

Finish re-authentication works. Should now work for OIMs as well.

  • Property mode set to 100644
File size: 28.5 KB
RevLine 
[4e4af1b]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2010 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* MSN module - All the SOAPy XML stuff.
8   Some manager at Microsoft apparently thought MSNP wasn't XMLy enough so
9   someone stepped up and changed that. This is the result. Kilobytes and
10   more kilobytes of XML vomit to transfer tiny bits of informaiton. */
11
12/*
13  This program is free software; you can redistribute it and/or modify
14  it under the terms of the GNU General Public License as published by
15  the Free Software Foundation; either version 2 of the License, or
16  (at your option) any later version.
17
18  This program is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  GNU General Public License for more details.
22
23  You should have received a copy of the GNU General Public License with
24  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
25  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
26  Suite 330, Boston, MA  02111-1307  USA
27*/
[e5a8118]28
29#include "http_client.h"
30#include "soap.h"
31#include "msn.h"
32#include "bitlbee.h"
33#include "url.h"
34#include "misc.h"
[523fb23]35#include "sha1.h"
[e5a8118]36#include "base64.h"
37#include "xmltree.h"
38#include <ctype.h>
39#include <errno.h>
40
[4e4af1b]41/* This file tries to make SOAP stuff pretty simple to do by letting you just
42   provide a function to build a request, a few functions to parse various
43   parts of the response, and a function to run when the full response was
44   received and parsed. See the various examples below. */
45
[e5a8118]46typedef enum
47{
48        MSN_SOAP_OK,
49        MSN_SOAP_RETRY,
[27053b5]50        MSN_SOAP_REAUTH,
[e5a8118]51        MSN_SOAP_ABORT,
52} msn_soap_result_t;
53
[12767e3]54struct msn_soap_req_data;
55typedef int (*msn_soap_func) ( struct msn_soap_req_data * );
56
[e5a8118]57struct msn_soap_req_data
58{
59        void *data;
60        struct im_connection *ic;
61        int ttl;
62       
63        char *url, *action, *payload;
64        struct http_request *http_req;
65       
66        const struct xt_handler_entry *xml_parser;
67        msn_soap_func build_request, handle_response, free_data;
68};
69
70static int msn_soap_send_request( struct msn_soap_req_data *req );
[f2520b5]71static void msn_soap_debug_print( const char *headers, const char *payload );
[e5a8118]72
73static int msn_soap_start( struct im_connection *ic,
74                    void *data,
75                    msn_soap_func build_request,
76                    const struct xt_handler_entry *xml_parser,
77                    msn_soap_func handle_response,
78                    msn_soap_func free_data )
79{
80        struct msn_soap_req_data *req = g_new0( struct msn_soap_req_data, 1 );
81       
82        req->ic = ic;
83        req->data = data;
84        req->xml_parser = xml_parser;
85        req->build_request = build_request;
86        req->handle_response = handle_response;
87        req->free_data = free_data;
88        req->ttl = 3;
89       
90        return msn_soap_send_request( req );
91}
92
93static void msn_soap_handle_response( struct http_request *http_req );
94
95static int msn_soap_send_request( struct msn_soap_req_data *soap_req )
96{
97        char *http_req;
[523fb23]98        char *soap_action = NULL;
[e5a8118]99        url_t url;
100       
101        soap_req->build_request( soap_req );
102       
[523fb23]103        if( soap_req->action )
104                soap_action = g_strdup_printf( "SOAPAction: \"%s\"\r\n", soap_req->action );
[7db65b7]105       
[e5a8118]106        url_set( &url, soap_req->url );
107        http_req = g_strdup_printf( SOAP_HTTP_REQUEST, url.file, url.host,
[523fb23]108                soap_action ? soap_action : "",
[7db65b7]109                strlen( soap_req->payload ), soap_req->payload );
[e5a8118]110       
[f2520b5]111        msn_soap_debug_print( http_req, soap_req->payload );
112       
[e5a8118]113        soap_req->http_req = http_dorequest( url.host, url.port, url.proto == PROTO_HTTPS,
114                http_req, msn_soap_handle_response, soap_req );
115       
[ffb6dea]116        g_free( http_req );
[523fb23]117        g_free( soap_action );
[ffb6dea]118       
[e5a8118]119        return soap_req->http_req != NULL;
120}
121
122static void msn_soap_handle_response( struct http_request *http_req )
123{
124        struct msn_soap_req_data *soap_req = http_req->data;
125        int st;
126       
[801b90b]127        if( g_slist_find( msn_connections, soap_req->ic ) == NULL )
128        {
129                soap_req->free_data( soap_req );
130                g_free( soap_req->url );
131                g_free( soap_req->action );
132                g_free( soap_req->payload );
133                g_free( soap_req );
134                return;
135        }
136       
[27053b5]137        msn_soap_debug_print( http_req->reply_headers, http_req->reply_body );
138       
[e5a8118]139        if( http_req->body_size > 0 )
140        {
141                struct xt_parser *parser;
[4aa8a04]142                struct xt_node *err;
[e5a8118]143               
144                parser = xt_new( soap_req->xml_parser, soap_req );
145                xt_feed( parser, http_req->reply_body, http_req->body_size );
[4aa8a04]146                if( http_req->status_code == 500 &&
147                    ( err = xt_find_path( parser->root, "soap:Body/soap:Fault/detail/errorcode" ) ) &&
[27053b5]148                    err->text_len > 0 )
[4aa8a04]149                {
[27053b5]150                        if( strcmp( err->text, "PassportAuthFail" ) == 0 )
151                        {
152                                xt_free( parser );
153                                st = MSN_SOAP_REAUTH;
154                                goto fail;
155                        }
156                        /* TODO: Handle/report other errors. */
[4aa8a04]157                }
158               
[e5a8118]159                xt_handle( parser, NULL, -1 );
160                xt_free( parser );
161        }
162       
163        st = soap_req->handle_response( soap_req );
[27053b5]164
165fail:   
[ffb6dea]166        g_free( soap_req->url );
167        g_free( soap_req->action );
168        g_free( soap_req->payload );
169        soap_req->url = soap_req->action = soap_req->payload = NULL;
170       
[e5a8118]171        if( st == MSN_SOAP_RETRY && --soap_req->ttl )
[27053b5]172        {
[e5a8118]173                msn_soap_send_request( soap_req );
[27053b5]174        }
175        else if( st == MSN_SOAP_REAUTH )
176        {
177                struct msn_data *md = soap_req->ic->proto_data;
178               
179                if( !( md->flags & MSN_REAUTHING ) )
180                {
181                        /* Nonce shouldn't actually be touched for re-auths. */
182                        msn_soap_passport_sso_request( soap_req->ic, "blaataap" );
183                        md->flags |= MSN_REAUTHING; 
184                }
185                md->soapq = g_slist_append( md->soapq, soap_req );
186        }
[e5a8118]187        else
188        {
189                soap_req->free_data( soap_req );
190                g_free( soap_req );
191        }
192}
193
[6ddb223]194static char *msn_soap_abservice_build( const char *body_fmt, const char *scenario, const char *ticket, ... )
195{
196        va_list params;
197        char *ret, *format, *body;
198       
199        format = g_markup_printf_escaped( SOAP_ABSERVICE_PAYLOAD, scenario, ticket );
200       
201        va_start( params, ticket );
202        body = g_strdup_vprintf( body_fmt, params );
203        va_end( params );
204       
205        ret = g_strdup_printf( format, body );
206        g_free( body );
207        g_free( format );
208       
209        return ret;
210}
211
[f2520b5]212static void msn_soap_debug_print( const char *headers, const char *payload )
213{
214        char *s;
215       
216        if( !getenv( "BITLBEE_DEBUG" ) )
217                return;
218       
219        if( ( s = strstr( headers, "\r\n\r\n" ) ) )
220                write( 1, s, s - headers + 4 );
221        else
222                write( 1, headers, strlen( headers ) );
223       
224#ifdef DEBUG
225        {
226                struct xt_node *xt = xt_from_string( payload );
227                if( xt )
228                        xt_print( xt );
229                xt_free_node( xt );
230        }
231#endif
232}
233
[4aa8a04]234static int msn_soapq_empty( struct im_connection *ic )
235{
236        struct msn_data *md = ic->proto_data;
237       
238        while( md->soapq )
239        {
240                msn_soap_send_request( (struct msn_soap_req_data*) md->soapq->data );
241                md->soapq = g_slist_remove( md->soapq, md->soapq->data );
242        }
243       
244        return MSN_SOAP_OK;
245}
246
[bc090f0]247
[523fb23]248/* passport_sso: Authentication MSNP15+ */
249
250struct msn_soap_passport_sso_data
251{
252        char *nonce;
253        char *secret;
[660cb00]254        char *error;
[523fb23]255};
256
257static int msn_soap_passport_sso_build_request( struct msn_soap_req_data *soap_req )
258{
259        struct im_connection *ic = soap_req->ic;
[4aa8a04]260        struct msn_data *md = ic->proto_data;
[523fb23]261       
[73efe3a]262        if( g_str_has_suffix( ic->acc->user, "@msn.com" ) )
263                soap_req->url = g_strdup( SOAP_PASSPORT_SSO_URL_MSN );
264        else
265                soap_req->url = g_strdup( SOAP_PASSPORT_SSO_URL );
266       
[523fb23]267        soap_req->payload = g_markup_printf_escaped( SOAP_PASSPORT_SSO_PAYLOAD,
[4aa8a04]268                ic->acc->user, ic->acc->pass, md->pp_policy );
[523fb23]269       
270        return MSN_SOAP_OK;
271}
272
273static xt_status msn_soap_passport_sso_token( struct xt_node *node, gpointer data )
274{
275        struct msn_soap_req_data *soap_req = data;
276        struct msn_soap_passport_sso_data *sd = soap_req->data;
277        struct msn_data *md = soap_req->ic->proto_data;
278        struct xt_node *p;
279        char *id;
280       
281        if( ( id = xt_find_attr( node, "Id" ) ) == NULL )
282                return XT_HANDLED;
283        id += strlen( id ) - 1;
284        if( *id == '1' &&
[d97f51b]285            ( p = xt_find_path( node, "../../wst:RequestedProofToken/wst:BinarySecret" ) ) &&
[523fb23]286            p->text )
287                sd->secret = g_strdup( p->text );
288       
[91d6e91]289        *id -= '1';
[80175a1]290        if( *id >= 0 && *id < sizeof( md->tokens ) / sizeof( md->tokens[0] ) )
[91d6e91]291        {
292                g_free( md->tokens[(int)*id] );
293                md->tokens[(int)*id] = g_strdup( node->text );
294        }
[523fb23]295       
296        return XT_HANDLED;
297}
298
[660cb00]299static xt_status msn_soap_passport_failure( struct xt_node *node, gpointer data )
300{
301        struct msn_soap_req_data *soap_req = data;
302        struct msn_soap_passport_sso_data *sd = soap_req->data;
303        struct xt_node *code = xt_find_node( node->children, "faultcode" );
304        struct xt_node *string = xt_find_node( node->children, "faultstring" );
305       
306        if( code == NULL || code->text_len == 0 )
307                sd->error = g_strdup( "Unknown error" );
308        else
309                sd->error = g_strdup_printf( "%s (%s)", code->text, string && string->text_len ?
310                                             string->text : "no description available" );
311       
312        return XT_HANDLED;
313}
314
[523fb23]315static const struct xt_handler_entry msn_soap_passport_sso_parser[] = {
316        { "wsse:BinarySecurityToken", "wst:RequestedSecurityToken", msn_soap_passport_sso_token },
[660cb00]317        { "S:Fault", "S:Envelope", msn_soap_passport_failure },
[523fb23]318        { NULL, NULL, NULL }
319};
320
321static char *msn_key_fuckery( char *key, int key_len, char *type )
322{
323        unsigned char hash1[20+strlen(type)+1];
324        unsigned char hash2[20];
325        char *ret;
326       
327        sha1_hmac( key, key_len, type, 0, hash1 );
328        strcpy( (char*) hash1 + 20, type );
329        sha1_hmac( key, key_len, (char*) hash1, sizeof( hash1 ) - 1, hash2 );
330       
331        /* This is okay as hash1 is read completely before it's overwritten. */
332        sha1_hmac( key, key_len, (char*) hash1, 20, hash1 );
333        sha1_hmac( key, key_len, (char*) hash1, sizeof( hash1 ) - 1, hash1 );
334       
335        ret = g_malloc( 24 );
336        memcpy( ret, hash2, 20 );
337        memcpy( ret + 20, hash1, 4 );
338        return ret;
339}
340
341static int msn_soap_passport_sso_handle_response( struct msn_soap_req_data *soap_req )
342{
343        struct msn_soap_passport_sso_data *sd = soap_req->data;
344        struct im_connection *ic = soap_req->ic;
[4aa8a04]345        struct msn_data *md = ic->proto_data;
[523fb23]346        char *key1, *key2, *key3, *blurb64;
347        int key1_len;
348        unsigned char *padnonce, *des3res;
349        struct
350        {
351                unsigned int uStructHeaderSize; // 28. Does not count data
352                unsigned int uCryptMode; // CRYPT_MODE_CBC (1)
353                unsigned int uCipherType; // TripleDES (0x6603)
354                unsigned int uHashType; // SHA1 (0x8004)
355                unsigned int uIVLen;    // 8
356                unsigned int uHashLen;  // 20
357                unsigned int uCipherLen; // 72
358                unsigned char iv[8];
359                unsigned char hash[20];
360                unsigned char cipherbytes[72];
361        } blurb = {
362                GUINT32_TO_LE( 28 ),
363                GUINT32_TO_LE( 1 ),
364                GUINT32_TO_LE( 0x6603 ),
365                GUINT32_TO_LE( 0x8004 ),
366                GUINT32_TO_LE( 8 ),
367                GUINT32_TO_LE( 20 ),
368                GUINT32_TO_LE( 72 ),
369        };
[660cb00]370       
[4aa8a04]371        if( md->soapq )
372                return msn_soapq_empty( ic );
373       
[660cb00]374        if( sd->secret == NULL )
375        {
376                msn_auth_got_passport_token( ic, NULL, sd->error );
377                return MSN_SOAP_OK;
378        }
[523fb23]379
380        key1_len = base64_decode( sd->secret, (unsigned char**) &key1 );
381       
382        key2 = msn_key_fuckery( key1, key1_len, "WS-SecureConversationSESSION KEY HASH" );
383        key3 = msn_key_fuckery( key1, key1_len, "WS-SecureConversationSESSION KEY ENCRYPTION" );
384       
385        sha1_hmac( key2, 24, sd->nonce, 0, blurb.hash );
386        padnonce = g_malloc( strlen( sd->nonce ) + 8 );
387        strcpy( (char*) padnonce, sd->nonce );
388        memset( padnonce + strlen( sd->nonce ), 8, 8 );
389       
390        random_bytes( blurb.iv, 8 );
391       
392        ssl_des3_encrypt( (unsigned char*) key3, 24, padnonce, strlen( sd->nonce ) + 8, blurb.iv, &des3res );
393        memcpy( blurb.cipherbytes, des3res, 72 );
394       
395        blurb64 = base64_encode( (unsigned char*) &blurb, sizeof( blurb ) );
[660cb00]396        msn_auth_got_passport_token( ic, blurb64, NULL );
[523fb23]397       
398        g_free( padnonce );
399        g_free( blurb64 );
400        g_free( des3res );
401        g_free( key1 );
402        g_free( key2 );
403        g_free( key3 );
404       
405        return MSN_SOAP_OK;
406}
407
408static int msn_soap_passport_sso_free_data( struct msn_soap_req_data *soap_req )
409{
410        struct msn_soap_passport_sso_data *sd = soap_req->data;
411       
412        g_free( sd->nonce );
413        g_free( sd->secret );
[660cb00]414        g_free( sd->error );
[523fb23]415       
416        return MSN_SOAP_OK;
417}
418
[4aa8a04]419int msn_soap_passport_sso_request( struct im_connection *ic, const char *nonce )
[523fb23]420{
421        struct msn_soap_passport_sso_data *sd = g_new0( struct msn_soap_passport_sso_data, 1 );
422       
423        sd->nonce = g_strdup( nonce );
424       
425        return msn_soap_start( ic, sd, msn_soap_passport_sso_build_request,
426                                       msn_soap_passport_sso_parser,
427                                       msn_soap_passport_sso_handle_response,
428                                       msn_soap_passport_sso_free_data );
429}
430
431
[bc090f0]432/* oim_send: Sending offline messages */
433
434struct msn_soap_oim_send_data
435{
436        char *to;
437        char *msg;
438        int number;
[27053b5]439        msn_soap_result_t need_retry;
[bc090f0]440};
441
[e5a8118]442static int msn_soap_oim_build_request( struct msn_soap_req_data *soap_req )
443{
444        struct msn_soap_oim_send_data *oim = soap_req->data;
445        struct im_connection *ic = soap_req->ic;
446        struct msn_data *md = ic->proto_data;
447        char *display_name_b64;
448       
[91d6e91]449        display_name_b64 = tobase64( set_getstr( &ic->acc->set, "display_name" ) );
[e5a8118]450       
451        soap_req->url = g_strdup( SOAP_OIM_SEND_URL );
[7db65b7]452        soap_req->action = g_strdup( SOAP_OIM_SEND_ACTION );
[e5a8118]453        soap_req->payload = g_markup_printf_escaped( SOAP_OIM_SEND_PAYLOAD,
[91d6e91]454                ic->acc->user, display_name_b64, MSNP_VER, MSNP_BUILD,
455                oim->to, md->tokens[2],
[5fecede]456                MSNP11_PROD_ID, md->lock_key ? md->lock_key : "",
457                oim->number, oim->number, oim->msg );
[e5a8118]458       
459        g_free( display_name_b64 );
[27053b5]460        oim->need_retry = MSN_SOAP_OK;
[e5a8118]461       
[523fb23]462        return MSN_SOAP_OK;
[e5a8118]463}
464
[27053b5]465static xt_status msn_soap_oim_reauth( struct xt_node *node, gpointer data )
[e5a8118]466{
467        struct msn_soap_req_data *soap_req = data;
468        struct msn_soap_oim_send_data *oim = soap_req->data;
469        struct im_connection *ic = soap_req->ic;
470        struct msn_data *md = ic->proto_data;
[27053b5]471        struct xt_node *c;
[e5a8118]472       
[27053b5]473        if( ( c = xt_find_node( node->children, "LockKeyChallenge" ) ) && c->text_len > 0 )
474        {
475                g_free( md->lock_key );
476                md->lock_key = msn_p11_challenge( c->text );
477                oim->need_retry = MSN_SOAP_RETRY;
478        }
479        if( xt_find_node( node->children, "RequiredAuthPolicy" ) )
480        {
481                oim->need_retry = MSN_SOAP_REAUTH;
482        }
[e5a8118]483       
484        return XT_HANDLED;
485}
486
487static const struct xt_handler_entry msn_soap_oim_send_parser[] = {
[27053b5]488        { "detail", "soap:Fault", msn_soap_oim_reauth },
489        { NULL,     NULL,         NULL                }
[e5a8118]490};
491
492static int msn_soap_oim_handle_response( struct msn_soap_req_data *soap_req )
493{
494        struct msn_soap_oim_send_data *oim = soap_req->data;
495       
[bc090f0]496        if( soap_req->http_req->status_code == 500 && oim->need_retry && soap_req->ttl > 0 )
[e5a8118]497        {
[27053b5]498                return oim->need_retry;
[e5a8118]499        }
500        else if( soap_req->http_req->status_code == 200 )
[bc090f0]501        {
502                imcb_log( soap_req->ic, "Offline message successfully delivered to %s", oim->to );
[e5a8118]503                return MSN_SOAP_OK;
[bc090f0]504        }
[e5a8118]505        else
[bc090f0]506        {
507                imcb_log( soap_req->ic, "Failed to deliver offline message to %s:\n%s", oim->to, oim->msg );
[e5a8118]508                return MSN_SOAP_ABORT;
[bc090f0]509        }
[e5a8118]510}
511
512static int msn_soap_oim_free_data( struct msn_soap_req_data *soap_req )
513{
514        struct msn_soap_oim_send_data *oim = soap_req->data;
515       
516        g_free( oim->to );
517        g_free( oim->msg );
518        g_free( oim );
519       
[523fb23]520        return MSN_SOAP_OK;
[e5a8118]521}
522
523int msn_soap_oim_send( struct im_connection *ic, const char *to, const char *msg )
524{
525        struct msn_soap_oim_send_data *data;
526       
527        data = g_new0( struct msn_soap_oim_send_data, 1 );
528        data->to = g_strdup( to );
529        data->msg = tobase64( msg );
530        data->number = 1;
531       
532        return msn_soap_start( ic, data, msn_soap_oim_build_request,
533                                         msn_soap_oim_send_parser,
534                                         msn_soap_oim_handle_response,
535                                         msn_soap_oim_free_data );
536}
[bc090f0]537
538int msn_soap_oim_send_queue( struct im_connection *ic, GSList **msgq )
539{
540        GSList *l;
541        char *n = NULL;
542       
543        for( l = *msgq; l; l = l->next )
544        {
545                struct msn_message *m = l->data;
546               
547                if( n == NULL )
548                        n = m->who;
549                if( strcmp( n, m->who ) == 0 )
550                        msn_soap_oim_send( ic, m->who, m->text );
551        }
552       
553        while( *msgq != NULL )
554        {
555                struct msn_message *m = (*msgq)->data;
556               
557                g_free( m->who );
558                g_free( m->text );
559                g_free( m );
560               
561                *msgq = g_slist_remove( *msgq, m );
562        }
[523fb23]563       
564        return 1;
[bc090f0]565}
[7db65b7]566
567
568/* memlist: Fetching the membership list (NOT address book) */
569
570static int msn_soap_memlist_build_request( struct msn_soap_req_data *soap_req )
571{
[7f34ce2]572        struct msn_data *md = soap_req->ic->proto_data;
573       
[7db65b7]574        soap_req->url = g_strdup( SOAP_MEMLIST_URL );
575        soap_req->action = g_strdup( SOAP_MEMLIST_ACTION );
[6ddb223]576        soap_req->payload = msn_soap_abservice_build( SOAP_MEMLIST_PAYLOAD, "Initial", md->tokens[1] );
[7db65b7]577       
578        return 1;
579}
580
[7f34ce2]581static xt_status msn_soap_memlist_member( struct xt_node *node, gpointer data )
582{
583        bee_user_t *bu;
584        struct msn_buddy_data *bd;
585        struct xt_node *p;
586        char *role = NULL, *handle = NULL;
587        struct msn_soap_req_data *soap_req = data;
588        struct im_connection *ic = soap_req->ic;
589       
[d97f51b]590        if( ( p = xt_find_path( node, "../../MemberRole" ) ) )
[7f34ce2]591                role = p->text;
592       
593        if( ( p = xt_find_node( node->children, "PassportName" ) ) )
594                handle = p->text;
595       
596        if( !role || !handle || 
597            !( ( bu = bee_user_by_handle( ic->bee, ic, handle ) ) ||
598               ( bu = bee_user_new( ic->bee, ic, handle, 0 ) ) ) )
599                return XT_HANDLED;
600       
601        bd = bu->data;
602        if( strcmp( role, "Allow" ) == 0 )
603                bd->flags |= MSN_BUDDY_AL;
604        else if( strcmp( role, "Block" ) == 0 )
605                bd->flags |= MSN_BUDDY_BL;
606        else if( strcmp( role, "Reverse" ) == 0 )
607                bd->flags |= MSN_BUDDY_RL;
608        else if( strcmp( role, "Pending" ) == 0 )
609                bd->flags |= MSN_BUDDY_PL;
[e5854a8]610       
[327af51]611        printf( "%p %s %d\n", bu, handle, bd->flags );
[7f34ce2]612       
613        return XT_HANDLED;
614}
615
[7db65b7]616static const struct xt_handler_entry msn_soap_memlist_parser[] = {
[7f34ce2]617        { "Member", "Members", msn_soap_memlist_member },
[7db65b7]618        { NULL,               NULL,     NULL                        }
619};
620
621static int msn_soap_memlist_handle_response( struct msn_soap_req_data *soap_req )
622{
[7f34ce2]623        msn_soap_addressbook_request( soap_req->ic );
624       
625        return MSN_SOAP_OK;
[7db65b7]626}
627
628static int msn_soap_memlist_free_data( struct msn_soap_req_data *soap_req )
629{
630        return 0;
631}
632
633int msn_soap_memlist_request( struct im_connection *ic )
634{
635        return msn_soap_start( ic, NULL, msn_soap_memlist_build_request,
636                                         msn_soap_memlist_parser,
637                                         msn_soap_memlist_handle_response,
638                                         msn_soap_memlist_free_data );
639}
[7f34ce2]640
[193dc74]641/* Variant: Adding/Removing people */
642struct msn_soap_memlist_edit_data
643{
644        char *handle;
645        gboolean add;
646        msn_buddy_flags_t list;
647};
648
649static int msn_soap_memlist_edit_build_request( struct msn_soap_req_data *soap_req )
650{
651        struct msn_data *md = soap_req->ic->proto_data;
652        struct msn_soap_memlist_edit_data *med = soap_req->data;
653        char *add, *scenario, *list;
654       
655        soap_req->url = g_strdup( SOAP_MEMLIST_URL );
656        if( med->add )
657        {
658                soap_req->action = g_strdup( SOAP_MEMLIST_ADD_ACTION );
659                add = "Add";
660        }
661        else
662        {
663                soap_req->action = g_strdup( SOAP_MEMLIST_DEL_ACTION );
664                add = "Delete";
665        }
666        switch( med->list )
667        {
668        case MSN_BUDDY_AL:
669                scenario = "BlockUnblock";
670                list = "Allow";
671                break;
672        case MSN_BUDDY_BL:
673                scenario = "BlockUnblock";
674                list = "Block";
675                break;
676        case MSN_BUDDY_RL:
677                scenario = "Timer";
678                list = "Reverse";
679                break;
680        case MSN_BUDDY_PL:
681        default:
682                scenario = "Timer";
683                list = "Pending";
684                break;
685        }
[6ddb223]686        soap_req->payload = msn_soap_abservice_build( SOAP_MEMLIST_EDIT_PAYLOAD,
[193dc74]687                scenario, md->tokens[1], add, list, med->handle, add );
688       
689        return 1;
690}
691
692static int msn_soap_memlist_edit_handle_response( struct msn_soap_req_data *soap_req )
693{
694        return MSN_SOAP_OK;
695}
696
697static int msn_soap_memlist_edit_free_data( struct msn_soap_req_data *soap_req )
698{
699        struct msn_soap_memlist_edit_data *med = soap_req->data;
700       
701        g_free( med->handle );
702        g_free( med );
703       
704        return 0;
705}
706
707int msn_soap_memlist_edit( struct im_connection *ic, const char *handle, gboolean add, int list )
708{
709        struct msn_soap_memlist_edit_data *med;
710       
711        med = g_new0( struct msn_soap_memlist_edit_data, 1 );
712        med->handle = g_strdup( handle );
713        med->add = add;
714        med->list = list;
715       
716        return msn_soap_start( ic, med, msn_soap_memlist_edit_build_request,
717                                        NULL,
718                                        msn_soap_memlist_edit_handle_response,
719                                        msn_soap_memlist_edit_free_data );
720}
721
[7f34ce2]722
723/* addressbook: Fetching the membership list (NOT address book) */
724
725static int msn_soap_addressbook_build_request( struct msn_soap_req_data *soap_req )
726{
727        struct msn_data *md = soap_req->ic->proto_data;
728       
729        soap_req->url = g_strdup( SOAP_ADDRESSBOOK_URL );
730        soap_req->action = g_strdup( SOAP_ADDRESSBOOK_ACTION );
[6ddb223]731        soap_req->payload = msn_soap_abservice_build( SOAP_ADDRESSBOOK_PAYLOAD, "Initial", md->tokens[1] );
[7f34ce2]732       
733        return 1;
734}
735
736static xt_status msn_soap_addressbook_group( struct xt_node *node, gpointer data )
737{
738        struct xt_node *p;
739        char *id = NULL, *name = NULL;
740        struct msn_soap_req_data *soap_req = data;
[ff27648]741        struct msn_data *md = soap_req->ic->proto_data;
[7f34ce2]742       
[d97f51b]743        if( ( p = xt_find_path( node, "../groupId" ) ) )
[7f34ce2]744                id = p->text;
745       
746        if( ( p = xt_find_node( node->children, "name" ) ) )
747                name = p->text;
748       
[ff27648]749        if( id && name )
750        {
751                struct msn_group *mg = g_new0( struct msn_group, 1 );
752                mg->id = g_strdup( id );
753                mg->name = g_strdup( name );
754                md->groups = g_slist_prepend( md->groups, mg );
755        }
756       
[7f34ce2]757        printf( "%s %s\n", id, name );
758       
759        return XT_HANDLED;
760}
761
762static xt_status msn_soap_addressbook_contact( struct xt_node *node, gpointer data )
763{
764        bee_user_t *bu;
765        struct msn_buddy_data *bd;
766        struct xt_node *p;
[9b01339]767        char *id = NULL, *type = NULL, *handle = NULL, *is_msgr = "false",
[ff27648]768             *display_name = NULL, *group_id = NULL;
[7f34ce2]769        struct msn_soap_req_data *soap_req = data;
770        struct im_connection *ic = soap_req->ic;
[ff27648]771        struct msn_group *group;
[7f34ce2]772       
[d97f51b]773        if( ( p = xt_find_path( node, "../contactId" ) ) )
[7f34ce2]774                id = p->text;
775        if( ( p = xt_find_node( node->children, "contactType" ) ) )
776                type = p->text;
777        if( ( p = xt_find_node( node->children, "passportName" ) ) )
778                handle = p->text;
779        if( ( p = xt_find_node( node->children, "displayName" ) ) )
780                display_name = p->text;
[9b01339]781        if( ( p = xt_find_node( node->children, "isMessengerUser" ) ) )
782                is_msgr = p->text;
[ff27648]783        if( ( p = xt_find_path( node, "groupIds/guid" ) ) )
784                group_id = p->text;
[7f34ce2]785       
786        if( type && g_strcasecmp( type, "me" ) == 0 )
787        {
788                set_t *set = set_find( &ic->acc->set, "display_name" );
789                g_free( set->value );
790                set->value = g_strdup( display_name );
791               
[80175a1]792                /* Try to fetch the profile; if the user has one, that's where
793                   we can find the persistent display_name. */
794                if( ( p = xt_find_node( node->children, "CID" ) ) && p->text )
795                        msn_soap_profile_get( ic, p->text );
796               
[7f34ce2]797                return XT_HANDLED;
798        }
799       
[9b01339]800        if( !bool2int( is_msgr ) || handle == NULL )
[d97f51b]801                return XT_HANDLED;
802       
[7f34ce2]803        if( !( bu = bee_user_by_handle( ic->bee, ic, handle ) ) &&
804            !( bu = bee_user_new( ic->bee, ic, handle, 0 ) ) )
805                return XT_HANDLED;
806       
807        bd = bu->data;
808        bd->flags |= MSN_BUDDY_FL;
809        g_free( bd->cid );
810        bd->cid = g_strdup( id );
811       
812        imcb_rename_buddy( ic, handle, display_name );
813       
[ff27648]814        if( group_id && ( group = msn_group_by_id( ic, group_id ) ) )
815                imcb_add_buddy( ic, handle, group->name );
816       
[7f34ce2]817        printf( "%s %s %s %s\n", id, type, handle, display_name );
818       
819        return XT_HANDLED;
820}
821
822static const struct xt_handler_entry msn_soap_addressbook_parser[] = {
823        { "contactInfo", "Contact", msn_soap_addressbook_contact },
824        { "groupInfo", "Group", msn_soap_addressbook_group },
825        { NULL,               NULL,     NULL                        }
826};
827
828static int msn_soap_addressbook_handle_response( struct msn_soap_req_data *soap_req )
829{
[327af51]830        GSList *l;
831       
832        for( l = soap_req->ic->bee->users; l; l = l->next )
833        {
834                struct bee_user *bu = l->data;
835               
836                if( bu->ic == soap_req->ic )
837                        msn_buddy_ask( bu );
838        }
839       
[ca7de3a]840        msn_auth_got_contact_list( soap_req->ic );
[327af51]841       
[7f34ce2]842        return MSN_SOAP_OK;
843}
844
845static int msn_soap_addressbook_free_data( struct msn_soap_req_data *soap_req )
846{
847        return 0;
848}
849
850int msn_soap_addressbook_request( struct im_connection *ic )
851{
852        return msn_soap_start( ic, NULL, msn_soap_addressbook_build_request,
853                                         msn_soap_addressbook_parser,
854                                         msn_soap_addressbook_handle_response,
855                                         msn_soap_addressbook_free_data );
856}
[4452e69]857
858/* Variant: Change our display name. */
859static int msn_soap_ab_namechange_build_request( struct msn_soap_req_data *soap_req )
860{
861        struct msn_data *md = soap_req->ic->proto_data;
862       
863        soap_req->url = g_strdup( SOAP_ADDRESSBOOK_URL );
864        soap_req->action = g_strdup( SOAP_AB_NAMECHANGE_ACTION );
[6ddb223]865        soap_req->payload = msn_soap_abservice_build( SOAP_AB_NAMECHANGE_PAYLOAD,
[e0e1546]866                "Timer", md->tokens[1], (char *) soap_req->data );
[4452e69]867       
868        return 1;
869}
870
871static int msn_soap_ab_namechange_handle_response( struct msn_soap_req_data *soap_req )
872{
873        /* TODO: Ack the change? Not sure what the NAKs look like.. */
874        return MSN_SOAP_OK;
875}
876
877static int msn_soap_ab_namechange_free_data( struct msn_soap_req_data *soap_req )
878{
879        g_free( soap_req->data );
880        return 0;
881}
882
883int msn_soap_addressbook_set_display_name( struct im_connection *ic, const char *new )
884{
885        return msn_soap_start( ic, g_strdup( new ),
886                               msn_soap_ab_namechange_build_request,
887                               NULL,
888                               msn_soap_ab_namechange_handle_response,
889                               msn_soap_ab_namechange_free_data );
890}
[4fc95c5]891
892/* Add a contact. */
893static int msn_soap_ab_contact_add_build_request( struct msn_soap_req_data *soap_req )
894{
895        struct msn_data *md = soap_req->ic->proto_data;
896        bee_user_t *bu = soap_req->data;
897       
898        soap_req->url = g_strdup( SOAP_ADDRESSBOOK_URL );
899        soap_req->action = g_strdup( SOAP_AB_CONTACT_ADD_ACTION );
900        soap_req->payload = msn_soap_abservice_build( SOAP_AB_CONTACT_ADD_PAYLOAD,
901                "ContactSave", md->tokens[1], bu->handle, bu->fullname ? bu->fullname : bu->handle );
902       
903        return 1;
904}
905
906static xt_status msn_soap_ab_contact_add_cid( struct xt_node *node, gpointer data )
907{
908        struct msn_soap_req_data *soap_req = data;
909        bee_user_t *bu = soap_req->data;
910        struct msn_buddy_data *bd = bu->data;
911       
912        g_free( bd->cid );
913        bd->cid = g_strdup( node->text );
914       
915        return XT_HANDLED;
916}
917
918static const struct xt_handler_entry msn_soap_ab_contact_add_parser[] = {
919        { "guid", "ABContactAddResult", msn_soap_ab_contact_add_cid },
920        { NULL,               NULL,     NULL                        }
921};
922
923static int msn_soap_ab_contact_add_handle_response( struct msn_soap_req_data *soap_req )
924{
925        /* TODO: Ack the change? Not sure what the NAKs look like.. */
926        return MSN_SOAP_OK;
927}
928
929static int msn_soap_ab_contact_add_free_data( struct msn_soap_req_data *soap_req )
930{
931        return 0;
932}
933
934int msn_soap_ab_contact_add( struct im_connection *ic, bee_user_t *bu )
935{
936        return msn_soap_start( ic, bu,
937                               msn_soap_ab_contact_add_build_request,
938                               msn_soap_ab_contact_add_parser,
939                               msn_soap_ab_contact_add_handle_response,
940                               msn_soap_ab_contact_add_free_data );
941}
942
943/* Remove a contact. */
944static int msn_soap_ab_contact_del_build_request( struct msn_soap_req_data *soap_req )
945{
946        struct msn_data *md = soap_req->ic->proto_data;
947        bee_user_t *bu = soap_req->data;
948        struct msn_buddy_data *bd = bu->data;
949       
950        soap_req->url = g_strdup( SOAP_ADDRESSBOOK_URL );
951        soap_req->action = g_strdup( SOAP_AB_CONTACT_DEL_ACTION );
952        soap_req->payload = msn_soap_abservice_build( SOAP_AB_CONTACT_DEL_PAYLOAD,
953                "Timer", md->tokens[1], bd->cid );
954       
955        return 1;
956}
957
958static int msn_soap_ab_contact_del_handle_response( struct msn_soap_req_data *soap_req )
959{
960        /* TODO: Ack the change? Not sure what the NAKs look like.. */
961        return MSN_SOAP_OK;
962}
963
964static int msn_soap_ab_contact_del_free_data( struct msn_soap_req_data *soap_req )
965{
966        return 0;
967}
968
969int msn_soap_ab_contact_del( struct im_connection *ic, bee_user_t *bu )
970{
971        return msn_soap_start( ic, bu,
972                               msn_soap_ab_contact_del_build_request,
973                               NULL,
974                               msn_soap_ab_contact_del_handle_response,
975                               msn_soap_ab_contact_del_free_data );
976}
[80175a1]977
978
979
980/* Storage stuff: Fetch profile. */
981static int msn_soap_profile_get_build_request( struct msn_soap_req_data *soap_req )
982{
983        struct msn_data *md = soap_req->ic->proto_data;
984       
985        soap_req->url = g_strdup( SOAP_STORAGE_URL );
986        soap_req->action = g_strdup( SOAP_PROFILE_GET_ACTION );
987        soap_req->payload = g_markup_printf_escaped( SOAP_PROFILE_GET_PAYLOAD,
988                md->tokens[3], (char*) soap_req->data );
989       
990        return 1;
991}
992
993static xt_status msn_soap_profile_get_result( struct xt_node *node, gpointer data )
994{
995        struct msn_soap_req_data *soap_req = data;
996        struct im_connection *ic = soap_req->ic;
997        struct msn_data *md = soap_req->ic->proto_data;
998        struct xt_node *dn;
999       
1000        if( ( dn = xt_find_node( node->children, "DisplayName" ) ) && dn->text )
1001        {
1002                set_t *set = set_find( &ic->acc->set, "display_name" );
1003                g_free( set->value );
1004                set->value = g_strdup( dn->text );
1005               
1006                md->flags |= MSN_GOT_PROFILE_DN;
1007        }
1008       
1009        return XT_HANDLED;
1010}
1011
1012static const struct xt_handler_entry msn_soap_profile_get_parser[] = {
1013        { "ExpressionProfile", "GetProfileResult", msn_soap_profile_get_result },
1014        { NULL,               NULL,     NULL                        }
1015};
1016
1017static int msn_soap_profile_get_handle_response( struct msn_soap_req_data *soap_req )
1018{
1019        struct msn_data *md = soap_req->ic->proto_data;
1020       
1021        md->flags |= MSN_GOT_PROFILE;
1022        msn_ns_finish_login( soap_req->ic );
1023       
1024        return MSN_SOAP_OK;
1025}
1026
1027static int msn_soap_profile_get_free_data( struct msn_soap_req_data *soap_req )
1028{
1029        g_free( soap_req->data );
1030        return 0;
1031}
1032
1033int msn_soap_profile_get( struct im_connection *ic, const char *cid )
1034{
1035        return msn_soap_start( ic, g_strdup( cid ),
1036                               msn_soap_profile_get_build_request,
1037                               msn_soap_profile_get_parser,
1038                               msn_soap_profile_get_handle_response,
1039                               msn_soap_profile_get_free_data );
1040}
Note: See TracBrowser for help on using the repository browser.