source: protocols/msn/soap.c @ dac74bd

Last change on this file since dac74bd was ff1616b, checked in by Wilmer van der Gaast <wilmer@…>, at 2011-11-09T00:07:22Z

Fixed bug in msn_soap_debug_print() failing to print HTTP headers of SOAP
queries. No, this doesn't fix #850, I just found this bug while trying to
get debugging info for that.

  • Property mode set to 100644
File size: 32.0 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2010 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* MSN module - All the SOAPy XML stuff.
8   Some manager at Microsoft apparently thought MSNP wasn't XMLy enough so
9   someone stepped up and changed that. This is the result. Kilobytes and
10   more kilobytes of XML vomit to transfer tiny bits of informaiton. */
11
12/*
13  This program is free software; you can redistribute it and/or modify
14  it under the terms of the GNU General Public License as published by
15  the Free Software Foundation; either version 2 of the License, or
16  (at your option) any later version.
17
18  This program is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  GNU General Public License for more details.
22
23  You should have received a copy of the GNU General Public License with
24  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
25  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
26  Suite 330, Boston, MA  02111-1307  USA
27*/
28
29#include "http_client.h"
30#include "soap.h"
31#include "msn.h"
32#include "bitlbee.h"
33#include "url.h"
34#include "misc.h"
35#include "sha1.h"
36#include "base64.h"
37#include "xmltree.h"
38#include <ctype.h>
39#include <errno.h>
40
41/* This file tries to make SOAP stuff pretty simple to do by letting you just
42   provide a function to build a request, a few functions to parse various
43   parts of the response, and a function to run when the full response was
44   received and parsed. See the various examples below. */
45
46typedef enum
47{
48        MSN_SOAP_OK,
49        MSN_SOAP_RETRY,
50        MSN_SOAP_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        int st;
213       
214        if( !getenv( "BITLBEE_DEBUG" ) )
215                return;
216       
217        if( ( s = strstr( headers, "\r\n\r\n" ) ) )
218                st = write( 1, headers, s - headers + 4 );
219        else
220                st = write( 1, headers, strlen( headers ) );
221       
222#ifdef DEBUG
223        {
224                struct xt_node *xt = xt_from_string( payload );
225                if( xt )
226                        xt_print( xt );
227                xt_free_node( xt );
228        }
229#endif
230}
231
232int msn_soapq_flush( struct im_connection *ic, gboolean resend )
233{
234        struct msn_data *md = ic->proto_data;
235       
236        while( md->soapq )
237        {
238                if( resend )
239                        msn_soap_send_request( (struct msn_soap_req_data*) md->soapq->data );
240                else
241                        msn_soap_free( (struct msn_soap_req_data*) md->soapq->data );
242                md->soapq = g_slist_remove( md->soapq, md->soapq->data );
243        }
244       
245        return MSN_SOAP_OK;
246}
247
248static void msn_soap_free( struct msn_soap_req_data *soap_req )
249{
250        soap_req->free_data( soap_req );
251        g_free( soap_req->url );
252        g_free( soap_req->action );
253        g_free( soap_req->payload );
254        g_free( soap_req );
255}
256
257
258/* passport_sso: Authentication MSNP15+ */
259
260struct msn_soap_passport_sso_data
261{
262        char *nonce;
263        char *secret;
264        char *error;
265        char *redirect;
266};
267
268static int msn_soap_passport_sso_build_request( struct msn_soap_req_data *soap_req )
269{
270        struct msn_soap_passport_sso_data *sd = soap_req->data;
271        struct im_connection *ic = soap_req->ic;
272        struct msn_data *md = ic->proto_data;
273        char pass[MAX_PASSPORT_PWLEN+1];
274       
275        if( sd->redirect )
276        {
277                soap_req->url = sd->redirect;
278                sd->redirect = NULL;
279        }
280        /* MS changed this URL and broke the old MSN-specific one. The generic
281           one works, forwarding us to a msn.com URL that works. Takes an extra
282           second, but that's better than not being able to log in at all. :-/
283        else if( g_str_has_suffix( ic->acc->user, "@msn.com" ) )
284                soap_req->url = g_strdup( SOAP_PASSPORT_SSO_URL_MSN );
285        */
286        else
287                soap_req->url = g_strdup( SOAP_PASSPORT_SSO_URL );
288       
289        strncpy( pass, ic->acc->pass, MAX_PASSPORT_PWLEN );
290        pass[MAX_PASSPORT_PWLEN] = '\0';
291        soap_req->payload = g_markup_printf_escaped( SOAP_PASSPORT_SSO_PAYLOAD,
292                ic->acc->user, pass, md->pp_policy );
293       
294        return MSN_SOAP_OK;
295}
296
297static xt_status msn_soap_passport_sso_token( struct xt_node *node, gpointer data )
298{
299        struct msn_soap_req_data *soap_req = data;
300        struct msn_soap_passport_sso_data *sd = soap_req->data;
301        struct msn_data *md = soap_req->ic->proto_data;
302        struct xt_node *p;
303        char *id;
304       
305        if( ( id = xt_find_attr( node, "Id" ) ) == NULL )
306                return XT_HANDLED;
307        id += strlen( id ) - 1;
308        if( *id == '1' &&
309            ( p = xt_find_path( node, "../../wst:RequestedProofToken/wst:BinarySecret" ) ) &&
310            p->text )
311                sd->secret = g_strdup( p->text );
312       
313        *id -= '1';
314        if( *id >= 0 && *id < sizeof( md->tokens ) / sizeof( md->tokens[0] ) )
315        {
316                g_free( md->tokens[(int)*id] );
317                md->tokens[(int)*id] = g_strdup( node->text );
318        }
319       
320        return XT_HANDLED;
321}
322
323static xt_status msn_soap_passport_failure( struct xt_node *node, gpointer data )
324{
325        struct msn_soap_req_data *soap_req = data;
326        struct msn_soap_passport_sso_data *sd = soap_req->data;
327        struct xt_node *code = xt_find_node( node->children, "faultcode" );
328        struct xt_node *string = xt_find_node( node->children, "faultstring" );
329        struct xt_node *url;
330       
331        if( code == NULL || code->text_len == 0 )
332                sd->error = g_strdup( "Unknown error" );
333        else if( strcmp( code->text, "psf:Redirect" ) == 0 &&
334                 ( url = xt_find_node( node->children, "psf:redirectUrl" ) ) &&
335                 url->text_len > 0 )
336                sd->redirect = g_strdup( url->text );
337        else
338                sd->error = g_strdup_printf( "%s (%s)", code->text, string && string->text_len ?
339                                             string->text : "no description available" );
340       
341        return XT_HANDLED;
342}
343
344static const struct xt_handler_entry msn_soap_passport_sso_parser[] = {
345        { "wsse:BinarySecurityToken", "wst:RequestedSecurityToken", msn_soap_passport_sso_token },
346        { "S:Fault", "S:Envelope", msn_soap_passport_failure },
347        { NULL, NULL, NULL }
348};
349
350static char *msn_key_fuckery( char *key, int key_len, char *type )
351{
352        unsigned char hash1[20+strlen(type)+1];
353        unsigned char hash2[20];
354        char *ret;
355       
356        sha1_hmac( key, key_len, type, 0, hash1 );
357        strcpy( (char*) hash1 + 20, type );
358        sha1_hmac( key, key_len, (char*) hash1, sizeof( hash1 ) - 1, hash2 );
359       
360        /* This is okay as hash1 is read completely before it's overwritten. */
361        sha1_hmac( key, key_len, (char*) hash1, 20, hash1 );
362        sha1_hmac( key, key_len, (char*) hash1, sizeof( hash1 ) - 1, hash1 );
363       
364        ret = g_malloc( 24 );
365        memcpy( ret, hash2, 20 );
366        memcpy( ret + 20, hash1, 4 );
367        return ret;
368}
369
370static int msn_soap_passport_sso_handle_response( struct msn_soap_req_data *soap_req )
371{
372        struct msn_soap_passport_sso_data *sd = soap_req->data;
373        struct im_connection *ic = soap_req->ic;
374        struct msn_data *md = ic->proto_data;
375        char *key1, *key2, *key3, *blurb64;
376        int key1_len;
377        unsigned char *padnonce, *des3res;
378        struct
379        {
380                unsigned int uStructHeaderSize; // 28. Does not count data
381                unsigned int uCryptMode; // CRYPT_MODE_CBC (1)
382                unsigned int uCipherType; // TripleDES (0x6603)
383                unsigned int uHashType; // SHA1 (0x8004)
384                unsigned int uIVLen;    // 8
385                unsigned int uHashLen;  // 20
386                unsigned int uCipherLen; // 72
387                unsigned char iv[8];
388                unsigned char hash[20];
389                unsigned char cipherbytes[72];
390        } blurb = {
391                GUINT32_TO_LE( 28 ),
392                GUINT32_TO_LE( 1 ),
393                GUINT32_TO_LE( 0x6603 ),
394                GUINT32_TO_LE( 0x8004 ),
395                GUINT32_TO_LE( 8 ),
396                GUINT32_TO_LE( 20 ),
397                GUINT32_TO_LE( 72 ),
398        };
399       
400        if( sd->redirect )
401                return MSN_SOAP_RETRY;
402       
403        if( md->soapq )
404        {
405                md->flags &= ~MSN_REAUTHING; 
406                return msn_soapq_flush( ic, TRUE );
407        }
408       
409        if( sd->secret == NULL )
410        {
411                msn_auth_got_passport_token( ic, NULL, sd->error );
412                return MSN_SOAP_OK;
413        }
414
415        key1_len = base64_decode( sd->secret, (unsigned char**) &key1 );
416       
417        key2 = msn_key_fuckery( key1, key1_len, "WS-SecureConversationSESSION KEY HASH" );
418        key3 = msn_key_fuckery( key1, key1_len, "WS-SecureConversationSESSION KEY ENCRYPTION" );
419       
420        sha1_hmac( key2, 24, sd->nonce, 0, blurb.hash );
421        padnonce = g_malloc( strlen( sd->nonce ) + 8 );
422        strcpy( (char*) padnonce, sd->nonce );
423        memset( padnonce + strlen( sd->nonce ), 8, 8 );
424       
425        random_bytes( blurb.iv, 8 );
426       
427        ssl_des3_encrypt( (unsigned char*) key3, 24, padnonce, strlen( sd->nonce ) + 8, blurb.iv, &des3res );
428        memcpy( blurb.cipherbytes, des3res, 72 );
429       
430        blurb64 = base64_encode( (unsigned char*) &blurb, sizeof( blurb ) );
431        msn_auth_got_passport_token( ic, blurb64, NULL );
432       
433        g_free( padnonce );
434        g_free( blurb64 );
435        g_free( des3res );
436        g_free( key1 );
437        g_free( key2 );
438        g_free( key3 );
439       
440        return MSN_SOAP_OK;
441}
442
443static int msn_soap_passport_sso_free_data( struct msn_soap_req_data *soap_req )
444{
445        struct msn_soap_passport_sso_data *sd = soap_req->data;
446       
447        g_free( sd->nonce );
448        g_free( sd->secret );
449        g_free( sd->error );
450        g_free( sd->redirect );
451        g_free( sd );
452       
453        return MSN_SOAP_OK;
454}
455
456int msn_soap_passport_sso_request( struct im_connection *ic, const char *nonce )
457{
458        struct msn_soap_passport_sso_data *sd = g_new0( struct msn_soap_passport_sso_data, 1 );
459       
460        sd->nonce = g_strdup( nonce );
461       
462        return msn_soap_start( ic, sd, msn_soap_passport_sso_build_request,
463                                       msn_soap_passport_sso_parser,
464                                       msn_soap_passport_sso_handle_response,
465                                       msn_soap_passport_sso_free_data );
466}
467
468
469/* oim_send: Sending offline messages */
470
471struct msn_soap_oim_send_data
472{
473        char *to;
474        char *msg;
475        int number;
476        msn_soap_result_t need_retry;
477};
478
479static int msn_soap_oim_build_request( struct msn_soap_req_data *soap_req )
480{
481        struct msn_soap_oim_send_data *oim = soap_req->data;
482        struct im_connection *ic = soap_req->ic;
483        struct msn_data *md = ic->proto_data;
484        char *display_name_b64;
485       
486        display_name_b64 = tobase64( set_getstr( &ic->acc->set, "display_name" ) );
487       
488        soap_req->url = g_strdup( SOAP_OIM_SEND_URL );
489        soap_req->action = g_strdup( SOAP_OIM_SEND_ACTION );
490        soap_req->payload = g_markup_printf_escaped( SOAP_OIM_SEND_PAYLOAD,
491                ic->acc->user, display_name_b64, MSNP_VER, MSNP_BUILD,
492                oim->to, md->tokens[2],
493                MSNP11_PROD_ID, md->lock_key ? md->lock_key : "",
494                oim->number, oim->number, oim->msg );
495       
496        g_free( display_name_b64 );
497        oim->need_retry = MSN_SOAP_OK;
498       
499        return MSN_SOAP_OK;
500}
501
502static xt_status msn_soap_oim_reauth( struct xt_node *node, gpointer data )
503{
504        struct msn_soap_req_data *soap_req = data;
505        struct msn_soap_oim_send_data *oim = soap_req->data;
506        struct im_connection *ic = soap_req->ic;
507        struct msn_data *md = ic->proto_data;
508        struct xt_node *c;
509       
510        if( ( c = xt_find_node( node->children, "LockKeyChallenge" ) ) && c->text_len > 0 )
511        {
512                g_free( md->lock_key );
513                md->lock_key = msn_p11_challenge( c->text );
514                oim->need_retry = MSN_SOAP_RETRY;
515        }
516        if( xt_find_node( node->children, "RequiredAuthPolicy" ) )
517        {
518                oim->need_retry = MSN_SOAP_REAUTH;
519        }
520       
521        return XT_HANDLED;
522}
523
524static const struct xt_handler_entry msn_soap_oim_send_parser[] = {
525        { "detail", "soap:Fault", msn_soap_oim_reauth },
526        { NULL,     NULL,         NULL                }
527};
528
529static int msn_soap_oim_handle_response( struct msn_soap_req_data *soap_req )
530{
531        struct msn_soap_oim_send_data *oim = soap_req->data;
532       
533        if( soap_req->http_req->status_code == 500 && oim->need_retry && soap_req->ttl > 0 )
534        {
535                return oim->need_retry;
536        }
537        else if( soap_req->http_req->status_code == 200 )
538        {
539                /* Noise..
540                imcb_log( soap_req->ic, "Offline message successfully delivered to %s", oim->to );
541                */
542                return MSN_SOAP_OK;
543        }
544        else
545        {
546                char *dec = frombase64( oim->msg );
547                imcb_log( soap_req->ic, "Failed to deliver offline message to %s:\n%s", oim->to, dec );
548                g_free( dec );
549                return MSN_SOAP_ABORT;
550        }
551}
552
553static int msn_soap_oim_free_data( struct msn_soap_req_data *soap_req )
554{
555        struct msn_soap_oim_send_data *oim = soap_req->data;
556       
557        g_free( oim->to );
558        g_free( oim->msg );
559        g_free( oim );
560       
561        return MSN_SOAP_OK;
562}
563
564int msn_soap_oim_send( struct im_connection *ic, const char *to, const char *msg )
565{
566        struct msn_soap_oim_send_data *data;
567       
568        /* Don't send any of the special messages since they creep people out. :-) */
569        if( strncmp( msg, "\r\r", 2 ) == 0 )
570                return 0;
571       
572        data = g_new0( struct msn_soap_oim_send_data, 1 );
573        data->to = g_strdup( to );
574        data->msg = tobase64( msg );
575        data->number = 1;
576       
577        return msn_soap_start( ic, data, msn_soap_oim_build_request,
578                                         msn_soap_oim_send_parser,
579                                         msn_soap_oim_handle_response,
580                                         msn_soap_oim_free_data );
581}
582
583int msn_soap_oim_send_queue( struct im_connection *ic, GSList **msgq )
584{
585        GSList *l;
586        char *n = NULL;
587       
588        for( l = *msgq; l; l = l->next )
589        {
590                struct msn_message *m = l->data;
591               
592                if( n == NULL )
593                        n = m->who;
594                if( strcmp( n, m->who ) == 0 )
595                        msn_soap_oim_send( ic, m->who, m->text );
596        }
597       
598        while( *msgq != NULL )
599        {
600                struct msn_message *m = (*msgq)->data;
601               
602                g_free( m->who );
603                g_free( m->text );
604                g_free( m );
605               
606                *msgq = g_slist_remove( *msgq, m );
607        }
608       
609        return 1;
610}
611
612
613/* memlist: Fetching the membership list (NOT address book) */
614
615static int msn_soap_memlist_build_request( struct msn_soap_req_data *soap_req )
616{
617        struct msn_data *md = soap_req->ic->proto_data;
618       
619        soap_req->url = g_strdup( SOAP_MEMLIST_URL );
620        soap_req->action = g_strdup( SOAP_MEMLIST_ACTION );
621        soap_req->payload = msn_soap_abservice_build( SOAP_MEMLIST_PAYLOAD, "Initial", md->tokens[1] );
622       
623        return 1;
624}
625
626static xt_status msn_soap_memlist_member( struct xt_node *node, gpointer data )
627{
628        bee_user_t *bu;
629        struct msn_buddy_data *bd;
630        struct xt_node *p;
631        char *role = NULL, *handle = NULL;
632        struct msn_soap_req_data *soap_req = data;
633        struct im_connection *ic = soap_req->ic;
634       
635        if( ( p = xt_find_path( node, "../../MemberRole" ) ) )
636                role = p->text;
637       
638        if( ( p = xt_find_node( node->children, "PassportName" ) ) )
639                handle = p->text;
640       
641        if( !role || !handle || 
642            !( ( bu = bee_user_by_handle( ic->bee, ic, handle ) ) ||
643               ( bu = bee_user_new( ic->bee, ic, handle, 0 ) ) ) )
644                return XT_HANDLED;
645       
646        bd = bu->data;
647        if( strcmp( role, "Allow" ) == 0 )
648        {
649                bd->flags |= MSN_BUDDY_AL;
650                ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) );
651        }
652        else if( strcmp( role, "Block" ) == 0 )
653        {
654                bd->flags |= MSN_BUDDY_BL;
655                ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) );
656        }
657        else if( strcmp( role, "Reverse" ) == 0 )
658                bd->flags |= MSN_BUDDY_RL;
659        else if( strcmp( role, "Pending" ) == 0 )
660                bd->flags |= MSN_BUDDY_PL;
661       
662        if( getenv( "BITLBEE_DEBUG" ) )
663                printf( "%p %s %d\n", bu, handle, bd->flags );
664       
665        return XT_HANDLED;
666}
667
668static const struct xt_handler_entry msn_soap_memlist_parser[] = {
669        { "Member", "Members", msn_soap_memlist_member },
670        { NULL,               NULL,     NULL                        }
671};
672
673static int msn_soap_memlist_handle_response( struct msn_soap_req_data *soap_req )
674{
675        msn_soap_addressbook_request( soap_req->ic );
676       
677        return MSN_SOAP_OK;
678}
679
680static int msn_soap_memlist_free_data( struct msn_soap_req_data *soap_req )
681{
682        return 0;
683}
684
685int msn_soap_memlist_request( struct im_connection *ic )
686{
687        return msn_soap_start( ic, NULL, msn_soap_memlist_build_request,
688                                         msn_soap_memlist_parser,
689                                         msn_soap_memlist_handle_response,
690                                         msn_soap_memlist_free_data );
691}
692
693/* Variant: Adding/Removing people */
694struct msn_soap_memlist_edit_data
695{
696        char *handle;
697        gboolean add;
698        msn_buddy_flags_t list;
699};
700
701static int msn_soap_memlist_edit_build_request( struct msn_soap_req_data *soap_req )
702{
703        struct msn_data *md = soap_req->ic->proto_data;
704        struct msn_soap_memlist_edit_data *med = soap_req->data;
705        char *add, *scenario, *list;
706       
707        soap_req->url = g_strdup( SOAP_MEMLIST_URL );
708        if( med->add )
709        {
710                soap_req->action = g_strdup( SOAP_MEMLIST_ADD_ACTION );
711                add = "Add";
712        }
713        else
714        {
715                soap_req->action = g_strdup( SOAP_MEMLIST_DEL_ACTION );
716                add = "Delete";
717        }
718        switch( med->list )
719        {
720        case MSN_BUDDY_AL:
721                scenario = "BlockUnblock";
722                list = "Allow";
723                break;
724        case MSN_BUDDY_BL:
725                scenario = "BlockUnblock";
726                list = "Block";
727                break;
728        case MSN_BUDDY_RL:
729                scenario = "Timer";
730                list = "Reverse";
731                break;
732        case MSN_BUDDY_PL:
733        default:
734                scenario = "Timer";
735                list = "Pending";
736                break;
737        }
738        soap_req->payload = msn_soap_abservice_build( SOAP_MEMLIST_EDIT_PAYLOAD,
739                scenario, md->tokens[1], add, list, med->handle, add );
740       
741        return 1;
742}
743
744static int msn_soap_memlist_edit_handle_response( struct msn_soap_req_data *soap_req )
745{
746        return MSN_SOAP_OK;
747}
748
749static int msn_soap_memlist_edit_free_data( struct msn_soap_req_data *soap_req )
750{
751        struct msn_soap_memlist_edit_data *med = soap_req->data;
752       
753        g_free( med->handle );
754        g_free( med );
755       
756        return 0;
757}
758
759int msn_soap_memlist_edit( struct im_connection *ic, const char *handle, gboolean add, int list )
760{
761        struct msn_soap_memlist_edit_data *med;
762       
763        med = g_new0( struct msn_soap_memlist_edit_data, 1 );
764        med->handle = g_strdup( handle );
765        med->add = add;
766        med->list = list;
767       
768        return msn_soap_start( ic, med, msn_soap_memlist_edit_build_request,
769                                        NULL,
770                                        msn_soap_memlist_edit_handle_response,
771                                        msn_soap_memlist_edit_free_data );
772}
773
774
775/* addressbook: Fetching the membership list (NOT address book) */
776
777static int msn_soap_addressbook_build_request( struct msn_soap_req_data *soap_req )
778{
779        struct msn_data *md = soap_req->ic->proto_data;
780       
781        soap_req->url = g_strdup( SOAP_ADDRESSBOOK_URL );
782        soap_req->action = g_strdup( SOAP_ADDRESSBOOK_ACTION );
783        soap_req->payload = msn_soap_abservice_build( SOAP_ADDRESSBOOK_PAYLOAD, "Initial", md->tokens[1] );
784       
785        return 1;
786}
787
788static xt_status msn_soap_addressbook_group( struct xt_node *node, gpointer data )
789{
790        struct xt_node *p;
791        char *id = NULL, *name = NULL;
792        struct msn_soap_req_data *soap_req = data;
793        struct msn_data *md = soap_req->ic->proto_data;
794       
795        if( ( p = xt_find_path( node, "../groupId" ) ) )
796                id = p->text;
797       
798        if( ( p = xt_find_node( node->children, "name" ) ) )
799                name = p->text;
800       
801        if( id && name )
802        {
803                struct msn_group *mg = g_new0( struct msn_group, 1 );
804                mg->id = g_strdup( id );
805                mg->name = g_strdup( name );
806                md->groups = g_slist_prepend( md->groups, mg );
807        }
808       
809        if( getenv( "BITLBEE_DEBUG" ) )
810                printf( "%s %s\n", id, name );
811       
812        return XT_HANDLED;
813}
814
815static xt_status msn_soap_addressbook_contact( struct xt_node *node, gpointer data )
816{
817        bee_user_t *bu;
818        struct msn_buddy_data *bd;
819        struct xt_node *p;
820        char *id = NULL, *type = NULL, *handle = NULL, *is_msgr = "false",
821             *display_name = NULL, *group_id = NULL;
822        struct msn_soap_req_data *soap_req = data;
823        struct im_connection *ic = soap_req->ic;
824        struct msn_group *group;
825       
826        if( ( p = xt_find_path( node, "../contactId" ) ) )
827                id = p->text;
828        if( ( p = xt_find_node( node->children, "contactType" ) ) )
829                type = p->text;
830        if( ( p = xt_find_node( node->children, "passportName" ) ) )
831                handle = p->text;
832        if( ( p = xt_find_node( node->children, "displayName" ) ) )
833                display_name = p->text;
834        if( ( p = xt_find_node( node->children, "isMessengerUser" ) ) )
835                is_msgr = p->text;
836        if( ( p = xt_find_path( node, "groupIds/guid" ) ) )
837                group_id = p->text;
838       
839        if( type && g_strcasecmp( type, "me" ) == 0 )
840        {
841                set_t *set = set_find( &ic->acc->set, "display_name" );
842                g_free( set->value );
843                set->value = g_strdup( display_name );
844               
845                /* Try to fetch the profile; if the user has one, that's where
846                   we can find the persistent display_name. */
847                if( ( p = xt_find_node( node->children, "CID" ) ) && p->text )
848                        msn_soap_profile_get( ic, p->text );
849               
850                return XT_HANDLED;
851        }
852       
853        if( !bool2int( is_msgr ) || handle == NULL )
854                return XT_HANDLED;
855       
856        if( !( bu = bee_user_by_handle( ic->bee, ic, handle ) ) &&
857            !( bu = bee_user_new( ic->bee, ic, handle, 0 ) ) )
858                return XT_HANDLED;
859       
860        bd = bu->data;
861        bd->flags |= MSN_BUDDY_FL;
862        g_free( bd->cid );
863        bd->cid = g_strdup( id );
864       
865        imcb_rename_buddy( ic, handle, display_name );
866       
867        if( group_id && ( group = msn_group_by_id( ic, group_id ) ) )
868                imcb_add_buddy( ic, handle, group->name );
869       
870        if( getenv( "BITLBEE_DEBUG" ) )
871                printf( "%s %s %s %s\n", id, type, handle, display_name );
872       
873        return XT_HANDLED;
874}
875
876static const struct xt_handler_entry msn_soap_addressbook_parser[] = {
877        { "contactInfo", "Contact", msn_soap_addressbook_contact },
878        { "groupInfo", "Group", msn_soap_addressbook_group },
879        { NULL,               NULL,     NULL                        }
880};
881
882static int msn_soap_addressbook_handle_response( struct msn_soap_req_data *soap_req )
883{
884        GSList *l;
885        int wtf = 0;
886       
887        for( l = soap_req->ic->bee->users; l; l = l->next )
888        {
889                struct bee_user *bu = l->data;
890                struct msn_buddy_data *bd = bu->data;
891               
892                if( bu->ic == soap_req->ic && bd )
893                {
894                        msn_buddy_ask( bu );
895                       
896                        if( ( bd->flags & ( MSN_BUDDY_AL | MSN_BUDDY_BL ) ) ==
897                                          ( MSN_BUDDY_AL | MSN_BUDDY_BL ) )
898                        {
899                                bd->flags &= ~MSN_BUDDY_BL;
900                                wtf++;
901                        }
902                }
903        }
904       
905        if( wtf )
906                imcb_log( soap_req->ic, "Warning: %d contacts were in both your "
907                          "block and your allow list. Assuming they're all "
908                          "allowed. Use the official WLM client once to fix "
909                          "this.", wtf );
910       
911        msn_auth_got_contact_list( soap_req->ic );
912       
913        return MSN_SOAP_OK;
914}
915
916static int msn_soap_addressbook_free_data( struct msn_soap_req_data *soap_req )
917{
918        return 0;
919}
920
921int msn_soap_addressbook_request( struct im_connection *ic )
922{
923        return msn_soap_start( ic, NULL, msn_soap_addressbook_build_request,
924                                         msn_soap_addressbook_parser,
925                                         msn_soap_addressbook_handle_response,
926                                         msn_soap_addressbook_free_data );
927}
928
929/* Variant: Change our display name. */
930static int msn_soap_ab_namechange_build_request( struct msn_soap_req_data *soap_req )
931{
932        struct msn_data *md = soap_req->ic->proto_data;
933       
934        soap_req->url = g_strdup( SOAP_ADDRESSBOOK_URL );
935        soap_req->action = g_strdup( SOAP_AB_NAMECHANGE_ACTION );
936        soap_req->payload = msn_soap_abservice_build( SOAP_AB_NAMECHANGE_PAYLOAD,
937                "Timer", md->tokens[1], (char *) soap_req->data );
938       
939        return 1;
940}
941
942static int msn_soap_ab_namechange_handle_response( struct msn_soap_req_data *soap_req )
943{
944        /* TODO: Ack the change? Not sure what the NAKs look like.. */
945        return MSN_SOAP_OK;
946}
947
948static int msn_soap_ab_namechange_free_data( struct msn_soap_req_data *soap_req )
949{
950        g_free( soap_req->data );
951        return 0;
952}
953
954int msn_soap_addressbook_set_display_name( struct im_connection *ic, const char *new )
955{
956        return msn_soap_start( ic, g_strdup( new ),
957                               msn_soap_ab_namechange_build_request,
958                               NULL,
959                               msn_soap_ab_namechange_handle_response,
960                               msn_soap_ab_namechange_free_data );
961}
962
963/* Add a contact. */
964static int msn_soap_ab_contact_add_build_request( struct msn_soap_req_data *soap_req )
965{
966        struct msn_data *md = soap_req->ic->proto_data;
967        bee_user_t *bu = soap_req->data;
968       
969        soap_req->url = g_strdup( SOAP_ADDRESSBOOK_URL );
970        soap_req->action = g_strdup( SOAP_AB_CONTACT_ADD_ACTION );
971        soap_req->payload = msn_soap_abservice_build( SOAP_AB_CONTACT_ADD_PAYLOAD,
972                "ContactSave", md->tokens[1], bu->handle, bu->fullname ? bu->fullname : bu->handle );
973       
974        return 1;
975}
976
977static xt_status msn_soap_ab_contact_add_cid( struct xt_node *node, gpointer data )
978{
979        struct msn_soap_req_data *soap_req = data;
980        bee_user_t *bu = soap_req->data;
981        struct msn_buddy_data *bd = bu->data;
982       
983        g_free( bd->cid );
984        bd->cid = g_strdup( node->text );
985       
986        return XT_HANDLED;
987}
988
989static const struct xt_handler_entry msn_soap_ab_contact_add_parser[] = {
990        { "guid", "ABContactAddResult", msn_soap_ab_contact_add_cid },
991        { NULL,               NULL,     NULL                        }
992};
993
994static int msn_soap_ab_contact_add_handle_response( struct msn_soap_req_data *soap_req )
995{
996        /* TODO: Ack the change? Not sure what the NAKs look like.. */
997        return MSN_SOAP_OK;
998}
999
1000static int msn_soap_ab_contact_add_free_data( struct msn_soap_req_data *soap_req )
1001{
1002        return 0;
1003}
1004
1005int msn_soap_ab_contact_add( struct im_connection *ic, bee_user_t *bu )
1006{
1007        return msn_soap_start( ic, bu,
1008                               msn_soap_ab_contact_add_build_request,
1009                               msn_soap_ab_contact_add_parser,
1010                               msn_soap_ab_contact_add_handle_response,
1011                               msn_soap_ab_contact_add_free_data );
1012}
1013
1014/* Remove a contact. */
1015static int msn_soap_ab_contact_del_build_request( struct msn_soap_req_data *soap_req )
1016{
1017        struct msn_data *md = soap_req->ic->proto_data;
1018        const char *cid = soap_req->data;
1019       
1020        soap_req->url = g_strdup( SOAP_ADDRESSBOOK_URL );
1021        soap_req->action = g_strdup( SOAP_AB_CONTACT_DEL_ACTION );
1022        soap_req->payload = msn_soap_abservice_build( SOAP_AB_CONTACT_DEL_PAYLOAD,
1023                "Timer", md->tokens[1], cid );
1024       
1025        return 1;
1026}
1027
1028static int msn_soap_ab_contact_del_handle_response( struct msn_soap_req_data *soap_req )
1029{
1030        /* TODO: Ack the change? Not sure what the NAKs look like.. */
1031        return MSN_SOAP_OK;
1032}
1033
1034static int msn_soap_ab_contact_del_free_data( struct msn_soap_req_data *soap_req )
1035{
1036        g_free( soap_req->data );
1037        return 0;
1038}
1039
1040int msn_soap_ab_contact_del( struct im_connection *ic, bee_user_t *bu )
1041{
1042        struct msn_buddy_data *bd = bu->data;
1043       
1044        return msn_soap_start( ic, g_strdup( bd->cid ),
1045                               msn_soap_ab_contact_del_build_request,
1046                               NULL,
1047                               msn_soap_ab_contact_del_handle_response,
1048                               msn_soap_ab_contact_del_free_data );
1049}
1050
1051
1052
1053/* Storage stuff: Fetch profile. */
1054static int msn_soap_profile_get_build_request( struct msn_soap_req_data *soap_req )
1055{
1056        struct msn_data *md = soap_req->ic->proto_data;
1057       
1058        soap_req->url = g_strdup( SOAP_STORAGE_URL );
1059        soap_req->action = g_strdup( SOAP_PROFILE_GET_ACTION );
1060        soap_req->payload = g_markup_printf_escaped( SOAP_PROFILE_GET_PAYLOAD,
1061                md->tokens[3], (char*) soap_req->data );
1062       
1063        return 1;
1064}
1065
1066static xt_status msn_soap_profile_get_result( struct xt_node *node, gpointer data )
1067{
1068        struct msn_soap_req_data *soap_req = data;
1069        struct im_connection *ic = soap_req->ic;
1070        struct msn_data *md = soap_req->ic->proto_data;
1071        struct xt_node *dn;
1072       
1073        if( ( dn = xt_find_node( node->children, "DisplayName" ) ) && dn->text )
1074        {
1075                set_t *set = set_find( &ic->acc->set, "display_name" );
1076                g_free( set->value );
1077                set->value = g_strdup( dn->text );
1078               
1079                md->flags |= MSN_GOT_PROFILE_DN;
1080        }
1081       
1082        return XT_HANDLED;
1083}
1084
1085static xt_status msn_soap_profile_get_rid( struct xt_node *node, gpointer data )
1086{
1087        struct msn_soap_req_data *soap_req = data;
1088        struct msn_data *md = soap_req->ic->proto_data;
1089       
1090        g_free( md->profile_rid );
1091        md->profile_rid = g_strdup( node->text );
1092       
1093        return XT_HANDLED;
1094}
1095
1096static const struct xt_handler_entry msn_soap_profile_get_parser[] = {
1097        { "ExpressionProfile", "GetProfileResult", msn_soap_profile_get_result },
1098        { "ResourceID",        "GetProfileResult", msn_soap_profile_get_rid },
1099        { NULL,               NULL,     NULL                        }
1100};
1101
1102static int msn_soap_profile_get_handle_response( struct msn_soap_req_data *soap_req )
1103{
1104        struct msn_data *md = soap_req->ic->proto_data;
1105       
1106        md->flags |= MSN_GOT_PROFILE;
1107        msn_ns_finish_login( soap_req->ic );
1108       
1109        return MSN_SOAP_OK;
1110}
1111
1112static int msn_soap_profile_get_free_data( struct msn_soap_req_data *soap_req )
1113{
1114        g_free( soap_req->data );
1115        return 0;
1116}
1117
1118int msn_soap_profile_get( struct im_connection *ic, const char *cid )
1119{
1120        return msn_soap_start( ic, g_strdup( cid ),
1121                               msn_soap_profile_get_build_request,
1122                               msn_soap_profile_get_parser,
1123                               msn_soap_profile_get_handle_response,
1124                               msn_soap_profile_get_free_data );
1125}
1126
1127/* Update profile (display name). */
1128static int msn_soap_profile_set_dn_build_request( struct msn_soap_req_data *soap_req )
1129{
1130        struct msn_data *md = soap_req->ic->proto_data;
1131       
1132        soap_req->url = g_strdup( SOAP_STORAGE_URL );
1133        soap_req->action = g_strdup( SOAP_PROFILE_SET_DN_ACTION );
1134        soap_req->payload = g_markup_printf_escaped( SOAP_PROFILE_SET_DN_PAYLOAD,
1135                md->tokens[3], md->profile_rid, (char*) soap_req->data );
1136       
1137        return 1;
1138}
1139
1140static const struct xt_handler_entry msn_soap_profile_set_dn_parser[] = {
1141        { NULL,               NULL,     NULL                        }
1142};
1143
1144static int msn_soap_profile_set_dn_handle_response( struct msn_soap_req_data *soap_req )
1145{
1146        return MSN_SOAP_OK;
1147}
1148
1149static int msn_soap_profile_set_dn_free_data( struct msn_soap_req_data *soap_req )
1150{
1151        g_free( soap_req->data );
1152        return 0;
1153}
1154
1155int msn_soap_profile_set_dn( struct im_connection *ic, const char *dn )
1156{
1157        return msn_soap_start( ic, g_strdup( dn ),
1158                               msn_soap_profile_set_dn_build_request,
1159                               msn_soap_profile_set_dn_parser,
1160                               msn_soap_profile_set_dn_handle_response,
1161                               msn_soap_profile_set_dn_free_data );
1162}
Note: See TracBrowser for help on using the repository browser.