source: protocols/msn/soap.c @ 0483e1e

Last change on this file since 0483e1e was 0483e1e, checked in by dequis <dx@…>, at 2016-11-28T19:26:16Z

Fix some compiler warnings

warn_unused_result on write() is particularly annoying. You can't just
add (void) to ignore it due to gcc bug 66425.

I replaced some of those with fwrite() and used a variable marked with
the G_GNUC_UNUSED attribute for the writes from signal handlers.

  • Property mode set to 100644
File size: 28.9 KB
Line 
1/********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2012 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., 51 Franklin St.,
26  Fifth Floor, Boston, MA  02110-1301  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        MSN_SOAP_OK,
48        MSN_SOAP_RETRY,
49        MSN_SOAP_REAUTH,
50        MSN_SOAP_ABORT,
51} msn_soap_result_t;
52
53struct msn_soap_req_data;
54typedef int (*msn_soap_func) (struct msn_soap_req_data *);
55
56struct msn_soap_req_data {
57        void *data;
58        struct im_connection *ic;
59        int ttl;
60        char *error;
61
62        char *url, *action, *payload;
63        struct http_request *http_req;
64
65        const struct xt_handler_entry *xml_parser;
66        msn_soap_func build_request, handle_response, free_data;
67};
68
69static int msn_soap_send_request(struct msn_soap_req_data *req);
70static void msn_soap_free(struct msn_soap_req_data *soap_req);
71static void msn_soap_debug_print(const char *headers, const char *payload);
72
73static int msn_soap_start(struct im_connection *ic,
74                          void *data,
75                          msn_soap_func build_request,
76                          const struct xt_handler_entry *xml_parser,
77                          msn_soap_func handle_response,
78                          msn_soap_func free_data)
79{
80        struct msn_soap_req_data *req = g_new0(struct msn_soap_req_data, 1);
81
82        req->ic = ic;
83        req->data = data;
84        req->xml_parser = xml_parser;
85        req->build_request = build_request;
86        req->handle_response = handle_response;
87        req->free_data = free_data;
88        req->ttl = 3;
89
90        return msn_soap_send_request(req);
91}
92
93static void msn_soap_handle_response(struct http_request *http_req);
94
95static int msn_soap_send_request(struct msn_soap_req_data *soap_req)
96{
97        char *http_req;
98        char *soap_action = NULL;
99        url_t url;
100
101        soap_req->build_request(soap_req);
102
103        if (soap_req->action) {
104                soap_action = g_strdup_printf("SOAPAction: \"%s\"\r\n", soap_req->action);
105        }
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                msn_soap_free(soap_req);
130                return;
131        }
132
133        msn_soap_debug_print(http_req->reply_headers, http_req->reply_body);
134
135        if (http_req->body_size > 0) {
136                struct xt_parser *parser;
137                struct xt_node *err;
138
139                parser = xt_new(soap_req->xml_parser, soap_req);
140                xt_feed(parser, http_req->reply_body, http_req->body_size);
141                if (http_req->status_code == 500 &&
142                    (err = xt_find_path(parser->root, "soap:Body/soap:Fault/detail/errorcode")) &&
143                    err->text_len > 0) {
144                        if (strcmp(err->text, "PassportAuthFail") == 0) {
145                                xt_free(parser);
146                                st = MSN_SOAP_REAUTH;
147                                goto fail;
148                        }
149                        /* TODO: Handle/report other errors. */
150                }
151
152                xt_handle(parser, NULL, -1);
153                xt_free(parser);
154        }
155
156        if (http_req->status_code != 200) {
157                soap_req->error = g_strdup(http_req->status_string);
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        g_free(soap_req->error);
167        soap_req->url = soap_req->action = soap_req->payload = soap_req->error = NULL;
168
169        if (st == MSN_SOAP_RETRY && --soap_req->ttl) {
170                msn_soap_send_request(soap_req);
171        } else if (st == MSN_SOAP_REAUTH) {
172                struct msn_data *md = soap_req->ic->proto_data;
173
174                if (!(md->flags & MSN_REAUTHING)) {
175                        /* Nonce shouldn't actually be touched for re-auths. */
176                        msn_soap_passport_sso_request(soap_req->ic, "blaataap");
177                        md->flags |= MSN_REAUTHING;
178                }
179                md->soapq = g_slist_append(md->soapq, soap_req);
180        } else {
181                soap_req->free_data(soap_req);
182                g_free(soap_req);
183        }
184}
185
186static char *msn_soap_abservice_build(const char *body_fmt, const char *scenario, const char *ticket, ...)
187{
188        va_list params;
189        char *ret, *format, *body;
190
191        format = g_markup_printf_escaped(SOAP_ABSERVICE_PAYLOAD, scenario, ticket);
192
193        va_start(params, ticket);
194        body = g_strdup_vprintf(body_fmt, params);
195        va_end(params);
196
197        ret = g_strdup_printf(format, body);
198        g_free(body);
199        g_free(format);
200
201        return ret;
202}
203
204static void msn_soap_debug_print(const char *headers, const char *payload)
205{
206        char *s;
207
208        if (!getenv("BITLBEE_DEBUG")) {
209                return;
210        }
211        fprintf(stderr, "\n\x1b[90mSOAP:\n");
212
213        if (headers) {
214                if ((s = strstr(headers, "\r\n\r\n"))) {
215                        fwrite(headers, s - headers + 4, 1, stderr);
216                } else {
217                        fwrite(headers, strlen(headers), 1, stderr);
218                }
219        }
220
221        if (payload) {
222                struct xt_node *xt = xt_from_string(payload, 0);
223                if (xt) {
224                        xt_print(xt);
225                }
226                xt_free_node(xt);
227        }
228        fprintf(stderr, "\n\x1b[97m\n");
229}
230
231int msn_soapq_flush(struct im_connection *ic, gboolean resend)
232{
233        struct msn_data *md = ic->proto_data;
234
235        while (md->soapq) {
236                if (resend) {
237                        msn_soap_send_request((struct msn_soap_req_data*) md->soapq->data);
238                } else {
239                        msn_soap_free((struct msn_soap_req_data*) md->soapq->data);
240                }
241                md->soapq = g_slist_remove(md->soapq, md->soapq->data);
242        }
243
244        return MSN_SOAP_OK;
245}
246
247static void msn_soap_free(struct msn_soap_req_data *soap_req)
248{
249        soap_req->free_data(soap_req);
250        g_free(soap_req->url);
251        g_free(soap_req->action);
252        g_free(soap_req->payload);
253        g_free(soap_req->error);
254        g_free(soap_req);
255}
256
257
258/* passport_sso: Authentication MSNP15+ */
259
260struct msn_soap_passport_sso_data {
261        char *nonce;
262        char *secret;
263        char *error;
264        char *redirect;
265};
266
267static int msn_soap_passport_sso_build_request(struct msn_soap_req_data *soap_req)
268{
269        struct msn_soap_passport_sso_data *sd = soap_req->data;
270        struct im_connection *ic = soap_req->ic;
271        struct msn_data *md = ic->proto_data;
272
273        if (sd->redirect) {
274                soap_req->url = sd->redirect;
275                sd->redirect = NULL;
276        }
277        /* MS changed this URL and broke the old MSN-specific one. The generic
278           one works, forwarding us to a msn.com URL that works. Takes an extra
279           second, but that's better than not being able to log in at all. :-/
280        else if( g_str_has_suffix( ic->acc->user, "@msn.com" ) )
281                soap_req->url = g_strdup( SOAP_PASSPORT_SSO_URL_MSN );
282        */
283        else {
284                soap_req->url = g_strdup(SOAP_PASSPORT_SSO_URL);
285        }
286
287        soap_req->payload = g_markup_printf_escaped(SOAP_PASSPORT_SSO_PAYLOAD,
288                                                    ic->acc->user, ic->acc->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        }
304        id += strlen(id) - 1;
305        if (*id == '1' &&
306            (p = xt_find_path(node, "../../wst:RequestedProofToken/wst:BinarySecret")) &&
307            p->text) {
308                sd->secret = g_strdup(p->text);
309        }
310
311        *id -= '1';
312        if (*id >= 0 && *id < sizeof(md->tokens) / sizeof(md->tokens[0])) {
313                g_free(md->tokens[(int) *id]);
314                md->tokens[(int) *id] = g_strdup(node->text);
315        }
316
317        return XT_HANDLED;
318}
319
320static xt_status msn_soap_passport_failure(struct xt_node *node, gpointer data)
321{
322        struct msn_soap_req_data *soap_req = data;
323        struct msn_soap_passport_sso_data *sd = soap_req->data;
324        struct xt_node *code = xt_find_node(node->children, "faultcode");
325        struct xt_node *string = xt_find_node(node->children, "faultstring");
326        struct xt_node *reqstatus = xt_find_path(node, "psf:pp/psf:reqstatus");
327        struct xt_node *url;
328
329        if (code == NULL || code->text_len == 0) {
330                sd->error = g_strdup("Unknown error");
331        } else if (strcmp(code->text, "psf:Redirect") == 0 &&
332                   (url = xt_find_node(node->children, "psf:redirectUrl")) &&
333                   url->text_len > 0) {
334                sd->redirect = g_strdup(url->text);
335        } else if (reqstatus && strcmp(reqstatus->text, "0x800488fe") == 0) {
336                char *msg = "Location blocked. Log in to live.com, go to recent activity and click 'this was me'";
337                sd->error = g_strdup_printf("%s (%s)", code->text, msg);
338        } else {
339                sd->error = g_strdup_printf("%s (%s)", code->text, string && string->text_len ?
340                                            string->text : "no description available");
341        }
342
343        return XT_HANDLED;
344}
345
346static const struct xt_handler_entry msn_soap_passport_sso_parser[] = {
347        { "wsse:BinarySecurityToken", "wst:RequestedSecurityToken", msn_soap_passport_sso_token },
348        { "S:Fault", "S:Envelope", msn_soap_passport_failure },
349        { "S:Fault", "wst:RequestSecurityTokenResponse", msn_soap_passport_failure },
350        { NULL, NULL, NULL }
351};
352
353static char *msn_key_fuckery(char *key, int key_len, char *type)
354{
355        unsigned char hash1[20 + strlen(type) + 1];
356        unsigned char hash2[20];
357        char *ret;
358
359        sha1_hmac(key, key_len, type, 0, hash1);
360        strcpy((char *) hash1 + 20, type);
361        sha1_hmac(key, key_len, (char *) hash1, sizeof(hash1) - 1, hash2);
362
363        /* This is okay as hash1 is read completely before it's overwritten. */
364        sha1_hmac(key, key_len, (char *) hash1, 20, hash1);
365        sha1_hmac(key, key_len, (char *) hash1, sizeof(hash1) - 1, hash1);
366
367        ret = g_malloc(24);
368        memcpy(ret, hash2, 20);
369        memcpy(ret + 20, hash1, 4);
370        return ret;
371}
372
373static int msn_soap_passport_sso_handle_response(struct msn_soap_req_data *soap_req)
374{
375        struct msn_soap_passport_sso_data *sd = soap_req->data;
376        struct im_connection *ic = soap_req->ic;
377        struct msn_data *md = ic->proto_data;
378        char *key1, *key2, *key3, *blurb64;
379        int key1_len;
380        unsigned char *padnonce, *des3res;
381
382        struct {
383                unsigned int uStructHeaderSize; // 28. Does not count data
384                unsigned int uCryptMode; // CRYPT_MODE_CBC (1)
385                unsigned int uCipherType; // TripleDES (0x6603)
386                unsigned int uHashType; // SHA1 (0x8004)
387                unsigned int uIVLen;    // 8
388                unsigned int uHashLen;  // 20
389                unsigned int uCipherLen; // 72
390                unsigned char iv[8];
391                unsigned char hash[20];
392                unsigned char cipherbytes[72];
393        } blurb = {
394                GUINT32_TO_LE(28),
395                GUINT32_TO_LE(1),
396                GUINT32_TO_LE(0x6603),
397                GUINT32_TO_LE(0x8004),
398                GUINT32_TO_LE(8),
399                GUINT32_TO_LE(20),
400                GUINT32_TO_LE(72),
401        };
402
403        if (sd->redirect) {
404                return MSN_SOAP_RETRY;
405        }
406
407        if (md->soapq) {
408                md->flags &= ~MSN_REAUTHING;
409                return msn_soapq_flush(ic, TRUE);
410        }
411
412        if (sd->secret == NULL) {
413                msn_auth_got_passport_token(ic, NULL, sd->error ? sd->error : soap_req->error);
414                return MSN_SOAP_OK;
415        }
416
417        key1_len = base64_decode(sd->secret, (unsigned char **) &key1);
418
419        key2 = msn_key_fuckery(key1, key1_len, "WS-SecureConversationSESSION KEY HASH");
420        key3 = msn_key_fuckery(key1, key1_len, "WS-SecureConversationSESSION KEY ENCRYPTION");
421
422        sha1_hmac(key2, 24, sd->nonce, 0, blurb.hash);
423        padnonce = g_malloc(strlen(sd->nonce) + 8);
424        strcpy((char *) padnonce, sd->nonce);
425        memset(padnonce + strlen(sd->nonce), 8, 8);
426
427        random_bytes(blurb.iv, 8);
428
429        ssl_des3_encrypt((unsigned char *) key3, 24, padnonce, strlen(sd->nonce) + 8, blurb.iv, &des3res);
430        memcpy(blurb.cipherbytes, des3res, 72);
431
432        blurb64 = base64_encode((unsigned char *) &blurb, sizeof(blurb));
433        msn_auth_got_passport_token(ic, blurb64, NULL);
434
435        g_free(padnonce);
436        g_free(blurb64);
437        g_free(des3res);
438        g_free(key1);
439        g_free(key2);
440        g_free(key3);
441
442        return MSN_SOAP_OK;
443}
444
445static int msn_soap_passport_sso_free_data(struct msn_soap_req_data *soap_req)
446{
447        struct msn_soap_passport_sso_data *sd = soap_req->data;
448
449        g_free(sd->nonce);
450        g_free(sd->secret);
451        g_free(sd->error);
452        g_free(sd->redirect);
453        g_free(sd);
454
455        return MSN_SOAP_OK;
456}
457
458int msn_soap_passport_sso_request(struct im_connection *ic, const char *nonce)
459{
460        struct msn_soap_passport_sso_data *sd = g_new0(struct msn_soap_passport_sso_data, 1);
461
462        sd->nonce = g_strdup(nonce);
463
464        return msn_soap_start(ic, sd, msn_soap_passport_sso_build_request,
465                              msn_soap_passport_sso_parser,
466                              msn_soap_passport_sso_handle_response,
467                              msn_soap_passport_sso_free_data);
468}
469
470
471/* memlist: Fetching the membership list (NOT address book) */
472
473static int msn_soap_memlist_build_request(struct msn_soap_req_data *soap_req)
474{
475        struct msn_data *md = soap_req->ic->proto_data;
476
477        soap_req->url = g_strdup(SOAP_MEMLIST_URL);
478        soap_req->action = g_strdup(SOAP_MEMLIST_ACTION);
479        soap_req->payload = msn_soap_abservice_build(SOAP_MEMLIST_PAYLOAD, "Initial", md->tokens[1]);
480
481        return 1;
482}
483
484static xt_status msn_soap_memlist_member(struct xt_node *node, gpointer data)
485{
486        bee_user_t *bu;
487        struct msn_buddy_data *bd;
488        struct xt_node *p;
489        char *role = NULL, *handle = NULL;
490        struct msn_soap_req_data *soap_req = data;
491        struct im_connection *ic = soap_req->ic;
492
493        if ((p = xt_find_path(node, "../../MemberRole"))) {
494                role = p->text;
495        }
496
497        if ((p = xt_find_node(node->children, "PassportName"))) {
498                handle = p->text;
499        }
500
501        if (!role || !handle ||
502            !((bu = bee_user_by_handle(ic->bee, ic, handle)) ||
503              (bu = bee_user_new(ic->bee, ic, handle, 0)))) {
504                return XT_HANDLED;
505        }
506
507        bd = bu->data;
508        if (strcmp(role, "Allow") == 0) {
509                bd->flags |= MSN_BUDDY_AL;
510                ic->permit = g_slist_prepend(ic->permit, g_strdup(handle));
511        } else if (strcmp(role, "Block") == 0) {
512                bd->flags |= MSN_BUDDY_BL;
513                ic->deny = g_slist_prepend(ic->deny, g_strdup(handle));
514        } else if (strcmp(role, "Reverse") == 0) {
515                bd->flags |= MSN_BUDDY_RL;
516        } else if (strcmp(role, "Pending") == 0) {
517                bd->flags |= MSN_BUDDY_PL;
518        }
519
520        if (getenv("BITLBEE_DEBUG")) {
521                fprintf(stderr, "%p %s %d\n", bu, handle, bd->flags);
522        }
523
524        return XT_HANDLED;
525}
526
527static const struct xt_handler_entry msn_soap_memlist_parser[] = {
528        { "Member", "Members", msn_soap_memlist_member },
529        { NULL,               NULL,     NULL                        }
530};
531
532static int msn_soap_memlist_handle_response(struct msn_soap_req_data *soap_req)
533{
534        msn_soap_addressbook_request(soap_req->ic);
535
536        return MSN_SOAP_OK;
537}
538
539static int msn_soap_memlist_free_data(struct msn_soap_req_data *soap_req)
540{
541        return 0;
542}
543
544int msn_soap_memlist_request(struct im_connection *ic)
545{
546        return msn_soap_start(ic, NULL, msn_soap_memlist_build_request,
547                              msn_soap_memlist_parser,
548                              msn_soap_memlist_handle_response,
549                              msn_soap_memlist_free_data);
550}
551
552/* Variant: Adding/Removing people */
553struct msn_soap_memlist_edit_data {
554        char *handle;
555        gboolean add;
556        msn_buddy_flags_t list;
557};
558
559static int msn_soap_memlist_edit_build_request(struct msn_soap_req_data *soap_req)
560{
561        struct msn_data *md = soap_req->ic->proto_data;
562        struct msn_soap_memlist_edit_data *med = soap_req->data;
563        char *add, *scenario, *list;
564
565        soap_req->url = g_strdup(SOAP_MEMLIST_URL);
566        if (med->add) {
567                soap_req->action = g_strdup(SOAP_MEMLIST_ADD_ACTION);
568                add = "Add";
569        } else {
570                soap_req->action = g_strdup(SOAP_MEMLIST_DEL_ACTION);
571                add = "Delete";
572        }
573        switch (med->list) {
574        case MSN_BUDDY_AL:
575                scenario = "BlockUnblock";
576                list = "Allow";
577                break;
578        case MSN_BUDDY_BL:
579                scenario = "BlockUnblock";
580                list = "Block";
581                break;
582        case MSN_BUDDY_RL:
583                scenario = "Timer";
584                list = "Reverse";
585                break;
586        case MSN_BUDDY_PL:
587        default:
588                scenario = "Timer";
589                list = "Pending";
590                break;
591        }
592        soap_req->payload = msn_soap_abservice_build(SOAP_MEMLIST_EDIT_PAYLOAD,
593                                                     scenario, md->tokens[1], add, list, med->handle, add);
594
595        return 1;
596}
597
598static int msn_soap_memlist_edit_handle_response(struct msn_soap_req_data *soap_req)
599{
600        return MSN_SOAP_OK;
601}
602
603static int msn_soap_memlist_edit_free_data(struct msn_soap_req_data *soap_req)
604{
605        struct msn_soap_memlist_edit_data *med = soap_req->data;
606
607        g_free(med->handle);
608        g_free(med);
609
610        return 0;
611}
612
613int msn_soap_memlist_edit(struct im_connection *ic, const char *handle, gboolean add, int list)
614{
615        struct msn_soap_memlist_edit_data *med;
616
617        med = g_new0(struct msn_soap_memlist_edit_data, 1);
618        med->handle = g_strdup(handle);
619        med->add = add;
620        med->list = list;
621
622        return msn_soap_start(ic, med, msn_soap_memlist_edit_build_request,
623                              NULL,
624                              msn_soap_memlist_edit_handle_response,
625                              msn_soap_memlist_edit_free_data);
626}
627
628
629/* addressbook: Fetching the membership list (NOT address book) */
630
631static int msn_soap_addressbook_build_request(struct msn_soap_req_data *soap_req)
632{
633        struct msn_data *md = soap_req->ic->proto_data;
634
635        soap_req->url = g_strdup(SOAP_ADDRESSBOOK_URL);
636        soap_req->action = g_strdup(SOAP_ADDRESSBOOK_ACTION);
637        soap_req->payload = msn_soap_abservice_build(SOAP_ADDRESSBOOK_PAYLOAD, "Initial", md->tokens[1]);
638
639        return 1;
640}
641
642static xt_status msn_soap_addressbook_group(struct xt_node *node, gpointer data)
643{
644        struct xt_node *p;
645        char *id = NULL, *name = NULL;
646        struct msn_soap_req_data *soap_req = data;
647        struct msn_data *md = soap_req->ic->proto_data;
648
649        if ((p = xt_find_path(node, "../groupId"))) {
650                id = p->text;
651        }
652
653        if ((p = xt_find_node(node->children, "name"))) {
654                name = p->text;
655        }
656
657        if (id && name) {
658                struct msn_group *mg = g_new0(struct msn_group, 1);
659                mg->id = g_strdup(id);
660                mg->name = g_strdup(name);
661                md->groups = g_slist_prepend(md->groups, mg);
662        }
663
664        if (getenv("BITLBEE_DEBUG")) {
665                fprintf(stderr, "%s %s\n", id, name);
666        }
667
668        return XT_HANDLED;
669}
670
671static xt_status msn_soap_addressbook_contact(struct xt_node *node, gpointer data)
672{
673        bee_user_t *bu;
674        struct msn_buddy_data *bd;
675        struct xt_node *p;
676        char *id = NULL, *type = NULL, *handle = NULL, *is_msgr = "false",
677        *display_name = NULL, *group_id = NULL;
678        struct msn_soap_req_data *soap_req = data;
679        struct im_connection *ic = soap_req->ic;
680        struct msn_group *group;
681
682        if ((p = xt_find_path(node, "../contactId"))) {
683                id = p->text;
684        }
685        if ((p = xt_find_node(node->children, "contactType"))) {
686                type = p->text;
687        }
688        if ((p = xt_find_node(node->children, "passportName"))) {
689                handle = p->text;
690        }
691        if ((p = xt_find_node(node->children, "displayName"))) {
692                display_name = p->text;
693        }
694        if ((p = xt_find_node(node->children, "isMessengerUser"))) {
695                is_msgr = p->text;
696        }
697        if ((p = xt_find_path(node, "groupIds/guid"))) {
698                group_id = p->text;
699        }
700
701        if (type && g_strcasecmp(type, "me") == 0) {
702                set_t *set = set_find(&ic->acc->set, "display_name");
703                g_free(set->value);
704                set->value = g_strdup(display_name);
705
706                /* Try to fetch the profile; if the user has one, that's where
707                   we can find the persistent display_name. */
708                if ((p = xt_find_node(node->children, "CID")) && p->text) {
709                        msn_soap_profile_get(ic, p->text);
710                }
711
712                return XT_HANDLED;
713        }
714
715        if (!bool2int(is_msgr) || handle == NULL) {
716                return XT_HANDLED;
717        }
718
719        if (!(bu = bee_user_by_handle(ic->bee, ic, handle)) &&
720            !(bu = bee_user_new(ic->bee, ic, handle, 0))) {
721                return XT_HANDLED;
722        }
723
724        bd = bu->data;
725        bd->flags |= MSN_BUDDY_FL;
726        g_free(bd->cid);
727        bd->cid = g_strdup(id);
728
729        imcb_rename_buddy(ic, handle, display_name);
730
731        if (group_id && (group = msn_group_by_id(ic, group_id))) {
732                imcb_add_buddy(ic, handle, group->name);
733        }
734
735        if (getenv("BITLBEE_DEBUG")) {
736                fprintf(stderr, "%s %s %s %s\n", id, type, handle, display_name);
737        }
738
739        return XT_HANDLED;
740}
741
742static const struct xt_handler_entry msn_soap_addressbook_parser[] = {
743        { "contactInfo", "Contact", msn_soap_addressbook_contact },
744        { "groupInfo", "Group", msn_soap_addressbook_group },
745        { NULL,               NULL,     NULL                        }
746};
747
748static int msn_soap_addressbook_handle_response(struct msn_soap_req_data *soap_req)
749{
750        GSList *l;
751        int wtf = 0;
752
753        for (l = soap_req->ic->bee->users; l; l = l->next) {
754                struct bee_user *bu = l->data;
755                struct msn_buddy_data *bd = bu->data;
756
757                if (bu->ic == soap_req->ic && bd) {
758                        msn_buddy_ask(bu);
759
760                        if ((bd->flags & (MSN_BUDDY_AL | MSN_BUDDY_BL)) ==
761                            (MSN_BUDDY_AL | MSN_BUDDY_BL)) {
762                                /* both allow and block, delete block, add wtf */
763                                bd->flags &= ~MSN_BUDDY_BL;
764                                wtf++;
765                        }
766
767
768                        if ((bd->flags & (MSN_BUDDY_AL | MSN_BUDDY_BL)) == 0) {
769                                /* neither allow or block, add allow */
770                                bd->flags |= MSN_BUDDY_AL;
771                        }
772                }
773        }
774
775        if (wtf) {
776                imcb_log(soap_req->ic, "Warning: %d contacts were in both your "
777                         "block and your allow list. Assuming they're all "
778                         "allowed.", wtf);
779        }
780
781        msn_auth_got_contact_list(soap_req->ic);
782
783        return MSN_SOAP_OK;
784}
785
786static int msn_soap_addressbook_free_data(struct msn_soap_req_data *soap_req)
787{
788        return 0;
789}
790
791int msn_soap_addressbook_request(struct im_connection *ic)
792{
793        return msn_soap_start(ic, NULL, msn_soap_addressbook_build_request,
794                              msn_soap_addressbook_parser,
795                              msn_soap_addressbook_handle_response,
796                              msn_soap_addressbook_free_data);
797}
798
799/* Variant: Change our display name. */
800static int msn_soap_ab_namechange_build_request(struct msn_soap_req_data *soap_req)
801{
802        struct msn_data *md = soap_req->ic->proto_data;
803
804        soap_req->url = g_strdup(SOAP_ADDRESSBOOK_URL);
805        soap_req->action = g_strdup(SOAP_AB_NAMECHANGE_ACTION);
806        soap_req->payload = msn_soap_abservice_build(SOAP_AB_NAMECHANGE_PAYLOAD,
807                                                     "Timer", md->tokens[1], (char *) soap_req->data);
808
809        return 1;
810}
811
812static int msn_soap_ab_namechange_handle_response(struct msn_soap_req_data *soap_req)
813{
814        /* TODO: Ack the change? Not sure what the NAKs look like.. */
815        return MSN_SOAP_OK;
816}
817
818static int msn_soap_ab_namechange_free_data(struct msn_soap_req_data *soap_req)
819{
820        g_free(soap_req->data);
821        return 0;
822}
823
824int msn_soap_addressbook_set_display_name(struct im_connection *ic, const char *new)
825{
826        return msn_soap_start(ic, g_strdup(new),
827                              msn_soap_ab_namechange_build_request,
828                              NULL,
829                              msn_soap_ab_namechange_handle_response,
830                              msn_soap_ab_namechange_free_data);
831}
832
833/* Add a contact. */
834static int msn_soap_ab_contact_add_build_request(struct msn_soap_req_data *soap_req)
835{
836        struct msn_data *md = soap_req->ic->proto_data;
837        bee_user_t *bu = soap_req->data;
838
839        soap_req->url = g_strdup(SOAP_ADDRESSBOOK_URL);
840        soap_req->action = g_strdup(SOAP_AB_CONTACT_ADD_ACTION);
841        soap_req->payload = msn_soap_abservice_build(SOAP_AB_CONTACT_ADD_PAYLOAD,
842                                                     "ContactSave", md->tokens[1], bu->handle,
843                                                     bu->fullname ? bu->fullname : bu->handle);
844
845        return 1;
846}
847
848static xt_status msn_soap_ab_contact_add_cid(struct xt_node *node, gpointer data)
849{
850        struct msn_soap_req_data *soap_req = data;
851        bee_user_t *bu = soap_req->data;
852        struct msn_buddy_data *bd = bu->data;
853
854        g_free(bd->cid);
855        bd->cid = g_strdup(node->text);
856
857        return XT_HANDLED;
858}
859
860static const struct xt_handler_entry msn_soap_ab_contact_add_parser[] = {
861        { "guid", "ABContactAddResult", msn_soap_ab_contact_add_cid },
862        { NULL,               NULL,     NULL                        }
863};
864
865static int msn_soap_ab_contact_add_handle_response(struct msn_soap_req_data *soap_req)
866{
867        /* TODO: Ack the change? Not sure what the NAKs look like.. */
868        return MSN_SOAP_OK;
869}
870
871static int msn_soap_ab_contact_add_free_data(struct msn_soap_req_data *soap_req)
872{
873        return 0;
874}
875
876int msn_soap_ab_contact_add(struct im_connection *ic, bee_user_t *bu)
877{
878        return msn_soap_start(ic, bu,
879                              msn_soap_ab_contact_add_build_request,
880                              msn_soap_ab_contact_add_parser,
881                              msn_soap_ab_contact_add_handle_response,
882                              msn_soap_ab_contact_add_free_data);
883}
884
885/* Remove a contact. */
886static int msn_soap_ab_contact_del_build_request(struct msn_soap_req_data *soap_req)
887{
888        struct msn_data *md = soap_req->ic->proto_data;
889        const char *cid = soap_req->data;
890
891        soap_req->url = g_strdup(SOAP_ADDRESSBOOK_URL);
892        soap_req->action = g_strdup(SOAP_AB_CONTACT_DEL_ACTION);
893        soap_req->payload = msn_soap_abservice_build(SOAP_AB_CONTACT_DEL_PAYLOAD,
894                                                     "Timer", md->tokens[1], cid);
895
896        return 1;
897}
898
899static int msn_soap_ab_contact_del_handle_response(struct msn_soap_req_data *soap_req)
900{
901        /* TODO: Ack the change? Not sure what the NAKs look like.. */
902        return MSN_SOAP_OK;
903}
904
905static int msn_soap_ab_contact_del_free_data(struct msn_soap_req_data *soap_req)
906{
907        g_free(soap_req->data);
908        return 0;
909}
910
911int msn_soap_ab_contact_del(struct im_connection *ic, bee_user_t *bu)
912{
913        struct msn_buddy_data *bd = bu->data;
914
915        return msn_soap_start(ic, g_strdup(bd->cid),
916                              msn_soap_ab_contact_del_build_request,
917                              NULL,
918                              msn_soap_ab_contact_del_handle_response,
919                              msn_soap_ab_contact_del_free_data);
920}
921
922
923
924/* Storage stuff: Fetch profile. */
925static int msn_soap_profile_get_build_request(struct msn_soap_req_data *soap_req)
926{
927        struct msn_data *md = soap_req->ic->proto_data;
928
929        soap_req->url = g_strdup(SOAP_STORAGE_URL);
930        soap_req->action = g_strdup(SOAP_PROFILE_GET_ACTION);
931        soap_req->payload = g_markup_printf_escaped(SOAP_PROFILE_GET_PAYLOAD,
932                                                    md->tokens[3], (char *) soap_req->data);
933
934        return 1;
935}
936
937static xt_status msn_soap_profile_get_result(struct xt_node *node, gpointer data)
938{
939        struct msn_soap_req_data *soap_req = data;
940        struct im_connection *ic = soap_req->ic;
941        struct msn_data *md = soap_req->ic->proto_data;
942        struct xt_node *dn;
943
944        if ((dn = xt_find_node(node->children, "DisplayName")) && dn->text) {
945                set_t *set = set_find(&ic->acc->set, "display_name");
946                g_free(set->value);
947                set->value = g_strdup(dn->text);
948
949                md->flags |= MSN_GOT_PROFILE_DN;
950        }
951
952        return XT_HANDLED;
953}
954
955static xt_status msn_soap_profile_get_rid(struct xt_node *node, gpointer data)
956{
957        struct msn_soap_req_data *soap_req = data;
958        struct msn_data *md = soap_req->ic->proto_data;
959
960        g_free(md->profile_rid);
961        md->profile_rid = g_strdup(node->text);
962
963        return XT_HANDLED;
964}
965
966static const struct xt_handler_entry msn_soap_profile_get_parser[] = {
967        { "ExpressionProfile", "GetProfileResult", msn_soap_profile_get_result },
968        { "ResourceID",        "GetProfileResult", msn_soap_profile_get_rid },
969        { NULL,               NULL,     NULL                        }
970};
971
972static int msn_soap_profile_get_handle_response(struct msn_soap_req_data *soap_req)
973{
974        struct msn_data *md = soap_req->ic->proto_data;
975
976        md->flags |= MSN_GOT_PROFILE;
977        msn_ns_finish_login(soap_req->ic);
978
979        return MSN_SOAP_OK;
980}
981
982static int msn_soap_profile_get_free_data(struct msn_soap_req_data *soap_req)
983{
984        g_free(soap_req->data);
985        return 0;
986}
987
988int msn_soap_profile_get(struct im_connection *ic, const char *cid)
989{
990        return msn_soap_start(ic, g_strdup(cid),
991                              msn_soap_profile_get_build_request,
992                              msn_soap_profile_get_parser,
993                              msn_soap_profile_get_handle_response,
994                              msn_soap_profile_get_free_data);
995}
996
997/* Update profile (display name). */
998static int msn_soap_profile_set_dn_build_request(struct msn_soap_req_data *soap_req)
999{
1000        struct msn_data *md = soap_req->ic->proto_data;
1001
1002        soap_req->url = g_strdup(SOAP_STORAGE_URL);
1003        soap_req->action = g_strdup(SOAP_PROFILE_SET_DN_ACTION);
1004        soap_req->payload = g_markup_printf_escaped(SOAP_PROFILE_SET_DN_PAYLOAD,
1005                                                    md->tokens[3], md->profile_rid, (char *) soap_req->data);
1006
1007        return 1;
1008}
1009
1010static const struct xt_handler_entry msn_soap_profile_set_dn_parser[] = {
1011        { NULL,               NULL,     NULL                        }
1012};
1013
1014static int msn_soap_profile_set_dn_handle_response(struct msn_soap_req_data *soap_req)
1015{
1016        return MSN_SOAP_OK;
1017}
1018
1019static int msn_soap_profile_set_dn_free_data(struct msn_soap_req_data *soap_req)
1020{
1021        g_free(soap_req->data);
1022        return 0;
1023}
1024
1025int msn_soap_profile_set_dn(struct im_connection *ic, const char *dn)
1026{
1027        return msn_soap_start(ic, g_strdup(dn),
1028                              msn_soap_profile_set_dn_build_request,
1029                              msn_soap_profile_set_dn_parser,
1030                              msn_soap_profile_set_dn_handle_response,
1031                              msn_soap_profile_set_dn_free_data);
1032}
Note: See TracBrowser for help on using the repository browser.