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
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. */
[5ebff60]176                        msn_soap_passport_sso_request(soap_req->ic, "blaataap");
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"))) {
[0483e1e]215                        fwrite(headers, s - headers + 4, 1, stderr);
[5ebff60]216                } else {
[0483e1e]217                        fwrite(headers, strlen(headers), 1, stderr);
[5ebff60]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 {
[523fb23]261        char *nonce;
262        char *secret;
[660cb00]263        char *error;
[2c6b0f4]264        char *redirect;
[523fb23]265};
266
[5ebff60]267static int msn_soap_passport_sso_build_request(struct msn_soap_req_data *soap_req)
[523fb23]268{
[2c6b0f4]269        struct msn_soap_passport_sso_data *sd = soap_req->data;
[523fb23]270        struct im_connection *ic = soap_req->ic;
[4aa8a04]271        struct msn_data *md = ic->proto_data;
[5ebff60]272
273        if (sd->redirect) {
[2c6b0f4]274                soap_req->url = sd->redirect;
275                sd->redirect = NULL;
276        }
[5282ffd]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. :-/
[2c6b0f4]280        else if( g_str_has_suffix( ic->acc->user, "@msn.com" ) )
[5ebff60]281                soap_req->url = g_strdup( SOAP_PASSPORT_SSO_URL_MSN );
[5282ffd]282        */
[5ebff60]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,
[cdd1ded]288                                                    ic->acc->user, ic->acc->pass, md->pp_policy);
[5ebff60]289
[523fb23]290        return MSN_SOAP_OK;
291}
292
[5ebff60]293static xt_status msn_soap_passport_sso_token(struct xt_node *node, gpointer data)
[523fb23]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;
[5ebff60]300
301        if ((id = xt_find_attr(node, "Id")) == NULL) {
[523fb23]302                return XT_HANDLED;
[5ebff60]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
[91d6e91]311        *id -= '1';
[5ebff60]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);
[91d6e91]315        }
[5ebff60]316
[523fb23]317        return XT_HANDLED;
318}
319
[5ebff60]320static xt_status msn_soap_passport_failure(struct xt_node *node, gpointer data)
[660cb00]321{
322        struct msn_soap_req_data *soap_req = data;
323        struct msn_soap_passport_sso_data *sd = soap_req->data;
[5ebff60]324        struct xt_node *code = xt_find_node(node->children, "faultcode");
325        struct xt_node *string = xt_find_node(node->children, "faultstring");
[12442ed]326        struct xt_node *reqstatus = xt_find_path(node, "psf:pp/psf:reqstatus");
[2c6b0f4]327        struct xt_node *url;
[5ebff60]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);
[12442ed]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);
[5ebff60]338        } else {
339                sd->error = g_strdup_printf("%s (%s)", code->text, string && string->text_len ?
340                                            string->text : "no description available");
341        }
342
[660cb00]343        return XT_HANDLED;
344}
345
[523fb23]346static const struct xt_handler_entry msn_soap_passport_sso_parser[] = {
347        { "wsse:BinarySecurityToken", "wst:RequestedSecurityToken", msn_soap_passport_sso_token },
[660cb00]348        { "S:Fault", "S:Envelope", msn_soap_passport_failure },
[12442ed]349        { "S:Fault", "wst:RequestSecurityTokenResponse", msn_soap_passport_failure },
[523fb23]350        { NULL, NULL, NULL }
351};
352
[5ebff60]353static char *msn_key_fuckery(char *key, int key_len, char *type)
[523fb23]354{
[5ebff60]355        unsigned char hash1[20 + strlen(type) + 1];
[523fb23]356        unsigned char hash2[20];
357        char *ret;
[5ebff60]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
[523fb23]363        /* This is okay as hash1 is read completely before it's overwritten. */
[5ebff60]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);
[523fb23]370        return ret;
371}
372
[5ebff60]373static int msn_soap_passport_sso_handle_response(struct msn_soap_req_data *soap_req)
[523fb23]374{
375        struct msn_soap_passport_sso_data *sd = soap_req->data;
376        struct im_connection *ic = soap_req->ic;
[4aa8a04]377        struct msn_data *md = ic->proto_data;
[523fb23]378        char *key1, *key2, *key3, *blurb64;
379        int key1_len;
380        unsigned char *padnonce, *des3res;
[5ebff60]381
382        struct {
[523fb23]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 = {
[5ebff60]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),
[523fb23]401        };
[5ebff60]402
403        if (sd->redirect) {
[2c6b0f4]404                return MSN_SOAP_RETRY;
[76c89dc7]405        }
[5ebff60]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);
[660cb00]414                return MSN_SOAP_OK;
415        }
[523fb23]416
[5ebff60]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
[523fb23]442        return MSN_SOAP_OK;
443}
444
[5ebff60]445static int msn_soap_passport_sso_free_data(struct msn_soap_req_data *soap_req)
[523fb23]446{
447        struct msn_soap_passport_sso_data *sd = soap_req->data;
[5ebff60]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
[523fb23]455        return MSN_SOAP_OK;
456}
457
[5ebff60]458int msn_soap_passport_sso_request(struct im_connection *ic, const char *nonce)
[523fb23]459{
[5ebff60]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);
[523fb23]468}
469
470
[7db65b7]471/* memlist: Fetching the membership list (NOT address book) */
472
[5ebff60]473static int msn_soap_memlist_build_request(struct msn_soap_req_data *soap_req)
[7db65b7]474{
[7f34ce2]475        struct msn_data *md = soap_req->ic->proto_data;
[5ebff60]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
[7db65b7]481        return 1;
482}
483
[5ebff60]484static xt_status msn_soap_memlist_member(struct xt_node *node, gpointer data)
[7f34ce2]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;
[5ebff60]492
493        if ((p = xt_find_path(node, "../../MemberRole"))) {
[7f34ce2]494                role = p->text;
[5ebff60]495        }
496
497        if ((p = xt_find_node(node->children, "PassportName"))) {
[7f34ce2]498                handle = p->text;
[5ebff60]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)))) {
[7f34ce2]504                return XT_HANDLED;
[5ebff60]505        }
506
[7f34ce2]507        bd = bu->data;
[5ebff60]508        if (strcmp(role, "Allow") == 0) {
[7f34ce2]509                bd->flags |= MSN_BUDDY_AL;
[5ebff60]510                ic->permit = g_slist_prepend(ic->permit, g_strdup(handle));
511        } else if (strcmp(role, "Block") == 0) {
[7f34ce2]512                bd->flags |= MSN_BUDDY_BL;
[5ebff60]513                ic->deny = g_slist_prepend(ic->deny, g_strdup(handle));
514        } else if (strcmp(role, "Reverse") == 0) {
[7f34ce2]515                bd->flags |= MSN_BUDDY_RL;
[5ebff60]516        } else if (strcmp(role, "Pending") == 0) {
[7f34ce2]517                bd->flags |= MSN_BUDDY_PL;
[5ebff60]518        }
519
520        if (getenv("BITLBEE_DEBUG")) {
521                fprintf(stderr, "%p %s %d\n", bu, handle, bd->flags);
522        }
523
[7f34ce2]524        return XT_HANDLED;
525}
526
[7db65b7]527static const struct xt_handler_entry msn_soap_memlist_parser[] = {
[7f34ce2]528        { "Member", "Members", msn_soap_memlist_member },
[7db65b7]529        { NULL,               NULL,     NULL                        }
530};
531
[5ebff60]532static int msn_soap_memlist_handle_response(struct msn_soap_req_data *soap_req)
[7db65b7]533{
[5ebff60]534        msn_soap_addressbook_request(soap_req->ic);
535
[7f34ce2]536        return MSN_SOAP_OK;
[7db65b7]537}
538
[5ebff60]539static int msn_soap_memlist_free_data(struct msn_soap_req_data *soap_req)
[7db65b7]540{
541        return 0;
542}
543
[5ebff60]544int msn_soap_memlist_request(struct im_connection *ic)
[7db65b7]545{
[5ebff60]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);
[7db65b7]550}
[7f34ce2]551
[193dc74]552/* Variant: Adding/Removing people */
[5ebff60]553struct msn_soap_memlist_edit_data {
[193dc74]554        char *handle;
555        gboolean add;
556        msn_buddy_flags_t list;
557};
558
[5ebff60]559static int msn_soap_memlist_edit_build_request(struct msn_soap_req_data *soap_req)
[193dc74]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;
[5ebff60]564
565        soap_req->url = g_strdup(SOAP_MEMLIST_URL);
566        if (med->add) {
567                soap_req->action = g_strdup(SOAP_MEMLIST_ADD_ACTION);
[193dc74]568                add = "Add";
[5ebff60]569        } else {
570                soap_req->action = g_strdup(SOAP_MEMLIST_DEL_ACTION);
[193dc74]571                add = "Delete";
572        }
[5ebff60]573        switch (med->list) {
[193dc74]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        }
[5ebff60]592        soap_req->payload = msn_soap_abservice_build(SOAP_MEMLIST_EDIT_PAYLOAD,
593                                                     scenario, md->tokens[1], add, list, med->handle, add);
594
[193dc74]595        return 1;
596}
597
[5ebff60]598static int msn_soap_memlist_edit_handle_response(struct msn_soap_req_data *soap_req)
[193dc74]599{
600        return MSN_SOAP_OK;
601}
602
[5ebff60]603static int msn_soap_memlist_edit_free_data(struct msn_soap_req_data *soap_req)
[193dc74]604{
605        struct msn_soap_memlist_edit_data *med = soap_req->data;
[5ebff60]606
607        g_free(med->handle);
608        g_free(med);
609
[193dc74]610        return 0;
611}
612
[5ebff60]613int msn_soap_memlist_edit(struct im_connection *ic, const char *handle, gboolean add, int list)
[193dc74]614{
615        struct msn_soap_memlist_edit_data *med;
[5ebff60]616
617        med = g_new0(struct msn_soap_memlist_edit_data, 1);
618        med->handle = g_strdup(handle);
[193dc74]619        med->add = add;
620        med->list = list;
[5ebff60]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);
[193dc74]626}
627
[7f34ce2]628
629/* addressbook: Fetching the membership list (NOT address book) */
630
[5ebff60]631static int msn_soap_addressbook_build_request(struct msn_soap_req_data *soap_req)
[7f34ce2]632{
633        struct msn_data *md = soap_req->ic->proto_data;
[5ebff60]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
[7f34ce2]639        return 1;
640}
641
[5ebff60]642static xt_status msn_soap_addressbook_group(struct xt_node *node, gpointer data)
[7f34ce2]643{
644        struct xt_node *p;
645        char *id = NULL, *name = NULL;
646        struct msn_soap_req_data *soap_req = data;
[ff27648]647        struct msn_data *md = soap_req->ic->proto_data;
[5ebff60]648
649        if ((p = xt_find_path(node, "../groupId"))) {
[7f34ce2]650                id = p->text;
[5ebff60]651        }
652
653        if ((p = xt_find_node(node->children, "name"))) {
[7f34ce2]654                name = p->text;
[ff27648]655        }
[5ebff60]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
[7f34ce2]668        return XT_HANDLED;
669}
670
[5ebff60]671static xt_status msn_soap_addressbook_contact(struct xt_node *node, gpointer data)
[7f34ce2]672{
673        bee_user_t *bu;
674        struct msn_buddy_data *bd;
675        struct xt_node *p;
[9b01339]676        char *id = NULL, *type = NULL, *handle = NULL, *is_msgr = "false",
[5ebff60]677        *display_name = NULL, *group_id = NULL;
[7f34ce2]678        struct msn_soap_req_data *soap_req = data;
679        struct im_connection *ic = soap_req->ic;
[ff27648]680        struct msn_group *group;
[5ebff60]681
682        if ((p = xt_find_path(node, "../contactId"))) {
[7f34ce2]683                id = p->text;
[5ebff60]684        }
685        if ((p = xt_find_node(node->children, "contactType"))) {
[7f34ce2]686                type = p->text;
[5ebff60]687        }
688        if ((p = xt_find_node(node->children, "passportName"))) {
[7f34ce2]689                handle = p->text;
[5ebff60]690        }
691        if ((p = xt_find_node(node->children, "displayName"))) {
[7f34ce2]692                display_name = p->text;
[5ebff60]693        }
694        if ((p = xt_find_node(node->children, "isMessengerUser"))) {
[9b01339]695                is_msgr = p->text;
[5ebff60]696        }
697        if ((p = xt_find_path(node, "groupIds/guid"))) {
[ff27648]698                group_id = p->text;
[5ebff60]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
[80175a1]706                /* Try to fetch the profile; if the user has one, that's where
707                   we can find the persistent display_name. */
[5ebff60]708                if ((p = xt_find_node(node->children, "CID")) && p->text) {
709                        msn_soap_profile_get(ic, p->text);
710                }
711
[7f34ce2]712                return XT_HANDLED;
713        }
[5ebff60]714
715        if (!bool2int(is_msgr) || handle == NULL) {
[d97f51b]716                return XT_HANDLED;
[5ebff60]717        }
718
719        if (!(bu = bee_user_by_handle(ic->bee, ic, handle)) &&
720            !(bu = bee_user_new(ic->bee, ic, handle, 0))) {
[7f34ce2]721                return XT_HANDLED;
[5ebff60]722        }
723
[7f34ce2]724        bd = bu->data;
725        bd->flags |= MSN_BUDDY_FL;
[5ebff60]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
[7f34ce2]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
[5ebff60]748static int msn_soap_addressbook_handle_response(struct msn_soap_req_data *soap_req)
[7f34ce2]749{
[327af51]750        GSList *l;
[4eb75b2]751        int wtf = 0;
[5ebff60]752
753        for (l = soap_req->ic->bee->users; l; l = l->next) {
[327af51]754                struct bee_user *bu = l->data;
[4eb75b2]755                struct msn_buddy_data *bd = bu->data;
[5ebff60]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)) {
[aa6bcd8]762                                /* both allow and block, delete block, add wtf */
[d68365c]763                                bd->flags &= ~MSN_BUDDY_BL;
764                                wtf++;
765                        }
[aa6bcd8]766
767
[5ebff60]768                        if ((bd->flags & (MSN_BUDDY_AL | MSN_BUDDY_BL)) == 0) {
[aa6bcd8]769                                /* neither allow or block, add allow */
770                                bd->flags |= MSN_BUDDY_AL;
771                        }
[4eb75b2]772                }
[327af51]773        }
[5ebff60]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 "
[64b8c9f]778                         "allowed.", wtf);
[5ebff60]779        }
780
781        msn_auth_got_contact_list(soap_req->ic);
782
[7f34ce2]783        return MSN_SOAP_OK;
784}
785
[5ebff60]786static int msn_soap_addressbook_free_data(struct msn_soap_req_data *soap_req)
[7f34ce2]787{
788        return 0;
789}
790
[5ebff60]791int msn_soap_addressbook_request(struct im_connection *ic)
[7f34ce2]792{
[5ebff60]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);
[7f34ce2]797}
[4452e69]798
799/* Variant: Change our display name. */
[5ebff60]800static int msn_soap_ab_namechange_build_request(struct msn_soap_req_data *soap_req)
[4452e69]801{
802        struct msn_data *md = soap_req->ic->proto_data;
[5ebff60]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
[4452e69]809        return 1;
810}
811
[5ebff60]812static int msn_soap_ab_namechange_handle_response(struct msn_soap_req_data *soap_req)
[4452e69]813{
814        /* TODO: Ack the change? Not sure what the NAKs look like.. */
815        return MSN_SOAP_OK;
816}
817
[5ebff60]818static int msn_soap_ab_namechange_free_data(struct msn_soap_req_data *soap_req)
[4452e69]819{
[5ebff60]820        g_free(soap_req->data);
[4452e69]821        return 0;
822}
823
[5ebff60]824int msn_soap_addressbook_set_display_name(struct im_connection *ic, const char *new)
[4452e69]825{
[5ebff60]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);
[4452e69]831}
[4fc95c5]832
833/* Add a contact. */
[5ebff60]834static int msn_soap_ab_contact_add_build_request(struct msn_soap_req_data *soap_req)
[4fc95c5]835{
836        struct msn_data *md = soap_req->ic->proto_data;
837        bee_user_t *bu = soap_req->data;
[5ebff60]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
[4fc95c5]845        return 1;
846}
847
[5ebff60]848static xt_status msn_soap_ab_contact_add_cid(struct xt_node *node, gpointer data)
[4fc95c5]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;
[5ebff60]853
854        g_free(bd->cid);
855        bd->cid = g_strdup(node->text);
856
[4fc95c5]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
[5ebff60]865static int msn_soap_ab_contact_add_handle_response(struct msn_soap_req_data *soap_req)
[4fc95c5]866{
867        /* TODO: Ack the change? Not sure what the NAKs look like.. */
868        return MSN_SOAP_OK;
869}
870
[5ebff60]871static int msn_soap_ab_contact_add_free_data(struct msn_soap_req_data *soap_req)
[4fc95c5]872{
873        return 0;
874}
875
[5ebff60]876int msn_soap_ab_contact_add(struct im_connection *ic, bee_user_t *bu)
[4fc95c5]877{
[5ebff60]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);
[4fc95c5]883}
884
885/* Remove a contact. */
[5ebff60]886static int msn_soap_ab_contact_del_build_request(struct msn_soap_req_data *soap_req)
[4fc95c5]887{
888        struct msn_data *md = soap_req->ic->proto_data;
[52f5e90]889        const char *cid = soap_req->data;
[5ebff60]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
[4fc95c5]896        return 1;
897}
898
[5ebff60]899static int msn_soap_ab_contact_del_handle_response(struct msn_soap_req_data *soap_req)
[4fc95c5]900{
901        /* TODO: Ack the change? Not sure what the NAKs look like.. */
902        return MSN_SOAP_OK;
903}
904
[5ebff60]905static int msn_soap_ab_contact_del_free_data(struct msn_soap_req_data *soap_req)
[4fc95c5]906{
[5ebff60]907        g_free(soap_req->data);
[4fc95c5]908        return 0;
909}
910
[5ebff60]911int msn_soap_ab_contact_del(struct im_connection *ic, bee_user_t *bu)
[4fc95c5]912{
[52f5e90]913        struct msn_buddy_data *bd = bu->data;
[5ebff60]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);
[4fc95c5]920}
[80175a1]921
922
923
924/* Storage stuff: Fetch profile. */
[5ebff60]925static int msn_soap_profile_get_build_request(struct msn_soap_req_data *soap_req)
[80175a1]926{
927        struct msn_data *md = soap_req->ic->proto_data;
[5ebff60]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
[80175a1]934        return 1;
935}
936
[5ebff60]937static xt_status msn_soap_profile_get_result(struct xt_node *node, gpointer data)
[80175a1]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;
[5ebff60]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
[80175a1]949                md->flags |= MSN_GOT_PROFILE_DN;
950        }
[5ebff60]951
[80175a1]952        return XT_HANDLED;
953}
954
[5ebff60]955static xt_status msn_soap_profile_get_rid(struct xt_node *node, gpointer data)
[76c89dc7]956{
957        struct msn_soap_req_data *soap_req = data;
958        struct msn_data *md = soap_req->ic->proto_data;
[5ebff60]959
960        g_free(md->profile_rid);
961        md->profile_rid = g_strdup(node->text);
962
[76c89dc7]963        return XT_HANDLED;
964}
965
[80175a1]966static const struct xt_handler_entry msn_soap_profile_get_parser[] = {
967        { "ExpressionProfile", "GetProfileResult", msn_soap_profile_get_result },
[76c89dc7]968        { "ResourceID",        "GetProfileResult", msn_soap_profile_get_rid },
[80175a1]969        { NULL,               NULL,     NULL                        }
970};
971
[5ebff60]972static int msn_soap_profile_get_handle_response(struct msn_soap_req_data *soap_req)
[80175a1]973{
974        struct msn_data *md = soap_req->ic->proto_data;
[5ebff60]975
[80175a1]976        md->flags |= MSN_GOT_PROFILE;
[5ebff60]977        msn_ns_finish_login(soap_req->ic);
978
[80175a1]979        return MSN_SOAP_OK;
980}
981
[5ebff60]982static int msn_soap_profile_get_free_data(struct msn_soap_req_data *soap_req)
[80175a1]983{
[5ebff60]984        g_free(soap_req->data);
[80175a1]985        return 0;
986}
987
[5ebff60]988int msn_soap_profile_get(struct im_connection *ic, const char *cid)
[80175a1]989{
[5ebff60]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);
[80175a1]995}
[76c89dc7]996
997/* Update profile (display name). */
[5ebff60]998static int msn_soap_profile_set_dn_build_request(struct msn_soap_req_data *soap_req)
[76c89dc7]999{
1000        struct msn_data *md = soap_req->ic->proto_data;
[5ebff60]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
[76c89dc7]1007        return 1;
1008}
1009
1010static const struct xt_handler_entry msn_soap_profile_set_dn_parser[] = {
1011        { NULL,               NULL,     NULL                        }
1012};
1013
[5ebff60]1014static int msn_soap_profile_set_dn_handle_response(struct msn_soap_req_data *soap_req)
[76c89dc7]1015{
1016        return MSN_SOAP_OK;
1017}
1018
[5ebff60]1019static int msn_soap_profile_set_dn_free_data(struct msn_soap_req_data *soap_req)
[76c89dc7]1020{
[5ebff60]1021        g_free(soap_req->data);
[76c89dc7]1022        return 0;
1023}
1024
[5ebff60]1025int msn_soap_profile_set_dn(struct im_connection *ic, const char *dn)
[76c89dc7]1026{
[5ebff60]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);
[76c89dc7]1032}
Note: See TracBrowser for help on using the repository browser.