source: protocols/msn/soap.c @ 52f5e90

Last change on this file since 52f5e90 was 52f5e90, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-10-02T04:21:50Z

Fixed possible crash bug on removing contacts while the auth cookie expired.

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