source: protocols/msn/soap.c @ 3c28bd4

Last change on this file since 3c28bd4 was 6a89740, checked in by dequis <dx@…>, at 2015-05-31T02:40:04Z

msn/soap: remove MSNP15 SSO crypto code

  • Property mode set to 100644
File size: 26.0 KB
RevLine 
[5ebff60]1/********************************************************************\
[4e4af1b]2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[0e788f5]4  * Copyright 2002-2012 Wilmer van der Gaast and others                *
[4e4af1b]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;
[6f10697]25  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
26  Fifth Floor, Boston, MA  02110-1301  USA
[4e4af1b]27*/
[e5a8118]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"
[523fb23]35#include "sha1.h"
[e5a8118]36#include "base64.h"
37#include "xmltree.h"
38#include <ctype.h>
39#include <errno.h>
40
[4e4af1b]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
[5ebff60]46typedef enum {
[e5a8118]47        MSN_SOAP_OK,
48        MSN_SOAP_RETRY,
[27053b5]49        MSN_SOAP_REAUTH,
[e5a8118]50        MSN_SOAP_ABORT,
51} msn_soap_result_t;
52
[12767e3]53struct msn_soap_req_data;
[5ebff60]54typedef int (*msn_soap_func) (struct msn_soap_req_data *);
[12767e3]55
[5ebff60]56struct msn_soap_req_data {
[e5a8118]57        void *data;
58        struct im_connection *ic;
59        int ttl;
[9f958f7]60        char *error;
[5ebff60]61
[e5a8118]62        char *url, *action, *payload;
63        struct http_request *http_req;
[5ebff60]64
[e5a8118]65        const struct xt_handler_entry *xml_parser;
66        msn_soap_func build_request, handle_response, free_data;
67};
68
[5ebff60]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);
[e5a8118]72
[5ebff60]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)
[e5a8118]79{
[5ebff60]80        struct msn_soap_req_data *req = g_new0(struct msn_soap_req_data, 1);
81
[e5a8118]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;
[5ebff60]89
90        return msn_soap_send_request(req);
[e5a8118]91}
92
[5ebff60]93static void msn_soap_handle_response(struct http_request *http_req);
[e5a8118]94
[5ebff60]95static int msn_soap_send_request(struct msn_soap_req_data *soap_req)
[e5a8118]96{
97        char *http_req;
[523fb23]98        char *soap_action = NULL;
[e5a8118]99        url_t url;
[5ebff60]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
[e5a8118]120        return soap_req->http_req != NULL;
121}
122
[5ebff60]123static void msn_soap_handle_response(struct http_request *http_req)
[e5a8118]124{
125        struct msn_soap_req_data *soap_req = http_req->data;
126        int st;
[5ebff60]127
128        if (g_slist_find(msn_connections, soap_req->ic) == NULL) {
129                msn_soap_free(soap_req);
[801b90b]130                return;
131        }
[5ebff60]132
133        msn_soap_debug_print(http_req->reply_headers, http_req->reply_body);
134
135        if (http_req->body_size > 0) {
[e5a8118]136                struct xt_parser *parser;
[4aa8a04]137                struct xt_node *err;
[5ebff60]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);
[27053b5]146                                st = MSN_SOAP_REAUTH;
147                                goto fail;
148                        }
149                        /* TODO: Handle/report other errors. */
[4aa8a04]150                }
[5ebff60]151
152                xt_handle(parser, NULL, -1);
153                xt_free(parser);
[e5a8118]154        }
[5ebff60]155
156        if (http_req->status_code != 200) {
157                soap_req->error = g_strdup(http_req->status_string);
[27053b5]158        }
[5ebff60]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) {
[27053b5]172                struct msn_data *md = soap_req->ic->proto_data;
[5ebff60]173
174                if (!(md->flags & MSN_REAUTHING)) {
[27053b5]175                        /* Nonce shouldn't actually be touched for re-auths. */
[6a89740]176                        msn_soap_passport_sso_request(soap_req->ic);
[5ebff60]177                        md->flags |= MSN_REAUTHING;
[27053b5]178                }
[5ebff60]179                md->soapq = g_slist_append(md->soapq, soap_req);
180        } else {
181                soap_req->free_data(soap_req);
182                g_free(soap_req);
[e5a8118]183        }
184}
185
[5ebff60]186static char *msn_soap_abservice_build(const char *body_fmt, const char *scenario, const char *ticket, ...)
[6ddb223]187{
188        va_list params;
189        char *ret, *format, *body;
[5ebff60]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
[6ddb223]201        return ret;
202}
203
[5ebff60]204static void msn_soap_debug_print(const char *headers, const char *payload)
[f2520b5]205{
206        char *s;
[5ebff60]207
208        if (!getenv("BITLBEE_DEBUG")) {
[f2520b5]209                return;
[3bd2f17]210        }
[693aca0]211        fprintf(stderr, "\n\x1b[90mSOAP:\n");
[5ebff60]212
213        if (headers) {
214                if ((s = strstr(headers, "\r\n\r\n"))) {
215                        write(2, headers, s - headers + 4);
216                } else {
217                        write(2, headers, strlen(headers));
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);
[f2520b5]227        }
[693aca0]228        fprintf(stderr, "\n\x1b[97m\n");
[f2520b5]229}
230
[5ebff60]231int msn_soapq_flush(struct im_connection *ic, gboolean resend)
[4aa8a04]232{
233        struct msn_data *md = ic->proto_data;
[5ebff60]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);
[4aa8a04]242        }
[5ebff60]243
[4aa8a04]244        return MSN_SOAP_OK;
245}
246
[5ebff60]247static void msn_soap_free(struct msn_soap_req_data *soap_req)
[4e1be76]248{
[5ebff60]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);
[4e1be76]255}
256
[bc090f0]257
[523fb23]258/* passport_sso: Authentication MSNP15+ */
259
[5ebff60]260struct msn_soap_passport_sso_data {
[660cb00]261        char *error;
[2c6b0f4]262        char *redirect;
[523fb23]263};
264
[5ebff60]265static int msn_soap_passport_sso_build_request(struct msn_soap_req_data *soap_req)
[523fb23]266{
[2c6b0f4]267        struct msn_soap_passport_sso_data *sd = soap_req->data;
[523fb23]268        struct im_connection *ic = soap_req->ic;
[5ebff60]269        char pass[MAX_PASSPORT_PWLEN + 1];
270
271        if (sd->redirect) {
[2c6b0f4]272                soap_req->url = sd->redirect;
273                sd->redirect = NULL;
[6a89740]274        } else {
[5ebff60]275                soap_req->url = g_strdup(SOAP_PASSPORT_SSO_URL);
276        }
277
278        strncpy(pass, ic->acc->pass, MAX_PASSPORT_PWLEN);
[385fbc4]279        pass[MAX_PASSPORT_PWLEN] = '\0';
[5ebff60]280        soap_req->payload = g_markup_printf_escaped(SOAP_PASSPORT_SSO_PAYLOAD,
[6a89740]281                                                    ic->acc->user, pass);
[5ebff60]282
[523fb23]283        return MSN_SOAP_OK;
284}
285
[5ebff60]286static xt_status msn_soap_passport_sso_token(struct xt_node *node, gpointer data)
[523fb23]287{
288        struct msn_soap_req_data *soap_req = data;
289        struct msn_data *md = soap_req->ic->proto_data;
290        char *id;
[5ebff60]291
292        if ((id = xt_find_attr(node, "Id")) == NULL) {
[523fb23]293                return XT_HANDLED;
[5ebff60]294        }
295        id += strlen(id) - 1;
296
[91d6e91]297        *id -= '1';
[5ebff60]298        if (*id >= 0 && *id < sizeof(md->tokens) / sizeof(md->tokens[0])) {
299                g_free(md->tokens[(int) *id]);
300                md->tokens[(int) *id] = g_strdup(node->text);
[91d6e91]301        }
[5ebff60]302
[523fb23]303        return XT_HANDLED;
304}
305
[5ebff60]306static xt_status msn_soap_passport_failure(struct xt_node *node, gpointer data)
[660cb00]307{
308        struct msn_soap_req_data *soap_req = data;
309        struct msn_soap_passport_sso_data *sd = soap_req->data;
[5ebff60]310        struct xt_node *code = xt_find_node(node->children, "faultcode");
311        struct xt_node *string = xt_find_node(node->children, "faultstring");
[2c6b0f4]312        struct xt_node *url;
[5ebff60]313
314        if (code == NULL || code->text_len == 0) {
315                sd->error = g_strdup("Unknown error");
316        } else if (strcmp(code->text, "psf:Redirect") == 0 &&
317                   (url = xt_find_node(node->children, "psf:redirectUrl")) &&
318                   url->text_len > 0) {
319                sd->redirect = g_strdup(url->text);
320        } else {
321                sd->error = g_strdup_printf("%s (%s)", code->text, string && string->text_len ?
322                                            string->text : "no description available");
323        }
324
[660cb00]325        return XT_HANDLED;
326}
327
[523fb23]328static const struct xt_handler_entry msn_soap_passport_sso_parser[] = {
329        { "wsse:BinarySecurityToken", "wst:RequestedSecurityToken", msn_soap_passport_sso_token },
[660cb00]330        { "S:Fault", "S:Envelope", msn_soap_passport_failure },
[523fb23]331        { NULL, NULL, NULL }
332};
333
[5ebff60]334static int msn_soap_passport_sso_handle_response(struct msn_soap_req_data *soap_req)
[523fb23]335{
336        struct msn_soap_passport_sso_data *sd = soap_req->data;
337        struct im_connection *ic = soap_req->ic;
[4aa8a04]338        struct msn_data *md = ic->proto_data;
[5ebff60]339
340        if (sd->redirect) {
[2c6b0f4]341                return MSN_SOAP_RETRY;
[76c89dc7]342        }
[5ebff60]343
344        if (md->soapq) {
345                md->flags &= ~MSN_REAUTHING;
346                return msn_soapq_flush(ic, TRUE);
347        }
348
[6a89740]349        if (md->tokens[0] == NULL) {
[5ebff60]350                msn_auth_got_passport_token(ic, NULL, sd->error ? sd->error : soap_req->error);
[660cb00]351                return MSN_SOAP_OK;
352        }
[523fb23]353
[6a89740]354        msn_auth_got_passport_token(ic, md->tokens[0], NULL);
[5ebff60]355
[523fb23]356        return MSN_SOAP_OK;
357}
358
[5ebff60]359static int msn_soap_passport_sso_free_data(struct msn_soap_req_data *soap_req)
[523fb23]360{
361        struct msn_soap_passport_sso_data *sd = soap_req->data;
[5ebff60]362
363        g_free(sd->error);
364        g_free(sd->redirect);
365        g_free(sd);
366
[523fb23]367        return MSN_SOAP_OK;
368}
369
[6a89740]370int msn_soap_passport_sso_request(struct im_connection *ic)
[523fb23]371{
[5ebff60]372        struct msn_soap_passport_sso_data *sd = g_new0(struct msn_soap_passport_sso_data, 1);
373
374        return msn_soap_start(ic, sd, msn_soap_passport_sso_build_request,
375                              msn_soap_passport_sso_parser,
376                              msn_soap_passport_sso_handle_response,
377                              msn_soap_passport_sso_free_data);
[523fb23]378}
379
380
[7db65b7]381/* memlist: Fetching the membership list (NOT address book) */
382
[5ebff60]383static int msn_soap_memlist_build_request(struct msn_soap_req_data *soap_req)
[7db65b7]384{
[7f34ce2]385        struct msn_data *md = soap_req->ic->proto_data;
[5ebff60]386
387        soap_req->url = g_strdup(SOAP_MEMLIST_URL);
388        soap_req->action = g_strdup(SOAP_MEMLIST_ACTION);
389        soap_req->payload = msn_soap_abservice_build(SOAP_MEMLIST_PAYLOAD, "Initial", md->tokens[1]);
390
[7db65b7]391        return 1;
392}
393
[5ebff60]394static xt_status msn_soap_memlist_member(struct xt_node *node, gpointer data)
[7f34ce2]395{
396        bee_user_t *bu;
397        struct msn_buddy_data *bd;
398        struct xt_node *p;
399        char *role = NULL, *handle = NULL;
400        struct msn_soap_req_data *soap_req = data;
401        struct im_connection *ic = soap_req->ic;
[5ebff60]402
403        if ((p = xt_find_path(node, "../../MemberRole"))) {
[7f34ce2]404                role = p->text;
[5ebff60]405        }
406
407        if ((p = xt_find_node(node->children, "PassportName"))) {
[7f34ce2]408                handle = p->text;
[5ebff60]409        }
410
411        if (!role || !handle ||
412            !((bu = bee_user_by_handle(ic->bee, ic, handle)) ||
413              (bu = bee_user_new(ic->bee, ic, handle, 0)))) {
[7f34ce2]414                return XT_HANDLED;
[5ebff60]415        }
416
[7f34ce2]417        bd = bu->data;
[5ebff60]418        if (strcmp(role, "Allow") == 0) {
[7f34ce2]419                bd->flags |= MSN_BUDDY_AL;
[5ebff60]420                ic->permit = g_slist_prepend(ic->permit, g_strdup(handle));
421        } else if (strcmp(role, "Block") == 0) {
[7f34ce2]422                bd->flags |= MSN_BUDDY_BL;
[5ebff60]423                ic->deny = g_slist_prepend(ic->deny, g_strdup(handle));
424        } else if (strcmp(role, "Reverse") == 0) {
[7f34ce2]425                bd->flags |= MSN_BUDDY_RL;
[5ebff60]426        } else if (strcmp(role, "Pending") == 0) {
[7f34ce2]427                bd->flags |= MSN_BUDDY_PL;
[5ebff60]428        }
429
430        if (getenv("BITLBEE_DEBUG")) {
431                fprintf(stderr, "%p %s %d\n", bu, handle, bd->flags);
432        }
433
[7f34ce2]434        return XT_HANDLED;
435}
436
[7db65b7]437static const struct xt_handler_entry msn_soap_memlist_parser[] = {
[7f34ce2]438        { "Member", "Members", msn_soap_memlist_member },
[7db65b7]439        { NULL,               NULL,     NULL                        }
440};
441
[5ebff60]442static int msn_soap_memlist_handle_response(struct msn_soap_req_data *soap_req)
[7db65b7]443{
[5ebff60]444        msn_soap_addressbook_request(soap_req->ic);
445
[7f34ce2]446        return MSN_SOAP_OK;
[7db65b7]447}
448
[5ebff60]449static int msn_soap_memlist_free_data(struct msn_soap_req_data *soap_req)
[7db65b7]450{
451        return 0;
452}
453
[5ebff60]454int msn_soap_memlist_request(struct im_connection *ic)
[7db65b7]455{
[5ebff60]456        return msn_soap_start(ic, NULL, msn_soap_memlist_build_request,
457                              msn_soap_memlist_parser,
458                              msn_soap_memlist_handle_response,
459                              msn_soap_memlist_free_data);
[7db65b7]460}
[7f34ce2]461
[193dc74]462/* Variant: Adding/Removing people */
[5ebff60]463struct msn_soap_memlist_edit_data {
[193dc74]464        char *handle;
465        gboolean add;
466        msn_buddy_flags_t list;
467};
468
[5ebff60]469static int msn_soap_memlist_edit_build_request(struct msn_soap_req_data *soap_req)
[193dc74]470{
471        struct msn_data *md = soap_req->ic->proto_data;
472        struct msn_soap_memlist_edit_data *med = soap_req->data;
473        char *add, *scenario, *list;
[5ebff60]474
475        soap_req->url = g_strdup(SOAP_MEMLIST_URL);
476        if (med->add) {
477                soap_req->action = g_strdup(SOAP_MEMLIST_ADD_ACTION);
[193dc74]478                add = "Add";
[5ebff60]479        } else {
480                soap_req->action = g_strdup(SOAP_MEMLIST_DEL_ACTION);
[193dc74]481                add = "Delete";
482        }
[5ebff60]483        switch (med->list) {
[193dc74]484        case MSN_BUDDY_AL:
485                scenario = "BlockUnblock";
486                list = "Allow";
487                break;
488        case MSN_BUDDY_BL:
489                scenario = "BlockUnblock";
490                list = "Block";
491                break;
492        case MSN_BUDDY_RL:
493                scenario = "Timer";
494                list = "Reverse";
495                break;
496        case MSN_BUDDY_PL:
497        default:
498                scenario = "Timer";
499                list = "Pending";
500                break;
501        }
[5ebff60]502        soap_req->payload = msn_soap_abservice_build(SOAP_MEMLIST_EDIT_PAYLOAD,
503                                                     scenario, md->tokens[1], add, list, med->handle, add);
504
[193dc74]505        return 1;
506}
507
[5ebff60]508static int msn_soap_memlist_edit_handle_response(struct msn_soap_req_data *soap_req)
[193dc74]509{
510        return MSN_SOAP_OK;
511}
512
[5ebff60]513static int msn_soap_memlist_edit_free_data(struct msn_soap_req_data *soap_req)
[193dc74]514{
515        struct msn_soap_memlist_edit_data *med = soap_req->data;
[5ebff60]516
517        g_free(med->handle);
518        g_free(med);
519
[193dc74]520        return 0;
521}
522
[5ebff60]523int msn_soap_memlist_edit(struct im_connection *ic, const char *handle, gboolean add, int list)
[193dc74]524{
525        struct msn_soap_memlist_edit_data *med;
[5ebff60]526
527        med = g_new0(struct msn_soap_memlist_edit_data, 1);
528        med->handle = g_strdup(handle);
[193dc74]529        med->add = add;
530        med->list = list;
[5ebff60]531
532        return msn_soap_start(ic, med, msn_soap_memlist_edit_build_request,
533                              NULL,
534                              msn_soap_memlist_edit_handle_response,
535                              msn_soap_memlist_edit_free_data);
[193dc74]536}
537
[7f34ce2]538
539/* addressbook: Fetching the membership list (NOT address book) */
540
[5ebff60]541static int msn_soap_addressbook_build_request(struct msn_soap_req_data *soap_req)
[7f34ce2]542{
543        struct msn_data *md = soap_req->ic->proto_data;
[5ebff60]544
545        soap_req->url = g_strdup(SOAP_ADDRESSBOOK_URL);
546        soap_req->action = g_strdup(SOAP_ADDRESSBOOK_ACTION);
547        soap_req->payload = msn_soap_abservice_build(SOAP_ADDRESSBOOK_PAYLOAD, "Initial", md->tokens[1]);
548
[7f34ce2]549        return 1;
550}
551
[5ebff60]552static xt_status msn_soap_addressbook_group(struct xt_node *node, gpointer data)
[7f34ce2]553{
554        struct xt_node *p;
555        char *id = NULL, *name = NULL;
556        struct msn_soap_req_data *soap_req = data;
[ff27648]557        struct msn_data *md = soap_req->ic->proto_data;
[5ebff60]558
559        if ((p = xt_find_path(node, "../groupId"))) {
[7f34ce2]560                id = p->text;
[5ebff60]561        }
562
563        if ((p = xt_find_node(node->children, "name"))) {
[7f34ce2]564                name = p->text;
[ff27648]565        }
[5ebff60]566
567        if (id && name) {
568                struct msn_group *mg = g_new0(struct msn_group, 1);
569                mg->id = g_strdup(id);
570                mg->name = g_strdup(name);
571                md->groups = g_slist_prepend(md->groups, mg);
572        }
573
574        if (getenv("BITLBEE_DEBUG")) {
575                fprintf(stderr, "%s %s\n", id, name);
576        }
577
[7f34ce2]578        return XT_HANDLED;
579}
580
[5ebff60]581static xt_status msn_soap_addressbook_contact(struct xt_node *node, gpointer data)
[7f34ce2]582{
583        bee_user_t *bu;
584        struct msn_buddy_data *bd;
585        struct xt_node *p;
[9b01339]586        char *id = NULL, *type = NULL, *handle = NULL, *is_msgr = "false",
[5ebff60]587        *display_name = NULL, *group_id = NULL;
[7f34ce2]588        struct msn_soap_req_data *soap_req = data;
589        struct im_connection *ic = soap_req->ic;
[ff27648]590        struct msn_group *group;
[5ebff60]591
592        if ((p = xt_find_path(node, "../contactId"))) {
[7f34ce2]593                id = p->text;
[5ebff60]594        }
595        if ((p = xt_find_node(node->children, "contactType"))) {
[7f34ce2]596                type = p->text;
[5ebff60]597        }
598        if ((p = xt_find_node(node->children, "passportName"))) {
[7f34ce2]599                handle = p->text;
[5ebff60]600        }
601        if ((p = xt_find_node(node->children, "displayName"))) {
[7f34ce2]602                display_name = p->text;
[5ebff60]603        }
604        if ((p = xt_find_node(node->children, "isMessengerUser"))) {
[9b01339]605                is_msgr = p->text;
[5ebff60]606        }
607        if ((p = xt_find_path(node, "groupIds/guid"))) {
[ff27648]608                group_id = p->text;
[5ebff60]609        }
610
611        if (type && g_strcasecmp(type, "me") == 0) {
612                set_t *set = set_find(&ic->acc->set, "display_name");
613                g_free(set->value);
614                set->value = g_strdup(display_name);
615
[80175a1]616                /* Try to fetch the profile; if the user has one, that's where
617                   we can find the persistent display_name. */
[5ebff60]618                if ((p = xt_find_node(node->children, "CID")) && p->text) {
619                        msn_soap_profile_get(ic, p->text);
620                }
621
[7f34ce2]622                return XT_HANDLED;
623        }
[5ebff60]624
625        if (!bool2int(is_msgr) || handle == NULL) {
[d97f51b]626                return XT_HANDLED;
[5ebff60]627        }
628
629        if (!(bu = bee_user_by_handle(ic->bee, ic, handle)) &&
630            !(bu = bee_user_new(ic->bee, ic, handle, 0))) {
[7f34ce2]631                return XT_HANDLED;
[5ebff60]632        }
633
[7f34ce2]634        bd = bu->data;
635        bd->flags |= MSN_BUDDY_FL;
[5ebff60]636        g_free(bd->cid);
637        bd->cid = g_strdup(id);
638
639        imcb_rename_buddy(ic, handle, display_name);
640
641        if (group_id && (group = msn_group_by_id(ic, group_id))) {
642                imcb_add_buddy(ic, handle, group->name);
643        }
644
645        if (getenv("BITLBEE_DEBUG")) {
646                fprintf(stderr, "%s %s %s %s\n", id, type, handle, display_name);
647        }
648
[7f34ce2]649        return XT_HANDLED;
650}
651
652static const struct xt_handler_entry msn_soap_addressbook_parser[] = {
653        { "contactInfo", "Contact", msn_soap_addressbook_contact },
654        { "groupInfo", "Group", msn_soap_addressbook_group },
655        { NULL,               NULL,     NULL                        }
656};
657
[5ebff60]658static int msn_soap_addressbook_handle_response(struct msn_soap_req_data *soap_req)
[7f34ce2]659{
[327af51]660        GSList *l;
[4eb75b2]661        int wtf = 0;
[5ebff60]662
663        for (l = soap_req->ic->bee->users; l; l = l->next) {
[327af51]664                struct bee_user *bu = l->data;
[4eb75b2]665                struct msn_buddy_data *bd = bu->data;
[5ebff60]666
667                if (bu->ic == soap_req->ic && bd) {
668                        msn_buddy_ask(bu);
669
670                        if ((bd->flags & (MSN_BUDDY_AL | MSN_BUDDY_BL)) ==
671                            (MSN_BUDDY_AL | MSN_BUDDY_BL)) {
[aa6bcd8]672                                /* both allow and block, delete block, add wtf */
[d68365c]673                                bd->flags &= ~MSN_BUDDY_BL;
674                                wtf++;
675                        }
[aa6bcd8]676
677
[5ebff60]678                        if ((bd->flags & (MSN_BUDDY_AL | MSN_BUDDY_BL)) == 0) {
[aa6bcd8]679                                /* neither allow or block, add allow */
680                                bd->flags |= MSN_BUDDY_AL;
681                        }
[4eb75b2]682                }
[327af51]683        }
[5ebff60]684
685        if (wtf) {
686                imcb_log(soap_req->ic, "Warning: %d contacts were in both your "
687                         "block and your allow list. Assuming they're all "
688                         "allowed. Use the official WLM client once to fix "
689                         "this.", wtf);
690        }
691
692        msn_auth_got_contact_list(soap_req->ic);
693
[7f34ce2]694        return MSN_SOAP_OK;
695}
696
[5ebff60]697static int msn_soap_addressbook_free_data(struct msn_soap_req_data *soap_req)
[7f34ce2]698{
699        return 0;
700}
701
[5ebff60]702int msn_soap_addressbook_request(struct im_connection *ic)
[7f34ce2]703{
[5ebff60]704        return msn_soap_start(ic, NULL, msn_soap_addressbook_build_request,
705                              msn_soap_addressbook_parser,
706                              msn_soap_addressbook_handle_response,
707                              msn_soap_addressbook_free_data);
[7f34ce2]708}
[4452e69]709
710/* Variant: Change our display name. */
[5ebff60]711static int msn_soap_ab_namechange_build_request(struct msn_soap_req_data *soap_req)
[4452e69]712{
713        struct msn_data *md = soap_req->ic->proto_data;
[5ebff60]714
715        soap_req->url = g_strdup(SOAP_ADDRESSBOOK_URL);
716        soap_req->action = g_strdup(SOAP_AB_NAMECHANGE_ACTION);
717        soap_req->payload = msn_soap_abservice_build(SOAP_AB_NAMECHANGE_PAYLOAD,
718                                                     "Timer", md->tokens[1], (char *) soap_req->data);
719
[4452e69]720        return 1;
721}
722
[5ebff60]723static int msn_soap_ab_namechange_handle_response(struct msn_soap_req_data *soap_req)
[4452e69]724{
725        /* TODO: Ack the change? Not sure what the NAKs look like.. */
726        return MSN_SOAP_OK;
727}
728
[5ebff60]729static int msn_soap_ab_namechange_free_data(struct msn_soap_req_data *soap_req)
[4452e69]730{
[5ebff60]731        g_free(soap_req->data);
[4452e69]732        return 0;
733}
734
[5ebff60]735int msn_soap_addressbook_set_display_name(struct im_connection *ic, const char *new)
[4452e69]736{
[5ebff60]737        return msn_soap_start(ic, g_strdup(new),
738                              msn_soap_ab_namechange_build_request,
739                              NULL,
740                              msn_soap_ab_namechange_handle_response,
741                              msn_soap_ab_namechange_free_data);
[4452e69]742}
[4fc95c5]743
744/* Add a contact. */
[5ebff60]745static int msn_soap_ab_contact_add_build_request(struct msn_soap_req_data *soap_req)
[4fc95c5]746{
747        struct msn_data *md = soap_req->ic->proto_data;
748        bee_user_t *bu = soap_req->data;
[5ebff60]749
750        soap_req->url = g_strdup(SOAP_ADDRESSBOOK_URL);
751        soap_req->action = g_strdup(SOAP_AB_CONTACT_ADD_ACTION);
752        soap_req->payload = msn_soap_abservice_build(SOAP_AB_CONTACT_ADD_PAYLOAD,
753                                                     "ContactSave", md->tokens[1], bu->handle,
754                                                     bu->fullname ? bu->fullname : bu->handle);
755
[4fc95c5]756        return 1;
757}
758
[5ebff60]759static xt_status msn_soap_ab_contact_add_cid(struct xt_node *node, gpointer data)
[4fc95c5]760{
761        struct msn_soap_req_data *soap_req = data;
762        bee_user_t *bu = soap_req->data;
763        struct msn_buddy_data *bd = bu->data;
[5ebff60]764
765        g_free(bd->cid);
766        bd->cid = g_strdup(node->text);
767
[4fc95c5]768        return XT_HANDLED;
769}
770
771static const struct xt_handler_entry msn_soap_ab_contact_add_parser[] = {
772        { "guid", "ABContactAddResult", msn_soap_ab_contact_add_cid },
773        { NULL,               NULL,     NULL                        }
774};
775
[5ebff60]776static int msn_soap_ab_contact_add_handle_response(struct msn_soap_req_data *soap_req)
[4fc95c5]777{
778        /* TODO: Ack the change? Not sure what the NAKs look like.. */
779        return MSN_SOAP_OK;
780}
781
[5ebff60]782static int msn_soap_ab_contact_add_free_data(struct msn_soap_req_data *soap_req)
[4fc95c5]783{
784        return 0;
785}
786
[5ebff60]787int msn_soap_ab_contact_add(struct im_connection *ic, bee_user_t *bu)
[4fc95c5]788{
[5ebff60]789        return msn_soap_start(ic, bu,
790                              msn_soap_ab_contact_add_build_request,
791                              msn_soap_ab_contact_add_parser,
792                              msn_soap_ab_contact_add_handle_response,
793                              msn_soap_ab_contact_add_free_data);
[4fc95c5]794}
795
796/* Remove a contact. */
[5ebff60]797static int msn_soap_ab_contact_del_build_request(struct msn_soap_req_data *soap_req)
[4fc95c5]798{
799        struct msn_data *md = soap_req->ic->proto_data;
[52f5e90]800        const char *cid = soap_req->data;
[5ebff60]801
802        soap_req->url = g_strdup(SOAP_ADDRESSBOOK_URL);
803        soap_req->action = g_strdup(SOAP_AB_CONTACT_DEL_ACTION);
804        soap_req->payload = msn_soap_abservice_build(SOAP_AB_CONTACT_DEL_PAYLOAD,
805                                                     "Timer", md->tokens[1], cid);
806
[4fc95c5]807        return 1;
808}
809
[5ebff60]810static int msn_soap_ab_contact_del_handle_response(struct msn_soap_req_data *soap_req)
[4fc95c5]811{
812        /* TODO: Ack the change? Not sure what the NAKs look like.. */
813        return MSN_SOAP_OK;
814}
815
[5ebff60]816static int msn_soap_ab_contact_del_free_data(struct msn_soap_req_data *soap_req)
[4fc95c5]817{
[5ebff60]818        g_free(soap_req->data);
[4fc95c5]819        return 0;
820}
821
[5ebff60]822int msn_soap_ab_contact_del(struct im_connection *ic, bee_user_t *bu)
[4fc95c5]823{
[52f5e90]824        struct msn_buddy_data *bd = bu->data;
[5ebff60]825
826        return msn_soap_start(ic, g_strdup(bd->cid),
827                              msn_soap_ab_contact_del_build_request,
828                              NULL,
829                              msn_soap_ab_contact_del_handle_response,
830                              msn_soap_ab_contact_del_free_data);
[4fc95c5]831}
[80175a1]832
833
834
835/* Storage stuff: Fetch profile. */
[5ebff60]836static int msn_soap_profile_get_build_request(struct msn_soap_req_data *soap_req)
[80175a1]837{
838        struct msn_data *md = soap_req->ic->proto_data;
[5ebff60]839
840        soap_req->url = g_strdup(SOAP_STORAGE_URL);
841        soap_req->action = g_strdup(SOAP_PROFILE_GET_ACTION);
842        soap_req->payload = g_markup_printf_escaped(SOAP_PROFILE_GET_PAYLOAD,
843                                                    md->tokens[3], (char *) soap_req->data);
844
[80175a1]845        return 1;
846}
847
[5ebff60]848static xt_status msn_soap_profile_get_result(struct xt_node *node, gpointer data)
[80175a1]849{
850        struct msn_soap_req_data *soap_req = data;
851        struct im_connection *ic = soap_req->ic;
852        struct msn_data *md = soap_req->ic->proto_data;
853        struct xt_node *dn;
[5ebff60]854
855        if ((dn = xt_find_node(node->children, "DisplayName")) && dn->text) {
856                set_t *set = set_find(&ic->acc->set, "display_name");
857                g_free(set->value);
858                set->value = g_strdup(dn->text);
859
[80175a1]860                md->flags |= MSN_GOT_PROFILE_DN;
861        }
[5ebff60]862
[80175a1]863        return XT_HANDLED;
864}
865
[5ebff60]866static xt_status msn_soap_profile_get_rid(struct xt_node *node, gpointer data)
[76c89dc7]867{
868        struct msn_soap_req_data *soap_req = data;
869        struct msn_data *md = soap_req->ic->proto_data;
[5ebff60]870
871        g_free(md->profile_rid);
872        md->profile_rid = g_strdup(node->text);
873
[76c89dc7]874        return XT_HANDLED;
875}
876
[80175a1]877static const struct xt_handler_entry msn_soap_profile_get_parser[] = {
878        { "ExpressionProfile", "GetProfileResult", msn_soap_profile_get_result },
[76c89dc7]879        { "ResourceID",        "GetProfileResult", msn_soap_profile_get_rid },
[80175a1]880        { NULL,               NULL,     NULL                        }
881};
882
[5ebff60]883static int msn_soap_profile_get_handle_response(struct msn_soap_req_data *soap_req)
[80175a1]884{
885        struct msn_data *md = soap_req->ic->proto_data;
[5ebff60]886
[80175a1]887        md->flags |= MSN_GOT_PROFILE;
[5ebff60]888        msn_ns_finish_login(soap_req->ic);
889
[80175a1]890        return MSN_SOAP_OK;
891}
892
[5ebff60]893static int msn_soap_profile_get_free_data(struct msn_soap_req_data *soap_req)
[80175a1]894{
[5ebff60]895        g_free(soap_req->data);
[80175a1]896        return 0;
897}
898
[5ebff60]899int msn_soap_profile_get(struct im_connection *ic, const char *cid)
[80175a1]900{
[5ebff60]901        return msn_soap_start(ic, g_strdup(cid),
902                              msn_soap_profile_get_build_request,
903                              msn_soap_profile_get_parser,
904                              msn_soap_profile_get_handle_response,
905                              msn_soap_profile_get_free_data);
[80175a1]906}
[76c89dc7]907
908/* Update profile (display name). */
[5ebff60]909static int msn_soap_profile_set_dn_build_request(struct msn_soap_req_data *soap_req)
[76c89dc7]910{
911        struct msn_data *md = soap_req->ic->proto_data;
[5ebff60]912
913        soap_req->url = g_strdup(SOAP_STORAGE_URL);
914        soap_req->action = g_strdup(SOAP_PROFILE_SET_DN_ACTION);
915        soap_req->payload = g_markup_printf_escaped(SOAP_PROFILE_SET_DN_PAYLOAD,
916                                                    md->tokens[3], md->profile_rid, (char *) soap_req->data);
917
[76c89dc7]918        return 1;
919}
920
921static const struct xt_handler_entry msn_soap_profile_set_dn_parser[] = {
922        { NULL,               NULL,     NULL                        }
923};
924
[5ebff60]925static int msn_soap_profile_set_dn_handle_response(struct msn_soap_req_data *soap_req)
[76c89dc7]926{
927        return MSN_SOAP_OK;
928}
929
[5ebff60]930static int msn_soap_profile_set_dn_free_data(struct msn_soap_req_data *soap_req)
[76c89dc7]931{
[5ebff60]932        g_free(soap_req->data);
[76c89dc7]933        return 0;
934}
935
[5ebff60]936int msn_soap_profile_set_dn(struct im_connection *ic, const char *dn)
[76c89dc7]937{
[5ebff60]938        return msn_soap_start(ic, g_strdup(dn),
939                              msn_soap_profile_set_dn_build_request,
940                              msn_soap_profile_set_dn_parser,
941                              msn_soap_profile_set_dn_handle_response,
942                              msn_soap_profile_set_dn_free_data);
[76c89dc7]943}
Note: See TracBrowser for help on using the repository browser.