source: protocols/msn/soap.c @ f2520b5

Last change on this file since f2520b5 was f2520b5, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-20T08:22:28Z

In debugging mode, dump all SOAP requests + responses with some indentation
for easier debugging.

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