source: protocols/msn/ns.c @ 913a663

Last change on this file since 913a663 was 913a663, checked in by dequis <dx@…>, at 2015-04-10T17:10:41Z

msn: implement the rest of the http gateway support, enable by default

  • Property mode set to 100644
File size: 16.8 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"
[b7d3cc34]34
[5ebff60]35static gboolean msn_ns_connected(gpointer data, gint source, b_input_condition cond);
36static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond);
[b7d3cc34]37
[5ebff60]38static void msn_ns_send_adl_start(struct im_connection *ic);
39static void msn_ns_send_adl(struct im_connection *ic);
[b7d3cc34]40
[5ebff60]41int msn_ns_write(struct im_connection *ic, int fd, const char *fmt, ...)
[64768d4]42{
43        struct msn_data *md = ic->proto_data;
44        va_list params;
45        char *out;
46        size_t len;
47        int st;
[5ebff60]48
49        va_start(params, fmt);
50        out = g_strdup_vprintf(fmt, params);
51        va_end(params);
52
53        if (fd < 0) {
[11e42dc]54                fd = md->fd;
[5ebff60]55        }
56
57        if (getenv("BITLBEE_DEBUG")) {
[693aca0]58                fprintf(stderr, "\x1b[91m>>>[NS%d] %s\n\x1b[97m", fd, 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 {
67                st = write(fd, out, len);
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
[11e42dc]80gboolean msn_ns_connect(struct im_connection *ic, const char *host, int port)
[b7d3cc34]81{
[11e42dc]82        struct msn_data *handler = ic->proto_data;
83
[5ebff60]84        if (handler->fd >= 0) {
85                closesocket(handler->fd);
86        }
87
[913a663]88        if (handler->is_http) {
89                handler->gw = msn_gw_new(handler);
90                handler->gw->callback = msn_ns_callback;
91                msn_ns_connected(handler, -1, B_EV_IO_READ);
92        } else {
93                handler->fd = proxy_connect(host, port, msn_ns_connected, handler);
94                if (handler->fd < 0) {
95                        imcb_error(ic, "Could not connect to server");
96                        imc_logout(ic, TRUE);
97                        return FALSE;
98                }
[b7d3cc34]99        }
[5ebff60]100
[bae0617]101        return TRUE;
102}
103
[5ebff60]104static gboolean msn_ns_connected(gpointer data, gint source, b_input_condition cond)
[bae0617]105{
[11e42dc]106        struct msn_data *md = data;
107        struct msn_data *handler = md;
108        struct im_connection *ic = md->ic;
[5ebff60]109
[913a663]110        if (source == -1 && !md->is_http) {
[5ebff60]111                imcb_error(ic, "Could not connect to server");
112                imc_logout(ic, TRUE);
[bae0617]113                return FALSE;
[b7d3cc34]114        }
[5ebff60]115
116        g_free(handler->rxq);
[bae0617]117        handler->rxlen = 0;
[5ebff60]118        handler->rxq = g_new0(char, 1);
119
120        if (md->uuid == NULL) {
[f9258ae]121                struct utsname name;
122                sha1_state_t sha[1];
[5ebff60]123
[f9258ae]124                /* UUID == SHA1("BitlBee" + my hostname + MSN username) */
[5ebff60]125                sha1_init(sha);
126                sha1_append(sha, (void *) "BitlBee", 7);
127                if (uname(&name) == 0) {
128                        sha1_append(sha, (void *) name.nodename, strlen(name.nodename));
[f9258ae]129                }
[5ebff60]130                sha1_append(sha, (void *) ic->acc->user, strlen(ic->acc->user));
131                md->uuid = sha1_random_uuid(sha);
132                memcpy(md->uuid, "b171be3e", 8);   /* :-P */
[f9258ae]133        }
[5ebff60]134
135        if (msn_ns_write(ic, source, "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER)) {
[913a663]136                if (!handler->is_http) {
137                        handler->inpa = b_input_add(handler->fd, B_EV_IO_READ, msn_ns_callback, handler);
138                }
[5ebff60]139                imcb_log(ic, "Connected to server, waiting for reply");
[b7d3cc34]140        }
[5ebff60]141
[ba9edaa]142        return FALSE;
[b7d3cc34]143}
144
[11e42dc]145void msn_ns_close(struct msn_data *handler)
[bae0617]146{
[913a663]147        if (handler->gw) {
148                if (handler->gw->waiting) {
149                        /* mark it as closed, let the request callback clean it */
150                        handler->gw->open = FALSE;
151                } else {
152                        msn_gw_free(handler->gw);
153                }
154        }
[5ebff60]155        if (handler->fd >= 0) {
156                closesocket(handler->fd);
157                b_event_remove(handler->inpa);
[bae0617]158        }
[5ebff60]159
[bae0617]160        handler->fd = handler->inpa = -1;
[5ebff60]161        g_free(handler->rxq);
162        g_free(handler->cmd_text);
163
[bae0617]164        handler->rxlen = 0;
165        handler->rxq = NULL;
166        handler->cmd_text = NULL;
167}
168
[5ebff60]169static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond)
[b7d3cc34]170{
[11e42dc]171        struct msn_data *handler = data;
172        struct im_connection *ic = handler->ic;
[913a663]173        char *bytes;
[a4be2f6]174        int st;
[5ebff60]175
[913a663]176        if (handler->is_http) {
177                st = msn_gw_read(handler->gw, &bytes);
178        } else {
179                bytes = g_malloc(1024);
180                st = read(handler->fd, bytes, 1024);
181        }
182
[a4be2f6]183        if (st <= 0) {
[5ebff60]184                imcb_error(ic, "Error while reading from server");
185                imc_logout(ic, TRUE);
[ba9edaa]186                return FALSE;
[5ebff60]187        }
[a4be2f6]188
189        msn_queue_feed(handler, bytes, st);
190
[913a663]191        g_free(bytes);
192
[a4be2f6]193        /* Ignore ret == 0, it's already disconnected then. */
194        msn_handler(handler);
195
196        return TRUE;
197       
[b7d3cc34]198}
199
[11e42dc]200int msn_ns_command(struct msn_data *handler, char **cmd, int num_parts)
[b7d3cc34]201{
[11e42dc]202        struct im_connection *ic = handler->ic;
203        struct msn_data *md = handler;
[5ebff60]204
205        if (num_parts == 0) {
[b7d3cc34]206                /* Hrrm... Empty command...? Ignore? */
[5ebff60]207                return(1);
[b7d3cc34]208        }
[5ebff60]209
210        if (strcmp(cmd[0], "VER") == 0) {
211                if (cmd[2] && strncmp(cmd[2], MSNP_VER, 5) != 0) {
212                        imcb_error(ic, "Unsupported protocol");
213                        imc_logout(ic, FALSE);
214                        return(0);
[b7d3cc34]215                }
[5ebff60]216
[254a4da]217                return(msn_ns_write(ic, handler->fd, "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s VmVyc2lvbjogMQ0KWGZyQ291bnQ6IDINClhmclNlbnRVVENUaW1lOiA2MzU2MTQ3OTU5NzgzOTAwMDANCklzR2VvWGZyOiB0cnVlDQo=\r\n",
[5ebff60]218                                    ++md->trId, ic->acc->user));
219        } else if (strcmp(cmd[0], "CVR") == 0) {
[b7d3cc34]220                /* We don't give a damn about the information we just received */
[5ebff60]221                return msn_ns_write(ic, handler->fd, "USR %d SSO I %s\r\n", ++md->trId, ic->acc->user);
222        } else if (strcmp(cmd[0], "XFR") == 0) {
[b7d3cc34]223                char *server;
224                int port;
[5ebff60]225
226                if (num_parts >= 6 && strcmp(cmd[2], "NS") == 0) {
227                        b_event_remove(handler->inpa);
[bae0617]228                        handler->inpa = -1;
[5ebff60]229
230                        server = strchr(cmd[3], ':');
231                        if (!server) {
232                                imcb_error(ic, "Syntax error");
233                                imc_logout(ic, TRUE);
234                                return(0);
[b7d3cc34]235                        }
236                        *server = 0;
[5ebff60]237                        port = atoi(server + 1);
[b7d3cc34]238                        server = cmd[3];
[5ebff60]239
240                        imcb_log(ic, "Transferring to other server");
[11e42dc]241                        return msn_ns_connect(ic, server, port);
[5ebff60]242                } else {
243                        imcb_error(ic, "Syntax error");
244                        imc_logout(ic, TRUE);
245                        return(0);
246                }
247        } else if (strcmp(cmd[0], "USR") == 0) {
248                if (num_parts >= 6 && strcmp(cmd[2], "SSO") == 0 &&
249                    strcmp(cmd[3], "S") == 0) {
250                        g_free(md->pp_policy);
251                        md->pp_policy = g_strdup(cmd[4]);
252                        msn_soap_passport_sso_request(ic, cmd[5]);
253                } else if (strcmp(cmd[2], "OK") == 0) {
[ed0589c]254                        /* If the number after the handle is 0, the e-mail
255                           address is unverified, which means we can't change
256                           the display name. */
[5ebff60]257                        if (cmd[4][0] == '0') {
[ed0589c]258                                md->flags |= MSN_EMAIL_UNVERIFIED;
[5ebff60]259                        }
260
261                        imcb_log(ic, "Authenticated, getting buddy list");
262                        msn_soap_memlist_request(ic);
263                } else {
264                        imcb_error(ic, "Unknown authentication type");
265                        imc_logout(ic, FALSE);
266                        return(0);
267                }
268        } else if (strcmp(cmd[0], "MSG") == 0) {
269                if (num_parts < 4) {
270                        imcb_error(ic, "Syntax error");
271                        imc_logout(ic, TRUE);
272                        return(0);
[e5854a8]273                }
[5ebff60]274
275                handler->msglen = atoi(cmd[3]);
276
277                if (handler->msglen <= 0) {
278                        imcb_error(ic, "Syntax error");
279                        imc_logout(ic, TRUE);
280                        return(0);
281                }
282        } else if (strcmp(cmd[0], "ADL") == 0) {
283                if (num_parts >= 3 && strcmp(cmd[2], "OK") == 0) {
284                        msn_ns_send_adl(ic);
285                        return msn_ns_finish_login(ic);
286                } else if (num_parts >= 3) {
287                        handler->msglen = atoi(cmd[2]);
288                }
289        } else if (strcmp(cmd[0], "CHL") == 0) {
[be7a180]290                char *resp;
[64768d4]291                int st;
[5ebff60]292
293                if (num_parts < 3) {
294                        imcb_error(ic, "Syntax error");
295                        imc_logout(ic, TRUE);
296                        return(0);
[b7d3cc34]297                }
[5ebff60]298
299                resp = msn_p11_challenge(cmd[2]);
300
301                st =  msn_ns_write(ic, -1, "QRY %d %s %zd\r\n%s",
302                                   ++md->trId, MSNP11_PROD_ID,
303                                   strlen(resp), resp);
304                g_free(resp);
[64768d4]305                return st;
[5ebff60]306        } else if (strcmp(cmd[0], "OUT") == 0) {
[c2fb3809]307                int allow_reconnect = TRUE;
[5ebff60]308
309                if (cmd[1] && strcmp(cmd[1], "OTH") == 0) {
310                        imcb_error(ic, "Someone else logged in with your account");
[c2fb3809]311                        allow_reconnect = FALSE;
[5ebff60]312                } else if (cmd[1] && strcmp(cmd[1], "SSD") == 0) {
313                        imcb_error(ic, "Terminating session because of server shutdown");
314                } else {
315                        imcb_error(ic, "Session terminated by remote server (%s)",
316                                   cmd[1] ? cmd[1] : "reason unknown)");
[b7d3cc34]317                }
[5ebff60]318
319                imc_logout(ic, allow_reconnect);
320                return(0);
[d550358]321        } else if (strcmp(cmd[0], "GCF") == 0) {
[5fecede]322                /* Coming up is cmd[2] bytes of stuff we're supposed to
323                   censore. Meh. */
[5ebff60]324                handler->msglen = atoi(cmd[2]);
[11e42dc]325        } else if ((strcmp(cmd[0], "NFY") == 0) || (strcmp(cmd[0], "SDG") == 0)) {
[254a4da]326                if (num_parts >= 3) {
327                        handler->msglen = atoi(cmd[2]);
328                }
[5ebff60]329        } else if (strcmp(cmd[0], "QNG") == 0) {
[e132b60]330                ic->flags |= OPT_PONGED;
[5ebff60]331        } else if (g_ascii_isdigit(cmd[0][0])) {
332                int num = atoi(cmd[0]);
333                const struct msn_status_code *err = msn_status_by_number(num);
334
335                imcb_error(ic, "Error reported by MSN server: %s", err->text);
336
337                if (err->flags & STATUS_FATAL) {
338                        imc_logout(ic, TRUE);
339                        return(0);
[b7d3cc34]340                }
[5ebff60]341
[02bb9db]342                /* Oh yes, errors can have payloads too now. Discard them for now. */
[5ebff60]343                if (num_parts >= 3) {
344                        handler->msglen = atoi(cmd[2]);
345                }
346        } else {
[d550358]347                imcb_error(ic, "Received unknown command from main server: %s", cmd[0]);
[b7d3cc34]348        }
[5ebff60]349
350        return(1);
[b7d3cc34]351}
352
[11e42dc]353int msn_ns_message(struct msn_data *handler, char *msg, int msglen, char **cmd, int num_parts)
[b7d3cc34]354{
[11e42dc]355        struct im_connection *ic = handler->ic;
[b7d3cc34]356        char *body;
357        int blen = 0;
[5ebff60]358
359        if (!num_parts) {
360                return(1);
361        }
362
363        if ((body = strstr(msg, "\r\n\r\n"))) {
[b7d3cc34]364                body += 4;
[5ebff60]365                blen = msglen - (body - msg);
[b7d3cc34]366        }
[5ebff60]367
368        if (strcmp(cmd[0], "MSG") == 0) {
369                if (g_strcasecmp(cmd[1], "Hotmail") == 0) {
370                        char *ct = get_rfc822_header(msg, "Content-Type:", msglen);
371
372                        if (!ct) {
373                                return(1);
374                        }
375
376                        if (g_strncasecmp(ct, "application/x-msmsgssystemmessage", 33) == 0) {
[b7d3cc34]377                                char *mtype;
378                                char *arg1;
[5ebff60]379
380                                if (!body) {
381                                        return(1);
[b7d3cc34]382                                }
[5ebff60]383
384                                mtype = get_rfc822_header(body, "Type:", blen);
385                                arg1 = get_rfc822_header(body, "Arg1:", blen);
386
387                                if (mtype && strcmp(mtype, "1") == 0) {
388                                        if (arg1) {
389                                                imcb_log(ic, "The server is going down for maintenance in %s minutes.",
390                                                         arg1);
391                                        }
392                                }
393
394                                g_free(arg1);
395                                g_free(mtype);
396                        } else if (g_strncasecmp(ct, "text/x-msmsgsprofile", 20) == 0) {
[b7d3cc34]397                                /* We don't care about this profile for now... */
[5ebff60]398                        } else if (g_strncasecmp(ct, "text/x-msmsgsinitialemailnotification", 37) == 0) {
399                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
400                                        char *inbox = get_rfc822_header(body, "Inbox-Unread:", blen);
401                                        char *folders = get_rfc822_header(body, "Folders-Unread:", blen);
402
403                                        if (inbox && folders) {
404                                                imcb_log(ic,
405                                                         "INBOX contains %s new messages, plus %s messages in other folders.", inbox,
406                                                         folders);
407                                        }
408
409                                        g_free(inbox);
410                                        g_free(folders);
[b7d3cc34]411                                }
[5ebff60]412                        } else if (g_strncasecmp(ct, "text/x-msmsgsemailnotification", 30) == 0) {
413                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
414                                        char *from = get_rfc822_header(body, "From-Addr:", blen);
415                                        char *fromname = get_rfc822_header(body, "From:", blen);
416
417                                        if (from && fromname) {
418                                                imcb_log(ic, "Received an e-mail message from %s <%s>.", fromname,
419                                                         from);
420                                        }
421
422                                        g_free(from);
423                                        g_free(fromname);
[b7d3cc34]424                                }
[5ebff60]425                        } else if (g_strncasecmp(ct, "text/x-msmsgsactivemailnotification", 35) == 0) {
[d550358]426                                /* Notification that a message has been read... Ignore it */
[5ebff60]427                        } else {
428                                debug("Can't handle %s packet from notification server", ct);
[b7d3cc34]429                        }
[5ebff60]430
431                        g_free(ct);
[b7d3cc34]432                }
[5ebff60]433        } else if (strcmp(cmd[0], "ADL") == 0) {
[e5854a8]434                struct xt_node *adl, *d, *c;
[5ebff60]435
436                if (!(adl = xt_from_string(msg, msglen))) {
[e5854a8]437                        return 1;
[5ebff60]438                }
439
440                for (d = adl->children; d; d = d->next) {
[e5854a8]441                        char *dn;
[5ebff60]442                        if (strcmp(d->name, "d") != 0 ||
443                            (dn = xt_find_attr(d, "n")) == NULL) {
[e5854a8]444                                continue;
[5ebff60]445                        }
446                        for (c = d->children; c; c = c->next) {
[e5854a8]447                                bee_user_t *bu;
448                                struct msn_buddy_data *bd;
449                                char *cn, *handle, *f, *l;
450                                int flags;
[5ebff60]451
452                                if (strcmp(c->name, "c") != 0 ||
453                                    (l = xt_find_attr(c, "l")) == NULL ||
454                                    (cn = xt_find_attr(c, "n")) == NULL) {
[e5854a8]455                                        continue;
[5ebff60]456                                }
457
[3901b5d]458                                /* FIXME: Use "t" here, guess I should just add it
459                                   as a prefix like elsewhere in the protocol. */
[5ebff60]460                                handle = g_strdup_printf("%s@%s", cn, dn);
461                                if (!((bu = bee_user_by_handle(ic->bee, ic, handle)) ||
462                                      (bu = bee_user_new(ic->bee, ic, handle, 0)))) {
463                                        g_free(handle);
[e5854a8]464                                        continue;
465                                }
[5ebff60]466                                g_free(handle);
[e5854a8]467                                bd = bu->data;
[5ebff60]468
469                                if ((f = xt_find_attr(c, "f"))) {
470                                        http_decode(f);
471                                        imcb_rename_buddy(ic, bu->handle, f);
[e5854a8]472                                }
[5ebff60]473
474                                flags = atoi(l) & 15;
475                                if (bd->flags != flags) {
[e5854a8]476                                        bd->flags = flags;
[5ebff60]477                                        msn_buddy_ask(bu);
[e5854a8]478                                }
479                        }
480                }
[11e42dc]481        } else if (strcmp(cmd[0], "SDG") == 0) {
482                char **parts = g_strsplit(msg, "\r\n\r\n", 4);
483                char *from = NULL;
484                char *mt = NULL;
485                char *who = NULL;
486                char *s = NULL;
[5ebff60]487
[11e42dc]488                if ((from = get_rfc822_header(parts[0], "From", 0)) &&
489                    (mt = get_rfc822_header(parts[2], "Message-Type", 0)) &&
490                    (s = strchr(from, ';'))) {
[5ebff60]491
[11e42dc]492                        who = g_strndup(from + 2, s - from - 2);
493
494                        if (strcmp(mt, "Control/Typing") == 0) {
495                                imcb_buddy_typing(ic, who, OPT_TYPING);
496                        } else if (strcmp(mt, "Text") == 0) {
497                                imcb_buddy_msg(ic, who, parts[3], 0, 0);
498                        }
499                }
500                g_free(from);
501                g_free(mt);
502                g_free(who);
503                return 1;
[3901b5d]504        }
[5ebff60]505
[3901b5d]506        return 1;
[b7d3cc34]507}
508
[5ebff60]509void msn_auth_got_passport_token(struct im_connection *ic, const char *token, const char *error)
[b7d3cc34]510{
[d84e2a9]511        struct msn_data *md;
[5ebff60]512
[d84e2a9]513        /* Dead connection? */
[5ebff60]514        if (g_slist_find(msn_connections, ic) == NULL) {
[d84e2a9]515                return;
[b7d3cc34]516        }
[5ebff60]517
518        md = ic->proto_data;
519
520        if (token) {
521                msn_ns_write(ic, -1, "USR %d SSO S %s %s {%s}\r\n", ++md->trId, md->tokens[0], token, md->uuid);
522        } else {
523                imcb_error(ic, "Error during Passport authentication: %s", error);
524                imc_logout(ic, TRUE);
[660cb00]525        }
[b7d3cc34]526}
[e3413cc]527
[5ebff60]528void msn_auth_got_contact_list(struct im_connection *ic)
[ca7de3a]529{
530        /* Dead connection? */
[5ebff60]531        if (g_slist_find(msn_connections, ic) == NULL) {
[ca7de3a]532                return;
[5ebff60]533        }
534
[254a4da]535        msn_ns_send_adl_start(ic);
536        msn_ns_finish_login(ic);
[ca7de3a]537}
538
[5ebff60]539static gboolean msn_ns_send_adl_1(gpointer key, gpointer value, gpointer data)
[ca7de3a]540{
[254a4da]541        struct xt_node *adl = data, *d, *c, *s;
[ca7de3a]542        struct bee_user *bu = value;
543        struct msn_buddy_data *bd = bu->data;
[5a7af1b]544        struct msn_data *md = bu->ic->proto_data;
[4f161e3]545        char handle[strlen(bu->handle) + 1];
[ca7de3a]546        char *domain;
547        char l[4];
[5ebff60]548
549        if ((bd->flags & 7) == 0 || (bd->flags & MSN_BUDDY_ADL_SYNCED)) {
550                return FALSE;
551        }
552
553        strcpy(handle, bu->handle);
554        if ((domain = strchr(handle, '@')) == NULL) {    /* WTF */
[e5854a8]555                return FALSE;
[5ebff60]556        }
[ca7de3a]557        *domain = '\0';
[5ebff60]558        domain++;
559
560        if ((d = adl->children) == NULL ||
561            g_strcasecmp(xt_find_attr(d, "n"), domain) != 0) {
562                d = xt_new_node("d", NULL, NULL);
563                xt_add_attr(d, "n", domain);
564                xt_insert_child(adl, d);
[ca7de3a]565        }
[5ebff60]566
567        g_snprintf(l, sizeof(l), "%d", bd->flags & 7);
568        c = xt_new_node("c", NULL, NULL);
569        xt_add_attr(c, "n", handle);
570        xt_add_attr(c, "t", "1");   /* FIXME: Network type, i.e. 32 for Y!MSG */
[254a4da]571        s = xt_new_node("s", NULL, NULL);
572        xt_add_attr(s, "n", "IM");
573        xt_add_attr(s, "l", l);
574        xt_insert_child(c, s);
[5ebff60]575        xt_insert_child(d, c);
576
[5a7af1b]577        /* Do this in batches of 100. */
578        bd->flags |= MSN_BUDDY_ADL_SYNCED;
579        return (--md->adl_todo % 140) == 0;
[ca7de3a]580}
581
[5ebff60]582static void msn_ns_send_adl(struct im_connection *ic)
[ca7de3a]583{
584        struct xt_node *adl;
[5a7af1b]585        struct msn_data *md = ic->proto_data;
[64768d4]586        char *adls;
[5ebff60]587
588        adl = xt_new_node("ml", NULL, NULL);
589        xt_add_attr(adl, "l", "1");
590        g_tree_foreach(md->domaintree, msn_ns_send_adl_1, adl);
591        if (adl->children == NULL) {
[5a7af1b]592                /* This tells the caller that we're done now. */
593                md->adl_todo = -1;
[5ebff60]594                xt_free_node(adl);
[5a7af1b]595                return;
596        }
[5ebff60]597
598        adls = xt_to_string(adl);
599        xt_free_node(adl);
600        msn_ns_write(ic, -1, "ADL %d %zd\r\n%s", ++md->trId, strlen(adls), adls);
601        g_free(adls);
[5a7af1b]602}
603
[5ebff60]604static void msn_ns_send_adl_start(struct im_connection *ic)
[5a7af1b]605{
606        struct msn_data *md;
607        GSList *l;
[5ebff60]608
[5a7af1b]609        /* Dead connection? */
[5ebff60]610        if (g_slist_find(msn_connections, ic) == NULL) {
[5a7af1b]611                return;
[5ebff60]612        }
613
[5a7af1b]614        md = ic->proto_data;
615        md->adl_todo = 0;
[5ebff60]616        for (l = ic->bee->users; l; l = l->next) {
[5a7af1b]617                bee_user_t *bu = l->data;
618                struct msn_buddy_data *bd = bu->data;
[5ebff60]619
620                if (bu->ic != ic || (bd->flags & 7) == 0) {
[5a7af1b]621                        continue;
[5ebff60]622                }
623
[5a7af1b]624                bd->flags &= ~MSN_BUDDY_ADL_SYNCED;
625                md->adl_todo++;
626        }
[5ebff60]627
628        msn_ns_send_adl(ic);
[ca7de3a]629}
[80175a1]630
[5ebff60]631int msn_ns_finish_login(struct im_connection *ic)
[80175a1]632{
633        struct msn_data *md = ic->proto_data;
[5ebff60]634
635        if (ic->flags & OPT_LOGGED_IN) {
[80175a1]636                return 1;
[5ebff60]637        }
638
639        if (md->adl_todo < 0) {
[80175a1]640                md->flags |= MSN_DONE_ADL;
[ed0589c]641        }
[5ebff60]642
643        if ((md->flags & MSN_DONE_ADL) && (md->flags & MSN_GOT_PROFILE)) {
[d550358]644                imcb_connected(ic);
[5ebff60]645        }
646
[ed0589c]647        return 1;
[80175a1]648}
[bc676ac]649
[11e42dc]650// TODO: typing notifications, nudges lol, etc
[5ebff60]651int msn_ns_sendmessage(struct im_connection *ic, bee_user_t *bu, const char *text)
[bc676ac]652{
653        struct msn_data *md = ic->proto_data;
[11e42dc]654        int retval = 0;
655        char *buf;
[5ebff60]656
657        if (strncmp(text, "\r\r\r", 3) == 0) {
[bc676ac]658                /* Err. Shouldn't happen but I guess it can. Don't send others
659                   any of the "SHAKE THAT THING" messages. :-D */
660                return 1;
[5ebff60]661        }
662
[11e42dc]663        buf = g_strdup_printf(MSN_MESSAGE_HEADERS, bu->handle, ic->acc->user, md->uuid, strlen(text), text);
664        retval = msn_ns_write(ic, -1, "SDG %d %zd\r\n%s", ++md->trId, strlen(buf), buf);
665        g_free(buf);
666        return retval;
[bc676ac]667}
Note: See TracBrowser for help on using the repository browser.