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

Last change on this file since 4e1be76 was 4e1be76, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-09-04T17:23:46Z

Clean up any stuff stuck in the soap queue at disconnect time.

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