source: protocols/msn/soap.c @ 5ebff60

Last change on this file since 5ebff60 was 5ebff60, checked in by dequis <dx@…>, at 2015-02-20T22:50:54Z

Reindent everything to K&R style with tabs

Used uncrustify, with the configuration file in ./doc/uncrustify.cfg

Commit author set to "Indent <please@…>" so that it's easier to
skip while doing git blame.

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