source: protocols/msn/soap.c @ a366cca

Last change on this file since a366cca was 801b90b, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-20T19:30:12Z

Check if a connection is down before handling its SOAP responses.

  • Property mode set to 100644
File size: 27.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_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        {
557                bd->flags |= MSN_BUDDY_RL;
558                msn_buddy_ask( bu );
559        }
560        else if( strcmp( role, "Pending" ) == 0 )
561        {
562                bd->flags |= MSN_BUDDY_PL;
563                msn_buddy_ask( bu );
564        }
565       
566        printf( "%s %d\n", handle, bd->flags );
567       
568        return XT_HANDLED;
569}
570
571static const struct xt_handler_entry msn_soap_memlist_parser[] = {
572        { "Member", "Members", msn_soap_memlist_member },
573        { NULL,               NULL,     NULL                        }
574};
575
576static int msn_soap_memlist_handle_response( struct msn_soap_req_data *soap_req )
577{
578        msn_soap_addressbook_request( soap_req->ic );
579       
580        return MSN_SOAP_OK;
581}
582
583static int msn_soap_memlist_free_data( struct msn_soap_req_data *soap_req )
584{
585        return 0;
586}
587
588int msn_soap_memlist_request( struct im_connection *ic )
589{
590        return msn_soap_start( ic, NULL, msn_soap_memlist_build_request,
591                                         msn_soap_memlist_parser,
592                                         msn_soap_memlist_handle_response,
593                                         msn_soap_memlist_free_data );
594}
595
596/* Variant: Adding/Removing people */
597struct msn_soap_memlist_edit_data
598{
599        char *handle;
600        gboolean add;
601        msn_buddy_flags_t list;
602};
603
604static int msn_soap_memlist_edit_build_request( struct msn_soap_req_data *soap_req )
605{
606        struct msn_data *md = soap_req->ic->proto_data;
607        struct msn_soap_memlist_edit_data *med = soap_req->data;
608        char *add, *scenario, *list;
609       
610        soap_req->url = g_strdup( SOAP_MEMLIST_URL );
611        if( med->add )
612        {
613                soap_req->action = g_strdup( SOAP_MEMLIST_ADD_ACTION );
614                add = "Add";
615        }
616        else
617        {
618                soap_req->action = g_strdup( SOAP_MEMLIST_DEL_ACTION );
619                add = "Delete";
620        }
621        switch( med->list )
622        {
623        case MSN_BUDDY_AL:
624                scenario = "BlockUnblock";
625                list = "Allow";
626                break;
627        case MSN_BUDDY_BL:
628                scenario = "BlockUnblock";
629                list = "Block";
630                break;
631        case MSN_BUDDY_RL:
632                scenario = "Timer";
633                list = "Reverse";
634                break;
635        case MSN_BUDDY_PL:
636        default:
637                scenario = "Timer";
638                list = "Pending";
639                break;
640        }
641        soap_req->payload = msn_soap_abservice_build( SOAP_MEMLIST_EDIT_PAYLOAD,
642                scenario, md->tokens[1], add, list, med->handle, add );
643       
644        return 1;
645}
646
647static int msn_soap_memlist_edit_handle_response( struct msn_soap_req_data *soap_req )
648{
649        return MSN_SOAP_OK;
650}
651
652static int msn_soap_memlist_edit_free_data( struct msn_soap_req_data *soap_req )
653{
654        struct msn_soap_memlist_edit_data *med = soap_req->data;
655       
656        g_free( med->handle );
657        g_free( med );
658       
659        return 0;
660}
661
662int msn_soap_memlist_edit( struct im_connection *ic, const char *handle, gboolean add, int list )
663{
664        struct msn_soap_memlist_edit_data *med;
665       
666        med = g_new0( struct msn_soap_memlist_edit_data, 1 );
667        med->handle = g_strdup( handle );
668        med->add = add;
669        med->list = list;
670       
671        return msn_soap_start( ic, med, msn_soap_memlist_edit_build_request,
672                                        NULL,
673                                        msn_soap_memlist_edit_handle_response,
674                                        msn_soap_memlist_edit_free_data );
675}
676
677
678/* addressbook: Fetching the membership list (NOT address book) */
679
680static int msn_soap_addressbook_build_request( struct msn_soap_req_data *soap_req )
681{
682        struct msn_data *md = soap_req->ic->proto_data;
683       
684        soap_req->url = g_strdup( SOAP_ADDRESSBOOK_URL );
685        soap_req->action = g_strdup( SOAP_ADDRESSBOOK_ACTION );
686        soap_req->payload = msn_soap_abservice_build( SOAP_ADDRESSBOOK_PAYLOAD, "Initial", md->tokens[1] );
687       
688        return 1;
689}
690
691static xt_status msn_soap_addressbook_group( struct xt_node *node, gpointer data )
692{
693        struct xt_node *p;
694        char *id = NULL, *name = NULL;
695        struct msn_soap_req_data *soap_req = data;
696        struct msn_data *md = soap_req->ic->proto_data;
697       
698        if( ( p = xt_find_path( node, "../groupId" ) ) )
699                id = p->text;
700       
701        if( ( p = xt_find_node( node->children, "name" ) ) )
702                name = p->text;
703       
704        if( id && name )
705        {
706                struct msn_group *mg = g_new0( struct msn_group, 1 );
707                mg->id = g_strdup( id );
708                mg->name = g_strdup( name );
709                md->groups = g_slist_prepend( md->groups, mg );
710        }
711       
712        printf( "%s %s\n", id, name );
713       
714        return XT_HANDLED;
715}
716
717static xt_status msn_soap_addressbook_contact( struct xt_node *node, gpointer data )
718{
719        bee_user_t *bu;
720        struct msn_buddy_data *bd;
721        struct xt_node *p;
722        char *id = NULL, *type = NULL, *handle = NULL, *is_msgr = "false",
723             *display_name = NULL, *group_id = NULL;
724        struct msn_soap_req_data *soap_req = data;
725        struct im_connection *ic = soap_req->ic;
726        struct msn_group *group;
727       
728        if( ( p = xt_find_path( node, "../contactId" ) ) )
729                id = p->text;
730        if( ( p = xt_find_node( node->children, "contactType" ) ) )
731                type = p->text;
732        if( ( p = xt_find_node( node->children, "passportName" ) ) )
733                handle = p->text;
734        if( ( p = xt_find_node( node->children, "displayName" ) ) )
735                display_name = p->text;
736        if( ( p = xt_find_node( node->children, "isMessengerUser" ) ) )
737                is_msgr = p->text;
738        if( ( p = xt_find_path( node, "groupIds/guid" ) ) )
739                group_id = p->text;
740       
741        if( type && g_strcasecmp( type, "me" ) == 0 )
742        {
743                set_t *set = set_find( &ic->acc->set, "display_name" );
744                g_free( set->value );
745                set->value = g_strdup( display_name );
746               
747                /* Try to fetch the profile; if the user has one, that's where
748                   we can find the persistent display_name. */
749                if( ( p = xt_find_node( node->children, "CID" ) ) && p->text )
750                        msn_soap_profile_get( ic, p->text );
751               
752                return XT_HANDLED;
753        }
754       
755        if( !bool2int( is_msgr ) || handle == NULL )
756                return XT_HANDLED;
757       
758        if( !( bu = bee_user_by_handle( ic->bee, ic, handle ) ) &&
759            !( bu = bee_user_new( ic->bee, ic, handle, 0 ) ) )
760                return XT_HANDLED;
761       
762        bd = bu->data;
763        bd->flags |= MSN_BUDDY_FL;
764        g_free( bd->cid );
765        bd->cid = g_strdup( id );
766       
767        imcb_rename_buddy( ic, handle, display_name );
768       
769        if( group_id && ( group = msn_group_by_id( ic, group_id ) ) )
770                imcb_add_buddy( ic, handle, group->name );
771       
772        printf( "%s %s %s %s\n", id, type, handle, display_name );
773       
774        return XT_HANDLED;
775}
776
777static const struct xt_handler_entry msn_soap_addressbook_parser[] = {
778        { "contactInfo", "Contact", msn_soap_addressbook_contact },
779        { "groupInfo", "Group", msn_soap_addressbook_group },
780        { NULL,               NULL,     NULL                        }
781};
782
783static int msn_soap_addressbook_handle_response( struct msn_soap_req_data *soap_req )
784{
785        msn_auth_got_contact_list( soap_req->ic );
786        return MSN_SOAP_OK;
787}
788
789static int msn_soap_addressbook_free_data( struct msn_soap_req_data *soap_req )
790{
791        return 0;
792}
793
794int msn_soap_addressbook_request( struct im_connection *ic )
795{
796        return msn_soap_start( ic, NULL, msn_soap_addressbook_build_request,
797                                         msn_soap_addressbook_parser,
798                                         msn_soap_addressbook_handle_response,
799                                         msn_soap_addressbook_free_data );
800}
801
802/* Variant: Change our display name. */
803static int msn_soap_ab_namechange_build_request( struct msn_soap_req_data *soap_req )
804{
805        struct msn_data *md = soap_req->ic->proto_data;
806       
807        soap_req->url = g_strdup( SOAP_ADDRESSBOOK_URL );
808        soap_req->action = g_strdup( SOAP_AB_NAMECHANGE_ACTION );
809        soap_req->payload = msn_soap_abservice_build( SOAP_AB_NAMECHANGE_PAYLOAD,
810                "Timer", md->tokens[1], (char *) soap_req->data );
811       
812        return 1;
813}
814
815static int msn_soap_ab_namechange_handle_response( struct msn_soap_req_data *soap_req )
816{
817        /* TODO: Ack the change? Not sure what the NAKs look like.. */
818        return MSN_SOAP_OK;
819}
820
821static int msn_soap_ab_namechange_free_data( struct msn_soap_req_data *soap_req )
822{
823        g_free( soap_req->data );
824        return 0;
825}
826
827int msn_soap_addressbook_set_display_name( struct im_connection *ic, const char *new )
828{
829        return msn_soap_start( ic, g_strdup( new ),
830                               msn_soap_ab_namechange_build_request,
831                               NULL,
832                               msn_soap_ab_namechange_handle_response,
833                               msn_soap_ab_namechange_free_data );
834}
835
836/* Add a contact. */
837static int msn_soap_ab_contact_add_build_request( struct msn_soap_req_data *soap_req )
838{
839        struct msn_data *md = soap_req->ic->proto_data;
840        bee_user_t *bu = soap_req->data;
841       
842        soap_req->url = g_strdup( SOAP_ADDRESSBOOK_URL );
843        soap_req->action = g_strdup( SOAP_AB_CONTACT_ADD_ACTION );
844        soap_req->payload = msn_soap_abservice_build( SOAP_AB_CONTACT_ADD_PAYLOAD,
845                "ContactSave", md->tokens[1], bu->handle, bu->fullname ? bu->fullname : bu->handle );
846       
847        return 1;
848}
849
850static xt_status msn_soap_ab_contact_add_cid( struct xt_node *node, gpointer data )
851{
852        struct msn_soap_req_data *soap_req = data;
853        bee_user_t *bu = soap_req->data;
854        struct msn_buddy_data *bd = bu->data;
855       
856        g_free( bd->cid );
857        bd->cid = g_strdup( node->text );
858       
859        return XT_HANDLED;
860}
861
862static const struct xt_handler_entry msn_soap_ab_contact_add_parser[] = {
863        { "guid", "ABContactAddResult", msn_soap_ab_contact_add_cid },
864        { NULL,               NULL,     NULL                        }
865};
866
867static int msn_soap_ab_contact_add_handle_response( struct msn_soap_req_data *soap_req )
868{
869        /* TODO: Ack the change? Not sure what the NAKs look like.. */
870        return MSN_SOAP_OK;
871}
872
873static int msn_soap_ab_contact_add_free_data( struct msn_soap_req_data *soap_req )
874{
875        return 0;
876}
877
878int msn_soap_ab_contact_add( struct im_connection *ic, bee_user_t *bu )
879{
880        return msn_soap_start( ic, bu,
881                               msn_soap_ab_contact_add_build_request,
882                               msn_soap_ab_contact_add_parser,
883                               msn_soap_ab_contact_add_handle_response,
884                               msn_soap_ab_contact_add_free_data );
885}
886
887/* Remove a contact. */
888static int msn_soap_ab_contact_del_build_request( struct msn_soap_req_data *soap_req )
889{
890        struct msn_data *md = soap_req->ic->proto_data;
891        bee_user_t *bu = soap_req->data;
892        struct msn_buddy_data *bd = bu->data;
893       
894        soap_req->url = g_strdup( SOAP_ADDRESSBOOK_URL );
895        soap_req->action = g_strdup( SOAP_AB_CONTACT_DEL_ACTION );
896        soap_req->payload = msn_soap_abservice_build( SOAP_AB_CONTACT_DEL_PAYLOAD,
897                "Timer", md->tokens[1], bd->cid );
898       
899        return 1;
900}
901
902static int msn_soap_ab_contact_del_handle_response( struct msn_soap_req_data *soap_req )
903{
904        /* TODO: Ack the change? Not sure what the NAKs look like.. */
905        return MSN_SOAP_OK;
906}
907
908static int msn_soap_ab_contact_del_free_data( struct msn_soap_req_data *soap_req )
909{
910        return 0;
911}
912
913int msn_soap_ab_contact_del( struct im_connection *ic, bee_user_t *bu )
914{
915        return msn_soap_start( ic, bu,
916                               msn_soap_ab_contact_del_build_request,
917                               NULL,
918                               msn_soap_ab_contact_del_handle_response,
919                               msn_soap_ab_contact_del_free_data );
920}
921
922
923
924/* Storage stuff: Fetch profile. */
925static int msn_soap_profile_get_build_request( struct msn_soap_req_data *soap_req )
926{
927        struct msn_data *md = soap_req->ic->proto_data;
928       
929        soap_req->url = g_strdup( SOAP_STORAGE_URL );
930        soap_req->action = g_strdup( SOAP_PROFILE_GET_ACTION );
931        soap_req->payload = g_markup_printf_escaped( SOAP_PROFILE_GET_PAYLOAD,
932                md->tokens[3], (char*) soap_req->data );
933       
934        return 1;
935}
936
937static xt_status msn_soap_profile_get_result( struct xt_node *node, gpointer data )
938{
939        struct msn_soap_req_data *soap_req = data;
940        struct im_connection *ic = soap_req->ic;
941        struct msn_data *md = soap_req->ic->proto_data;
942        struct xt_node *dn;
943       
944        if( ( dn = xt_find_node( node->children, "DisplayName" ) ) && dn->text )
945        {
946                set_t *set = set_find( &ic->acc->set, "display_name" );
947                g_free( set->value );
948                set->value = g_strdup( dn->text );
949               
950                md->flags |= MSN_GOT_PROFILE_DN;
951        }
952       
953        return XT_HANDLED;
954}
955
956static const struct xt_handler_entry msn_soap_profile_get_parser[] = {
957        { "ExpressionProfile", "GetProfileResult", msn_soap_profile_get_result },
958        { NULL,               NULL,     NULL                        }
959};
960
961static int msn_soap_profile_get_handle_response( struct msn_soap_req_data *soap_req )
962{
963        struct msn_data *md = soap_req->ic->proto_data;
964       
965        md->flags |= MSN_GOT_PROFILE;
966        msn_ns_finish_login( soap_req->ic );
967       
968        return MSN_SOAP_OK;
969}
970
971static int msn_soap_profile_get_free_data( struct msn_soap_req_data *soap_req )
972{
973        g_free( soap_req->data );
974        return 0;
975}
976
977int msn_soap_profile_get( struct im_connection *ic, const char *cid )
978{
979        return msn_soap_start( ic, g_strdup( cid ),
980                               msn_soap_profile_get_build_request,
981                               msn_soap_profile_get_parser,
982                               msn_soap_profile_get_handle_response,
983                               msn_soap_profile_get_free_data );
984}
Note: See TracBrowser for help on using the repository browser.