source: protocols/msn/ns.c @ 899c8e8

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

msn: misc MSNP24 command format changes

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