source: protocols/msn/soap.c @ c77406a

Last change on this file since c77406a was 3bd2f17, checked in by Wilmer van der Gaast <wilmer@…>, at 2011-11-25T12:16:05Z

msn_soap_debug_print may get called with NULL pointers. Pay attention.

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