source: protocols/msn/ns.c @ 89e0c94

Last change on this file since 89e0c94 was 89e0c94, checked in by dequis <dx@…>, at 2015-03-19T10:25:33Z

get_rfc822_header: allow headers at the end of a string

  • Property mode set to 100644
File size: 17.0 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) {
[ffa5b70]54                fd = md->fd;
[5ebff60]55        }
56
57        if (getenv("BITLBEE_DEBUG")) {
[fa8c775]58                fprintf(stderr, "\x1b[91m>>>[NS%d] %s\n\x1b[97m", fd, out);
[5ebff60]59        }
60
61        len = strlen(out);
[b4e96d6]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
[ffa5b70]80gboolean msn_ns_connect(struct im_connection *ic, const char *host, int port)
[b7d3cc34]81{
[ffa5b70]82        struct msn_data *handler = ic->proto_data;
83
[5ebff60]84        if (handler->fd >= 0) {
85                closesocket(handler->fd);
86        }
87
[b4e96d6]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{
[ffa5b70]106        struct msn_data *md = data;
107        struct msn_data *handler = md;
108        struct im_connection *ic = md->ic;
[5ebff60]109
[b4e96d6]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)) {
[b4e96d6]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
[ffa5b70]145void msn_ns_close(struct msn_data *handler)
[bae0617]146{
[b4e96d6]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{
[ffa5b70]171        struct msn_data *handler = data;
172        struct im_connection *ic = handler->ic;
[b4e96d6]173        char *bytes;
[c408298]174        int st;
[5ebff60]175
[b4e96d6]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
[c408298]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        }
[c408298]188
189        msn_queue_feed(handler, bytes, st);
190
[b4e96d6]191        g_free(bytes);
192
[c408298]193        /* Ignore ret == 0, it's already disconnected then. */
194        msn_handler(handler);
195
196        return TRUE;
197       
[b7d3cc34]198}
199
[ffa5b70]200int msn_ns_command(struct msn_data *handler, char **cmd, int num_parts)
[b7d3cc34]201{
[ffa5b70]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
[3752019]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");
[ffa5b70]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;
[89e0c94]306        } else if (strcmp(cmd[0], "QRY") == 0) {
307                /* CONGRATULATIONS */
[5ebff60]308        } else if (strcmp(cmd[0], "OUT") == 0) {
[c2fb3809]309                int allow_reconnect = TRUE;
[5ebff60]310
311                if (cmd[1] && strcmp(cmd[1], "OTH") == 0) {
312                        imcb_error(ic, "Someone else logged in with your account");
[c2fb3809]313                        allow_reconnect = FALSE;
[5ebff60]314                } else if (cmd[1] && strcmp(cmd[1], "SSD") == 0) {
315                        imcb_error(ic, "Terminating session because of server shutdown");
316                } else {
317                        imcb_error(ic, "Session terminated by remote server (%s)",
318                                   cmd[1] ? cmd[1] : "reason unknown)");
[b7d3cc34]319                }
[5ebff60]320
321                imc_logout(ic, allow_reconnect);
322                return(0);
[c5a1502]323        } else if (strcmp(cmd[0], "GCF") == 0) {
[5fecede]324                /* Coming up is cmd[2] bytes of stuff we're supposed to
325                   censore. Meh. */
[5ebff60]326                handler->msglen = atoi(cmd[2]);
[ffa5b70]327        } else if ((strcmp(cmd[0], "NFY") == 0) || (strcmp(cmd[0], "SDG") == 0)) {
[3752019]328                if (num_parts >= 3) {
329                        handler->msglen = atoi(cmd[2]);
330                }
[89e0c94]331        } else if (strcmp(cmd[0], "PUT") == 0) {
332                if (num_parts >= 4) {
333                        handler->msglen = atoi(cmd[3]);
334                }
[5ebff60]335        } else if (strcmp(cmd[0], "QNG") == 0) {
[e132b60]336                ic->flags |= OPT_PONGED;
[5ebff60]337        } else if (g_ascii_isdigit(cmd[0][0])) {
338                int num = atoi(cmd[0]);
339                const struct msn_status_code *err = msn_status_by_number(num);
340
341                imcb_error(ic, "Error reported by MSN server: %s", err->text);
342
343                if (err->flags & STATUS_FATAL) {
344                        imc_logout(ic, TRUE);
345                        return(0);
[b7d3cc34]346                }
[5ebff60]347
[02bb9db]348                /* Oh yes, errors can have payloads too now. Discard them for now. */
[5ebff60]349                if (num_parts >= 3) {
350                        handler->msglen = atoi(cmd[2]);
351                }
352        } else {
[c5a1502]353                imcb_error(ic, "Received unknown command from main server: %s", cmd[0]);
[b7d3cc34]354        }
[5ebff60]355
356        return(1);
[b7d3cc34]357}
358
[ffa5b70]359int msn_ns_message(struct msn_data *handler, char *msg, int msglen, char **cmd, int num_parts)
[b7d3cc34]360{
[ffa5b70]361        struct im_connection *ic = handler->ic;
[b7d3cc34]362        char *body;
363        int blen = 0;
[5ebff60]364
365        if (!num_parts) {
366                return(1);
367        }
368
369        if ((body = strstr(msg, "\r\n\r\n"))) {
[b7d3cc34]370                body += 4;
[5ebff60]371                blen = msglen - (body - msg);
[b7d3cc34]372        }
[5ebff60]373
374        if (strcmp(cmd[0], "MSG") == 0) {
375                if (g_strcasecmp(cmd[1], "Hotmail") == 0) {
376                        char *ct = get_rfc822_header(msg, "Content-Type:", msglen);
377
378                        if (!ct) {
379                                return(1);
380                        }
381
382                        if (g_strncasecmp(ct, "application/x-msmsgssystemmessage", 33) == 0) {
[b7d3cc34]383                                char *mtype;
384                                char *arg1;
[5ebff60]385
386                                if (!body) {
387                                        return(1);
[b7d3cc34]388                                }
[5ebff60]389
390                                mtype = get_rfc822_header(body, "Type:", blen);
391                                arg1 = get_rfc822_header(body, "Arg1:", blen);
392
393                                if (mtype && strcmp(mtype, "1") == 0) {
394                                        if (arg1) {
395                                                imcb_log(ic, "The server is going down for maintenance in %s minutes.",
396                                                         arg1);
397                                        }
398                                }
399
400                                g_free(arg1);
401                                g_free(mtype);
402                        } else if (g_strncasecmp(ct, "text/x-msmsgsprofile", 20) == 0) {
[b7d3cc34]403                                /* We don't care about this profile for now... */
[5ebff60]404                        } else if (g_strncasecmp(ct, "text/x-msmsgsinitialemailnotification", 37) == 0) {
405                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
406                                        char *inbox = get_rfc822_header(body, "Inbox-Unread:", blen);
407                                        char *folders = get_rfc822_header(body, "Folders-Unread:", blen);
408
409                                        if (inbox && folders) {
410                                                imcb_log(ic,
411                                                         "INBOX contains %s new messages, plus %s messages in other folders.", inbox,
412                                                         folders);
413                                        }
414
415                                        g_free(inbox);
416                                        g_free(folders);
[b7d3cc34]417                                }
[5ebff60]418                        } else if (g_strncasecmp(ct, "text/x-msmsgsemailnotification", 30) == 0) {
419                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
420                                        char *from = get_rfc822_header(body, "From-Addr:", blen);
421                                        char *fromname = get_rfc822_header(body, "From:", blen);
422
423                                        if (from && fromname) {
424                                                imcb_log(ic, "Received an e-mail message from %s <%s>.", fromname,
425                                                         from);
426                                        }
427
428                                        g_free(from);
429                                        g_free(fromname);
[b7d3cc34]430                                }
[5ebff60]431                        } else if (g_strncasecmp(ct, "text/x-msmsgsactivemailnotification", 35) == 0) {
[c5a1502]432                                /* Notification that a message has been read... Ignore it */
[5ebff60]433                        } else {
434                                debug("Can't handle %s packet from notification server", ct);
[b7d3cc34]435                        }
[5ebff60]436
437                        g_free(ct);
[b7d3cc34]438                }
[5ebff60]439        } else if (strcmp(cmd[0], "ADL") == 0) {
[e5854a8]440                struct xt_node *adl, *d, *c;
[5ebff60]441
442                if (!(adl = xt_from_string(msg, msglen))) {
[e5854a8]443                        return 1;
[5ebff60]444                }
445
446                for (d = adl->children; d; d = d->next) {
[e5854a8]447                        char *dn;
[5ebff60]448                        if (strcmp(d->name, "d") != 0 ||
449                            (dn = xt_find_attr(d, "n")) == NULL) {
[e5854a8]450                                continue;
[5ebff60]451                        }
452                        for (c = d->children; c; c = c->next) {
[e5854a8]453                                bee_user_t *bu;
454                                struct msn_buddy_data *bd;
455                                char *cn, *handle, *f, *l;
456                                int flags;
[5ebff60]457
458                                if (strcmp(c->name, "c") != 0 ||
459                                    (l = xt_find_attr(c, "l")) == NULL ||
460                                    (cn = xt_find_attr(c, "n")) == NULL) {
[e5854a8]461                                        continue;
[5ebff60]462                                }
463
[3901b5d]464                                /* FIXME: Use "t" here, guess I should just add it
465                                   as a prefix like elsewhere in the protocol. */
[5ebff60]466                                handle = g_strdup_printf("%s@%s", cn, dn);
467                                if (!((bu = bee_user_by_handle(ic->bee, ic, handle)) ||
468                                      (bu = bee_user_new(ic->bee, ic, handle, 0)))) {
469                                        g_free(handle);
[e5854a8]470                                        continue;
471                                }
[5ebff60]472                                g_free(handle);
[e5854a8]473                                bd = bu->data;
[5ebff60]474
475                                if ((f = xt_find_attr(c, "f"))) {
476                                        http_decode(f);
477                                        imcb_rename_buddy(ic, bu->handle, f);
[e5854a8]478                                }
[5ebff60]479
480                                flags = atoi(l) & 15;
481                                if (bd->flags != flags) {
[e5854a8]482                                        bd->flags = flags;
[5ebff60]483                                        msn_buddy_ask(bu);
[e5854a8]484                                }
485                        }
486                }
[ffa5b70]487        } else if (strcmp(cmd[0], "SDG") == 0) {
488                char **parts = g_strsplit(msg, "\r\n\r\n", 4);
489                char *from = NULL;
490                char *mt = NULL;
491                char *who = NULL;
492                char *s = NULL;
[5ebff60]493
[ffa5b70]494                if ((from = get_rfc822_header(parts[0], "From", 0)) &&
495                    (mt = get_rfc822_header(parts[2], "Message-Type", 0)) &&
496                    (s = strchr(from, ';'))) {
[5ebff60]497
[ffa5b70]498                        who = g_strndup(from + 2, s - from - 2);
499
500                        if (strcmp(mt, "Control/Typing") == 0) {
501                                imcb_buddy_typing(ic, who, OPT_TYPING);
502                        } else if (strcmp(mt, "Text") == 0) {
503                                imcb_buddy_msg(ic, who, parts[3], 0, 0);
504                        }
505                }
506                g_free(from);
507                g_free(mt);
508                g_free(who);
509                return 1;
[3901b5d]510        }
[5ebff60]511
[3901b5d]512        return 1;
[b7d3cc34]513}
514
[5ebff60]515void msn_auth_got_passport_token(struct im_connection *ic, const char *token, const char *error)
[b7d3cc34]516{
[d84e2a9]517        struct msn_data *md;
[5ebff60]518
[d84e2a9]519        /* Dead connection? */
[5ebff60]520        if (g_slist_find(msn_connections, ic) == NULL) {
[d84e2a9]521                return;
[b7d3cc34]522        }
[5ebff60]523
524        md = ic->proto_data;
525
526        if (token) {
527                msn_ns_write(ic, -1, "USR %d SSO S %s %s {%s}\r\n", ++md->trId, md->tokens[0], token, md->uuid);
528        } else {
529                imcb_error(ic, "Error during Passport authentication: %s", error);
530                imc_logout(ic, TRUE);
[660cb00]531        }
[b7d3cc34]532}
[e3413cc]533
[5ebff60]534void msn_auth_got_contact_list(struct im_connection *ic)
[ca7de3a]535{
536        /* Dead connection? */
[5ebff60]537        if (g_slist_find(msn_connections, ic) == NULL) {
[ca7de3a]538                return;
[5ebff60]539        }
540
[3752019]541        msn_ns_send_adl_start(ic);
542        msn_ns_finish_login(ic);
[ca7de3a]543}
544
[5ebff60]545static gboolean msn_ns_send_adl_1(gpointer key, gpointer value, gpointer data)
[ca7de3a]546{
[3752019]547        struct xt_node *adl = data, *d, *c, *s;
[ca7de3a]548        struct bee_user *bu = value;
549        struct msn_buddy_data *bd = bu->data;
[5a7af1b]550        struct msn_data *md = bu->ic->proto_data;
[4f161e3]551        char handle[strlen(bu->handle) + 1];
[ca7de3a]552        char *domain;
553        char l[4];
[5ebff60]554
555        if ((bd->flags & 7) == 0 || (bd->flags & MSN_BUDDY_ADL_SYNCED)) {
556                return FALSE;
557        }
558
559        strcpy(handle, bu->handle);
560        if ((domain = strchr(handle, '@')) == NULL) {    /* WTF */
[e5854a8]561                return FALSE;
[5ebff60]562        }
[ca7de3a]563        *domain = '\0';
[5ebff60]564        domain++;
565
566        if ((d = adl->children) == NULL ||
567            g_strcasecmp(xt_find_attr(d, "n"), domain) != 0) {
568                d = xt_new_node("d", NULL, NULL);
569                xt_add_attr(d, "n", domain);
570                xt_insert_child(adl, d);
[ca7de3a]571        }
[5ebff60]572
573        g_snprintf(l, sizeof(l), "%d", bd->flags & 7);
574        c = xt_new_node("c", NULL, NULL);
575        xt_add_attr(c, "n", handle);
576        xt_add_attr(c, "t", "1");   /* FIXME: Network type, i.e. 32 for Y!MSG */
[3752019]577        s = xt_new_node("s", NULL, NULL);
578        xt_add_attr(s, "n", "IM");
579        xt_add_attr(s, "l", l);
580        xt_insert_child(c, s);
[5ebff60]581        xt_insert_child(d, c);
582
[5a7af1b]583        /* Do this in batches of 100. */
584        bd->flags |= MSN_BUDDY_ADL_SYNCED;
585        return (--md->adl_todo % 140) == 0;
[ca7de3a]586}
587
[5ebff60]588static void msn_ns_send_adl(struct im_connection *ic)
[ca7de3a]589{
590        struct xt_node *adl;
[5a7af1b]591        struct msn_data *md = ic->proto_data;
[64768d4]592        char *adls;
[5ebff60]593
594        adl = xt_new_node("ml", NULL, NULL);
595        xt_add_attr(adl, "l", "1");
596        g_tree_foreach(md->domaintree, msn_ns_send_adl_1, adl);
597        if (adl->children == NULL) {
[5a7af1b]598                /* This tells the caller that we're done now. */
599                md->adl_todo = -1;
[5ebff60]600                xt_free_node(adl);
[5a7af1b]601                return;
602        }
[5ebff60]603
604        adls = xt_to_string(adl);
605        xt_free_node(adl);
606        msn_ns_write(ic, -1, "ADL %d %zd\r\n%s", ++md->trId, strlen(adls), adls);
607        g_free(adls);
[5a7af1b]608}
609
[5ebff60]610static void msn_ns_send_adl_start(struct im_connection *ic)
[5a7af1b]611{
612        struct msn_data *md;
613        GSList *l;
[5ebff60]614
[5a7af1b]615        /* Dead connection? */
[5ebff60]616        if (g_slist_find(msn_connections, ic) == NULL) {
[5a7af1b]617                return;
[5ebff60]618        }
619
[5a7af1b]620        md = ic->proto_data;
621        md->adl_todo = 0;
[5ebff60]622        for (l = ic->bee->users; l; l = l->next) {
[5a7af1b]623                bee_user_t *bu = l->data;
624                struct msn_buddy_data *bd = bu->data;
[5ebff60]625
626                if (bu->ic != ic || (bd->flags & 7) == 0) {
[5a7af1b]627                        continue;
[5ebff60]628                }
629
[5a7af1b]630                bd->flags &= ~MSN_BUDDY_ADL_SYNCED;
631                md->adl_todo++;
632        }
[5ebff60]633
634        msn_ns_send_adl(ic);
[ca7de3a]635}
[80175a1]636
[5ebff60]637int msn_ns_finish_login(struct im_connection *ic)
[80175a1]638{
639        struct msn_data *md = ic->proto_data;
[5ebff60]640
641        if (ic->flags & OPT_LOGGED_IN) {
[80175a1]642                return 1;
[5ebff60]643        }
644
645        if (md->adl_todo < 0) {
[80175a1]646                md->flags |= MSN_DONE_ADL;
[ed0589c]647        }
[5ebff60]648
649        if ((md->flags & MSN_DONE_ADL) && (md->flags & MSN_GOT_PROFILE)) {
[c5a1502]650                imcb_connected(ic);
[5ebff60]651        }
652
[ed0589c]653        return 1;
[80175a1]654}
[bc676ac]655
[ffa5b70]656// TODO: typing notifications, nudges lol, etc
[5ebff60]657int msn_ns_sendmessage(struct im_connection *ic, bee_user_t *bu, const char *text)
[bc676ac]658{
659        struct msn_data *md = ic->proto_data;
[ffa5b70]660        int retval = 0;
661        char *buf;
[5ebff60]662
663        if (strncmp(text, "\r\r\r", 3) == 0) {
[bc676ac]664                /* Err. Shouldn't happen but I guess it can. Don't send others
665                   any of the "SHAKE THAT THING" messages. :-D */
666                return 1;
[5ebff60]667        }
668
[ffa5b70]669        buf = g_strdup_printf(MSN_MESSAGE_HEADERS, bu->handle, ic->acc->user, md->uuid, strlen(text), text);
670        retval = msn_ns_write(ic, -1, "SDG %d %zd\r\n%s", ++md->trId, strlen(buf), buf);
671        g_free(buf);
672        return retval;
[bc676ac]673}
Note: See TracBrowser for help on using the repository browser.