source: protocols/msn/soap.c @ a72dc2b

Last change on this file since a72dc2b was 5dc7f90, checked in by Miklos Vajna <vmiklos@…>, at 2011-12-06T00:53:16Z

msn: unused-but-set-variables

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