source: protocols/msn/ns.c @ 27a057c

Last change on this file since 27a057c was 27a057c, checked in by dequis <dx@…>, at 2015-05-31T02:40:05Z

msn: fix leak when reconnecting ssl

  • Property mode set to 100644
File size: 18.3 KB
RevLine 
[5ebff60]1/********************************************************************\
[b7d3cc34]2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[f9258ae]4  * Copyright 2002-2012 Wilmer van der Gaast and others                *
[b7d3cc34]5  \********************************************************************/
6
7/* MSN module - Notification server callbacks                           */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
[6f10697]22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
[b7d3cc34]24*/
25
26#include <ctype.h>
[f9258ae]27#include <sys/utsname.h>
[b7d3cc34]28#include "nogaim.h"
29#include "msn.h"
30#include "md5.h"
[f9258ae]31#include "sha1.h"
[7db65b7]32#include "soap.h"
[ca7de3a]33#include "xmltree.h"
[767b2d1]34#include "ssl_client.h"
[b7d3cc34]35
[767b2d1]36static gboolean msn_ns_connected(gpointer data, int source, void *scd, b_input_condition cond);
[5ebff60]37static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond);
[b7d3cc34]38
[5ebff60]39static void msn_ns_send_adl_start(struct im_connection *ic);
40static void msn_ns_send_adl(struct im_connection *ic);
[5fbf815]41static void msn_ns_structured_message(struct msn_data *md, char *msg, int msglen, char **cmd);
42static void msn_ns_sdg(struct msn_data *md, char *who, char **parts, char *action);
43static void msn_ns_nfy(struct msn_data *md, char *who, char **parts, char *action, gboolean is_put);
[b7d3cc34]44
[074c9b6]45int msn_ns_write(struct im_connection *ic, const char *fmt, ...)
[64768d4]46{
47        struct msn_data *md = ic->proto_data;
48        va_list params;
49        char *out;
50        size_t len;
51        int st;
[5ebff60]52
53        va_start(params, fmt);
54        out = g_strdup_vprintf(fmt, params);
55        va_end(params);
56
57        if (getenv("BITLBEE_DEBUG")) {
[4107fea]58                fprintf(stderr, "\n\x1b[91m>>>[NS] %s\n\x1b[97m", out);
[5ebff60]59        }
60
61        len = strlen(out);
[913a663]62
63        if (md->is_http) {
64                st = len;
65                msn_gw_write(md->gw, out, len);
66        } else {
[767b2d1]67                st = ssl_write(md->ssl, out, len);
[913a663]68        }
69
[5ebff60]70        g_free(out);
71        if (st != len) {
72                imcb_error(ic, "Short write() to main server");
73                imc_logout(ic, TRUE);
[64768d4]74                return 0;
75        }
[5ebff60]76
[64768d4]77        return 1;
78}
79
[85dabae]80int msn_ns_write_cmd(struct im_connection *ic, const char *cmd, const char *params, const char *payload)
81{
82        struct msn_data *md = ic->proto_data;
83        int trid = ++md->trId;
84        const char *headers = "\r\n"; /* not needed yet */
85        size_t len = strlen(headers) + strlen(payload);
86
87        if (params && *params) {
88                return msn_ns_write(ic, "%s %d %s %zd\r\n%s%s", cmd, trid, params, len, headers, payload);
89        } else {
90                return msn_ns_write(ic, "%s %d %zd\r\n%s%s", cmd, trid, len, headers, payload);
91        }
92}
93
[11e42dc]94gboolean msn_ns_connect(struct im_connection *ic, const char *host, int port)
[b7d3cc34]95{
[5fbf815]96        struct msn_data *md = ic->proto_data;
[11e42dc]97
[5fbf815]98        if (md->is_http) {
[951aefd]99                md->gw = msn_gw_new(ic);
[5fbf815]100                md->gw->callback = msn_ns_callback;
[767b2d1]101                msn_ns_connected(md, 0, NULL, B_EV_IO_READ);
[913a663]102        } else {
[27a057c]103                if (md->fd >= 0) {
104                        closesocket(md->fd);
105                }
106
107                if (md->ssl) {
108                        ssl_disconnect(md->ssl);
109                        b_event_remove(md->inpa);
110                }
111
[767b2d1]112                md->ssl = ssl_connect((char *) host, port, TRUE, msn_ns_connected, md);
113                md->fd = md->ssl ? ssl_getfd(md->ssl) : -1;
[5fbf815]114                if (md->fd < 0) {
[913a663]115                        imcb_error(ic, "Could not connect to server");
116                        imc_logout(ic, TRUE);
117                        return FALSE;
118                }
[b7d3cc34]119        }
[5ebff60]120
[bae0617]121        return TRUE;
122}
123
[767b2d1]124static gboolean msn_ns_connected(gpointer data, int source, void *scd, b_input_condition cond)
[bae0617]125{
[11e42dc]126        struct msn_data *md = data;
127        struct im_connection *ic = md->ic;
[5ebff60]128
[767b2d1]129        if (!scd && !md->is_http) {
130                md->ssl = NULL;
[5ebff60]131                imcb_error(ic, "Could not connect to server");
132                imc_logout(ic, TRUE);
[bae0617]133                return FALSE;
[b7d3cc34]134        }
[5ebff60]135
[5fbf815]136        g_free(md->rxq);
137        md->rxlen = 0;
138        md->rxq = g_new0(char, 1);
[5ebff60]139
140        if (md->uuid == NULL) {
[f9258ae]141                struct utsname name;
142                sha1_state_t sha[1];
[5ebff60]143
[f9258ae]144                /* UUID == SHA1("BitlBee" + my hostname + MSN username) */
[5ebff60]145                sha1_init(sha);
146                sha1_append(sha, (void *) "BitlBee", 7);
147                if (uname(&name) == 0) {
148                        sha1_append(sha, (void *) name.nodename, strlen(name.nodename));
[f9258ae]149                }
[5ebff60]150                sha1_append(sha, (void *) ic->acc->user, strlen(ic->acc->user));
151                md->uuid = sha1_random_uuid(sha);
152                memcpy(md->uuid, "b171be3e", 8);   /* :-P */
[f9258ae]153        }
[5ebff60]154
[4107fea]155        if (msn_ns_write_cmd(ic, "CNT", "CON", "<connect><ver>2</ver><agent><os>winnt</os><osVer>5.2</osVer><proc>x86</proc><lcid>en-us</lcid></agent></connect>")) {
[5fbf815]156                if (!md->is_http) {
[b994034]157                        md->inpa = b_input_add(md->fd, B_EV_IO_READ, msn_ns_callback, ic);
[913a663]158                }
[5ebff60]159                imcb_log(ic, "Connected to server, waiting for reply");
[b7d3cc34]160        }
[5ebff60]161
[ba9edaa]162        return FALSE;
[b7d3cc34]163}
164
[5fbf815]165void msn_ns_close(struct msn_data *md)
[bae0617]166{
[5fbf815]167        if (md->gw) {
[951aefd]168                msn_gw_free(md->gw);
[913a663]169        }
[767b2d1]170
171        if (md->ssl) {
172                ssl_disconnect(md->ssl);
[5fbf815]173                b_event_remove(md->inpa);
[bae0617]174        }
[5ebff60]175
[767b2d1]176        md->ssl = NULL;
[5fbf815]177        md->fd = md->inpa = -1;
[767b2d1]178
[5fbf815]179        g_free(md->rxq);
180        g_free(md->cmd_text);
[5ebff60]181
[5fbf815]182        md->rxlen = 0;
183        md->rxq = NULL;
184        md->cmd_text = NULL;
[bae0617]185}
186
[5ebff60]187static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond)
[b7d3cc34]188{
[b994034]189        struct im_connection *ic = data;
190        struct msn_data *md;
[913a663]191        char *bytes;
[a4be2f6]192        int st;
[5ebff60]193
[b994034]194        if (g_slist_find(msn_connections, ic) == NULL) {
195                return FALSE;
196        }
197
198        md = ic->proto_data;
199
[5fbf815]200        if (md->is_http) {
201                st = msn_gw_read(md->gw, &bytes);
[913a663]202        } else {
203                bytes = g_malloc(1024);
[767b2d1]204                st = ssl_read(md->ssl, bytes, 1024);
[913a663]205        }
206
[767b2d1]207        if (st == 0 || (st < 0 && (md->is_http || !ssl_sockerr_again(md->ssl)))) {
[5ebff60]208                imcb_error(ic, "Error while reading from server");
209                imc_logout(ic, TRUE);
[088b070]210                g_free(bytes);
[ba9edaa]211                return FALSE;
[5ebff60]212        }
[a4be2f6]213
[5fbf815]214        msn_queue_feed(md, bytes, st);
[a4be2f6]215
[913a663]216        g_free(bytes);
217
[767b2d1]218        if (!md->is_http && ssl_pending(md->ssl)) {
219                return msn_ns_callback(data, source, cond);
220        }
221
[088b070]222        return msn_handler(md);
[b7d3cc34]223}
224
[d2411a1]225int msn_ns_command(struct msn_data *md, char **cmd, int num_parts, char *msg, int msglen)
[b7d3cc34]226{
[5fbf815]227        struct im_connection *ic = md->ic;
[4107fea]228        struct xt_node *xml;
[5ebff60]229
[4107fea]230        if (strcmp(cmd[0], "XFR") == 0) {
231                struct xt_node *target;
232                char *server, *p;
233                int port, st;
[5ebff60]234
[4107fea]235                if (!(xml = xt_from_string(msg + 2, msglen - 2)) ||
236                    !(target = xt_find_node(xml->children, "target")) ||
237                    !(server = target->text) ||
238                    !(p = strchr(server, ':'))) {
239                        return 1;
[b7d3cc34]240                }
[4107fea]241               
242                server = target->text;
243                *p = 0;
244                port = atoi(p + 1);
[5ebff60]245
[4107fea]246                b_event_remove(md->inpa);
247                md->inpa = -1;
[5ebff60]248
[4107fea]249                imcb_log(ic, "Transferring to other server");
250
251                st = msn_ns_connect(ic, server, port);
252
253                xt_free_node(xml);
254
255                return st;
256
257        } else if (strcmp(cmd[0], "CNT") == 0) {
258                msn_soap_passport_sso_request(ic);
259
260                /* continues in msn_auth_got_passport_token */
261
262        } else if (strcmp(cmd[0], "ATH") == 0) {
263                char *payload;
264
[ef9ee0e]265                if (md->flags & MSN_DONE_BND) {
266                        return 1;
267                }
268
269                md->flags |= MSN_DONE_BND;
270
271                // BND
[4107fea]272                payload = g_markup_printf_escaped(
273                        "<msgr><ver>1</ver><client><name>Skype</name><ver>2/4.3.0.37/174</ver></client>"
274                        "<epid>%s</epid></msgr>\r\n",
275                        md->uuid);
276
277                msn_ns_write_cmd(ic, "BND", "CON\\MSGR", payload);
278
279                g_free(payload);
280
281        } else if (strcmp(cmd[0], "BND") == 0) {
[ef9ee0e]282                struct xt_node *node;
283                char *nonce, *resp, *payload;
284
285                if (!(xml = xt_from_string(msg + 2, msglen - 2)) ||
286                    !(node = xt_find_node(xml->children, "nonce")) ||
287                    !(nonce = node->text)) {
288                        return 1;
289                }
290
291                resp = msn_p11_challenge(nonce);
292
293                // PUT MSGR\CHALLENGE
294                payload = g_markup_printf_escaped(
295                        "<challenge><appId>%s</appId><response>%s</response></challenge>",
296                        MSNP11_PROD_ID, resp);
297
298                msn_ns_write_cmd(ic, "PUT", "MSGR\\CHALLENGE", payload);
299
[4107fea]300                imcb_log(ic, "Authenticated, getting buddy list");
301                msn_soap_memlist_request(ic);
[5ebff60]302
[ef9ee0e]303                xt_free_node(xml);
304                g_free(payload);
305                g_free(resp);
306
[899c8e8]307        } else if (strcmp(cmd[0], "PUT") == 0) {
308                /* We could keep track TrIDs... or we could guess what this PUT means */
309                if ((md->flags & MSN_DONE_BND) && !(md->flags & MSN_DONE_ADL)) {
[5ebff60]310                        msn_ns_send_adl(ic);
311                        return msn_ns_finish_login(ic);
312                }
313        } else if (strcmp(cmd[0], "OUT") == 0) {
[2fe8297]314                imcb_error(ic, "Session terminated by remote server (%s)", cmd[1] ? cmd[1] : "reason unknown");
315                imc_logout(ic, TRUE);
[5ebff60]316                return(0);
317        } else if (strcmp(cmd[0], "QNG") == 0) {
[e132b60]318                ic->flags |= OPT_PONGED;
[5ebff60]319        } else if (g_ascii_isdigit(cmd[0][0])) {
320                int num = atoi(cmd[0]);
321                const struct msn_status_code *err = msn_status_by_number(num);
322
323                imcb_error(ic, "Error reported by MSN server: %s", err->text);
324
325                if (err->flags & STATUS_FATAL) {
326                        imc_logout(ic, TRUE);
327                        return(0);
[b7d3cc34]328                }
[95fdf22]329        } else if ((strcmp(cmd[0], "SDG") == 0) || (strcmp(cmd[0], "NFY") == 0)) {
330                msn_ns_structured_message(md, msg, msglen, cmd);
[5ebff60]331        } else {
[d550358]332                imcb_error(ic, "Received unknown command from main server: %s", cmd[0]);
[b7d3cc34]333        }
[5ebff60]334
335        return(1);
[b7d3cc34]336}
337
[5fbf815]338int msn_ns_message(struct msn_data *md, char *msg, int msglen, char **cmd, int num_parts)
[b7d3cc34]339{
[5fbf815]340        struct im_connection *ic = md->ic;
[b7d3cc34]341        char *body;
342        int blen = 0;
[5ebff60]343
344        if ((body = strstr(msg, "\r\n\r\n"))) {
[b7d3cc34]345                body += 4;
[5ebff60]346                blen = msglen - (body - msg);
[b7d3cc34]347        }
[5ebff60]348
349        if (strcmp(cmd[0], "MSG") == 0) {
350                if (g_strcasecmp(cmd[1], "Hotmail") == 0) {
351                        char *ct = get_rfc822_header(msg, "Content-Type:", msglen);
352
353                        if (!ct) {
354                                return(1);
355                        }
356
357                        if (g_strncasecmp(ct, "application/x-msmsgssystemmessage", 33) == 0) {
[b7d3cc34]358                                char *mtype;
359                                char *arg1;
[5ebff60]360
361                                if (!body) {
362                                        return(1);
[b7d3cc34]363                                }
[5ebff60]364
365                                mtype = get_rfc822_header(body, "Type:", blen);
366                                arg1 = get_rfc822_header(body, "Arg1:", blen);
367
368                                if (mtype && strcmp(mtype, "1") == 0) {
369                                        if (arg1) {
370                                                imcb_log(ic, "The server is going down for maintenance in %s minutes.",
371                                                         arg1);
372                                        }
373                                }
374
375                                g_free(arg1);
376                                g_free(mtype);
377                        } else if (g_strncasecmp(ct, "text/x-msmsgsprofile", 20) == 0) {
[b7d3cc34]378                                /* We don't care about this profile for now... */
[5ebff60]379                        } else if (g_strncasecmp(ct, "text/x-msmsgsinitialemailnotification", 37) == 0) {
380                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
381                                        char *inbox = get_rfc822_header(body, "Inbox-Unread:", blen);
382                                        char *folders = get_rfc822_header(body, "Folders-Unread:", blen);
383
384                                        if (inbox && folders) {
[0864a52]385                                                imcb_notify_email(ic,
[dd43c62]386                                                        "INBOX contains %s new messages, plus %s messages in other folders.", inbox,
387                                                        folders);
[5ebff60]388                                        }
389
390                                        g_free(inbox);
391                                        g_free(folders);
[b7d3cc34]392                                }
[5ebff60]393                        } else if (g_strncasecmp(ct, "text/x-msmsgsemailnotification", 30) == 0) {
394                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
395                                        char *from = get_rfc822_header(body, "From-Addr:", blen);
396                                        char *fromname = get_rfc822_header(body, "From:", blen);
397
398                                        if (from && fromname) {
[0864a52]399                                                imcb_notify_email(ic, "Received an e-mail message from %s <%s>.", fromname, from);
[5ebff60]400                                        }
401
402                                        g_free(from);
403                                        g_free(fromname);
[b7d3cc34]404                                }
[5ebff60]405                        } else if (g_strncasecmp(ct, "text/x-msmsgsactivemailnotification", 35) == 0) {
[d550358]406                                /* Notification that a message has been read... Ignore it */
[b7d3cc34]407                        }
[5ebff60]408
409                        g_free(ct);
[b7d3cc34]410                }
[5ebff60]411        } else if (strcmp(cmd[0], "ADL") == 0) {
[e5854a8]412                struct xt_node *adl, *d, *c;
[5ebff60]413
414                if (!(adl = xt_from_string(msg, msglen))) {
[e5854a8]415                        return 1;
[5ebff60]416                }
417
418                for (d = adl->children; d; d = d->next) {
[e5854a8]419                        char *dn;
[5ebff60]420                        if (strcmp(d->name, "d") != 0 ||
421                            (dn = xt_find_attr(d, "n")) == NULL) {
[e5854a8]422                                continue;
[5ebff60]423                        }
424                        for (c = d->children; c; c = c->next) {
[e5854a8]425                                bee_user_t *bu;
426                                struct msn_buddy_data *bd;
427                                char *cn, *handle, *f, *l;
428                                int flags;
[5ebff60]429
430                                if (strcmp(c->name, "c") != 0 ||
431                                    (l = xt_find_attr(c, "l")) == NULL ||
432                                    (cn = xt_find_attr(c, "n")) == NULL) {
[e5854a8]433                                        continue;
[5ebff60]434                                }
435
[3901b5d]436                                /* FIXME: Use "t" here, guess I should just add it
437                                   as a prefix like elsewhere in the protocol. */
[5ebff60]438                                handle = g_strdup_printf("%s@%s", cn, dn);
439                                if (!((bu = bee_user_by_handle(ic->bee, ic, handle)) ||
440                                      (bu = bee_user_new(ic->bee, ic, handle, 0)))) {
441                                        g_free(handle);
[e5854a8]442                                        continue;
443                                }
[5ebff60]444                                g_free(handle);
[e5854a8]445                                bd = bu->data;
[5ebff60]446
447                                if ((f = xt_find_attr(c, "f"))) {
448                                        http_decode(f);
449                                        imcb_rename_buddy(ic, bu->handle, f);
[e5854a8]450                                }
[5ebff60]451
452                                flags = atoi(l) & 15;
453                                if (bd->flags != flags) {
[e5854a8]454                                        bd->flags = flags;
[5ebff60]455                                        msn_buddy_ask(bu);
[e5854a8]456                                }
457                        }
458                }
[cd5fdcf]459        }
460
461        return 1;
462}
463
[5fbf815]464static void msn_ns_structured_message(struct msn_data *md, char *msg, int msglen, char **cmd)
[cd5fdcf]465{
466        char **parts = NULL;
467        char *semicolon = NULL;
468        char *action = NULL;
469        char *from = NULL;
470        char *who = NULL;
471
472        parts = g_strsplit(msg, "\r\n\r\n", 4);
473
474        if (!(from = get_rfc822_header(parts[0], "From", 0))) {
475                goto cleanup;
476        }
477
478        /* either the semicolon or the end of the string */
479        semicolon = strchr(from, ';') ? : (from + strlen(from));
480
481        who = g_strndup(from + 2, semicolon - from - 2);
482
483        if ((strcmp(cmd[0], "SDG") == 0) && (action = get_rfc822_header(parts[2], "Message-Type", 0))) {
[5fbf815]484                msn_ns_sdg(md, who, parts, action);
[cd5fdcf]485
486        } else if ((strcmp(cmd[0], "NFY") == 0) && (action = get_rfc822_header(parts[2], "Uri", 0))) {
[899c8e8]487                gboolean is_put = (strcmp(cmd[2], "MSGR\\PUT") == 0);
[5fbf815]488                msn_ns_nfy(md, who, parts, action, is_put);
[cd5fdcf]489        }
490
491cleanup:
492        g_strfreev(parts);
493        g_free(action);
494        g_free(from);
495        g_free(who);
496}
497
[5fbf815]498static void msn_ns_sdg(struct msn_data *md, char *who, char **parts, char *action)
[cd5fdcf]499{
[5fbf815]500        struct im_connection *ic = md->ic;
[cd5fdcf]501
502        if (strcmp(action, "Control/Typing") == 0) {
503                imcb_buddy_typing(ic, who, OPT_TYPING);
504        } else if (strcmp(action, "Text") == 0) {
505                imcb_buddy_msg(ic, who, parts[3], 0, 0);
506        }
507}
508
[5fbf815]509static void msn_ns_nfy(struct msn_data *md, char *who, char **parts, char *action, gboolean is_put)
[cd5fdcf]510{
[5fbf815]511        struct im_connection *ic = md->ic;
[cd5fdcf]512        struct xt_node *body = NULL;
513        struct xt_node *s = NULL;
514        const char *state = NULL;
515        char *nick = NULL;
516        char *psm = NULL;
517        int flags = OPT_LOGGED_IN;
518
519        if (strcmp(action, "/user") != 0) {
520                return;
521        }
522
523        if (!(body = xt_from_string(parts[3], 0))) {
524                goto cleanup;
525        }
526
527        s = body->children;
528        while ((s = xt_find_node(s, "s"))) {
529                struct xt_node *s2;
530                char *n = xt_find_attr(s, "n");  /* service name: IM, PE, etc */
531
532                if (strcmp(n, "IM") == 0) {
533                        /* IM has basic presence information */
534                        if (!is_put) {
535                                /* NFY DEL with a <s> usually means log out from the last endpoint */
536                                flags &= ~OPT_LOGGED_IN;
537                                break;
538                        }
539
540                        s2 = xt_find_node(s->children, "Status");
541                        if (s2 && s2->text_len) {
542                                const struct msn_away_state *msn_state = msn_away_state_by_code(s2->text);
543                                state = msn_state->name;
544                                if (msn_state != msn_away_state_list) {
545                                        flags |= OPT_AWAY;
546                                }
547                        }
548                } else if (strcmp(n, "PE") == 0) {
549                        if ((s2 = xt_find_node(s->children, "PSM")) && s2->text_len) {
550                                psm = s2->text;
551                        }
552                        if ((s2 = xt_find_node(s->children, "FriendlyName")) && s2->text_len) {
553                                nick = s2->text;
[11e42dc]554                        }
555                }
[cd5fdcf]556                s = s->next;
[3901b5d]557        }
[5ebff60]558
[cd5fdcf]559        imcb_buddy_status(ic, who, flags, state, psm);
560
561        if (nick) {
562                imcb_rename_buddy(ic, who, nick);
563        }
564
565cleanup:
566        xt_free_node(body);
[b7d3cc34]567}
568
[5ebff60]569void msn_auth_got_passport_token(struct im_connection *ic, const char *token, const char *error)
[b7d3cc34]570{
[d84e2a9]571        struct msn_data *md;
[4107fea]572        char *payload = NULL;
[5ebff60]573
[d84e2a9]574        /* Dead connection? */
[5ebff60]575        if (g_slist_find(msn_connections, ic) == NULL) {
[d84e2a9]576                return;
[b7d3cc34]577        }
[5ebff60]578
579        md = ic->proto_data;
580
[4107fea]581        if (!token) {
[5ebff60]582                imcb_error(ic, "Error during Passport authentication: %s", error);
583                imc_logout(ic, TRUE);
[3fb7139]584                return;
[660cb00]585        }
[4107fea]586
587        // ATH
588        payload = g_markup_printf_escaped(
589                "<user><ssl-compact-ticket>%s</ssl-compact-ticket>"
590                "<ssl-site-name>chatservice.live.com</ssl-site-name></user>",
591                md->tokens[0]);
592
593        msn_ns_write_cmd(ic, "ATH", "CON\\USER", payload);
594
595        g_free(payload);
596
597        /* continues in msn_ns_command */
[b7d3cc34]598}
[e3413cc]599
[5ebff60]600void msn_auth_got_contact_list(struct im_connection *ic)
[ca7de3a]601{
602        /* Dead connection? */
[5ebff60]603        if (g_slist_find(msn_connections, ic) == NULL) {
[ca7de3a]604                return;
[5ebff60]605        }
606
[254a4da]607        msn_ns_send_adl_start(ic);
608        msn_ns_finish_login(ic);
[ca7de3a]609}
610
[5ebff60]611static gboolean msn_ns_send_adl_1(gpointer key, gpointer value, gpointer data)
[ca7de3a]612{
[254a4da]613        struct xt_node *adl = data, *d, *c, *s;
[ca7de3a]614        struct bee_user *bu = value;
615        struct msn_buddy_data *bd = bu->data;
[5a7af1b]616        struct msn_data *md = bu->ic->proto_data;
[4f161e3]617        char handle[strlen(bu->handle) + 1];
[ca7de3a]618        char *domain;
619        char l[4];
[5ebff60]620
[2730c79]621        if ((bd->flags & (MSN_BUDDY_FL | MSN_BUDDY_AL)) == 0 || (bd->flags & MSN_BUDDY_ADL_SYNCED)) {
[5ebff60]622                return FALSE;
623        }
624
625        strcpy(handle, bu->handle);
626        if ((domain = strchr(handle, '@')) == NULL) {    /* WTF */
[e5854a8]627                return FALSE;
[5ebff60]628        }
[ca7de3a]629        *domain = '\0';
[5ebff60]630        domain++;
631
632        if ((d = adl->children) == NULL ||
633            g_strcasecmp(xt_find_attr(d, "n"), domain) != 0) {
634                d = xt_new_node("d", NULL, NULL);
635                xt_add_attr(d, "n", domain);
636                xt_insert_child(adl, d);
[ca7de3a]637        }
[5ebff60]638
[2730c79]639        g_snprintf(l, sizeof(l), "%d", bd->flags & (MSN_BUDDY_FL | MSN_BUDDY_AL));
[5ebff60]640        c = xt_new_node("c", NULL, NULL);
641        xt_add_attr(c, "n", handle);
642        xt_add_attr(c, "t", "1");   /* FIXME: Network type, i.e. 32 for Y!MSG */
[254a4da]643        s = xt_new_node("s", NULL, NULL);
644        xt_add_attr(s, "n", "IM");
645        xt_add_attr(s, "l", l);
646        xt_insert_child(c, s);
[5ebff60]647        xt_insert_child(d, c);
648
[5a7af1b]649        /* Do this in batches of 100. */
650        bd->flags |= MSN_BUDDY_ADL_SYNCED;
651        return (--md->adl_todo % 140) == 0;
[ca7de3a]652}
653
[5ebff60]654static void msn_ns_send_adl(struct im_connection *ic)
[ca7de3a]655{
656        struct xt_node *adl;
[5a7af1b]657        struct msn_data *md = ic->proto_data;
[64768d4]658        char *adls;
[5ebff60]659
660        adl = xt_new_node("ml", NULL, NULL);
661        xt_add_attr(adl, "l", "1");
662        g_tree_foreach(md->domaintree, msn_ns_send_adl_1, adl);
663        if (adl->children == NULL) {
[5a7af1b]664                /* This tells the caller that we're done now. */
665                md->adl_todo = -1;
[5ebff60]666                xt_free_node(adl);
[5a7af1b]667                return;
668        }
[5ebff60]669
670        adls = xt_to_string(adl);
671        xt_free_node(adl);
[899c8e8]672        msn_ns_write_cmd(ic, "PUT", "MSGR\\CONTACTS", adls);
[5ebff60]673        g_free(adls);
[5a7af1b]674}
675
[5ebff60]676static void msn_ns_send_adl_start(struct im_connection *ic)
[5a7af1b]677{
678        struct msn_data *md;
679        GSList *l;
[5ebff60]680
[5a7af1b]681        /* Dead connection? */
[5ebff60]682        if (g_slist_find(msn_connections, ic) == NULL) {
[5a7af1b]683                return;
[5ebff60]684        }
685
[5a7af1b]686        md = ic->proto_data;
687        md->adl_todo = 0;
[5ebff60]688        for (l = ic->bee->users; l; l = l->next) {
[5a7af1b]689                bee_user_t *bu = l->data;
690                struct msn_buddy_data *bd = bu->data;
[5ebff60]691
[2730c79]692                if (bu->ic != ic || (bd->flags & (MSN_BUDDY_FL | MSN_BUDDY_AL)) == 0) {
[5a7af1b]693                        continue;
[5ebff60]694                }
695
[5a7af1b]696                bd->flags &= ~MSN_BUDDY_ADL_SYNCED;
697                md->adl_todo++;
698        }
[5ebff60]699
700        msn_ns_send_adl(ic);
[ca7de3a]701}
[80175a1]702
[5ebff60]703int msn_ns_finish_login(struct im_connection *ic)
[80175a1]704{
705        struct msn_data *md = ic->proto_data;
[5ebff60]706
707        if (ic->flags & OPT_LOGGED_IN) {
[80175a1]708                return 1;
[5ebff60]709        }
710
711        if (md->adl_todo < 0) {
[80175a1]712                md->flags |= MSN_DONE_ADL;
[ed0589c]713        }
[5ebff60]714
715        if ((md->flags & MSN_DONE_ADL) && (md->flags & MSN_GOT_PROFILE)) {
[d550358]716                imcb_connected(ic);
[5ebff60]717        }
718
[ed0589c]719        return 1;
[80175a1]720}
[bc676ac]721
[99fe030]722static int msn_ns_send_sdg(struct im_connection *ic, bee_user_t *bu, const char *message_type, const char *text)
[bc676ac]723{
724        struct msn_data *md = ic->proto_data;
[11e42dc]725        int retval = 0;
726        char *buf;
[5ebff60]727
[99fe030]728        buf = g_strdup_printf(MSN_MESSAGE_HEADERS, bu->handle, ic->acc->user, md->uuid, message_type, strlen(text), text);
[899c8e8]729        retval = msn_ns_write_cmd(ic, "SDG", "MSGR", buf);
[11e42dc]730        g_free(buf);
731        return retval;
[bc676ac]732}
[99fe030]733
734int msn_ns_send_typing(struct im_connection *ic, bee_user_t *bu)
735{
736        return msn_ns_send_sdg(ic, bu, "Control/Typing", "");
737}
738
739int msn_ns_send_message(struct im_connection *ic, bee_user_t *bu, const char *text)
740{
741        return msn_ns_send_sdg(ic, bu, "Text", text);
742}
743
Note: See TracBrowser for help on using the repository browser.