source: protocols/msn/soap.c @ 8eec79d

Last change on this file since 8eec79d was 8eec79d, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-12-13T00:33:56Z

MSN: Don't send any of the special messages offline since they creep people
out. :-)

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