source: protocols/msn/soap.c @ 4aa8a04

Last change on this file since 4aa8a04 was 4aa8a04, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-09-04T15:54:52Z

This works (includes some token sabotage code to ease testing), but I just
realised there's probably no need for the additional temporary NS connection.

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