source: protocols/msn/soap.c @ 04cd284

Last change on this file since 04cd284 was 04cd284, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-10-02T05:19:27Z

Export block/allow list again. The way this is done is ugly though and needs
to change.

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