source: protocols/msn/soap.c @ 327af51

Last change on this file since 327af51 was 327af51, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-21T17:27:32Z

Some general cleanup, plus fixing a bug in the memberlist parsing code:
the lists can come in in any order, so parse it *completely* before showing
auth requests.

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